package com.jxy.marketall.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.marketall.dto.DateBigDecimalDto;
import com.jxy.marketall.enums.SubscriptionType;
import com.jxy.marketall.service.DataPushService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Service;

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


/**
 * @Description ETF查询业务处理
 * @Date 2022/5/19 17:51
 * @Author 杜懿
 */
@Service
public class DataPushServiceImpl implements DataPushService {


    @Override
    public boolean BTCRealTimeV2e1DataPush(String msg) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        Map<String, DateBigDecimalDto> map = new HashMap<>();
        //解析data
        System.out.println(msg);
        JSONObject jsonObject = JSON.parseObject(msg);
        System.out.println("从数据源处收到数据:"+msg);

        //dateStr是秒 要转换成LONG再乘以1000,即转毫秒再转Date
        String dateStr = JSONObject.toJSONString(jsonObject.get("unixdt"));
        Long timestamp = Convert.toLong(dateStr, 0L);
        if(timestamp <= 0){
            //数据异常,不进入推送流程
            System.out.println("数据日期异常");
            return false;
        }else{
            /**  构造数据 **/
            Date date = new Date(timestamp *1000);
            BigDecimal fees = jsonObject.getBigDecimal(SubscriptionType.BTC_FEES_REALTIME.getMetrics());

            //存入unixdt 用户后面时间判断
            DateBigDecimalDto unixdtData = new DateBigDecimalDto();
            unixdtData.setDate(date);
            map.put("unixdt",unixdtData);

        /***************************      以下 内容可以考虑优化        ************************/

            if(StringUtils.isNotNull(fees)){
                DateBigDecimalDto feesDto = new DateBigDecimalDto();
                feesDto.setDate(date);
                feesDto.setValue(fees);
                map.put(SubscriptionType.BTC_FEES_REALTIME.getId(),feesDto);
            }

            BigDecimal activeAddress= jsonObject.getBigDecimal(SubscriptionType.BTC_ACTIVE_ADDRESS_REALTIME.getMetrics());
            if(StringUtils.isNotNull(activeAddress)) {
                DateBigDecimalDto activeAddressDto = new DateBigDecimalDto();
                activeAddressDto.setDate(date);
                activeAddressDto.setValue(activeAddress);
                map.put(SubscriptionType.BTC_ACTIVE_ADDRESS_REALTIME.getId(), activeAddressDto);
            }

            BigDecimal sendAddress = jsonObject.getBigDecimal(SubscriptionType.BTC_SEND_ADDRESS_REALTIME.getMetrics());
            if(StringUtils.isNotNull(sendAddress)) {
                DateBigDecimalDto sendAddressDto = new DateBigDecimalDto();
                sendAddressDto.setDate(date);
                sendAddressDto.setValue(sendAddress);
                map.put(SubscriptionType.BTC_SEND_ADDRESS_REALTIME.getId(), sendAddressDto);
            }

            BigDecimal receiveAddress = jsonObject.getBigDecimal(SubscriptionType.BTC_RECEIVE_ADDRESS_REALTIME.getMetrics());
            if(StringUtils.isNotNull(receiveAddress)) {
                DateBigDecimalDto receiveAddressDto = new DateBigDecimalDto();
                receiveAddressDto.setDate(date);
                receiveAddressDto.setValue(receiveAddress);
                map.put(SubscriptionType.BTC_RECEIVE_ADDRESS_REALTIME.getId(), receiveAddressDto);
            }

            BigDecimal volume = jsonObject.getBigDecimal(SubscriptionType.BTC_VOLUME_REALTIME.getMetrics());
            if(StringUtils.isNotNull(volume)) {
                DateBigDecimalDto volumeDto = new DateBigDecimalDto();
                volumeDto.setDate(date);
                volumeDto.setValue(volume);
                map.put(SubscriptionType.BTC_VOLUME_REALTIME.getId(), volumeDto);
            }

            BigDecimal eavolume = jsonObject.getBigDecimal(SubscriptionType.BTC_EAVOLUME_REALTIME.getMetrics());
            if(StringUtils.isNotNull(eavolume)) {
                DateBigDecimalDto eavolumeDto = new DateBigDecimalDto();
                eavolumeDto.setDate(date);
                eavolumeDto.setValue(eavolume);
                map.put(SubscriptionType.BTC_EAVOLUME_REALTIME.getId(), eavolumeDto);
            }

            BigDecimal asol = jsonObject.getBigDecimal(SubscriptionType.BTC_ASOL_REALTIME.getMetrics());
            if(StringUtils.isNotNull(asol)) {
                DateBigDecimalDto asolDto = new DateBigDecimalDto();
                asolDto.setDate(date);
                asolDto.setValue(asol);
                map.put(SubscriptionType.BTC_ASOL_REALTIME.getId(), asolDto);
            }

            BigDecimal cdd = jsonObject.getBigDecimal(SubscriptionType.BTC_CDD_REALTIME.getMetrics());
            if(StringUtils.isNotNull(cdd)) {
                DateBigDecimalDto cddDto = new DateBigDecimalDto();
                cddDto.setDate(date);
                cddDto.setValue(cdd);
                map.put(SubscriptionType.BTC_CDD_REALTIME.getId(), cddDto);
            }

            /***************************      以上 内容可以考虑优化        ************************/

            //1.创建消息的生产者,指定生产者的组名
            DefaultMQProducer producer=new DefaultMQProducer("producer-group");

            //2.指定NameServer地址
            //producer.setNamesrvAddr("10.168.2.125:9876");
            producer.setNamesrvAddr("54.254.93.204:9876");

            //3.启动生产者
            producer.start();

            String mapJsonStr = JSON.toJSONString(map);
            System.out.println("从数据源处收到数据后处理结果为："+mapJsonStr);
            //4.创建消息对象  指定主题  标签  消息体
            Message message=new Message("BTC_v2e1_rt","realtime",mapJsonStr.getBytes());

            //5.发送消息  顺序消息
            SendResult send = producer.send(message, 10000);

            System.out.println(send);

            //6.关闭生产者
            producer.shutdown();
            return true;
        }
    }
}
