package com.xhkjedu.smarking.service.wholeparam;

import com.xhkjedu.smarking.mapper.wholeparam.MspReportparamMapper;
import com.xhkjedu.smarking.model.wholeparam.MspReportparam;
import com.xhkjedu.smarking.vo.wholeparam.GradeParams;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description：阅卷指标参数-报考参数表
 * @Author: WN
 * @Date: 2024/12/2 16:19:44
 **/
@Service
public class MspReportparamService {
    @Resource
    private MspReportparamMapper mspReportparamMapper;

    /*
     * @Description 保存参数
     * @Date 2024/12/2 16:46:10
     * @Author WN
     * @Param [mspGradeparam]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void saveGradeparam(GradeParams gradeParams) {
        List<MspReportparam> originalParams = mspReportparamMapper.getGradeParamByGradeAndBelong(gradeParams);

        Integer timestamp = N_Utils.getSecondTimestamp();
        List<MspReportparam> params = gradeParams.getParams();
        List<Integer> delids = new ArrayList<>();
        for(MspReportparam param : params) {
            param.setRpbelong(gradeParams.getRpbelong());
            param.setGradeid(gradeParams.getGradeid());
            param.setHandleid(gradeParams.getHandleid());
            param.setSchoolid(gradeParams.getSchoolid());
            param.setHandletime(timestamp);
            if(param.getRptype() == null) param.setRptype(0);
            if(param.getGradenum() == null) param.setGradenum(0);

            //如果同类型同科目的数据已存在，删除原数据
            if(originalParams!= null && originalParams.size() > 0) {
                MspReportparam oldParam;
                if(N_Utils.isNotEmpty(param.getSubjectid())){
                    oldParam = originalParams.stream().filter(p -> param.getRptype().equals(p.getRptype()) && param.getSubjectid().equals(p.getSubjectid())).findFirst().orElse(null);
                }else{
                    oldParam = originalParams.stream().filter(p -> param.getRptype().equals(p.getRptype())).findFirst().orElse(null);
                }
                if(oldParam!= null) {
                    delids.add(oldParam.getRpid());
                }
            }
        }
        if(delids.size() > 0) {
            mspReportparamMapper.deleteGradeparam(delids);
        }

        mspReportparamMapper.insertList(params);
    }

    /*
     * @Description 获取参数
     * @Date 2024/12/2 16:46:15
     * @Author WN
     * @Param [mspGradeparam]
     * @Return com.xhkjedu.smarking.model.wholeparam.MspGradeparam
     **/
    public List<MspReportparam> getMspGradeparam(GradeParams mspGradeparam) {
        return mspReportparamMapper.getGradeParamByGradeAndBelong(mspGradeparam);
    }
}
