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

import com.xisc.pm.api.dto.PmProContYieldRequestDTO;
import com.xisc.pm.api.dto.RuleParamDTO;
import com.xisc.pm.app.service.strategy.RuleExecStrategyFactory;
import com.xisc.pm.domain.vo.MmPlateVO;
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;

/**
 * 
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class PlateLenAndWidthRuleExecStrategy implements RuleExecStrategy {

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

    @Override
    public boolean execRule(RuleParamDTO ruleParam) {
        Map<String, Object> bizData = ruleParam.getBizData();
        String orderNo = (String) bizData.get(RuleSetConstants.ORDER_NO);
        PmProContYieldRequestDTO pmProContYield = (PmProContYieldRequestDTO) bizData.get(RuleSetConstants.CALC_REQUEST);
        MmPlateVO mmPlateVO = pmProContYield.getPmContFilterRequest().getMmPlate();

        // 若组合长度combine_plate_len大于材料实际长度mat_act_len，则校验不通过，提示信息：组合长【combine_plate_len】大于钢板实际长度【mat_act_len】。
        // 若组合宽度combine_plate_width大于材料实际宽度mat_act_width，则校验不通过，提示信息：组合宽【combine_plate_width】大于钢板实际宽度【mat_act_width】

        if (pmProContYield.getCompositeBoard().getSzLength().compareTo(mmPlateVO.getMatActLen()) > 0) {
            // 组合长【{0}】大于钢板实际长度【{1}】
            throw new CommonException("pm_pd_rule_set_058",
                    pmProContYield.getCompositeBoard().getSzLength().toPlainString(), mmPlateVO.getMatActLen().toPlainString());
        }
        if (pmProContYield.getCompositeBoard().getSzWidth().compareTo(mmPlateVO.getMatActWid()) > 0) {
            // 组合宽【{0}】大于钢板实际宽度【{1}】
            throw new CommonException("pm_pd_rule_set_057",
                    pmProContYield.getCompositeBoard().getSzWidth().toPlainString(), orderNo,
                    mmPlateVO.getMatActWid().toPlainString());
        }
        return true;
    }

}
