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.infra.enums.PdProcessType;

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:43
 * @description
 */
@Component
public class ProcessGenerateStrategyP1 extends DefaultProcessGenerateStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        ProcessGenerateStrategyFactory.register(PdProcessType.P1, this);
    }
    
    @Override
    public String addSplittingProcess(String slabNo, String rollLineNo, Long tenantId) {
        if (StringUtils.isEmpty(slabNo)) {
            return null;
        }
        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:
                }
            }
        }
        return null;
    }

    @Override
    public String getProcessCode(ProcessGenerateStrategyDTO dto) {
        String result = "";
        // 第一道轧制工序
        String process = rollingProcess(dto.getMatDesignKind(), dto.getRollLineNo());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        // 复合坯分张工序
        process = splittingProcess(dto.getMatDesignKind(), dto.getRollLineNo(), dto.getSlabNo(), dto.getTenantId());
        if (StringUtils.isNotBlank(process)) {
            result = result + process + "-";
        }

        return result;
    }

    /**
     * 第一道轧制工序
     *
     * @param matDesignKind 材料设计类型
     * @param rollLineNo    轧制产线号
     * @return
     */
    public String rollingProcess(String matDesignKind, String rollLineNo) {
        if ("S".equals(matDesignKind) || "C".equals(matDesignKind)) {
            // 当前预定工序代码
            switch (rollLineNo) {
                case "10":
                    return "1R1";
                case "20":
                    return "2R1";
                case "30":
                    return "5R1";
                default:
            }
        }
        return null;
    }
}
