/**
 * @filename:CreditScoreQuotaServiceImpl 2024-06-11
 * @project trade-client V1.0 Copyright(c) 2024 wanglang Co. Ltd. All right reserved.
 */
package com.trade.client.advocacypage.service.impl;

import com.trade.client.advocacypage.domain.CreditScoreQuota;
import com.trade.client.advocacypage.dto.CreditQuotaContext;
import com.trade.client.advocacypage.dto.EstimatedCreditQuotaDto;
import com.trade.client.advocacypage.mapper.CreditScoreQuotaMapper;
import com.trade.client.advocacypage.service.CreditScoreQuotaService;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Description:授信分数额度服务实现
 * @version: V1.0
 * @author: wanglang
 */
@Service
public class CreditScoreQuotaServiceImpl extends ServiceImpl<CreditScoreQuotaMapper, CreditScoreQuota> implements CreditScoreQuotaService {

    @Resource
    private CreditScoreQuotaMapper creditScoreQuotaMapper;
    @Resource
    private FlowExecutor flowExecutor;

    @Override
    public CreditQuotaContext getCreditScoreQuota(EstimatedCreditQuotaDto quotaDto) {
        LiteflowResponse response = flowExecutor.execute2Resp("chain_quota", quotaDto, CreditQuotaContext.class);
        if (!response.isSuccess()) {
            throw new ServiceException(response.getMessage(), HttpStatus.BAD_REQUEST);
        }
        CreditQuotaContext creditQuotaContext = response.getContextBean(CreditQuotaContext.class);
        if (creditQuotaContext.getScore() <= 0) {
            return creditQuotaContext;
        }
        CreditScoreQuota creditScoreQuota = creditScoreQuotaMapper.selectByScore(creditQuotaContext.getScore());
        if (creditScoreQuota == null) {
            throw new ServiceException("未找到该区间内的额度", HttpStatus.BAD_REQUEST);
        }
        creditQuotaContext.setAmount(new BigDecimal(creditScoreQuota.getQuota()));
        creditQuotaContext.setCurrency(creditScoreQuota.getCurrencyCode());
        return creditQuotaContext;
    }
}
