package com.trade.client.advocacypage.cmp;

import com.trade.client.advocacypage.constant.QuotaDimensionEnum;
import com.trade.client.advocacypage.constant.StandardSettingEnum;
import com.trade.client.advocacypage.domain.CreditQuotaLogic;
import com.trade.client.advocacypage.dto.CreditQuotaContext;
import com.trade.client.advocacypage.dto.EstimatedCreditQuotaDto;
import com.trade.client.advocacypage.service.CreditQuotaLogicService;
import com.trade.service.system.domain.SysCountry;
import com.trade.service.system.service.SysCountryService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeBooleanComponent;
import com.yomahub.liteflow.core.NodeComponent;

import javax.annotation.Resource;

/**
 * @description: 第一梯队国家
 * @Author: wanglang
 * @Date: 2024/6/7 11:21
 * @Version: 1.0
 */
@LiteflowComponent("countryRegionCmp")
public class CountryRegionCmp extends NodeBooleanComponent {

    @Resource
    private CreditQuotaLogicService creditQuotaLogicService;
    @Resource
    private SysCountryService sysCountryService;

    @Override
    public boolean processBoolean() throws Exception {
        EstimatedCreditQuotaDto req = this.getRequestData();
        CreditQuotaContext creditQuotaContext = this.getContextBean(CreditQuotaContext.class);
        // 查询国家 确定梯队
        SysCountry sysCountry = sysCountryService.queryCountryByCode(req.getCountry());
        if (sysCountry != null
                && !StandardSettingEnum.CR_FIVE.getCode().equals(String.valueOf(sysCountry.getLevel()))) {
            String echelon = StandardSettingEnum.getStandardSetting(String.valueOf(sysCountry.getLevel())).getCode();
            CreditQuotaLogic creditQuotaLogic = creditQuotaLogicService.lambdaQuery()
                    .eq(CreditQuotaLogic::getDimension, QuotaDimensionEnum.COUNTRY_REGION.getCode())
                    .eq(CreditQuotaLogic::getStandardSetting, echelon).one();
            // 计算分数
            double score = creditQuotaLogic.getEstimateScore() + creditQuotaContext.getScore();
            creditQuotaContext.setScore(score);
        } else {
            return false;
        }
        return true;
    }
}
