package com.qmth.boot.core.solar.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 应用部署控制参数
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class AppControl implements Serializable {

    private static final long serialVersionUID = -3748275799910491373L;

    //过期时间戳
    private Long expireTime;

    //最大在线人数
    private Integer maxOnlineUserCount;

    //自定义控制参数
    private Map<String, Object> custom = new HashMap<>();

    /**
     * 判断是否已过期
     *
     * @return
     */
    public boolean hasExpired() {
        return expireTime != null && System.currentTimeMillis() > expireTime;
    }

    /**
     * 判断是否超过最大在线用户数
     *
     * @param count
     * @return
     */
    public boolean exceedOnlineUserCount(int count) {
        return maxOnlineUserCount != null && count > maxOnlineUserCount;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public Integer getMaxOnlineUserCount() {
        return maxOnlineUserCount;
    }

    public void setMaxOnlineUserCount(Integer maxOnlineUserCount) {
        this.maxOnlineUserCount = maxOnlineUserCount;
    }

    public Map<String, Object> getCustom() {
        return custom;
    }

    public void setCustom(Map<String, Object> custom) {
        this.custom = custom;
    }

    /**
     * 读取自定义参数
     *
     * @param key
     * @param <T>
     * @return 自定进行类型转换，参数不存在或类型不匹配都返回null
     */
    public <T> T getCustomParam(String key) {
        Object value = custom != null ? custom.get(key) : null;
        try {
            return value != null ? (T) value : null;
        } catch (Exception e) {
            return null;
        }
    }
}
