package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.params.analyze.GradeModuleKnowledgeDefineParam;
import com.qmth.distributed.print.business.bean.params.analyze.GradePaperDefineParam;
import com.qmth.distributed.print.business.entity.GradeModuleDefine;
import com.qmth.teachcloud.common.enums.DimensionEnum;
import com.qmth.distributed.print.business.mapper.GradeModuleDefineMapper;
import com.qmth.distributed.print.business.service.GradeBatchPaperService;
import com.qmth.distributed.print.business.service.GradeModuleDefineService;
import com.qmth.teachcloud.common.entity.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 分析-模块二级考察点维度熟练度配置，及模块简要描述表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Service
public class GradeModuleDefineServiceImpl extends ServiceImpl<GradeModuleDefineMapper, GradeModuleDefine> implements GradeModuleDefineService {
    @Resource
    private GradeBatchPaperService gradeBatchPaperService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveGradeModuleDefineBatch(GradePaperDefineParam gradePaperDefineParam, SysUser requestUser) {
        Long schoolId = requestUser.getSchoolId();
        String paperNumber = gradePaperDefineParam.getPaperNumber();
        String paperType = gradePaperDefineParam.getPaperType();
        String paperName = gradePaperDefineParam.getPaperName();

        String knowledgeInterpret = gradePaperDefineParam.getKnowledgeInterpret();
        String abilityInterpret = gradePaperDefineParam.getAbilityInterpret();

        List<GradeModuleKnowledgeDefineParam> gradeModuleKnowledgeDefineParamList = gradePaperDefineParam.getGradeModuleDefineParamList();
        // 针对知识的
        List<GradeModuleDefine> gradeModuleDefineList = gradeModuleKnowledgeDefineParamList.stream().flatMap(e -> {
            GradeModuleDefine gradeModuleDefine = new GradeModuleDefine();
            gradeModuleDefine.setSchoolId(schoolId);
            gradeModuleDefine.setPaperNumber(paperNumber);
            gradeModuleDefine.setPaperType(paperType);
            gradeModuleDefine.setPaperName(paperName);
            gradeModuleDefine.setModuleType(DimensionEnum.KNOWLEDGE);
            gradeModuleDefine.setInterpret(knowledgeInterpret);
            gradeModuleDefine.setDefine(e.getDefine());
            gradeModuleDefine.setLevel(e.getLevel());
            gradeModuleDefine.setMin(e.getMin());
            gradeModuleDefine.setMax(e.getMax());
            gradeModuleDefine.setScope(e.getScope());
            return Stream.of(gradeModuleDefine);
        }).collect(Collectors.toList());
        // 针对能力的
        GradeModuleDefine abilityDefine = new GradeModuleDefine();
        abilityDefine.setSchoolId(schoolId);
        abilityDefine.setPaperNumber(paperNumber);
        abilityDefine.setPaperType(paperType);
        abilityDefine.setPaperName(paperName);
        abilityDefine.setModuleType(DimensionEnum.ABILITY);
        abilityDefine.setInterpret(abilityInterpret);
        gradeModuleDefineList.add(abilityDefine);

        gradeBatchPaperService.checkOperateAuth(schoolId,paperNumber,paperType);
        this.remove(new QueryWrapper<GradeModuleDefine>()
                .lambda()
                .eq(GradeModuleDefine::getSchoolId,schoolId)
                .eq(GradeModuleDefine::getPaperNumber,paperNumber)
                .eq(GradeModuleDefine::getPaperType,paperType));
        this.saveBatch(gradeModuleDefineList);
    }

    @Override
    public List<GradeModuleDefine> findBySchoolIdAndPaperNumberAndPaperType(Long schoolId, String paperNumber, String paperType) {
        QueryWrapper<GradeModuleDefine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GradeModuleDefine::getSchoolId, schoolId)
                .eq(GradeModuleDefine::getPaperNumber, paperNumber)
                .eq(GradeModuleDefine::getPaperType, paperType);
        return this.baseMapper.selectList(queryWrapper);
    }
}
