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

import com.xisc.pm.domain.entity.PmCmdLargePlt;
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.domain.repository.PmCompositeSlabListRepository;
import com.xisc.pm.domain.repository.PmCompositeSlabRepository;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/12/17 15:22
 * @description
 */
public abstract class DefaultProcessGenerateStrategy  implements ProcessGenerateStrategy {

    @Autowired
    protected PmCompositeSlabRepository pmCompositeSlabRepository;

    @Autowired
    protected PmCompositeSlabListRepository pmCompositeSlabListRepository;

    /**
     * 分张工序
     *
     * @param matDesignKind 材料设计类型
     * @param rollLineNo    轧制产线号
     * @param slabNo        板坯号
     * @param tenantId      租户
     * @return
     */
    public String splittingProcess(String matDesignKind, String rollLineNo, String slabNo, Long tenantId) {
        if (StringUtils.isEmpty(slabNo)) {
            return null;
        }
        if (("S".equals(matDesignKind) || "L".equals(matDesignKind))) {
            return addSplittingProcess(slabNo, rollLineNo, tenantId);
        }
        return null;
    }

    public String addSplittingProcess(String slabNo, String rollLineNo, Long tenantId) {
        List<PmCompositeSlabList> pmCompositeSlabLists = pmCompositeSlabListRepository.selectByCondition(Condition.builder(PmCompositeSlabList.class)
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlabList.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmCompositeSlabList.FIELD_BASE_MAT_NO, slabNo))
                .build());
        if (pmCompositeSlabLists != null && !pmCompositeSlabLists.isEmpty()) {
            Long slabId = pmCompositeSlabLists.get(0).getSlabId();
            PmCompositeSlab pmCompositeSlab = pmCompositeSlabRepository.selectByPrimary(slabId);
            if (pmCompositeSlab.getSlabNo().startsWith("E")) {
                switch (rollLineNo) {
                    case "10":
                        return "1X1";
                    case "20":
                        return "2X1";
                    case "30":
                        return "5X1";
                    default:
                }
            } else if (pmCompositeSlab.getSlabNo().startsWith("F")) {
                switch (rollLineNo) {
                    case "10":
                        return "1S1";
                    case "20":
                        return "2S1";
                    case "30":
                        return "5S1";
                    default:
                }
            }
        }
        return null;
    }

    /**
     * 探伤工序
     *
     * @param plateDtCode
     * @param rollLineNo
     * @param utCheckCode
     * @return
     */
    public String generateCrackProcess(String plateDtCode, String rollLineNo, String utCheckCode, String witnessStatus,  int ultDetectStd) {
        StringBuilder crackProcessBuilder = new StringBuilder();
        if ("10".equals(rollLineNo)) {
            crackProcessBuilder.append("1");
        } else if ("20".equals(rollLineNo)) {
            crackProcessBuilder.append("2");
        } else if ("30".equals(rollLineNo)) {
            crackProcessBuilder.append("5");
        } else {
            throw new CommonException("pm.cmd_cont_001");
        }

        // 厚板流向代码为2或者3，含有见证探伤，多个探伤标准，则U4
        if ("2".equals(plateDtCode.substring(0, 1)) || "3".equals(plateDtCode.substring(0, 1)) || StringUtils.isNotEmpty(witnessStatus) || ultDetectStd > 1) {
            crackProcessBuilder.append("U4");
        } else if ("1".equals(plateDtCode.substring(0, 1))) {
            // 扫查模式为1
            if ("1".equals(utCheckCode)) {
                crackProcessBuilder.append("U4");
            } else {
                crackProcessBuilder.append("U1");
            }
        }
//
//        if ("1".equals(plateDtCode.substring(0, 1))) {
//            if ("1".equals(utCheckCode)) {
//                crackProcessBuilder.append("U4");
//            } else {
//                crackProcessBuilder.append("U1");
//            }
//        } else {
//            crackProcessBuilder.append("U4");
//        }
        return crackProcessBuilder.toString();
    }

    /**
     * 热处理后的火切工序
     *
     * @param axisPmContMscTech
     * @param rollLineNo
     * @param pmCmdLargePlt
     * @return
     */
    public String fireProcessAfterHeatTreat(PmContMscTech axisPmContMscTech, String rollLineNo, PmCmdLargePlt pmCmdLargePlt) {
        String result = "";
        if (("3".equals(axisPmContMscTech.getPlateDtCode().substring(0, 1)) || "2".equals(axisPmContMscTech.getPlateDtCode().substring(0, 1))) ||
                Objects.nonNull(pmCmdLargePlt.getShpCode())) {
            switch (rollLineNo) {
                case "10":
                    result = "1G2";
                    break;
                case "20":
                    result = "2G2";
                    break;
                case "30":
                    result = "5G2";
                    break;
                default:
            }
        }
        return result;
    }

    /**
     * 热处理抛丸工序
     *
     * @param shotHt
     * @param rollLineNo
     * @return
     */
    public String finishedShotProcess(String shotHt, String rollLineNo) {
        if ("1".equals(shotHt)) {
            switch (rollLineNo) {
                case "10":
                    return "1C2";
                case "20":
                    return "2C2";
                case "30":
                    return "5C2";
                default:
            }
        }
        return null;
    }
}
