package com.jxy.marketall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 com.jxy.marketall.dto.*;
import com.jxy.marketall.entity.BiAn;
import com.jxy.marketall.mapper.BiAnMapper;
import com.jxy.marketall.mapper.BiAnMapper2;
import com.jxy.marketall.service.BiAnService;
import com.jxy.marketall.vo.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;


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

    @Autowired
    private BiAnMapper2 biAnMapper2;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 使用https访问接口,并忽略ssl证书验证
     */
    @Resource(name = "restTemplateIgnoreSSL")
    private RestTemplate restTemplateIgnoreSSL;

    @Override
    public List<BiAn> getRealTimeData(BiAnVo biAnVo) {

        //当前请求发起的时间

        //拼接 从请求发起时到目前前位置
        List<BiAn> list = new ArrayList<>();

        String interval = biAnVo.getTimeInterval();

        if(interval == null || interval.contains("min")|| interval.contains("M")|| interval.contains("w")){
            if(interval.contains("min")){
                if (!"1min".equals(interval)){
                    biAnVo.setSymbol(interval.replaceAll("min","m").concat(biAnVo.getSymbol()));
                }
            }
            //按短时间范围查询 1分钟默认是null
            list = baseMapper.selectShortTimeDataByCondition(biAnVo);
        }else if(interval.contains("h") || interval.contains("d") ){
            biAnVo.setSymbol(interval.concat(biAnVo.getSymbol()));
            list = biAnMapper2.selectLongTimeDataByCondition(biAnVo);
        }else{
            //return AjaxResult.error("时间间隔传参有误");
        }

        return list;
    }

    @Override
    public AjaxResult getTotalValueList(BianceVolumeVo vo) {

        if(StringUtils.isBlank(vo.getStart()) || StringUtils.isBlank(vo.getEnd())){
            return AjaxResult.error("请传递时间参数");
        }
        if(StringUtils.isBlank(vo.getInterval())){
            return AjaxResult.error("interval参数不能为空");
        }
        String[] intervals = {"1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "6h","1d"};
        boolean ivFlag = false;
        for (String interval: intervals) {
            if(interval.equals(vo.getInterval())){
                ivFlag = true;
            }
        }
        if(!ivFlag){
            return AjaxResult.error("interval参数有误 请检查传参是否有误！");
        }

        if(StringUtils.isNotEmpty(vo.getStart())){
            vo.setStart(vo.getStart()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(vo.getEnd())){
            vo.setEnd(vo.getEnd()+" 23:59:59");
        }

        String url = "https://13.214.133.132:11447/biance/volume?interval="+vo.getInterval()+"&count="+vo.getCount()+"&start="+vo.getStart()+"&end="+vo.getEnd();

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.AUTHORIZATION,"Basic bGl6aXh1YW46THp4MjAyMUA=");
        HttpEntity<String> httpEntity = new HttpEntity(null, httpHeaders);

        ResponseEntity<ArrayList> exchange = restTemplateIgnoreSSL.exchange(url, HttpMethod.GET, httpEntity, ArrayList.class);
        List<BiAnTotalValueDto> list = new ArrayList<>();
        if (HttpStatus.OK == exchange.getStatusCode()){
            ArrayList dataList = exchange.getBody();
            for(int i=0; i< dataList.size();i++){
                LinkedHashMap dataArray = (LinkedHashMap) dataList.get(i);
                BiAnTotalValueDto dto = new BiAnTotalValueDto();

                dto.setPrice(Convert.toDouble(dataArray.get("y_value").toString()));
                dto.setVolume(Convert.toDouble(dataArray.get("volume").toString()));

                list.add(dto);
            }
        }else{
            return AjaxResult.error("后台请求异常，请联系后台管理员");
        }



        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getKLine(BourseVo vo){
        //处理symbol和interval 得出条件
        String[] intervals={"1m","3m","5m","15m","30m","1h","2h","4h","6h","8h","12h","1d","3d","1w","1M"};

        if(StringUtils.isNull(vo.getInterval())){
            vo.setInterval("");
        }

        if(!Arrays.asList(intervals).contains(vo.getInterval())){
            return AjaxResult.error("时间间隔传参有误，取值范围:{1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M}");
        }

        if("1m".equals(vo.getInterval())){
            vo.setInterval("");
        }

        if(StringUtils.isBlank(vo.getSymbol())){
            return AjaxResult.error("币种不能为空");
        }


        String table = vo.getInterval().concat(vo.getSymbol());

        if(StringUtils.isBlank(vo.getEnd()) && StringUtils.isBlank(vo.getStart())){
            return AjaxResult.error("时间不能为空");
        }
        List<BiAn> list = new ArrayList<>();
        if(StringUtils.isNotBlank(vo.getEnd())){
            list = biAnMapper2.selectKLineByEndTime(table,vo.getEnd());
            Collections.reverse(list);
        }
        if(StringUtils.isNotBlank(vo.getStart())){
            list = biAnMapper2.selectKLineByStartTime(table,vo.getStart());
        }

        return AjaxResult.success(list);
    }

    //public void insertBtcusdPerp(BiAnWSDto dto,String table){
    //
    //    biAnMapper2.insertBtcusdtPERP(dto,table);
    //}
}
