package com.jxy.binance.thread;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.jxy.binance.dto.BiAnWSDto;
import com.jxy.binance.service.impl.BiAnServiceImpl;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.SpringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Date 2023/2/7 17:15
 * @Author 杜懿
 */
public class SstreamHistoryThread implements Runnable {

    private static BiAnServiceImpl biAnService;

    private static long start;

    private static long endtime;

    private static String table ;

    private static String symbol;

    private static String interval;



    public SstreamHistoryThread(String symbol,Long start,Long endtime,String table,String interval) {
        this.table = table;
        this.endtime = endtime;
        this.symbol = symbol;
        this.interval = interval;
        this.start = start;
    }


    @Override
    public void run() {

        biAnService = SpringUtils.getBean("biAnService");

        System.out.println("线程开始执行");

        try {
                while (!Thread.currentThread().isInterrupted()){
                    List<BiAnWSDto> list = getData(symbol,interval,endtime);
                    if(list.size() > 0){
                        //入库
                         biAnService.batchStock(list,table);
                         endtime = list.get(0).getOpenTime().getTime()  - 1;
                        if(start > endtime){
                            Date startDate = new Date(start);
                            System.out.println("开始时间："+startDate);
                            Date endDate = new Date(endtime);
                            System.out.println("结束时间"+endDate);
                            System.out.println("数据获取完成 停止线程");
                            break;
                        }
                    }else{
                        System.out.println("数据获取完成 停止线程");
                        Thread.currentThread();
                        break;
                    }
                    Thread.sleep(1000);
                }

            }catch (InterruptedException e){
                e.printStackTrace();
                Thread curr = Thread.currentThread();
                //再次调用interrupt方法中断自己，将中断状态设置为“中断”
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                curr.interrupt();
            }
    }

    public List<BiAnWSDto> getData(String symbol,String interval,Long endTime){
        String url = "https://data.binance.com/api/v3/klines?symbol="+symbol+"&interval="+interval+"&endTime="+endTime+"&limit="+500;
        String result = HttpUtil.get(url);
        List<String> list = JSONArray.parseArray(result,String.class);
        List<BiAnWSDto> returnList = new ArrayList<>();
        for(int i = 0 ; i < list.size();i++){
            Object[] objects = JSONArray.parseArray(list.get(i)).stream().toArray();
            BiAnWSDto biAnWSDto = new BiAnWSDto();
            biAnWSDto.setOpenTime(new Date(Convert.toLong(objects[0])+28800000));
            biAnWSDto.setOpenPrice(Convert.toBigDecimal(objects[1]));
            biAnWSDto.setHighPrice(Convert.toBigDecimal(objects[2]));
            biAnWSDto.setLowPrice(Convert.toBigDecimal(objects[3]));
            biAnWSDto.setClosePrice(Convert.toBigDecimal(objects[4]));
            biAnWSDto.setVolume(Convert.toBigDecimal(objects[5]));
            biAnWSDto.setCloseTime(new Date(Convert.toLong(objects[6])+28800000+1));
            biAnWSDto.setQuality(Convert.toBigDecimal(objects[7]));
            biAnWSDto.setNum(Convert.toBigDecimal(objects[8]));
            biAnWSDto.setBuyVolume(Convert.toBigDecimal(objects[9]));
            biAnWSDto.setBuyQuality(Convert.toBigDecimal(objects[10]));
            biAnWSDto.setSymbol(symbol.toUpperCase());
            returnList.add(biAnWSDto);
        }
        return returnList;
    }
}
