package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.SysMessageCountResult;
import com.qmth.sop.business.bean.result.SysMessageResult;
import com.qmth.sop.business.bean.result.SysMessageWorkResult;
import com.qmth.sop.business.entity.SysMessage;
import com.qmth.sop.business.entity.TBTask;
import com.qmth.sop.business.service.SysMessageService;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.business.templete.execute.AsyncSysMessageExportService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.MessageTypeEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Map;
import java.util.Objects;

/**
 * 系统消息 控制器
 * 
 * @author: shudonghui
 * @date: 2023-08-07 13:53:29
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "系统消息 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX+SystemConstant.PREFIX_URL_MESSAGE)
public class SysMessageController {


    @Resource
    SysMessageService sysMessageService;

    @Resource
    TBTaskService tbTaskService;

    @Resource
    AsyncSysMessageExportService asyncSysMessageExportService;

    @ApiOperation(value = "回执查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "回执查询结果", response = SysMessageResult.class)})
    public Result query(
                @ApiParam(value = "公告表id", required = true) @RequestParam(required = true) Long noticeId,
                @ApiParam(value = "区域城市", required = false) @RequestParam(required = false) String city,
                @ApiParam(value = "供应商", required = false) @RequestParam(required = false) Long supplierId,
                @ApiParam(value = "回执状态", required = false) @RequestParam(required = false) Boolean status,
                @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<SysMessageResult> resultIPage = sysMessageService.query(new Page<>(pageNumber, pageSize),noticeId,city,supplierId,status);
        return ResultUtil.ok(resultIPage);
    }

    @ApiOperation(value = "回执查询导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EXPORT)
    public Result export(@ApiParam(value = "公告表id", required = true) @RequestParam(required = true) Long noticeId,
                         @ApiParam(value = "区域城市", required = false) @RequestParam(required = false) String city,
                         @ApiParam(value = "供应商", required = false) @RequestParam(required = false) Long supplierId,
                         @ApiParam(value = "回执状态", required = false) @RequestParam(required = false) Boolean status) throws Exception {
        Map<String, Object> map = tbTaskService.saveTask(TaskTypeEnum.MESSAGE_EXPORT);
        map.put("noticeId", noticeId);
        map.put("city", Objects.nonNull(city) ?city: null);
        map.put("supplierId", Objects.nonNull(supplierId) ?supplierId: null);
        map.put("status", Objects.nonNull(status) ?status: null);
        asyncSysMessageExportService.exportTask(map);
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        return ResultUtil.ok(tbTask.getId());
    }

    /*
        统计已读和未读系统消息
     */
    @ApiOperation(value = "回执统计已读和未读")
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = SysMessageCountResult.class)})
    public Result count(@ApiParam(value = "公告表id", required = true) @RequestParam(required = true) long noticeId) throws Exception {
        return ResultUtil.ok(sysMessageService.count(noticeId));
    }


    /**
     * 标记消息已阅读
     */
   
    @ApiOperation(value = "标记消息已阅读")
    @RequestMapping(value = "/read", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result read(@ApiParam(value = "消息表id", required = true) @RequestParam(required = true) long id) {
        return ResultUtil.ok(sysMessageService.read(id));
    }

    /**
     * 查看消息详细
     */
   
    @ApiOperation(value = "查看消息详细")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = SysMessage.class)})
    public Result get(@ApiParam(value = "消息表id", required = true) @RequestParam(required = true) long id) {
        return ResultUtil.ok(sysMessageService.getById(id));
    }

    //按多个类型统计未读消息
   
    @ApiOperation(value = "按多个类型统计当前用户未读消息")
    @RequestMapping(value = "/countByTypes", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    public Result countByTypes(@ApiParam(value = "消息类型", required = true) @RequestParam(required = true) MessageTypeEnum[] types) {
        return ResultUtil.ok(sysMessageService.countByTypes(types));
    }

    //按多个类型查询未读消息
   
    @ApiOperation(value = "按多个类型查询当前用户未读消息")
    @RequestMapping(value = "/pageByTypes", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = SysMessageWorkResult.class)})
    public Result pageByTypes(@Valid @ApiParam(value = "消息类型", required = true) @RequestParam(required = true) MessageTypeEnum[] types,
                              @ApiParam(value = "是否已读", required = false) @RequestParam(required = false) Boolean status,
                              @ApiParam(value = "服务单元", required = false) @RequestParam(required = false) Long serviceId,
                              @ApiParam(value = "客户名称（模糊查询）", required = false) @RequestParam(required = false) String custom,
                              @ApiParam(value = "内容模糊查询", required = false) @RequestParam(required = false) String query,
                              @ApiParam(value = "发送时间开始", required = false) @RequestParam(required = false) Long startTime,
                              @ApiParam(value = "发送时间结束", required = false) @RequestParam(required = false) Long endTime,
                              @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                              @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<SysMessageWorkResult> resultIPage = sysMessageService.pageByTypes(new Page<>(pageNumber, pageSize),types,status,serviceId,custom,query,startTime,endTime);
        return ResultUtil.ok(resultIPage);
    }




}