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.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.PdRuleItemType;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 进行非轴合同和轴合同的见证要求匹配检查
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/11/6 16:02
 * @description
 */
@Component
public class WitnessMatchRuleExecStrategy implements RuleExecStrategy {

    @Override
    public boolean execRule(RuleParamDTO ruleParam) {
        Map<String, Object> bizData = ruleParam.getBizData();
        String orderWithnessProNo = (String) bizData.get(RuleSetConstants.WITNESS_PRO_NO);
        String orderAxisWithnessProNo = (String) bizData.get(RuleSetConstants.WITNESS_PRO_NO_AXIS);

        //若没有查到相关信息，返回错误：读见证项目合同没有找到记录，合同号【order_no】。
        if (StringUtils.isEmpty(orderWithnessProNo)) {
            throw new CommonException("pm.cd_rule_019", orderWithnessProNo);
        }
        if (StringUtils.isEmpty(orderAxisWithnessProNo)) {
            throw new CommonException("pm.cd_rule_019", orderAxisWithnessProNo);
        }
        //若轴合同与非轴合同的见证项目号WITNESS_PRO_NO不相等，则校验不通过
        if (!orderWithnessProNo.equals(orderAxisWithnessProNo)) {
            throw new CommonException("pm.cd_rule_020");
        }
        return true;
    }

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