package com.sckw.message.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.message.dao.KwmMessageUserMapper;
import com.sckw.message.model.KwmMessageUser;
import com.sckw.message.model.dto.UserMsgCenterLatestMsgDTO;
import com.sckw.message.model.dto.UserMsgCenterReqDTO;
import com.sckw.message.model.vo.res.UserCenterMsgVO;
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;
import java.util.stream.Collectors;

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

    private final KwmMessageUserMapper kwmMessageUserMapper;

    /**
     * @param userId
     * @return java.lang.Long
     * @desc: 获取用户未删除消息ids
     * @author: yzc
     * @date: 2023-06-09 14:24
     */
    public List<Long> getMsgIdsByUserId(Long userId) {
        List<KwmMessageUser> list = getByUserId(userId);
        return list.stream().map(KwmMessageUser::getMsgId).collect(Collectors.toList());
    }

    /**
     * @desc: 根据用户id获取
     * @author: yzc
     * @date: 2023-06-16 11:18
     * @Param userId:
     * @return: java.util.List<com.sckw.message.model.KwmMessageUser>
     */
    public List<KwmMessageUser> getByUserId(Long userId) {
        LambdaQueryWrapper<KwmMessageUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwmMessageUser::getUserId, userId).eq(KwmMessageUser::getDelFlag, 0);
        return CollectionUtils.emptyIfNull(kwmMessageUserMapper.selectList(wrapper));
    }

    /**
     * @param userId, msgIds
     * @return void
     * @desc: 更新为已读根据用户id及消息ids
     * @author: yzc
     * @date: 2023-06-09 14:30
     */
    @Transactional(rollbackFor = Exception.class)
    public void readByUserAndMsgIds(Long userId, List<Long> msgIds) {
        LambdaUpdateWrapper<KwmMessageUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwmMessageUser::getStatus, 1)
                .eq(KwmMessageUser::getUserId, userId).in(KwmMessageUser::getMsgId, msgIds);
        kwmMessageUserMapper.update(null, wrapper);
    }

    /**
     * @param userId, msgIds
     * @return void
     * @desc: 根据用户删除消息
     * @author: yzc
     * @date: 2023-06-09 15:07
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByUserAndMsgIds(Long userId, List<Long> msgIds) {
        LambdaUpdateWrapper<KwmMessageUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwmMessageUser::getDelFlag, 1)
                .eq(KwmMessageUser::getUserId, userId).in(KwmMessageUser::getMsgId, msgIds);
        kwmMessageUserMapper.update(null, wrapper);
    }


    /**
     * @param messageUsers
     * @return void
     * @desc: 批量插入
     * @author: yzc
     * @date: 2023-06-09 15:58
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(List<KwmMessageUser> messageUsers) {
        kwmMessageUserMapper.insertBatch(messageUsers);
    }

    /**
     * @desc: 通过消息id和userId获取
     * @author: yzc
     * @date: 2023-06-16 11:09
     * @Param msgId:
     * @Param userId:
     * @return: com.sckw.message.model.KwmMessageUser
     */
    public KwmMessageUser getByMsgIdAndUserId(Long msgId, Long userId) {
        LambdaQueryWrapper<KwmMessageUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwmMessageUser::getUserId, userId).eq(KwmMessageUser::getMsgId, msgId)
                .eq(KwmMessageUser::getDelFlag, 0).last("LIMIT 1");
        return kwmMessageUserMapper.selectOne(wrapper);
    }

    /**
     * @desc: 根据msgIds获取
     * @author: yzc
     * @date: 2023-09-04 11:50
     * @Param ids:
     * @return: java.util.List<com.sckw.message.model.KwmMessageUser>
     */
    public List<KwmMessageUser> getByMsgIds(List<Long> ids) {
        LambdaQueryWrapper<KwmMessageUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwmMessageUser::getId, ids)
                .eq(KwmMessageUser::getDelFlag, Global.NO);
        Integer isMain = LoginUserHolder.getIsMain();
        if (Objects.equals(isMain, 1)) {
            wrapper.eq(KwmMessageUser::getEntId, LoginUserHolder.getEntId());
        } else {
            wrapper.eq(KwmMessageUser::getUserId, LoginUserHolder.getUserId());
        }
        List<KwmMessageUser> list = kwmMessageUserMapper.selectList(wrapper);
        return CollectionUtils.emptyIfNull(list);
    }

    /**
     * @desc: 根据msgIds更新状态
     * @author: yzc
     * @date: 2023-09-04 11:56
     * @Param ids:
     * @return: void
     */
    public void readByMsgIds(List<Long> ids) {
        LambdaUpdateWrapper<KwmMessageUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwmMessageUser::getStatus, Global.YES)
                .in(KwmMessageUser::getId, ids);
        Integer isMain = LoginUserHolder.getIsMain();
        if (Objects.equals(isMain, 1)) {
            wrapper.eq(KwmMessageUser::getEntId, LoginUserHolder.getEntId());
        } else {
            wrapper.eq(KwmMessageUser::getUserId, LoginUserHolder.getUserId());
        }
        kwmMessageUserMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据用户或企业，状态升序，type分组统计消息数量，查找前五条
     * @author: yzc
     * @date: 2023-09-07 15:40
     * @Param dto:
     * @return: java.util.List<com.sckw.message.model.vo.res.UserCenterMsgVO>
     */
    public List<UserCenterMsgVO> userMsgCenter(UserMsgCenterReqDTO dto) {
        List<UserCenterMsgVO> list = kwmMessageUserMapper.userMsgCenter(dto);
        return CollectionUtils.emptyIfNull(list);
    }

    /**
     * @desc: 获取最新消息
     * @author: yzc
     * @date: 2023-09-07 17:42
     * @Param dto:
     * @return: com.sckw.message.model.dto.UserMsgCenterLatestMsgDTO
     */
    public UserMsgCenterLatestMsgDTO getLatestMsg(UserMsgCenterReqDTO dto) {
        return kwmMessageUserMapper.getLatestMsg(dto);
    }

    /**
     * @desc: 统计消息数量
     * @author: yzc
     * @date: 2023-09-21 15:18
     * @Param entId:
     * @Param userId:
     * @Param status:
     * @return: java.lang.Long
     */
    public Long count(Long entId, Long userId, Integer status) {
        LambdaUpdateWrapper<KwmMessageUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Objects.nonNull(entId), KwmMessageUser::getEntId, entId)
                .eq(Objects.nonNull(userId), KwmMessageUser::getUserId, userId)
                .eq(Objects.nonNull(status), KwmMessageUser::getStatus, status)
                .eq(KwmMessageUser::getDelFlag, Global.NO);
        return kwmMessageUserMapper.selectCount(wrapper);
    }
}
