package jnpf.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.mapper.FtbCultivateCourseMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseIdentityMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionExamIdentifyMapper;
import jnpf.cultivate.service.PositionCultivateIdentifyService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourseIdentity;
import jnpf.model.cultivate.po.position.FtbCultivatePositionExamIdentify;
import jnpf.model.cultivate.vo.identify.PostAndCourseCorrelationVo;
import jnpf.permission.vo.v2.position.PositionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class PositionCultivateIdentifyServiceImpl implements PositionCultivateIdentifyService {
    @Autowired
    private FtbCultivateCourseMapper ftbCultivateCourseMapper;
    @Autowired
    private FtbCultivatePositionExamIdentifyMapper ftbCultivatePositionExamIdentifyMapper;
    @Autowired
    private FtbCultivatePositionCourseIdentityMapper ftbCultivatePositionCourseIdentityMapper;

    @Autowired
    private UserApiV2Util userApiV2Util;

    @Override
    @SuppressWarnings("Duplicates")
    public List<PostAndCourseCorrelationVo> selectCorrelationDataList(String tableId) {
        List<PostAndCourseCorrelationVo> result = new ArrayList<>();
        // 岗位学习鉴定
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getIdentifyId, tableId);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        ftbCultivatePositionExamIdentifyMapper.selectList(queryWrapper).forEach(item -> {
            PostAndCourseCorrelationVo vo = new PostAndCourseCorrelationVo();
            // 岗位学习主键ID
            vo.setDataId(item.getPostLearnId());
//            PositionEntity positionInfoNewVO = positionApi.queryInfoById(item.getPostRankId());
            PositionVO positionInfoNewVO = userApiV2Util.infoPosition(item.getPostRankId(), null);
            if (Objects.nonNull(positionInfoNewVO)) {
                // 岗位名称
                vo.setPostName(positionInfoNewVO.getFullName());
            }
            result.add(vo);
        });
        // 岗位学习课程关联鉴定
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(FtbCultivatePositionCourseIdentity::getIdentityId, tableId);
        lambdaQuery.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        ftbCultivatePositionCourseIdentityMapper.selectList(lambdaQuery).forEach(item -> {
            PostAndCourseCorrelationVo vo = new PostAndCourseCorrelationVo();
            // 岗位学习主键ID
            vo.setDataId(item.getPostLearnId());
//            PositionEntity positionInfoNewVO = positionApi.queryInfoById(item.getPostRankId());
            PositionVO positionInfoNewVO = userApiV2Util.infoPosition(item.getPostRankId(), null);
            if (Objects.nonNull(positionInfoNewVO)) {
                // 岗位名称
                vo.setPostName(positionInfoNewVO.getFullName());
            }
            // 课程名
            FtbCultivateCourse ftbCultivateCourse = ftbCultivateCourseMapper.selectById(item.getCourseId());
            if (Objects.nonNull(ftbCultivateCourse)) {
                vo.setCourseName(ftbCultivateCourse.getName());
            }
            result.add(vo);
        });
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteCorrelationRecord(List<String> sourceIds) {
        // 岗位学习鉴定
        LambdaUpdateWrapper<FtbCultivatePositionExamIdentify> identifyLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        identifyLambdaUpdateWrapper.set(FtbCultivatePositionExamIdentify::getEnabledMark, 1);
        identifyLambdaUpdateWrapper.in(FtbCultivatePositionExamIdentify::getIdentifyId, sourceIds);
        ftbCultivatePositionExamIdentifyMapper.update(new FtbCultivatePositionExamIdentify(), identifyLambdaUpdateWrapper);
        // 岗位学习课程关联鉴定
        LambdaUpdateWrapper<FtbCultivatePositionCourseIdentity> courseLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        courseLambdaUpdateWrapper.set(FtbCultivatePositionCourseIdentity::getEnabledMark, 1);
        courseLambdaUpdateWrapper.in(FtbCultivatePositionCourseIdentity::getIdentityId, sourceIds);
        ftbCultivatePositionCourseIdentityMapper.update(new FtbCultivatePositionCourseIdentity(), courseLambdaUpdateWrapper);
    }
}
