package com.mortals.xhx.queue;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders;
import com.mortals.xhx.common.model.MessageHeader;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.UUID;

/**
 * 默认消息
 *
 * @author: zxfei
 * @date: 2021/11/22 10:59
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DefaultTbQueueMsg implements TbQueueMsg {
    /**
     * key 唯一标识
     */
    private  String key;
    /**
     * 数据载体
     */
    private  byte[] data;


    /**
     * 消息头信息
     */
    private  TbQueueMsgHeaders headers;

    public DefaultTbQueueMsg(TbQueueMsg msg) {
        this.key = msg.getKey();
        this.data = msg.getData();
        TbQueueMsgHeaders headers = new DefaultTbQueueMsgHeaders();

        msg.getHeaders().getData().entrySet().stream().forEach(item->
            headers.put(item.getKey(),item.getValue()));

        this.headers = headers;
    }

    public static void main(String[] args) {

        TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
        header.put(MessageHeader.MESSAGETYPE, "UPGREAD");
        // header.put(MessageHeader.CLIENTID, "abcd1234");
      //  header.put(MessageHeader.TIMESTAMP, DateUtils.getCurrStrDateTime());
        //header.put(MessageHeader.MESSAGESIGN,"ssdafasdfasdfasfd");
        TbQueueMsg queueMsg = new DefaultTbQueueMsg(IdUtil.fastUUID(), "abcd1234".getBytes() , header);

        String ret = JSON.toJSONString(queueMsg);
        System.out.println("pro:"+ret);
//
//        DefaultTbQueueMsg qu = JSON.parseObject(ret, DefaultTbQueueMsg.class);

    }

}
