package com.ms.biz.quque.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.ms.biz.quque.Topic;
import lombok.Data;
import org.apache.rocketmq.common.message.Message;

import java.util.UUID;

@Data
public class BaseMessage {
    @JSONField(serialize = false)
    protected String keys = "";

    @JSONField(serialize = false)
    protected String topic = "";

    @JSONField(serialize = false)
    protected String tags = "";

    BaseMessage() {
        this.keys = generateKey();
    }

    BaseMessage(String keys) {
        this.keys = keys;
    }

    protected byte[] getBody() {
        return JSON.toJSONString(this).getBytes();
    }

    public Message build() {
        Message message = new Message();
        message.setBody(this.getBody());
        message.setKeys(keys);
        return message;
    }

    public Message build(String topic) {
        return new Message(Topic.rebuild(topic), "", keys, this.getBody());
    }

    public Message build(String topic, String tags, int flag, boolean waitStoreMsgOK) {
        return new Message(Topic.rebuild(topic), tags, keys, flag, this.getBody(), waitStoreMsgOK);
    }

    public Message build(String topic, String tags) {
        return new Message(Topic.rebuild(topic), tags, keys, 0, this.getBody(), true);
    }

    protected String generateKey() {
        return UUID.randomUUID().toString();
    }
}
