package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.*;
import jnpf.cultivate.service.CultivateCourseMsgService;
import jnpf.model.cultivate.dto.course.app.FtbCultivateCourseMsgForAppDTO;
import jnpf.model.cultivate.dto.course.app.FtbUserLevelMessageReadDTO;
import jnpf.model.cultivate.entiy.BaseEntity;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.course.app.CultivateCourseMsg;
import jnpf.model.cultivate.po.mesgg.CultivateMessageInfo;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourceLearning;
import jnpf.model.cultivate.v2.course.vo.app.V2CultivateCourseMsgForAppDTO;
import jnpf.model.cultivate.vo.course.app.FtbCheckJobGradeChangeVO;
import jnpf.model.cultivate.vo.course.app.FtbCultivateCourseMsgForAppVO;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Title:CultivateCourseMsgServiceImpl
 * @Author:peng.hao
 * @create: 2023/12/2913:36
 */
@Slf4j
@Service
public class CultivateCourseMsgServiceImpl extends ServiceImpl<CultivateCourseMsgMapper, CultivateCourseMsg> implements CultivateCourseMsgService {

    @Resource
    private FtbCultivatePositionCourceLearningMapper ftbCultivatePositionCourceLearningMapper;

    @Resource
    private FtbCultivateMessageInfoMapper ftbCultivateMessageInfoMapper;


    @Override
    public List<FtbCultivateCourseMsgForAppVO> queryUserUnreadMsg() {
        String loginUserId = UserProvider.getLoginUserId();
        // 全部都是未学习课程，不提示，Special circumstances will not be considered
        LambdaQueryWrapper<FtbCultivatePositionCourceLearning> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourceLearning::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionCourceLearning::getUserId, loginUserId);
        queryWrapper.in(FtbCultivatePositionCourceLearning::getState, List.of(1, 2));
        if (ftbCultivatePositionCourceLearningMapper.selectCount(queryWrapper) > 0) {
            FtbCultivateCourseMsgForAppDTO dto = new FtbCultivateCourseMsgForAppDTO();
            dto.setUserId(loginUserId);
            dto.setStates(0);
            return baseMapper.queryUserUnreadMsg(dto);
        }
        return new ArrayList<>();
    }

    @Override
    public List<FtbCultivateCourseMsgForAppVO> queryUserUnreadMsgV2(String courseId) {
        String userId = UserProvider.getLoginUserId();

        List<FtbCultivateCourse> courseList = ftbCultivatePositionCourceLearningMapper.queryUserUnreadMsgV2(userId, courseId);

        if (CollUtil.isNotEmpty(courseList)) {
            V2CultivateCourseMsgForAppDTO dto = new V2CultivateCourseMsgForAppDTO();
            dto.setUserId(userId);
            dto.setCourseIds(courseList.stream().map(FtbCultivateCourse::getId).collect(Collectors.toList()));
            return baseMapper.queryUserUnreadMsgV2(dto);
        }
        return new ArrayList<>();
    }

    @Override
    public void updateInfoByMsgIdV2(String courseId) {
        String loginUserId = UserProvider.getLoginUserId();
        baseMapper.deleteInfoByMsgIdV2(loginUserId, courseId);
    }

    @Override
    public List<FtbCheckJobGradeChangeVO> checkJobGradeChanges(Integer type) {
        LambdaQueryWrapper<CultivateMessageInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CultivateMessageInfo::getUserId, UserProvider.getLoginUserId());
        queryWrapper.eq(CultivateMessageInfo::getSource, type);
        List<CultivateMessageInfo> cultivateMessageInfos = ftbCultivateMessageInfoMapper.selectList(queryWrapper);
        return cultivateMessageInfos.stream().map(cultivateMessageInfo -> {
            FtbCheckJobGradeChangeVO ftbCheckJobGradeChangeVO = new FtbCheckJobGradeChangeVO();
            ftbCheckJobGradeChangeVO.setId(cultivateMessageInfo.getId());
            return ftbCheckJobGradeChangeVO;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userLevelMessageHasBeenRead(FtbUserLevelMessageReadDTO ftbUserLevelMessageReadDTO) {
        LambdaQueryWrapper<CultivateMessageInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(BaseEntity::getId, ftbUserLevelMessageReadDTO.getPrimaryKeyId());
        ftbCultivateMessageInfoMapper.delete(queryWrapper);
    }

    @Override
    public List<FtbCultivateCourseMsgForAppVO> queryUserUnreadMsgUpdate(String courseId) {
        String loginUserId = UserProvider.getLoginUserId();
        // 全部都是未学习课程，不提示，Special circumstances will not be considered
        LambdaQueryWrapper<FtbCultivatePositionCourceLearning> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourceLearning::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionCourceLearning::getUserId, loginUserId);
        queryWrapper.in(FtbCultivatePositionCourceLearning::getState, List.of(1, 2));
        if (ftbCultivatePositionCourceLearningMapper.selectCount(queryWrapper) > 0) {
            FtbCultivateCourseMsgForAppDTO dto = new FtbCultivateCourseMsgForAppDTO();
            dto.setUserId(loginUserId);
            dto.setStates(1);
            dto.setCourseId(courseId);
            return baseMapper.queryUserUnreadMsg(dto);
        }
        return new ArrayList<>();
    }

}
