package com.jxy.marketall.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.jxy.common.core.exception.GlobalException;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.DataCheckDto;
import com.jxy.marketall.enums.ReqIntetfaceType;
import com.jxy.marketall.service.*;
import com.jxy.marketall.utils.EntityDtoUtils;
import com.jxy.marketall.utils.EnumUtils;
import com.jxy.marketall.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * @Description 所有接口统一调用业务处理
 * @Date 2022/5/19 17:51
 * @Author 杜懿
 */
@Service
public class MarketallServiceImpl implements MarketallService {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private CoinmetricsService coinmetricsService;

    @Autowired
    private CoinPriceService coinPriceService;

    @Autowired
    private NDAQService ndvqService;

    @Autowired
    private BTCService btcService;

    @Autowired
    private TransActionsService transactionsService;

    @Autowired
    private TransRateService transrateService;

    @Autowired
    private TransSizeMeanService transSizeMeanService;

    @Autowired
    private TransSizeTotalService transSizeTotalService;

    @Autowired
    private ETFService etfService;

    @Autowired
    private TotalAddressService totalAddressService;

    @Autowired
    private NewAddressService newAddressService;

    @Autowired
    private ActiveAddressService activeAddressService;

    @Autowired
    private SendingAddressService sendingAddressService;

    @Autowired
    private ReceivingAddressService receivingAddressService;

    @Autowired
    private OHLCPriceService ohlcPriceService;

    @Autowired
    private MarketService marketService;

    @Autowired
    private DistributionService distributionService;

    @Autowired
    private DerivativeService derivativeService;

    @Autowired
    private EthBigTxService ethBigTxService;

    @Autowired
    private RedisService redisService;

    @Override
    public AjaxResult findResultByRequestVo(RequestVo requestVo) {

        String req = requestVo.getReq();
        /** 判断请求接口是否存在 */
        if(!EnumUtils.isInclude(ReqIntetfaceType.class,req))
        {
            //todo 自定义异常 请求接口不对
                throw new  RuntimeException("请求的接口不对");
        }

        AjaxResult result = new AjaxResult();


        /** 到这一步req接口是已存在的几种之一 */
        //TODO 封装为一个具体方法
        /** coinmetrics接口 */
        if(req.toUpperCase().equals(ReqIntetfaceType.COINMETRICS.name())){

            TypeVo typeVo = EntityDtoUtils.toTypeVo(requestVo);

            result = coinmetricsService.findCoinmetricsByTime(typeVo);

        }
        /** NDAQ接口一 */
        else if(req.toUpperCase().equals(ReqIntetfaceType.NDAQ.name())){

            QueryVo queryVo = EntityDtoUtils.toQueryVo(requestVo);

            result = ndvqService.findNDAQByTime(queryVo);

        }
        /** 币价查询price接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.PRICE.name())){

            CoinPriceVo coinPriceVo = EntityDtoUtils.toCoinPriceVo(requestVo);

             if(!StringUtils.isEmpty(coinPriceVo.getOhlc())){
                /** 传了ohlc说明是按ohlc的时间查询 需要按ohlc的返回类型返回数据 */
                result = coinPriceService.findCoinPriceByOHLC(coinPriceVo);
            }else if(StringUtils.isEmpty(coinPriceVo.getCoin())){
                 //调用查询所有的接口
                 result = coinPriceService.findCoinMarketAll();
             }else if("BTC".equals(coinPriceVo.getCoin()) || "ETH".equals(coinPriceVo.getCoin())){
                 //调用查询BTC或者ETH的接口
                 //result = coinPriceService.findCoinPriceByTime(coinPriceVo);
                 QueryVo vo = new QueryVo();
                 vo.setStart(requestVo.getStart());
                 vo.setEnd(requestVo.getEnd());
                 if("BTC".equals(coinPriceVo.getCoin())){
                     result = btcService.getPrice(vo);
                 }else if("ETH".equals(coinPriceVo.getCoin())){
                     result = ethBigTxService.getPrice(vo);
                 }
             }
             else{
                //result = coinPriceService.findCoinMarketByTime(coinPriceVo);
            }
        }
        /** 日成交率transRate接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.TRANSRATE.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = transrateService.findTransrateByTime(coinVo);

        }
        /** 日交易数transactions接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.TRANSACTIONS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = transactionsService.findTransactionsByTime(coinVo);

        }
        /** 秒交易规模数transSizeMean接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.TRANSSIZEMEAN.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = transSizeMeanService.findTransSizeMeanByTime(coinVo);

        }
        /** 日交易规模数transSizeTotal接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.TRANSSIZETOTAL.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = transSizeTotalService.findTransSizeTotalByTime(coinVo);

        }
        /** EFT接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.ETF.name())){

            TypeVo typeVo = EntityDtoUtils.toTypeVo(requestVo);

            result = etfService.findETFByType(typeVo);

        }
        /** 地址总数totalAddress接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.TOTALADDRESS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = totalAddressService.findTotalAddressByCoin(coinVo);

        }
        /** 日新增地址数newAddress接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.NEWADDRESS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = newAddressService.findNewAddressByCoin(coinVo);

        }
        /** 日活跃地址数activeAddress接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.ACTIVEADDRESS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = activeAddressService.findActiveAddressByCoin(coinVo);

        }
        /** 日发起交易地址数sendingAddress接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.SENDINGADDRESS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = sendingAddressService.findSendingAddressByCoin(coinVo);

        }
        /** 日收到交易地址数receivingAddress接口接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.RECEIVINGADDRESS.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = receivingAddressService.findReceivingAddressByCoin(coinVo);

        }
        /** 每日币价K线数据OHLCPrice接口 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.OHLCPRICE.name())){

            CoinVo coinVo = EntityDtoUtils.toCoinVo(requestVo);

            result = ohlcPriceService.findOHLCPriceByCoin(coinVo);

        }
        /** 市场数据 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.MARKET.name())){

            CoinByTypeVo coinByTypeVo = EntityDtoUtils.toCoinByMarketTypeVo(requestVo);

            result = marketService.findMarketDataByTypeAndCoin(coinByTypeVo);

        }
        /** 分配数据 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.DISTRIBUTION.name())){

            CoinByTypeVo coinByTypeVo = EntityDtoUtils.toCoinByDistributionTypeVo(requestVo);

            result = distributionService.findDistributionByTypeAndCoin(coinByTypeVo);

        }
        /** 衍生数据 */
        else if (req.toUpperCase().equals(ReqIntetfaceType.DERIVATIVES.name())){

            CoinByTypeVo coinByTypeVo = EntityDtoUtils.toCoinByDerivativeTypeVo(requestVo);

            result = derivativeService.findDerivativeDataByTypeAndCoin(coinByTypeVo);

        }
        if(StringUtils.isNull(result)){
            return AjaxResult.error("未请求到数据");
        }

        return result;
    }

    @Override
    public AjaxResult findCoinMarketPrice(CoinVo coinVo) {

        return coinPriceService.findCoinMarketByTime(coinVo);
    }

    @Override
    public AjaxResult findCoinMarketAll() {
        return coinPriceService.findCoinMarketAll();
    }

    @Override
    public AjaxResult getNewDataList() {
        List<DataCheckDto> list  = redisService.getCacheList("NewDataList");
        return AjaxResult.success(list);
    }
}
