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.GradeModuleEvaluationDatasource;
import com.qmth.distributed.print.business.bean.params.analyze.GradeModuleEvaluationParam;
import com.qmth.distributed.print.business.bean.result.analyze.GradeModuleEvaluationResult;
import com.qmth.distributed.print.business.entity.GradeModuleEvaluation;
import com.qmth.distributed.print.business.mapper.GradeModuleEvaluationMapper;
import com.qmth.distributed.print.business.service.GradeBatchPaperService;
import com.qmth.distributed.print.business.service.GradeModuleEvaluationService;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
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;
import java.util.stream.Stream;

/**
 * <p>
 * 校验-考察点模块评价建议表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Service
public class GradeModuleEvaluationServiceImpl extends ServiceImpl<GradeModuleEvaluationMapper, GradeModuleEvaluation> implements GradeModuleEvaluationService {
    @Resource
    private GradeBatchPaperService gradeBatchPaperService;

    @Override
    public List<GradeModuleEvaluationResult> findGradeModuleEvaluation(String paperNumber, String paperType, SysUser requestUser) {
        Long schoolId = requestUser.getSchoolId();
        List<GradeModuleEvaluation> evaluationList = this.list(new QueryWrapper<GradeModuleEvaluation>()
                .lambda()
                .eq(GradeModuleEvaluation::getSchoolId, schoolId)
                .eq(GradeModuleEvaluation::getPaperNumber, paperNumber)
                .eq(GradeModuleEvaluation::getPaperType, paperType));

        List<GradeModuleEvaluationResult> result = new ArrayList<>();
        if (evaluationList != null && evaluationList.size() > 0) {
            result = evaluationList.stream().flatMap(e -> {
                GradeModuleEvaluationResult cell = new GradeModuleEvaluationResult();
                cell.setModuleType(e.getModuleType());
                cell.setFormula(e.getFormula());
                String scope = e.getScope();
                // 解析区间
                String[] scopeArr = scope.substring(1, scope.length() - 1).split(",");
                if (scopeArr.length != 2) {
                    throw ExceptionResultEnum.ERROR.exception("区间【" + scope + "】解析失败");
                }
                cell.setScope(scope);
                cell.setMin(scopeArr[0]);
                cell.setMax(scopeArr[1]);
                cell.setLevelCode(e.getLevelCode());
                cell.setLevelName(e.getLevelName());
                cell.setResult(e.getResult());
                cell.setAdvice(e.getAdvice());
                return Stream.of(cell);
            }).collect(Collectors.toList());
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveGradeModuleEvaluationBatch(GradeModuleEvaluationParam gradeModuleEvaluationParam, SysUser requestUser) {
        Long schoolId = requestUser.getSchoolId();
        String paperNumber = gradeModuleEvaluationParam.getPaperNumber();
        String paperType = gradeModuleEvaluationParam.getPaperType();
        String paperName = gradeModuleEvaluationParam.getPaperName();
        List<GradeModuleEvaluationDatasource> datasource = gradeModuleEvaluationParam.getGradeModuleEvaluationDatasourceList();

        List<GradeModuleEvaluation> gradeModuleEvaluationList = datasource.stream().flatMap(e -> {
            GradeModuleEvaluation cell = new GradeModuleEvaluation();
            cell.setSchoolId(schoolId);
            cell.setPaperNumber(paperNumber);
            cell.setPaperType(paperType);
            cell.setPaperName(paperName);
            cell.setModuleType(e.getModuleType());
            cell.setFormula(e.getFormula());
            cell.setScope(e.getScope());
            cell.setLevelCode(e.getLevelCode());
            cell.setLevelName(e.getLevelName());
            cell.setResult(e.getResult());
            cell.setAdvice(e.getAdvice());
            return Stream.of(cell);
        }).collect(Collectors.toList());

        // 根据试卷编号和试卷类型判断分析课程是否有操作权限(批次分析试卷没有在计算中的才能操作)
        gradeBatchPaperService.checkOperateAuth(schoolId, paperNumber, paperType);
        // 删除原有的模块评价
        this.remove(new QueryWrapper<GradeModuleEvaluation>()
                .lambda()
                .eq(GradeModuleEvaluation::getSchoolId, schoolId)
                .eq(GradeModuleEvaluation::getPaperNumber, paperNumber)
                .eq(GradeModuleEvaluation::getPaperType, paperType));

        // 新增
        this.saveBatch(gradeModuleEvaluationList);
    }

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