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

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hzero.core.base.BaseConstants;
import org.springframework.stereotype.Component;

import com.xisc.pm.api.dto.RuleParamDTO;
import com.xisc.pm.app.service.strategy.RuleExecStrategyFactory;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.FormPlateMode;
import com.xisc.pm.infra.enums.PdRuleItemType;

import io.choerodon.core.exception.CommonException;

/**
 * 奉送样只能A1或者S1组板检查（P01）
 * 
 * 
 */
@Component
public class GiveSampleMatchRuleExecStrategy implements RuleExecStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.GIVE_SAMPLE_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);
        String formPlateModeValue = (String) bizData.get(RuleSetConstants.FORM_PLATE_MODE);

        PmProContProdTtr pmProContProdTtr = (PmProContProdTtr) bizData.get(RuleSetConstants.PM_PRO_CONT_PROD_ATTR);

        if (pmProContProdTtr == null || StringUtils.isBlank(pmProContProdTtr.getGiveSampleCode())) {
            // 读生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        if(FormPlateMode.A1.getCode().equals(formPlateModeValue) || FormPlateMode.S1.getCode().equals(formPlateModeValue)) {
            return true ;
        }
        if(BaseConstants.Flag.YES.toString().equals(pmProContProdTtr.getGiveSampleCode())) {
            // 奉送样合同只能A1或者S1组板。
            throw new CommonException("pm.pm_pd_rule_set_027") ;
        }
        return true ;
    }

}
