package com.hzs.system.base.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.base.BdCurrency;
import com.hzs.system.base.mapper.BdCurrencyMapper;
import com.hzs.system.base.service.IBdCurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yuhui
 * @Description: 币种服务类
 */
@Service
public class BdCurrencyServiceImpl extends ServiceImpl<BdCurrencyMapper, BdCurrency> implements IBdCurrencyService {

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    public BdCurrency getCurrency(Integer pkCountry) {
        BdCurrency bdCurrency = redisService.getCacheObject(CacheConstants.BD_CURRENCY + pkCountry);
        if (null == bdCurrency) {
            QueryWrapper<BdCurrency> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
            queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
            queryWrapper.le(SystemFieldConstants.EFFECTIVE_DATE, DateUtils.currentDateTime());
            queryWrapper.orderByDesc("EFFECTIVE_DATE");
            List<BdCurrency> bdCurrencyList = baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(bdCurrencyList)) {
                bdCurrency = bdCurrencyList.get(0);
            }
            if (null != bdCurrency) {
                redisService.setCacheObject(CacheConstants.BD_CURRENCY + pkCountry, bdCurrency);
            }
        }
        return bdCurrency;
    }

    @Override
    public BdCurrency getByPkid(Integer pkId) {
        BdCurrency bdCurrency = redisService.getCacheObject(CacheConstants.BD_CURRENCY_PK + pkId);
        if (null == bdCurrency) {
            QueryWrapper<BdCurrency> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
            queryWrapper.le(SystemFieldConstants.EFFECTIVE_DATE, DateUtils.currentDateTime());
            queryWrapper.eq(SystemFieldConstants.PK_ID, pkId);
            bdCurrency = baseMapper.selectOne(queryWrapper);
            if (null != bdCurrency) {
                redisService.setCacheObject(CacheConstants.BD_CURRENCY_PK + pkId, bdCurrency);
            }
        }
        return bdCurrency;
    }
    /**
     *  根据id 查询所属历史币种
     * @return
     */
    @Override
    public BdCurrency getByHistoryPkid(Integer pkId) {
        BdCurrency bdCurrency = redisService.getCacheObject(CacheConstants.BD_HISTORY_CURRENCY_PK + pkId);
        if (null == bdCurrency) {
            QueryWrapper<BdCurrency> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SystemFieldConstants.PK_ID, pkId);
            bdCurrency = baseMapper.selectOne(queryWrapper);
            if (null != bdCurrency) {
                redisService.setCacheObject(CacheConstants.BD_HISTORY_CURRENCY_PK + pkId, bdCurrency);
            }
        }
        return bdCurrency;
    }
    /**
     * 本地币转换为美金
     *
     * @param localCurrency 本地币
     * @param pkCountry     结算国
     * @return
     */
    @Override
    public BigDecimal getDollar(BigDecimal localCurrency, Integer pkCountry) {
        BdCurrency bdCurrency = getCurrency(pkCountry);
        BigDecimal exchangeRate = bdCurrency.getOutExchangeRate();
        if (localCurrency == null || exchangeRate == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        return localCurrency.divide(exchangeRate, 2, RoundingMode.HALF_UP);
    }

    @Override
    public List<BdCurrency> queryAllBdCurrency() {
        QueryWrapper<BdCurrency> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pk_id,in_exchange_rate,out_exchange_rate,pk_country");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void updateCurrencyById(Integer pkId) {
        BdCurrency bdCurrency = baseMapper.selectById(pkId);
        if(bdCurrency != null){
            bdCurrency.setEnableState(EYesNo.YES.getIntValue());
        }
        baseMapper.updateById(bdCurrency);
    }
}
