package com.pj.listen;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.StaticLog;
import com.alibaba.fastjson2.JSONObject;
import com.pj.api.dto.OrderDto;
import com.pj.dto.DataDto;
import com.pj.enummj.FinishStatus;
import com.pj.ht_byte.HtByte;
import com.pj.ht_byte.HtByteMapper;
import com.pj.rabbitmq.RabbitMQ;
import com.pj.utils.ht.AESUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author Mechrevo
 * @Date 2023 08 25 12 30
 **/
@Component
public class BaseInfoListen {

    /**
     * 保存航通传过来的字节
     */
    @Autowired
    private HtByteMapper htByteMapper;

    /**
     * 接收: 信息同步
     * 测试已通过
     * @param json
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitMQ.TEST_ACCEPT_QUEUE))
    public void accept(Message json){
        byte[] content = json.getBody();
        //保存到数据库
        String jsonString = new String(content);
        StaticLog.info("\n接收:信息同步:{}",jsonString);
        htByteMapper.insert(new HtByte(RandomUtil.randomString(64),null,jsonString,RabbitMQ.TEST_ACCEPT_QUEUE, FinishStatus.FINISH_STATUS_ZERO.getCode(),new Date()));

    }


    /**
     * 测试接收
     * 测试已通过
     */
    /*@RabbitListener(queuesToDeclare = @Queue(RabbitMQ.TEST_SEND_QUEUE))
    public void sendMessage(Message dataDto1){
        System.out.println("\n测试:接收信息\n");
        byte[] body = dataDto1.getBody();
        String s = new String(body);
        DataDto dataDto = JSONObject.parseObject(s, DataDto.class);
        String cbc = AESUtil.decryptCBC(dataDto.getData(), RabbitMQ.ACC_KEY);
        OrderDto orderDto = JSONObject.parseObject(cbc, OrderDto.class);
        System.out.println("\n" + orderDto.toString() + "\n");
    }*/

}
