package com.kxs.stat.api.amqp.rabbit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kxs.common.mq.enums.MQSendTypeEnum;
import com.kxs.common.mq.model.AbstractMQ;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;

import java.util.UUID;

/**
 * 客小爽pos 广电 来客吧激活推送队列
 *
 * @author 没秃顶的码农
 * @date 2024-06-01
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class RabbitKxsActQueueMQ extends AbstractMQ {


    /**
     * 订单交易队列
     */
    public static final String QUEUE_NAME = "QUEUE_KXS_ACT_DIVISION";

    /**
     * 死队列名称
     */
    public static final String DEAD_QUEUE_NAME = null;

    /**
     * 内置msg 消息体定义
     **/
    private MsgEntity msgEntity;

    /**
     *  定义Msg消息载体
     **/
    @Data
    public static class MsgEntity {

        /**
         * 用户ID
         */
        private Long  userId;
        /**
         * 品牌 ID
         */
        private Integer  brandId;
        /**
         * 激活时间
         */
        private String actTime;
        /**
         * 绑定时间
         */
        private String bindTime;
        /**
         * sn
         */
        private String sn;

        /**
         * 激活类型 1 pos激活 2广电激活 3来客吧签约激活 4来客吧缴费激活
         */
        private Integer actType;


    }

    @Override
    public String getQueueName() {

        return QUEUE_NAME;
    }

    @Override
    public String getDeadQueueName() {

        return DEAD_QUEUE_NAME;
    }

    @Override
    public MQSendTypeEnum getMqType() {

        return MQSendTypeEnum.QUEUE;
    }

    @Override
    public Message toMessage() {
        String message = JSONObject.toJSONString(msgEntity);
        // 构建消息体
        return MessageBuilder.withBody(message.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                .setMessageId(UUID.randomUUID().toString())
                .build();
    }

    /**
     * 构造发送消息
     */
    public static RabbitKxsActQueueMQ build(MsgEntity message){

        return new RabbitKxsActQueueMQ(message);
    }

    /**
     * 解析MQ消息， 一般用于接收MQ消息时
     */
    public static MsgEntity parse(String msg){
        return JSON.parseObject(msg, MsgEntity.class);
    }

}
