package com.pj.feign;

import com.alibaba.fastjson.JSON;
import com.pj.api.dto.OrderDto;
import com.pj.api.dto.OrdersDto;
import com.pj.dto.DataDto;
import com.pj.rabbitmq.RabbitMQ;
import com.pj.utils.IdWorker;
import com.pj.utils.MD5;
import com.pj.utils.ht.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Mechrevo
 * @Date 2023 08 29 13 08
 **/
@Service
@Slf4j
public class AmqpTemplateFeign {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送对象至航通
     *
     * @param t
     * @param dataType
     */
    public <T> void sendObject(T t, String dataType) {
        log.info("send data to ht:{},{}", dataType,JSON.toJSONString(t));
        //todo: 数据暂时定死
        DataDto dataDto = new DataDto();
        //msgId
        //使用雪花算法生成,永远不会重复
        Long msgId = new IdWorker().nextId();
        dataDto.setMsgId(msgId + "");
        //appId
        String appId = "sjf01";
        dataDto.setAppId(appId);
        //dataType
        dataDto.setDataType(dataType);
        //data
        //加密
        String jsonString = JSON.toJSONString(t);
        String encryptCBC = AESUtil.encryptCBC(jsonString, RabbitMQ.ACC_KEY);
        dataDto.setData(encryptCBC);
        //msgTime
        String msgTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        dataDto.setMsgTime(msgTime);
        //sign
        String signString = "msgId=" + msgId + "&appId=" + appId + "&dataType=" + dataType + "&msgTime=" + msgTime + "&data=" + encryptCBC;
        String sign = MD5.getMd5(signString, 32);
        dataDto.setSign(sign);

        //mq发送信息
        amqpTemplate.convertAndSend(RabbitMQ.TEST_SEND_QUEUE, dataDto);
        log.info("send mq msg finish:{},{}", dataType,JSON.toJSONString(t));
    }


}
