package com.zd.laboratory.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.enums.UserMessageType;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.valid.ValidGroup;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.vo.LiteMessageNoticeVO;
import com.zd.laboratory.mapper.LabMessageUserMapper;
import com.zd.system.api.laboratory.domain.LabMessageContent;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import com.zd.laboratory.domain.dto.LabMessageContentListQuery;
import com.zd.laboratory.domain.vo.LabMessageContentVO;
import com.zd.laboratory.service.ILabMessageContentService;
import com.zd.laboratory.service.ILabMessageUserService;
import com.zd.system.api.laboratory.domain.vo.LabMessageUserListVO;
import com.zd.system.api.model.LoginUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 消息发布Controller
 *
 * @author zhoupan
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/content")
public class LabMessageContentController extends BaseController {
    @Autowired
    private ILabMessageContentService labMessageContentService;

    @Autowired
    private ILabMessageUserService messageUserService;

    @Autowired
    private LabMessageUserMapper messageUserMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询消息发布列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTENT + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabMessageContentListQuery labMessageContent) {
        startPage();
        List<LabMessageContentVO> list = labMessageContentService.selectLabMessageContentVOList(labMessageContent);
        return getDataTable(list);
    }

    /**
     * 获取消息发布详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTENT + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labMessageContentService.selectLabMessageContentById(id));
    }

    /**
     * 新增消息发布
     */
//    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTENT + PerFun.ADD)
    @Log(title = "消息发布", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@Validated(ValidGroup.Crud.Create.class) @RequestBody LabMessageContent labMessageContent) {
        return ResultData.result(labMessageContentService.sendMessage(labMessageContent), () -> "没有找到要发送的用户！");

    }

    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTENT + PerFun.REMOVE)
    @ApiOperation(value = "删除发布记录")
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(labMessageContentService.deleteLabMessageContentByIds(ids));
    }


    /**
     * 查询我的消息-(只查询管理员消息)
     */
    @RequestMapping("/my/list")
    public TableDataInfo<LabMessageUserListVO> mylist() {

        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser == null) return null;
        LabMessageUser labMessageContent = new LabMessageUser();
        labMessageContent.setJoinUserId(SecurityUtils.getUserId());
        labMessageContent.setJoinUserId(SecurityUtils.getUserId());
        labMessageContent.setDeptId(loginUser.getSysUser().getDeptId());
        labMessageContent.setMessageType(UserMessageType.ADMIN);
        startPage();
        List<LabMessageUserListVO> list = messageUserService.selectListVO(labMessageContent);
        return getDataTable(list);
    }

    /**
     * 查询我的消息-发送人分组
     */
    @RequestMapping("/my/group/list")
    public TableDataInfo myGrouplist() {
        startPage();
        List<LiteMessageNoticeVO> liteMessageNoticeVOS = messageUserService.queryLiteMessageNoticeVO();
        return getDataTable(liteMessageNoticeVOS);
    }

    /**
     * 查询我的消息统计数量
     */
    @RequestMapping("/my/group/totalCount")
    public AjaxResult myGroupTotalCount() {
        return AjaxResult.success(messageUserService.myGroupTotalCount());
    }

    /**
     * 查询安全准入消息信息
     */
    @RequestMapping("/my/group/read/{messageId}")
    public AjaxResult myRead(@PathVariable("messageId") Long messageId) {
        return AjaxResult.success(messageUserService.myRead(messageId));
    }

    /**
     * 查询我的消息-发送人分组-分组内消息
     */
    @RequestMapping("/my/{id}/group/list")
    public TableDataInfo<LabMessageUserListVO> myGrouplistById(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser == null) return null;
        LabMessageUserListVO labMessageUserListVO = new LabMessageUserListVO();
        labMessageUserListVO.setJoinUserId(SecurityUtils.getUserIdEx());
        labMessageUserListVO.setUserId(id);
        labMessageUserListVO.setDeptId(loginUser.getSysUser().getDeptId());
        startPage();
        List<LabMessageUser> labMessageUsers = messageUserService.selectLabMessageUserList(labMessageUserListVO);
        return getDataTable(labMessageUsers);
    }


    /**
     * 查询我的消息-(所有消息)
     */
    @RequestMapping("/myAll/list")
    public TableDataInfo<LabMessageUserListVO> myAlllist() {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser == null) return null;
        LabMessageUser labMessageContent = new LabMessageUser();
        labMessageContent.setJoinUserId(SecurityUtils.getUserId());
        labMessageContent.setDeptId(loginUser.getSysUser().getDeptId());
        startPage();
        List<LabMessageUserListVO> list = messageUserService.selectListVO(labMessageContent);
        return getDataTable(list);
    }

    /**
     * 查询我的消息-(所有数量)
     */
    @RequestMapping("/myAll/count")
    public ResultData myCount() {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser == null) return null;
        return ResultData.success(messageUserMapper.selectMyCount(loginUser.getUserid(), loginUser.getSysUser().getDeptId()));
    }

    /**
     * 查询我的消息
     */
    @GetMapping("/my/{id}")
    public LabMessageUser message(@PathVariable Long id) {
        LabMessageUser labMessageContent = new LabMessageUser();
        labMessageContent.setJoinUserId(SecurityUtils.getUserId());
        startPage();
        return messageUserService.selectLabMessageUserById(id);
    }

    /**
     * 查询预案向一体机推送的消息，如果超过两小时，需要清理
     */
    @PutMapping("/outTimeClearMessage")
    public AjaxResult outTimeClearMessage() {
        return toAjax(labMessageContentService.outTimeClearMessage());
    }


    @PostMapping("/readByLoginUser")
    public void readByLoginUser(@RequestBody LabMessageUser labMessageUser) {
        messageUserService.readByLoginUser(labMessageUser);
    }

}
