package com.yanzuoguang.mq.vo.req;


import com.yanzuoguang.util.helper.StringHelper;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送给指定服务器消息
 *
 * @param <T>
 * @author 颜佐光
 */
public class ServerMessageReqVo<T> extends ServerQueueReqVo {

    /**
     * 消息标记,在token为空时,发送给所有服务器队列,不为空时,发给token所在服务器的队列
     */
    @ApiModelProperty(notes = "消息标记,在token为空时,发送给所有服务器队列,不为空时,发给token所在服务器的队列")
    private String token;

    /**
     * 数据内容
     */
    @ApiModelProperty(notes = "数据内容")
    private T data;

    /**
     * 当前次数
     */
    @ApiModelProperty(notes = "当前次数")
    private int pos = 0;

    /**
     * 重试时间,默认为60*1000毫秒,达到指定次数的延迟时间
     */
    @ApiModelProperty(notes = "重试时间,默认为60*1000毫秒,达到指定次数的延迟时间")
    private Map<Integer, Long> repairTime = new HashMap<>();

    /**
     * 最大次数,达到最大次数时,会停止重发
     */
    @ApiModelProperty(notes = "最大次数,达到最大次数时,会停止重发。为0时,则默认为100")
    private int maxPos = 0;

    /**
     * 构造函数
     */
    public ServerMessageReqVo() {
        super(StringHelper.EMPTY);
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     */
    public ServerMessageReqVo(String queueName, String token, T data) {
        super(queueName);
        this.token = token;
        this.data = data;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     * @param maxPos
     */
    public ServerMessageReqVo(String queueName, String token, T data, int maxPos) {
        super(queueName);
        this.token = token;
        this.data = data;
        this.maxPos = maxPos;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     * @param repairTime
     */
    public ServerMessageReqVo(String queueName, String token, T data, Map<Integer, Long> repairTime) {
        super(queueName);
        this.token = token;
        this.data = data;
        this.repairTime = repairTime;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param maxPos
     */
    public ServerMessageReqVo(String queueName, int maxPos) {
        super(queueName);
        this.maxPos = maxPos;
    }

    /**
     * 添加当前执行位置
     */
    public void addPos() {
        this.pos++;
    }

    /**
     * 是否继续往下执行
     *
     * @return
     */
    public boolean isNext() {
        if (this.maxPos == 0) {
            this.maxPos = 3;
        }
        return this.pos < this.maxPos;
    }

    /**
     * 获取下次延迟执行时间
     *
     * @return
     */
    public long getNextDelayTime() {
        Map.Entry<Integer, Long> max = null;
        if (this.repairTime != null) {
            for (Map.Entry<Integer, Long> kvp : this.repairTime.entrySet()) {
                if (kvp.getKey() > this.pos) {
                    continue;
                }
                if (max == null || max.getKey() < kvp.getKey()) {
                    max = kvp;
                }
            }
        }
        return max == null ? 600000 : max.getValue();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    public Map<Integer, Long> getRepairTime() {
        return repairTime;
    }

    public void setRepairTime(Map<Integer, Long> repairTime) {
        this.repairTime = repairTime;
    }

    public int getMaxPos() {
        return maxPos;
    }

    public void setMaxPos(int maxPos) {
        this.maxPos = maxPos;
    }

}