package com.crm.pay.service.impl;

import com.crm.pay.dao.repository.FinanceRateRepository;
import com.crm.pay.service.FinanceRateService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.pojo.table.FinanceRateTable;
import com.crm.rely.backend.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @description:
 * @author: houn
 * @create: 2020-06-11 11:24
 **/
@Service
public class FinanceRateServiceImpl implements FinanceRateService {

    @Autowired
    private FinanceRateRepository financeRateRepository;

    @Autowired
    private RedisService redisService;


    @Override
    public BigDecimal getRate(String currency, String transformCurrency, Integer type) throws ServiceException {

        return getRate(currency, transformCurrency, type, null);
    }

    @Override
    public BigDecimal getRate(String currency, String transformCurrency, Integer type, Integer t) throws ServiceException {
        if (type == null) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        BigDecimal rate = null;

        if (currency.equals(transformCurrency)) {
            return BigDecimal.ONE;
        }

        if (t == null || t.equals(0)) {
            rate = redisService.getEntity(currency + transformCurrency + type, BigDecimal.class);
            if (rate != null) {
                return rate;
            }
        }

        //先用转化货币和货币进行获取
        FinanceRateTable financeRateTable = financeRateRepository.getByTypeAndCurrencyAndTransformCurrency(type,
                transformCurrency, currency);
        //如果不存在 则根据货币和转换货币获取汇率
        if (financeRateTable == null) {
            financeRateTable = financeRateRepository.getByTypeAndCurrencyAndTransformCurrency(type, currency,
                    transformCurrency);
            //如果不存在 则表示当前系统未设置此货币汇率
            if (financeRateTable == null) {
                if (type != null && type.equals(0)) {
                    throw new ServiceException(PayConstants.FINANCE_DEPOSIT_RATE_NOT_EMPTY);
                } else {
                    throw new ServiceException(PayConstants.FINANCE_WITHDRAW_RATE_NOT_EMPTY);
                }
            }
            int roundingMode = 1;
            if (type != null) {
                if (type.equals(0)) {
                    roundingMode = 0;
                } else if (type.equals(1)) {
                    roundingMode = 1;
                }
            }
            rate = BigDecimal.ONE.divide(financeRateTable.getRate(), 10, roundingMode);
        } else {
            rate = financeRateTable.getRate();
        }
        redisService.save(currency + transformCurrency + type, rate, 60 * 60 * 1000);

        return rate;
    }

    @Override
    public BigDecimal getRateAmount(String currency, String transformCurrency, BigDecimal amount) throws ServiceException {

        if (amount == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        if (amount.compareTo(BigDecimal.ZERO) == 0) {
            return amount;
        }
        int type;
        if (amount.compareTo(BigDecimal.ZERO) > 0) {
            type = 0;
        } else {
            type = 1;
        }
        BigDecimal rate = getRate(currency, transformCurrency, type, 1);

        return amount.multiply(rate);
    }

    @Override
    public BigDecimal getRateAmount(String currency, String transformCurrency, Integer type, BigDecimal amount) throws ServiceException {
        return getRateAmount(currency, transformCurrency, type, amount, 1);

    }

    @Override
    public BigDecimal getRateAmount(String currency, String transformCurrency, Integer type, BigDecimal amount,
                                    Integer t) throws ServiceException {

        if (amount == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        if (amount.compareTo(BigDecimal.ZERO) == 0) {
            return amount;
        }
        BigDecimal rate = getRate(currency, transformCurrency, type, t);

        return amount.multiply(rate);
    }
}
