package com.sckw.message.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.model.vo.TableTop;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.message.dao.KwmMessageMapper;
import com.sckw.message.model.KwmMessage;
import com.sckw.message.model.dto.MessageListDTO;
import com.sckw.message.model.dto.SelectMessagesDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @desc: 消息相关service
 * @author: yzc
 * @date: 2023-06-09 10:58
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwmMessageService {

    private final KwmMessageMapper kwmMessageMapper;


    /**
     * @param msgIds, category, type, status
     * @return java.util.List<com.sckw.message.model.KwmMessage>
     * @desc: 根据条件获取消息列表
     * @author: yzc
     * @date: 2023-06-09 14:27
     */
    public List<KwmMessage> getList(List<Long> msgIds, String category, String type, Integer status) {
        LambdaQueryWrapper<KwmMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CollectionUtils.isNotEmpty(msgIds), KwmMessage::getId, msgIds)
                .eq(StringUtils.isNotBlank(category), KwmMessage::getCategory, category)
                .eq(StringUtils.isNotBlank(type), KwmMessage::getType, type)
                .eq(Objects.nonNull(status), KwmMessage::getStatus, status)
                .eq(KwmMessage::getDelFlag, 0).orderByAsc(KwmMessage::getStatus).orderByDesc(KwmMessage::getCreateTime);

        return CollectionUtil.emptyIfNull(kwmMessageMapper.selectList(wrapper));
    }


    /**
     * @param msgIds
     * @return void
     * @desc: 更新已读根据消息ids
     * @author: yzc
     * @date: 2023-06-09 14:36
     */
    @Transactional(rollbackFor = Exception.class)
    public void readByMsgIds(List<Long> msgIds) {
        LambdaUpdateWrapper<KwmMessage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwmMessage::getStatus, 1).in(KwmMessage::getId, msgIds);
        kwmMessageMapper.update(null, wrapper);
    }

    /**
     * @param ids
     * @return void
     * @desc: 根据ids删除
     * @author: yzc
     * @date: 2023-06-09 15:08
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByMsgIds(List<Long> ids) {
        LambdaUpdateWrapper<KwmMessage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwmMessage::getDelFlag, 1).in(KwmMessage::getId, ids);
        kwmMessageMapper.update(null, wrapper);
    }

    /**
     * @param kwmMessage
     * @return java.lang.Long
     * @desc: 插入消息
     * @author: yzc
     * @date: 2023-06-09 15:49
     */
    @Transactional(rollbackFor = Exception.class)
    public Long insert(KwmMessage kwmMessage) {
        kwmMessageMapper.insert(kwmMessage);
        return kwmMessage.getId();
    }

    /**
     * @param dto
     * @return java.util.List<com.sckw.message.model.dto.MessageListDTO>
     * @desc: 分页查询
     * @author: yzc
     * @date: 2023-06-13 9:08
     */
    public List<MessageListDTO> findPage(SelectMessagesDTO dto) {
        return kwmMessageMapper.findPage(dto);
    }

    /**
     * @desc: 统计查询
     * @author: yzc
     * @date: 2023-09-06 9:21
     * @Param dto:
     * @return: java.util.List<com.sckw.core.model.vo.TableTop>
     */
    public List<TableTop> statistics(SelectMessagesDTO dto) {
        return kwmMessageMapper.statistics(dto);
    }

    /**
     * @param id
     * @return com.sckw.message.model.KwmMessage
     * @desc: 根据id获取详情
     * @author: yzc
     * @date: 2023-06-13 15:15
     */
    public KwmMessage getById(Long id) {
        return kwmMessageMapper.selectById(id);
    }

    /**
     * @param msgIds, category
     * @return java.util.List<com.sckw.message.model.KwmMessage>
     * @desc: 消息统计
     * @author: yzc
     * @date: 2023-06-14 9:21
     */
    public List<KwmMessage> statistics(List<Long> msgIds, String category) {
        LambdaQueryWrapper<KwmMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwmMessage::getId, msgIds).eq(KwmMessage::getDelFlag, 0)
                .eq(StringUtils.isNotBlank(category), KwmMessage::getCategory, category)
                .orderByAsc(KwmMessage::getStatus).orderByDesc(KwmMessage::getCreateTime);
        return CollectionUtil.emptyIfNull(kwmMessageMapper.selectList(wrapper));
    }
}
