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

import com.xisc.pm.api.dto.RuleParamDTO;
import com.xisc.pm.app.service.strategy.RuleExecStrategyFactory;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.PdRuleItemType;
import io.choerodon.core.exception.CommonException;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 非轴合同与轴合同的AD控制模式第134位相等匹配检查（A03）
 * 
 * @author jun.zeng
 * 
 * 
 */
@Component
public class AdModeMatchRuleExecStrategy implements RuleExecStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.AD_MODE_MATCH, this);
    }

    @Override
    public boolean execRule(RuleParamDTO ruleParam) {
        Map<String, Object> bizData = ruleParam.getBizData();
        String orderNo = (String) bizData.get(RuleSetConstants.ORDER_NO);
        String mscLineNo = (String) bizData.get(RuleSetConstants.MSC_LINE_NO);

        PmContMscTech axisPmContMscTech = (PmContMscTech) bizData.get(RuleSetConstants.AXIS_PM_CONT_MSC_TECH);
        PmContMscTech pmContMscTech = (PmContMscTech) bizData.get(RuleSetConstants.PM_CONT_MSC_TECH);

        if (axisPmContMscTech == null || pmContMscTech == null) {
            // 读生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        if(axisPmContMscTech.getAdModeCode() == null && pmContMscTech.getAdModeCode() == null) {
            return true ;
        }
        if(axisPmContMscTech.getAdModeCode() == null || pmContMscTech.getAdModeCode() == null) {
            // AD控制模式与轴合同不一致
            throw new CommonException("pm.pm_pd_rule_set_052");
        }
        String axisAdModeCode134 = String.valueOf(axisPmContMscTech.getAdModeCode().charAt(0))
                + (axisPmContMscTech.getAdModeCode().length() > 2 ? String.valueOf(axisPmContMscTech.getAdModeCode().charAt(2)) : "") 
                + (axisPmContMscTech.getAdModeCode().length() > 3 ? String.valueOf(axisPmContMscTech.getAdModeCode().charAt(3)) : "");
        String adModeCode134 = String.valueOf(pmContMscTech.getAdModeCode().charAt(0))
                + (pmContMscTech.getAdModeCode().length() > 2 ? String.valueOf(pmContMscTech.getAdModeCode().charAt(2)) : "")
                + (pmContMscTech.getAdModeCode().length() > 3 ? String.valueOf(pmContMscTech.getAdModeCode().charAt(3)) : "");
        if(!axisAdModeCode134.equals(adModeCode134)) {
            throw new CommonException("pm.pm_pd_rule_set_052");
        }
        return true;
    }

}
