package com.jxy.marketall.service.impl;

import com.jxy.common.core.utils.DateUtils;
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.*;
import com.jxy.marketall.enums.BTCRedisName;
import com.jxy.marketall.mapper.BTCNewV2Mapper;
import com.jxy.marketall.mapper.BTCRealTimeMapper;
import com.jxy.marketall.service.BTCRealTimeService;
import com.jxy.marketall.service.BTCV2Service;
import com.jxy.marketall.vo.EBalanceVo;
import com.jxy.marketall.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Date 2023/1/13 10:58
 * @Author 杜懿
 */
@Service
public class BTCRealTimeServiceImpl implements BTCRealTimeService {

    @Autowired
    private BTCRealTimeMapper rtMapper;

    @Override
    public AjaxResult getCDDList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<CoinDayDesDto> list = rtMapper.getRealTimeCDDList(vo);

        //list倒序输出 desc ->asc
        Collections.reverse(list);

        return CheckData(list);
    }


    /***********************************************112服务器 btcdb（4418端口）数据库相关接口***********************************************/
    @Override
    public AjaxResult getProfitRateList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DateBigDecimalDto> list = rtMapper.getRealTimeProfitRateList(vo);

        Collections.reverse(list);

        return  CheckData(list);
    }

    @Override
    public AjaxResult getFeesList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DateBigDecimalDto> list = rtMapper.getRealTimeFeesList(vo);

        Collections.reverse(list);


        return  CheckData(list);
    }

    @Override
    public AjaxResult getAddressList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<AddressDto> list = rtMapper.getRealTimeAddressList(vo);

        Collections.reverse(list);

        //异常数据处理 主要是对0数据
        return  CheckData(list);
    }

    @Override
    public AjaxResult getVolumeList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DateBigDecimalDto> list = rtMapper.getRealTimeVolumeList(vo);

        Collections.reverse(list);

        return  CheckData(list);
    }

    @Override
    public AjaxResult geteaVolumeList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DateBigDecimalDto> list = rtMapper.getRealTimeeaVolumeList(vo);

        Collections.reverse(list);

        return  CheckData(list);
    }

    @Override
    public AjaxResult getSoprList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<SoprDto> list = rtMapper.getRealTimeSoprList(vo);

        Collections.reverse(list);

        //异常数据处理 主要是对0数据
        return  CheckData(list);

    }

    @Override
    public AjaxResult getAsolList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<ASOLDto> list = rtMapper.getRealTimeAsolList(vo);

        Collections.reverse(list);

        //异常数据处理 主要是对0数据
        return  CheckData(list);
    }

    @Override
    public AjaxResult getDormancyList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DormancyDto> list = rtMapper.getRealTimeDormancyList(vo);

        Collections.reverse(list);

        //异常数据处理 主要是对0数据
        return  CheckData(list);
    }

    @Override
    public AjaxResult getSoagList(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<SOAGDto> list = rtMapper.getRealTimeSoagList(vo);

        if(list.size()<1){
            return AjaxResult.success(list);
        }

        Collections.reverse(list);

        for(int i=0 ;i<list.size();i++){
            SOAGDto soagDto = list.get(i);
            BigDecimal total = soagDto.getDay1().add(soagDto.getDay7()).add(soagDto.getDay30()).add(soagDto.getDay60()).add(soagDto.getDay90()).add(soagDto.getDay180()).add(soagDto.getDay365()).add(soagDto.getDay730());

            if(total.intValue() != 0){
                soagDto.setDay1(soagDto.getDay1().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay7(soagDto.getDay7().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay30(soagDto.getDay30().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay60(soagDto.getDay60().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay90(soagDto.getDay90().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay180(soagDto.getDay180().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay365(soagDto.getDay365().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
                soagDto.setDay730(soagDto.getDay730().multiply(BigDecimal.valueOf(100)).divide(total,3,BigDecimal.ROUND_HALF_UP));
            }
        }

        //异常数据处理 主要是对0数据
        return  CheckData(list);
    }


    @Override
    public AjaxResult getPrice(QueryVo vo) {

        vo.setStart(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD));

        List<DateBigDecimalDto> list = rtMapper.getRealTimePriceList(vo);

        Collections.reverse(list);

        //异常数据处理 主要是对0数据
        return  CheckData(list);

    }


    /**
     * 验证list是否为空
     * @param list
     * @return
     */
    public AjaxResult CheckData(List list) {

        if(list.size() < 1){
            return AjaxResult.error(201,"数据库中未查询到数据");
        }
        return AjaxResult.success(list);
    }


}
