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

import java.util.List;
import java.util.Map;

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;

/**
 * 特殊钢种组剖分板检查（C05）
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class PlanerFlagRuleExecStrategy implements RuleExecStrategy {

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

    @SuppressWarnings("unchecked")
    @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);
//        
        List<PmProContProdTtr> pmProContProdTtrList = (List<PmProContProdTtr>) bizData.get(RuleSetConstants.PM_PRO_CONT_PROD_ATTR_LIST);
        PmProContProdTtr pmProContProdTtrParam = (PmProContProdTtr) bizData.get(RuleSetConstants.PM_PRO_CONT_PROD_ATTR);
        if ((pmProContProdTtrList == null || pmProContProdTtrList.isEmpty()) && pmProContProdTtrParam == null) {
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        } 
        if(pmProContProdTtrParam != null) {
            FormPlateMode formPlateMode = FormPlateMode.getByCode(formPlateModeValue);
            if(BaseConstants.Flag.NO.toString().equals(pmProContProdTtrParam.getPlanerFlag()) && FormPlateMode.S1.getType().equals(formPlateMode.getType())) {
                // 合同【{0}】的剖分标记【{1}】与指定的组板方式【{2}】不一致。
                throw new CommonException("pm.pm_pd_rule_set_014" , pmProContProdTtrParam.getOrderNo() , pmProContProdTtrParam.getPlanerFlag() , formPlateMode.getCode()) ;
            }
        } else {
            for (PmProContProdTtr pmProContProdTtr : pmProContProdTtrList) {
                FormPlateMode formPlateMode = FormPlateMode.getByCode(formPlateModeValue);
                if(BaseConstants.Flag.NO.toString().equals(pmProContProdTtr.getPlanerFlag()) && FormPlateMode.S1.getType().equals(formPlateMode.getType())) {
                    // 合同【{0}】的剖分标记【{1}】与指定的组板方式【{2}】不一致。
                    throw new CommonException("pm.pm_pd_rule_set_014" , pmProContProdTtr.getOrderNo() , pmProContProdTtr.getPlanerFlag() , formPlateMode.getCode()) ;
                }
            }
        }
        return true ;
    }

}
