package com.elab.mq.model;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.elab.core.utils.StringUtils;

import java.util.Properties;

/**
 * 消息容器承装实体
 *
 * @author Liukx
 * @create 2018-04-09 18:38
 * @email liukx@elab-plus.com
 **/
public class MessageModel<T> extends Message {

    private T object;

    private String groupId;

    /**
     * 模块名称
     */
    private String moduleName;
    /**
     * 生产者表的主键编号
     */
    private String producerId;

    /**
     * 消费者运行耗时
     */
    private Long invokeTime;

    /**
     * 使用该model传递参数
     *
     * @param topic 主题
     * @param tags  区分
     * @param key   全局唯一编号
     * @param body  消息内容
     */
    public MessageModel(String topic, String tags, String key, T body) {
        setObject(body);
        setKey(key);
        setTopic(topic);
        setTag(tags);
        addDefaultData();
    }

    private void addDefaultData() {
        Properties userProperties = getUserProperties();
        if (producerId != null) {
            userProperties.setProperty("producerId", producerId + "");
        }
    }

    /**
     * 使用该model必须传递的参数
     *
     * @param topic
     * @param key
     * @param body
     */
    public MessageModel(String topic, String key, T body) {
        setObject(body);
        setKey(key);
        setTopic(topic);
        setTag("*");
        addDefaultData();
    }

    public MessageModel(Message message) {
        setKey(message.getKey());
        setMsgID(message.getMsgID());
        setBody(message.getBody());
        setTag(message.getTag());
        setTopic(message.getTopic());
        setBornHost(message.getBornHost());
        setReconsumeTimes(message.getReconsumeTimes());
        setShardingKey(message.getShardingKey());
        setStartDeliverTime(message.getStartDeliverTime());
        setUserProperties(message.getUserProperties());
    }

    /**
     * 获取对应的转换对象，切记必须为JSON格式的
     *
     * @param clazz
     * @return
     */
    public <T> T getObject(Class<T> clazz) {
        byte[] body = getBody();
        if (body != null) {
            String bodyString = new String(body);
            if (StringUtils.isNotEmpty(bodyString)) {
                return JSON.parseObject(bodyString, clazz);
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return super.toString() + " \t key : " + getKey();
    }

    /**
     * 设置传输对象
     *
     * @param object 必须为符合JSON转换的请求
     */
    public void setObject(Object object) {
        String s = JSON.toJSONString(object);
        setBody(s.getBytes());
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getProducerId() {
        return getDefault("producerId", null);
    }

    public void setProducerId(String producerId) {
        putUserProperties("producerId", producerId);
    }

    public String getDefault(String key, String defaultValue) {
        String userProperties = getUserProperties(key);
        if (userProperties == null || "".equals(userProperties.trim())) {
            return defaultValue;
        }
        return userProperties;
    }

    public Long getConsumerTime(){
        String consumeStartTime = getUserProperties("CONSUME_START_TIME");
        if(StringUtils.isNotEmpty(consumeStartTime)){
            return Long.valueOf(consumeStartTime);
        }
        return null;
    }

    public Long getInvokeTime() {
        return invokeTime;
    }

    public void setInvokeTime(Long invokeTime) {
        this.invokeTime = invokeTime;
    }
}
