package jnpf.model.cultivate.v2.position.req;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class FtbCultivatePositionSaveReq implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 培训岗位学习主键id，没有值则新增
     */
    private String id;

    /**
     * 岗位ID
     */
    @NotNull(message = "岗位ID不能为空")
    private String postId;

    /**
     * 是否配置到职级：[0-否，1-是]
     */
    private Integer isConfiguredToGrade;

    private List<FtbCultivatePositionSettingReq> setting;

    /**
     * 课程列表
     */
    private List<FtbCultivatePositionCourseReq> courseList;

    public void preCheckParams() {
        if (isConfiguredToGrade == null) {
            isConfiguredToGrade = 0;
        }
        //入参检查
        if (CollUtil.isEmpty(courseList)) {
            throw new RuntimeException("岗位学习信息不能为空");
        }
        //如果没有设置到职级 所有的都在岗位下 职级应该为空字符串
        Map<String, String> examRepateMap = new HashMap<>();
        Map<String, String> identityRepateMap = new HashMap<>();
        Map<String, String> practiceRepateMap = new HashMap<>();
        Map<String, String> certRepateMap = new HashMap<>();
        for (FtbCultivatePositionCourseReq course : courseList) {
            if(StringUtils.isEmpty(course.getGradeId())){
                course.setGradeId(StringUtils.EMPTY);
            }
            if (course.getExam() != null) {
                if (examRepateMap.containsKey(course.getExam().getExamId())) {
                    throw new RuntimeException("在课程[" + course.getCourseName() + "]和课程" + "[" + examRepateMap.get(course.getExam().getExamId()) + "]" + "考试重复");
                }
                examRepateMap.put(course.getExam().getExamId(), course.getCourseName());
            }
            if (course.getIdentity() != null) {
                if (identityRepateMap.containsKey(course.getIdentity().getIdentityId())) {
                    throw new RuntimeException("在课程[" + course.getCourseName() + "]和课程" + "[" + identityRepateMap.get(course.getIdentity().getIdentityId()) + "]" + "鉴定重复");
                }
                identityRepateMap.put(course.getIdentity().getIdentityId(), course.getCourseName());
            }
            if (course.getPracticeList() != null && !course.getPracticeList().isEmpty()) {
                for (FtbCultivatePositionCoursePracticeReq practice : course.getPracticeList()) {
                    if (practiceRepateMap.containsKey(practice.getBusinessId())) {
                        throw new RuntimeException("在课程[" + course.getCourseName() + "]和课程" + "[" + practiceRepateMap.get(practice.getBusinessId()) + "]" + "练习重复");
                    }
                    practiceRepateMap.put(practice.getBusinessId(), course.getCourseName());
                }
            }
            if (course.getCertificate() != null) {
                if (certRepateMap.containsKey(course.getCertificate().getCertificateId())) {
                    throw new RuntimeException("在课程[" + course.getCourseName() + "]和课程" + "[" + certRepateMap.get(course.getCertificate().getCertificateId()) + "]" + "证书重复");
                }
                certRepateMap.put(course.getCertificate().getCertificateId(), course.getCourseName());
            }
        }
        for (FtbCultivatePositionSettingReq settingReq : setting) {
            if (StringUtils.isEmpty(settingReq.getGradeId())) {
                settingReq.setGradeId(StringUtils.EMPTY);
            }
        }
    }
}
