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

import cn.hutool.core.collection.CollUtil;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionNew;
import jnpf.model.cultivate.v2.promotion.vo.V2CultivatePostAndGradeVo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
public class V2CultivatePromotionCreateReq implements Serializable {
    private static final long serialVersionUID = 1L;


    /**
     * 学习地图主键id
     */
    private String id;

    /**
     * 学习地图名称
     */
    @NotNull(message = "学习地图名称不能为空")
    @Size(max = 50, message = "学习地图名称不能超过50个字符")
    private String promotion;


    /**
     * 学习地图设置(1是,0否)
     * 岗位考试与岗位实操鉴定合格后才能进入下一阶段 (v1.2)
     */
    private Integer learningMapSetting;


    /**
     * 学习地图ID，生成规则见原型
     */
    private String promotionBusinessId;


    /**
     * 通道分级岗位
     */
    private List<V2CultivatePromotionPostReq> phaseChannel;

    /**
     * 范围 ：岗位 、职级 、人员
     */
    private List<V2CultivatePromotionScopeReq> scope;

    public static FtbCultivatePromotionNew coverFtbCultivatePromotionCreatDto(V2CultivatePromotionCreateReq creatDto,String userId) {
        if (creatDto == null) {
            return null;
        }
        FtbCultivatePromotionNew ftbCultivatePromotion = new FtbCultivatePromotionNew();
        ftbCultivatePromotion.setId(creatDto.getId());
        ftbCultivatePromotion.setPromotion(creatDto.getPromotion());
        ftbCultivatePromotion.setLearningMapSetting(creatDto.getLearningMapSetting());
        ftbCultivatePromotion.setLastModifyTime(new Date());
        ftbCultivatePromotion.setLastModifyUserId(userId);
        return ftbCultivatePromotion;
    }

    public void preParamCheck() {
        if (StringUtils.isEmpty(promotion)) {
            throw new IllegalArgumentException("学习地图名称不能为空");
        }
        if (CollUtil.isEmpty(scope)) {
            throw new IllegalArgumentException("分配学习地图不能为空");
        }
        if (CollUtil.isEmpty(phaseChannel)) {
            throw new IllegalArgumentException("地图阶段配置不能为空");
        }
        for (int i = 0; i < phaseChannel.size(); i++) {
            V2CultivatePromotionPostReq postReq = phaseChannel.get(i);
            if (CollUtil.isEmpty(postReq.getPostAndGradeVoList())) {
                throw new IllegalArgumentException("第" + (i + 1) + "阶段岗位配置不能为空");
            }
            if (postReq.getSelectCourseNumber() <= 0 || postReq.getSelectCourseNumber() > postReq.getPostAndGradeVoList().size()) {
                throw new IllegalArgumentException("第" + (i + 1) + "阶段选学数量不正确");
            }

            for (V2CultivatePostAndGradeVo v2CultivatePostAndGradeVo : postReq.getPostAndGradeVoList()) {
                if(StringUtils.isEmpty(v2CultivatePostAndGradeVo.getGradeId())){
                    v2CultivatePostAndGradeVo.setGradeId("");
                    v2CultivatePostAndGradeVo.setGradeName("");
                }
            }
        }
    }
}
