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.PmCompositeSlab;
import com.xisc.pm.domain.entity.PmCompositeSlabList;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.infra.enums.PdProcessType;
import io.choerodon.core.exception.CommonException;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.stereotype.Component;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/12/17 8:45
 * @description
 */
@Component
public class ProcessGenerateStrategyPA extends DefaultProcessGenerateStrategy {
    @Override
    public void afterPropertiesSet() throws Exception {
        ProcessGenerateStrategyFactory.register(PdProcessType.PA, this);
    }
    
    @Override
    public String addSplittingProcess(String slabNo, String rollLineNo, Long tenantId) {
        return null;
    }

    @Override
    public String getProcessCode(ProcessGenerateStrategyDTO dto) {
        String result = "";
        String process = "";

        // 探伤工序 - 不是见证合同，探伤标记为1，探伤时期代码为2或为空
        if (StringUtils.isEmpty(dto.getWitnessStatus()) && "1".equals(dto.getIsUt()) && ("2".equals(dto.getUtPdCode()) || StringUtils.isEmpty(dto.getUtPdCode()))) {
            process = generateCrackProcess(dto.getAxisPmContMscTech().getPlateDtCode(), dto.getRollLineNo(), dto.getUtCheckCode(),
                    dto.getWitnessStatus(), dto.getUltDetectStd());
            if (StringUtils.isNotEmpty(process)) {
                result = result + process + "-";
            }
        }

        // 热处理工序
        process = heatTreatmentProcess(dto.getMmHtmrsltNum(), dto.getAxisPmContMscTech(), dto.getShotHt(), dto.getRollLineNo());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 添加分张工序：如果mat_design_kind=S且板坯号第一位为F（复合/叠轧板坯组板）
        process = splittingProcess(dto.getMatDesignKind(), dto.getRollLineNo(), dto.getSlabNo(), dto.getTenantId());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }


        // 热处理后火切工序
        process = fireProcessAfterHeatTreat(dto.getAxisPmContMscTech(), dto.getRollLineNo(), dto.getPmCmdLargePlt());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 探伤工序 - 不是见证合同，探伤标记为1，探伤时期代码为2或为1
        if (StringUtils.isEmpty(dto.getWitnessStatus()) && "1".equals(dto.getIsUt()) && ("1".equals(dto.getUtPdCode()) || "2".equals(dto.getUtPdCode()))) {
            process = generateCrackProcess(dto.getAxisPmContMscTech().getPlateDtCode(), dto.getRollLineNo(), dto.getUtCheckCode(),
                    dto.getWitnessStatus(), dto.getUltDetectStd());
            if (StringUtils.isNotBlank(process)) {
                result = result + process + "-";
            }
        }
        return result;
    }

    public String heatTreatmentProcess(Long mmHtmrsltNum, PmContMscTech axisPmContMscTech, String shotHt, String rollLineNo) {
        String result = "";
//        if (mmHtmrsltNum != null && mmHtmrsltNum > 0) {
//            return null;
//        }
        // 如果热处理实绩表（MM_HTMRSLT）中查询不到生产流水号
        // 1.抛丸工序
        String process = finishedShotProcess(shotHt, rollLineNo);
        if (StringUtils.isNotEmpty(process)) {
            result = result + process + "-";
        }

        // 热处理工序
        if (StringUtils.isEmpty(axisPmContMscTech.getWholeHeatTreatmentType())) {
            throw new CommonException("pm.cont_msc_tech_003", axisPmContMscTech.getOrderNo(), axisPmContMscTech.getMscLineNo());
        }
        String[] heatTreatModeList = new String[axisPmContMscTech.getWholeHeatTreatmentType().length()];
        int j = 0;
        for (int i = 0; i < axisPmContMscTech.getWholeHeatTreatmentType().length(); i++) {
            String heatTreatMode = String.valueOf(axisPmContMscTech.getWholeHeatTreatmentType().charAt(i));
            switch (heatTreatMode) {
                case "A":
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1A0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2A0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5A0";
                            break;
                        default:
                    }
                    break;
                case "N":
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1N0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2N0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5N0";
                            break;
                        default:
                    }
                    break;
                case "Q":
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1Q0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2Q0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5Q0";
                            break;
                        default:
                    }
                    break;
                case "T":
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1T0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2T0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5T0";
                            break;
                        default:
                    }
                    break;
                case "B":
                    // 正火+回火
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1N0";
                            heatTreatModeList[j++] = "1T0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2N0";
                            heatTreatModeList[j++] = "2T0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5N0";
                            heatTreatModeList[j++] = "5T0";
                            break;
                        default:
                    }
                    break;
                case "D":
                    // 淬火+回火
                    switch (rollLineNo) {
                        case "10":
                            heatTreatModeList[j++] = "1Q0";
                            heatTreatModeList[j++] = "1T0";
                            break;
                        case "20":
                            heatTreatModeList[j++] = "2Q0";
                            heatTreatModeList[j++] = "2T0";
                            break;
                        case "30":
                            heatTreatModeList[j++] = "5Q0";
                            heatTreatModeList[j++] = "5T0";
                            break;
                        default:
                    }
                    break;
                default:
            }
        }
        for (int i = 0; i < heatTreatModeList.length; i++) {
            if (StringUtils.isNotEmpty(heatTreatModeList[i])) {
                result = result + heatTreatModeList[i] + "-";
            }
        }
        return result;
    }
}
