package com.gxwebsoft.common.mq.message;

import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 统一消息实体 - 用于各模块间的数据同步
 */
@Data
public class SyncMessage implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 消息唯一ID
     */
    private String messageId;

    /**
     * 消息类型：USER_SYNC, TENANT_SYNC, etc.
     */
    private String messageType;

    /**
     * 事件类型：CREATE, UPDATE, DELETE
     */
    private String eventType;

    /**
     * 目标系统标识
     */
    private String targetSystem;

    /**
     * 业务数据（Map格式，便于序列化）
     */
    private Map<String, Object> data;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 消息重试次数
     */
    private Integer retryCount;

    public SyncMessage() {
        this.createTime = LocalDateTime.now();
        this.retryCount = 0;
    }

    public SyncMessage(String messageType, String eventType, String targetSystem, Map<String, Object> data) {
        this();
        this.messageId = java.util.UUID.randomUUID().toString().replace("-", "");
        this.messageType = messageType;
        this.eventType = eventType;
        this.targetSystem = targetSystem;
        this.data = data;
    }

    /**
     * 创建用户同步消息
     */
    public static SyncMessage userCreate(String targetSystem, Map<String, Object> userData) {
        return new SyncMessage("USER_SYNC", "CREATE", targetSystem, userData);
    }

    public static SyncMessage userUpdate(String targetSystem, Map<String, Object> userData) {
        return new SyncMessage("USER_SYNC", "UPDATE", targetSystem, userData);
    }

    public static SyncMessage userDelete(String targetSystem, Map<String, Object> userData) {
        return new SyncMessage("USER_SYNC", "DELETE", targetSystem, userData);
    }
}
