package com.keao.edu.auth.web.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysMessageParams;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.auth.web.controller.queryInfo.SysMessageQueryInfo;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.SysMessage;
import com.keao.edu.common.enums.MessageSendMode;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.service.SysMessageService;

/**
 * @Author Joburgess
 * @Date 2020.07.02
 */
@RestController
@RequestMapping("sysMessage")
@Api(tags = "消息推送服务")
public class SysMessageController extends BaseController {

    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "发送消息")
    @PostMapping(value = "/batchSendMessage")
    public void batchSendMessage(@RequestBody SysMessageParams sysMessageParams){
        sysMessageService.batchSendMessage(sysMessageParams.getMessageType(), sysMessageParams.getReceivers(),
                sysMessageParams.getTriggerTime(), sysMessageParams.getReadStatus(), sysMessageParams.getUrl(), sysMessageParams.getJpushType(),
                sysMessageParams.getArgs());
    }

    @ApiOperation(value = "删除消息")
    @PostMapping(value = "/batchDelMessage")
    public void batchDelMessage(String ids){
        sysMessageService.batchDelMessage(ids);
    }

    @ApiOperation(value = "查询用户未读消息条数")
    @GetMapping("/queryCountOfUnread")
    public Object queryCountOfUnread() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(sysMessageService.queryCountOfUnread(MessageSendMode.PUSH, sysUser.getId()));
    }

    @ApiOperation("一键已读")
    @PostMapping("batchSetRead")
    public Object batchSetRead() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(sysMessageService.updateStatus(sysUser.getId(), 1));
    }

    @ApiOperation("设置已读")
    @PostMapping("setRead")
    public Object setRead(Long id) {
        int status = 1;
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(sysMessageService.updateOneStatus(id, status));
    }

    @ApiOperation("获取所有消息列表")
    @GetMapping(value = "list")
    public Object list(SysMessageQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(!sysUser.getIsSuperAdmin()){
            queryInfo.setUserId(sysUser.getId());
        }
        queryInfo.setType(MessageSendMode.PUSH.getCode());
        PageInfo<SysMessage> pageInfo = sysMessageService.queryPage(queryInfo);
        return succeed(pageInfo);
    }

}
