package com.yfd.platform.system.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.config.MessageConfig;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.system.domain.Message;
import com.yfd.platform.system.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * <p>
 * 消息通知 前端控制器
 * </p>
 *
 *
 * @since 2023-03-19
 */
@RestController
@RequestMapping("/system/message")
@Api(tags = "消息通知")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @Resource
    private MessageConfig messageConfig;

    @ApiOperation("查询消息")
    @GetMapping("/getMessageList")
    public ResponseResult getMessageList(Page<Message> page,
                                         String status, String title,
                                         String type, String startDate,
                                         String endDate) {
        if (StrUtil.isBlank(status)) {
            return ResponseResult.error("参数为空");
        }
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        if ("0".equals(status)) {
            queryWrapper.eq(Message::getStatus, "1");
        } else {
            List<String> statusList = new ArrayList<>();
            statusList.add("2");
            statusList.add("9");
            queryWrapper.in(Message::getStatus, statusList);

            if (StrUtil.isNotBlank(title)) {
                queryWrapper.like(Message::getTitle, title);
            }

            if (StrUtil.isNotBlank(type)) {
                queryWrapper.eq(Message::getType, type);
            }

            DateTime parseStartDate = DateUtil.parse(startDate);
            DateTime parseEndDate = DateUtil.parse(endDate);
            DateTime dateTime = DateUtil.offsetDay(parseEndDate, 1);

            if (parseStartDate != null && parseEndDate != null) {
                queryWrapper.ge(Message::getCreatetime, parseStartDate).lt(Message::getCreatetime, dateTime);
            }
        }
        queryWrapper.orderByDesc(Message::getCreatetime);
        Page<Message> pageList = messageService.page(page, queryWrapper);
        return ResponseResult.successData(pageList);
    }

    @ApiOperation("根据ID查询消息")
    @GetMapping("/getMessageById")
    public ResponseResult getMessageById(String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("参数为空");
        }
        Message message = messageService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("title", message.getTitle());
        map.put("content", message.getContent());
        map.put("createtime", message.getCreatetime());
        return ResponseResult.successData(map);
    }

    @Log(module = "消息通知", value = "根据ID删除消息",type = "0")
    @ApiOperation("根据ID删除消息")
    @PostMapping("/deleteMessageById")
    public ResponseResult deleteMessageById(@RequestParam String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("参数为空");
        }
        String[] split = id.split(",");
        List<String> idList = Arrays.asList(split);
        boolean ok = messageService.removeByIds(idList);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error("删除失败");
        }

    }

    @Log(module = "消息通知", value = "将消息标记为已阅状态",type = "0")
    @ApiOperation("标记已阅")
    @PostMapping("/setMessageStatus")
    public ResponseResult setMessageStatus(@RequestParam String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("参数为空");
        }
        String[] split = id.split(",");
        long time = System.currentTimeMillis();
        for (String mid : split) {
            Message message = messageService.getById(mid);
            if ("9".equals(message.getStatus())) {
                continue;
            }
            message.setStatus("2");
            message.setReadtime(new Timestamp(time));
            messageService.updateById(message);
        }
        messageConfig.sendMessage();
        return ResponseResult.success();
    }

    @ApiOperation("全部已阅")
    @PostMapping("/setAllMessageStatus")
    public ResponseResult setAllMessageStatus() {
        long time = System.currentTimeMillis();
        List<Message> list =
                messageService.list(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        for (Message message : list) {
            message.setStatus("2");
            message.setReadtime(new Timestamp(time));
            messageService.updateById(message);
        }
        messageConfig.sendMessage();
        return ResponseResult.success();
    }

    @ApiOperation("查看未读的消息数量")
    @GetMapping("/getMessageCount")
    public ResponseResult getMessageCount() {
        long count =
                messageService.count(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        return ResponseResult.successData(count);
    }

}
