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.utils.CollectionUtils;
import com.sckw.message.dao.KwmMessageUserMapper;
import com.sckw.message.model.KwmMessageUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @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).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: 批量插入（不直接批量插入，防止daoAspect切面失效）
     * @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);
    }
}
