package com.ym.mec.web.controller.education;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.ImGroupNotice;
import com.ym.mec.biz.dal.page.ImGroupNoticeQueryInfo;
import com.ym.mec.biz.service.ImGroupNoticeService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.im.ImFeignService;

/**
 * @Author Joburgess
 * @Date 2019/9/18
 */
@RequestMapping("imGroupNotice")
@Api(tags = "即时通讯群公告")
@RestController
public class ImGroupNoticeController extends BaseController {

    @Autowired
    private ImGroupNoticeService imGroupNoticeService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ImFeignService imFeignService;

    @ApiOperation(value = "新增")
    @RequestMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('imGroupNotice/add','system')")
    public Object add(ImGroupNotice imGroupNotice ) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        imGroupNotice.setOperatorId(sysUser.getId().longValue());
        return succeed(imGroupNoticeService.insert(imGroupNotice));
    }

    @ApiOperation(value = "修改")
    @RequestMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('imGroupNotice/update','system')")
    public Object update(ImGroupNotice imGroupNotice ) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        imGroupNotice.setOperatorId(sysUser.getId().longValue());
        imGroupNotice.setUpdateTime(new Date());
        imGroupNoticeService.update(imGroupNotice);
        return succeed();
    }

    @ApiOperation(value = "获取最新一条群公告")
    @RequestMapping("/getNewNotice")
    @PreAuthorize("@pcs.hasPermissions('imGroupNotice/getNewNotice','system')")
    public Object getNewNotice(Long groupId) {
        return succeed(imGroupNoticeService.queryLatestNotice(groupId));
    }

    @ApiOperation(value = "获取群公告列表")
    @RequestMapping("/queryNoticePage")
    @PreAuthorize("@pcs.hasPermissions('imGroupNotice/queryNoticePage','system')")
    public Object queryPage(ImGroupNoticeQueryInfo queryInfo) {
        return succeed(imGroupNoticeService.queryPage(queryInfo));
    }

    @ApiOperation(value = "删除")
    @RequestMapping("/del")
    @PreAuthorize("@pcs.hasPermissions('imGroupNotice/del','system')")
    public Object del(Long id) {
        imGroupNoticeService.delete(id);
        return succeed();
    }
}
