package com.jxy.binance.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.jxy.binance.dto.BiAnWSDto;
import com.jxy.binance.dto.InsertDBDto;
import com.jxy.binance.dto.InsertTimeDataToDBDto;
import com.jxy.binance.mapper.BiAnMapper2;
import com.jxy.binance.service.BiAnService;
import com.jxy.binance.thread.BstreamHistoryThread;
import com.jxy.binance.thread.SstreamHistoryThread;
import com.jxy.binance.thread.UstreamHistoryThread;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @Description BiAn查询业务处理
 * @Date 2022/6/2017:51
 * @Author 杜懿
 */
@Service("biAnService")
public class BiAnServiceImpl implements BiAnService {

    @Autowired
    private BiAnMapper2 biAnMapper2;

    public void insertUSDPerp(BiAnWSDto dto, String table){

        biAnMapper2.insertUSDPERP(dto,table);
    }

    public void insertUSDT(BiAnWSDto dto, String table){

        biAnMapper2.insertUSDT(dto,table);
    }

    public void insertStock(BiAnWSDto dto, String table){

        biAnMapper2.insertStock(dto,table);
    }

    public void batchUSDPerp(List<BiAnWSDto> list, String table){

        biAnMapper2.batchUSDPERP(list,table);
    }

    public void batchUSDT( List<BiAnWSDto> list, String table){

        biAnMapper2.batchUSDT(list,table);
    }

    public void batchStock(List<BiAnWSDto> list, String table){

        biAnMapper2.batchStock(list,table);
    }


    @Override
    public AjaxResult
    allBDataToDb(InsertDBDto dto) {

        String symbol = dto.getSymbol().toLowerCase();

        long endTime = DateUtils.parseDate(dto.getEnd()).getTime() - 1 ;

        long start = DateUtils.parseDate(dto.getStart()).getTime() - 1 ;

        Thread thread= new Thread(new BstreamHistoryThread(symbol,start,endTime,dto.getTable(),dto.getInterval()),symbol+"历史数据获取线程");
        thread.start();

        return AjaxResult.success();
    }

    @Override
    public AjaxResult allUDataToDb(InsertDBDto dto) {
        String symbol = dto.getSymbol().toLowerCase();

        long endTime = DateUtils.parseDate(dto.getEnd()).getTime() - 1 ;

        long start = DateUtils.parseDate(dto.getStart()).getTime() - 1 ;

        Thread thread= new Thread(new UstreamHistoryThread(symbol,start,endTime,dto.getTable(),dto.getInterval()),symbol+"历史数据获取线程");
        thread.start();

        return AjaxResult.success();
    }

    @Override
    public AjaxResult allStockDataToDb(InsertDBDto dto) {
        String symbol = dto.getSymbol().toLowerCase();

        long endTime = DateUtils.parseDate(dto.getEnd()).getTime() - 1 ;

        long start = DateUtils.parseDate(dto.getStart()).getTime() - 1 ;

        Thread thread= new Thread(new SstreamHistoryThread(symbol,start,endTime,dto.getTable(),dto.getInterval()),symbol+"历史数据获取线程");
        thread.start();

        return AjaxResult.success();
    }

    @Override
    public AjaxResult getTotalClosePrice(int num) {

        List<BiAnWSDto> list1 = biAnMapper2.getTotalBTCClosePrice();

        List<BiAnWSDto> list2 = biAnMapper2.getTotalETHClosePrice();

        BigDecimal total1 = BigDecimal.ZERO;
        BigDecimal total2 = BigDecimal.ZERO;

        for(int i=0 ;i < num;i++){
            total1 = total1.add(list1.get(i).getClosePrice());
        }
        for(int i=0 ;i < num;i++){
            total2 = total2.add(list2.get(i).getClosePrice());
        }

        String msg = "BTC:"+total1+"\nETH:"+total2;

        return AjaxResult.success(msg,list1);

    }

}
