package cn.axzo.msg.center.mq;

import cn.axzo.msg.center.api.mq.MqMessage;
import cn.axzo.msg.center.service.enums.MqMessageType;
import com.alibaba.fastjson.JSON;
import lombok.Getter;

/**
 * @author yanglin
 */
@Getter
public class MqMessageRecord<Message extends MqMessage> {

    private MqMessageType messageType;
    private Message message;
    private String operatorId;
    private String operatorType;
    private String shardingKey;
    private String tagInfo;
    private String messageKey;

    private MqMessageRecord() {}

    public static <Message extends MqMessage> MqMessageRecordBuilder<Message>
    builder(MqMessageType messageType, Message message) {
        return new MqMessageRecordBuilder<>(messageType, message);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static class MqMessageRecordBuilder<Message extends MqMessage> {
        private final MqMessageType messageType;
        private final Message message;
        private String operatorId;
        private String operatorType;
        private String shardingKey;
        private String tagInfo;
        private String messageKey;

        private MqMessageRecordBuilder(MqMessageType messageType, Message message) {
            this.message = message;
            this.messageType = messageType;
        }

        public MqMessageRecordBuilder<Message> shardingKey(String shardingKey) {
            this.shardingKey = shardingKey;
            return this;
        }

        public MqMessageRecordBuilder<Message> operatorId(Long operatorId) {
            this.operatorId = String.valueOf(operatorId);
            return this;
        }

        public MqMessageRecordBuilder<Message> operatorId(String operatorId) {
            this.operatorId = operatorId;
            return this;
        }

        public MqMessageRecordBuilder<Message> operatorType(String operatorType) {
            this.operatorType = operatorType;
            return this;
        }

        public MqMessageRecordBuilder<Message> tagInfo(String tagInfo) {
            this.tagInfo = tagInfo;
            return this;
        }

        public MqMessageRecordBuilder<Message> messageKey(Object key) {
            this.messageKey = String.valueOf(key);
            return this;
        }

        public MqMessageRecord<Message> build() {
            MqMessageRecord<Message> record = new MqMessageRecord<>();
            record.messageType = messageType;
            record.message = message;
            record.operatorId = operatorId;
            record.operatorType = operatorType;
            record.shardingKey = shardingKey;
            record.tagInfo = tagInfo;
            record.messageKey = messageKey;
            return record;
        }
    }

}