package org.springblade.modules.api.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.MessageTypeEnum;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.vo.ReadStatisVO;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.desk.vo.NoticeVO;
import org.springblade.modules.desk.wrapper.NoticeWrapper;
import org.springblade.modules.platform.entity.NoticeRead;
import org.springblade.modules.platform.entity.UserMessage;
import org.springblade.modules.platform.service.INoticeReadService;
import org.springblade.modules.platform.service.IUserMessageService;
import org.springblade.modules.platform.vo.UserMessageVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author pangqijun
 * Date 2023/7/6
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/message-center")
@Api(value = "消息中心", tags = "15.个人中心")
public class MessageCenterController {

    @Resource
    private INoticeService noticeService;
    @Resource
    private IUserMessageService userMessageService;
    @Resource
    private INoticeReadService noticeReadService;


    @GetMapping("/system/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取系统通知列表")
    public R<List<NoticeVO>> getSystemNoticeList(Query query) {
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = Wrappers.lambdaQuery(Notice.class).orderByDesc(BaseEntity::getCreateTime);
        IPage<Notice> pages = noticeService.page(Condition.getPage(query), noticeLambdaQueryWrapper);
        List<NoticeVO> noticeVOS = NoticeWrapper.build().listVO(pages.getRecords());
        if (CollUtil.isNotEmpty(noticeVOS)) {
            for (NoticeVO noticeVO : noticeVOS) {
                Integer count = noticeReadService.lambdaQuery().eq(NoticeRead::getNoticeId, noticeVO.getId()).eq(NoticeRead::getUserId, AuthUtil.getUserId()).count();
                noticeVO.setIsRead(count > 0);
            }
        }
        return R.data(noticeVOS);
    }

    @GetMapping("/system/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取系统通知详情")
    public R<NoticeVO> getSystemNoticeDetail(@ApiParam("ID") @RequestParam("id") Long id) {
        Notice notice = noticeService.getById(id);
        Assert.notNull(notice);
        return R.data(NoticeWrapper.build().entityVO(notice));
    }

    @GetMapping("/user/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取用户通知列表")
    public R<List<UserMessageVO>> getUserNoticeList(@ApiParam("类型 1-交易通知 2-物流通知") @RequestParam("type") Integer type, Query query) {
        return R.data(userMessageService.getUserNoticeList(Condition.getPage(query), type, AuthUtil.getUserId()).getRecords());
    }

    @GetMapping("/user/read")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "用户消息标记已读")
    public R userRead(@ApiParam("用户消息id") @RequestParam("id") Long id) {
        UserMessage userMessage = userMessageService.getById(id);
        userMessage.setIsRead(true);
        userMessageService.updateById(userMessage);
        return R.data("ok");
    }

    @GetMapping("/system/read")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "系统消息标记已读")
    public R systemRead(@ApiParam("系统消息id") @RequestParam("id") Long id) {
        Notice notice = noticeService.getById(id);
        Assert.notNull(notice);

        NoticeRead noticeRead = new NoticeRead();
        noticeRead.setUserId(AuthUtil.getUserId());
        noticeRead.setNoticeId(notice.getId());
        noticeReadService.save(noticeRead);
        return R.data("ok");
    }


    @GetMapping("/read/statis")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "查询消息中心是否有未读的消息")
    public R<List<ReadStatisVO>> readStatis() {
        List<ReadStatisVO> list = new ArrayList<>();
        Long userId = AuthUtil.getUserId();

        ReadStatisVO vo1 = new ReadStatisVO();
        Integer count = userMessageService.lambdaQuery().eq(UserMessage::getUserId, userId)
                .eq(UserMessage::getType, MessageTypeEnum.TRADE.getValue())
                .eq(UserMessage::getIsRead, false).count();
        vo1.setType(1);
        vo1.setRead(count > 0);

        ReadStatisVO vo2 = new ReadStatisVO();
        Integer count2 = userMessageService.lambdaQuery().eq(UserMessage::getUserId, userId)
                .eq(UserMessage::getType, MessageTypeEnum.WL.getValue())
                .eq(UserMessage::getIsRead, false).count();
        vo2.setType(2);
        vo2.setRead(count2 > 0);

        ReadStatisVO vo3 = new ReadStatisVO();
        Integer total = noticeService.lambdaQuery().count();
        Integer userTotal = noticeReadService.lambdaQuery().eq(NoticeRead::getUserId, userId).count();
        vo3.setType(3);
        vo3.setRead(userTotal < total);

        list.add(vo1);
        list.add(vo2);
        list.add(vo3);
        return R.data(list);
    }

}
