package com.qmth.teachcloud.report.business.templete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.report.business.entity.TBPaper;
import com.qmth.teachcloud.common.enums.AssignEnum;
import com.qmth.teachcloud.report.business.service.AnalyzeForReportService;
import com.qmth.teachcloud.report.business.service.TBExamService;
import com.qmth.teachcloud.report.business.service.TBPaperService;
import com.qmth.teachcloud.report.business.templete.service.TaskCalculateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 任务实现逻辑实现类
 * @Author: CaoZixuan
 * @Date: 2021-06-18
 */
@Service
public class TaskCalculateServiceImpl implements TaskCalculateService {
    @Resource
    private AnalyzeForReportService analyzeForReportService;
    @Resource
    private TBExamService tbExamService;
    @Resource
    private TBPaperService tbPaperService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> dataCalculate(Map<String, Object> map) throws Exception {
        Long examId = SystemConstant.convertIdToLong(String.valueOf(map.get("examId")));
        String courseCode = String.valueOf(map.get("courseCode"));
        BigDecimal coefficient = BigDecimal.valueOf(Double.parseDouble(String.valueOf(map.get("coefficient"))));
        List<TBPaper> tbPaperList = tbPaperService.list(new QueryWrapper<TBPaper>().lambda()
                .eq(TBPaper::getExamId, examId).eq(TBPaper::getCourseCode, courseCode));
        for (TBPaper tbPaper : tbPaperList) {
            tbPaper.setCoefficient(coefficient);
            tbPaper.setScoreType(AssignEnum.NEED_ASSIGN_SCORE);
        }
        tbPaperService.updateBatchById(tbPaperList);
        analyzeForReportService.normalAssignScore(examId,tbExamService.getById(examId).getSchoolId(),courseCode);

        analyzeForReportService.dataCalculate(examId,courseCode);
        return map;
    }
}
