package com.jxy.marketall.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxy.common.core.exception.GlobalException;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.marketall.dto.DataDto;
import com.jxy.marketall.entity.DistributionData;
import com.jxy.marketall.entity.MarketData;
import com.jxy.marketall.enums.CoinType;
import com.jxy.marketall.enums.DistributionType;
import com.jxy.marketall.enums.MarketType;
import com.jxy.marketall.service.DistributionService;
import com.jxy.marketall.service.MarketService;
import com.jxy.marketall.service.NodeJsMarketFeign;
import com.jxy.marketall.utils.EnumUtils;
import com.jxy.marketall.vo.CoinByTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @Description 分配数据查询业务处理
 * @Date 2022/6/10 10:25
 * @Author 杜懿
 */
@Service
public class DistributionServiceImpl implements DistributionService {

    @Autowired
    private NodeJsMarketFeign nodeJsMarketFeign;

    @Override
    public AjaxResult findDistributionByTypeAndCoin(CoinByTypeVo coinByTypeVo) {

        HashMap<String,String> map = new HashMap<>();
        String type = coinByTypeVo.getType();
        String coin = coinByTypeVo.getCoin();

        /** 条件范围 */
        if(!EnumUtils.isInclude(DistributionType.class,type)){
            //todo 自定义异常
            throw new GlobalException("type传参不在查询访问之内！");
        }
        if(!EnumUtils.isInclude(CoinType.class,coin)){
            //todo 自定义异常
            throw new  GlobalException("coin传参不在查询访问之内！");
        }

        DistributionType distributionType = (DistributionType) EnumUtils.get(DistributionType.class,type);
        map.put("type",distributionType.getInfo());

        CoinType coinType = (CoinType) EnumUtils.get(CoinType.class,coin);
        map.put("coin",coinType.getInfo());


        /** 判断起止时间是否为空 */
        if(!StringUtils.isEmpty(coinByTypeVo.getStart()) && !StringUtils.isEmpty(coinByTypeVo.getEnd())){
            map.put("start",coinByTypeVo.getStart());
            map.put("end",coinByTypeVo.getEnd());
        }

        /** 调用查询接口 */
        JSONObject returnDto = nodeJsMarketFeign.distribution(map);

        JSONArray data = JSON.parseArray(JSONObject.toJSONString(returnDto.get("data")));

        List<DistributionData> list = JSONObject.parseArray(data.toJSONString(),DistributionData.class);

        List<DataDto> dataList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            DataDto dataDto = new DataDto();
            DistributionData entry = list.get(i);
            dataDto.setId(entry.getId());
            dataDto.setTime(entry.getDate());
            dataDto.setValue(Convert.toDouble(entry.getValue()));
            dataList.add(i,dataDto);
        }

        return AjaxResult.success(dataList);
    }


}
