package com.kxs.system.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.system.api.model.KxsUserMsg;
import com.kxs.system.api.vo.kxsapp.NotReadCountVo;
import com.kxs.system.api.vo.kxsapp.userMsg.UserMsgPageVO;
import com.kxs.system.api.vo.kxsapp.userMsg.UserMsgVO;
import com.kxs.system.biz.constant.SysConstants;
import com.kxs.system.biz.mapper.KxsSysMsgMapper;
import com.kxs.system.biz.mapper.KxsSysMsgReadMapper;
import com.kxs.system.biz.mapper.KxsUserMsgMapper;
import com.kxs.system.biz.service.KxsUserMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 系统用户消息推送表(KxsUserMsg)表服务实现类
 *
 * @author 系统
 * @since 2023-12-15 12:16:32
 */
@Service("kxsUserMsgService")
@RequiredArgsConstructor
public class KxsUserMsgServiceImpl extends ServiceImpl<KxsUserMsgMapper, KxsUserMsg> implements KxsUserMsgService {


    private final RedisTemplate<String, Object> redisTemplate;

    private final KxsSysMsgMapper kxsSysMsgMapper;

    @Override
    public IPage<UserMsgPageVO> getByPage(Page<UserMsgPageVO> page) {

        return baseMapper.getByPage(page);
    }

    @Override
    public UserMsgVO getByData(Integer id) {

        KxsUserMsg kxsUserMsg = baseMapper.selectById(id);
        if(kxsUserMsg == null){
            return null;
        }
        //修改未读
        kxsUserMsg.setReadFlag(SysConstants.READ_FLAG_TRUE);
        baseMapper.updateById(kxsUserMsg);

        UserMsgVO userMsgVO = new UserMsgVO();
        BeanUtils.copyProperties(kxsUserMsg, userMsgVO);

        return userMsgVO;
    }

    @Override
    public void readAll() {

        Long userId = SecurityUtils.getUser().getId();

        LambdaUpdateWrapper<KxsUserMsg> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KxsUserMsg::getUserId, userId).set(KxsUserMsg::getReadFlag, SysConstants.READ_FLAG_TRUE);

        baseMapper.update(null, updateWrapper);

    }

    @Override
    public NotReadCountVo notreads() {

        NotReadCountVo notReadCountVo = new NotReadCountVo();

        Long userId = SecurityUtils.getUser().getId();
        //系统消息存储的key
        Long sysCount = kxsSysMsgMapper.getNotReadCount(userId);
        notReadCountVo.setPlacardCount(sysCount);

        //个人消息未读数
        Long userMsgCount = baseMapper.selectCount(Wrappers.<KxsUserMsg>lambdaQuery()
                .eq(KxsUserMsg::getUserId, userId)
                .eq(KxsUserMsg::getReadFlag, SysConstants.READ_FLAG_FALSE));
        notReadCountVo.setPersonalCount(userMsgCount);

        return notReadCountVo;
    }

    @Override
    public Boolean saveMsg(KxsUserMsg userMsg) {

        return this.save(userMsg);
    }


}

