package com.xisc.pm.app.service.strategy.processgenerate;

import com.xisc.pm.api.dto.ProcessGenerateStrategyDTO;
import com.xisc.pm.app.service.strategy.ProcessGenerateStrategyFactory;
import com.xisc.pm.domain.entity.PmCmdLargePlt;
import com.xisc.pm.domain.vo.MmPlateVO;
import com.xisc.pm.infra.enums.PdMatType;
import com.xisc.pm.infra.enums.PdProcessType;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/12/17 8:44
 * @description
 */
@Component
public class ProcessGenerateStrategyP6 extends DefaultProcessGenerateStrategy {
    @Override
    public void afterPropertiesSet() throws Exception {
        ProcessGenerateStrategyFactory.register(PdProcessType.P6, this);
    }

    @Override
    public String getProcessCode(ProcessGenerateStrategyDTO dto) {
        String result = "";
        String process = "";
        // 第一道堆冷工序
        process = finishingProcess(dto.getMatDesignKind(), dto.getCoolCode(), dto.getPlateDtCode(), dto.getRollLineNo());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 热处理前火切工序
        process = fireCuttingProcess(dto.getMatDesignKind(), dto.getPlateDtCode(), dto.getRollLineNo(), dto.getShpCode(), dto.getWholeBacklog(), dto.getOrderLen());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 余材钢板组板的火切工序
        process = plateProcess(dto.getMatDesignKind(), dto.getPmCmdLargePlt(), dto.getPlateDtCode(), dto.getRollLineNo(), dto.getWholeBacklog(),
                dto.getMmPlateVO(), dto.getMmHtmrsltNum(), dto.getOrderLen());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 抛丸工序
        process = finishedShotProcess(dto.getShotFlag(), dto.getRollLineNo());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 探伤工序 - 不是见证合同、不包含热处理工序、且是否探伤为1
        if (StringUtils.isEmpty(dto.getWitnessStatus()) && "1".equals(dto.getIsUt()) && !(dto.getWholeBacklog().contains("PA") || dto.getWholeBacklog().contains("PB")
                || dto.getWholeBacklog().contains("PC"))) {
            process = generateCrackProcess(dto.getPlateDtCode(), dto.getRollLineNo(), dto.getUtCheckCode(), dto.getWitnessStatus(), dto.getUltDetectStd());
            if (StringUtils.isNotBlank(process)) {
                result = result + process + "-";
            }
        }

        return result;
    }

    /**
     * 第一道精整工序-缓冷工序
     *
     * @param matDesignKind 材料设计类型
     * @param coolCode      缓冷代码
     * @param PlateDtCode   大板去向
     * @param rollLineNo    轧制产线号
     * @return
     */
    public String finishingProcess(String matDesignKind, String coolCode, String PlateDtCode, String rollLineNo) {
        String process = "";
        if ("122".equals(coolCode)) {
            if ("30".equals(rollLineNo)) {
                process = "5A9";
            } else {
                throw new CommonException("pm.cmd_large_plt_003");
            }
        } else if ((Objects.nonNull(coolCode) && !"N".equals(coolCode)
                && !"1".equals(coolCode.substring(0, 1))) && !PdMatType.P.getCode().equals(matDesignKind)
                && ("3".equals(PlateDtCode.substring(0, 1)) || "2".equals(PlateDtCode.substring(0, 1)))) {
            // 其他情况（缓冷代码不为N/缓冷代码不为空/缓冷代码第一位不为1，不是余材钢板组板，轧制大板去向取值为3或2）
            switch (rollLineNo) {
                case "10":
                    process = "1D0";
                    break;
                case "20":
                    process = "2D0";
                    break;
                case "30":
                    process = "5D0";
                    break;
                default:
            }
        }
        return process;
    }

    /**
     * 精整火切工序
     *
     * @param matDesignKind 材料设计类型
     * @param PlateDtCode   轧制大板去向
     * @param rollLineNo    轧制产线号
     * @param shpCode       钢板形状代码
     * @param wholeBacklog  全程工序途径码
     * @return
     */
    public String fireCuttingProcess(String matDesignKind, String PlateDtCode, String rollLineNo, String shpCode, String wholeBacklog, BigDecimal orderLen) {
        String process = "";
        // 非余材
        if (PdMatType.P.getCode().equals(matDesignKind)) {
            return process;
        }

        // 有热处理要求且合同长度小于4m，则生成G5
        if (wholeBacklog.contains("PA") && orderLen.compareTo(BigDecimal.valueOf(4000)) < 0) {
            switch (rollLineNo) {
                case "10":
                    process = "1G6";
                    break;
                case "20":
                    process = "2G6";
                    break;
                case "30":
                    process = "5G6";
                    break;
                default:
            }
        } else if ((("3".equals(PlateDtCode.substring(0, 1)) || "2".equals(PlateDtCode.substring(0, 1))) && !PdMatType.P.getCode().equals(matDesignKind)) ||
                Objects.nonNull(shpCode)) {
            // 精整火切工序，根据轧制大板去向、切割方式、组板方式决定。即“轧制大板去向取值为3或者2，材料设计类型不为余材钢板组板或者“钢板形状代码不为空
                switch (rollLineNo) {
                    case "10":
                        process = "1G1";
                        break;
                    case "20":
                        process = "2G1";
                        break;
                    case "30":
                        process = "5G1";
                        break;
                    default:
                }
        }
        return process;
    }

    public String plateProcess(String matDesignKind, PmCmdLargePlt pmCmdLargePlt, String plateDtCode, String rollLineNo,
                               String wholeBacklog, MmPlateVO mmPlateVO, Long mmHtmrsltNum, BigDecimal orderLen) {
        if (!PdMatType.P.getCode().equals(matDesignKind)) {
            return null;
        }
        String process = "";

        // 获取合同长度小于4m,则生成G5
        if (wholeBacklog.contains("PA") && orderLen.compareTo(BigDecimal.valueOf(4000)) < 0) {
            switch (rollLineNo) {
                case "10":
                    process = "1G6";
                    break;
                case "20":
                    process = "2G6";
                    break;
                case "30":
                    process = "5G6";
                    break;
                default:
            }
        } else {
            // 含有热处理模式，做过热梳理实绩
            if (wholeBacklog.contains("PA") && mmHtmrsltNum > 0 ) {
                switch (rollLineNo) {
                    case "10":
                        process = "1G2";
                        break;
                    case "20":
                        process = "2G2";
                        break;
                    case "30":
                        process = "5G2";
                        break;

                }
            }
            // 不包含热处理模式
            if (!wholeBacklog.contains("PA")){
                switch (rollLineNo) {
                    case "10":
                        process = "1G1";
                        break;
                    case "20":
                        process = "2G1";
                        break;
                    case "30":
                        process = "5G1";
                        break;
                    default:
                }
            }
        }
        return process;
//        if (pmCmdLargePlt.getTotSmallPlateNum() > 1) {
//            // 如果轧制大板去向不为3不为2且产线号第三四位为30，不做处理
//            if ((!"3".equals(plateDtCode.substring(0, 1)) && !"2".equals(plateDtCode.substring(0, 1)))
//                    && "30".equals(rollLineNo)) {
//                return null;
//            }
//            switch (rollLineNo) {
//                case "10":
//                    process = "1G1";
//                    break;
//                case "20":
//                    process = "2G1";
//                    break;
//                case "30":
//                    if (Objects.nonNull(pmCmdLargePlt.getShpCode())) {
//                        process = "5G2";
//                    } else {
//                        process = "5G1";
//                    }
//                    break;
//                default:
//            }
//
//        } else if (pmCmdLargePlt.getTotSmallPlateNum() == 1) {
//            // 如果“材料实际宽度>小板宽度”或“材料实际长度>小板长度”或“press_appoint不为0”或“头部试样长不为0”或“中部试样长不为0”或“尾部试样长不为0”或“奉送样长度不为0”：
//            if (mmPlateVO.getMatActWid().compareTo(Utils.getBigDecimalValue(pmCmdLargePlt.getCombinePlateWidth())) > 0
//                    || mmPlateVO.getMatActLen().compareTo(Utils.getBigDecimalValue(pmCmdLargePlt.getCombinePlateLen())) > 0
//                    || !"0".equals(pmCmdLargePlt.getPressAppoint())
//                    || Utils.getBigDecimalValue(pmCmdLargePlt.getSampleLenHead()).compareTo(BigDecimal.ZERO) != 0
//                    || Utils.getBigDecimalValue(pmCmdLargePlt.getSampleLenMid()).compareTo(BigDecimal.ZERO) != 0
//                    || Utils.getBigDecimalValue(pmCmdLargePlt.getSampleLenTail()).compareTo(BigDecimal.ZERO) != 0
//                    || Utils.getBigDecimalValue(pmCmdLargePlt.getSampleLenGive()).compareTo(BigDecimal.ZERO) != 0) {
//                Boolean b = wholeBacklog.contains("PA");
//                // 如果“a存在”且“b存在”且“轧制大板去向为2或3，不做处理
//                if (mmHtmrsltNum > 0 && b && ("3".equals(plateDtCode.substring(0, 1)) || "2".equals(plateDtCode.substring(0, 1)))) {
//                    return process;
//                }
//                if (mmHtmrsltNum > 0 && b) {
//                    switch (rollLineNo) {
//                        case "10":
//                            process = "1G2";
//                            break;
//                        case "20":
//                            process = "2G2";
//                            break;
//                        case "30":
//                            process = "5G2";
//                            break;
//                        default:
//                    }
//                } else {
//                    switch (rollLineNo) {
//                        case "10":
//                            process = "1G1";
//                            break;
//                        case "20":
//                            process = "2G1";
//                            break;
//                        case "30":
//                            if (Objects.nonNull(pmCmdLargePlt.getShpCode())) {
//                                process = "5G2";
//                            } else {
//                                process = "5G1";
//                            }
//                            break;
//                        default:
//                    }
//                }
//            }
//        }
//        return process;
    }

    /**
     * 销售成品抛丸工序
     *
     * @param shotFlag
     * @param rollLineNo
     * @return
     */
    public String finishedShotProcess(String shotFlag, String rollLineNo) {
        // ⑤如果有销售下的成品抛丸要求(根据轴合同号读取[质量模块]MES合同主表，shot_flag取值为1)
//        String shotFlag = pmPdSlabInfoMapper.selectShotFlag(pmCmdCont.getOrderNo());
        if ("1".equals(shotFlag)) {
            switch (rollLineNo) {
                case "10":
                    return "1C1";
                case "20":
                    return "2C1";
                case "30":
                    return "5C1";
                default:
            }
        }
        return null;
    }
}
