package cn.estsh.i3plus.pojo.model.license;

import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * @Description :
 * @Reference :
 * @Author : wei.peng
 * @CreateDate : 20-2-10 下午5:10
 * @Modify:
 **/
@Data
public class ImppLicense implements Serializable {

    private static final long serialVersionUID = 2365830603839063328L;

    private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     *
     * 授权人名称
     * 授权开始时间
     * 授权结束时间
     *
     * 授权工厂
     * 仓库
     * 产线
     * 用户
     * 授权描述
     * 同时最大在线数量
     *
     */

    /* 基础信息 */
    @ApiParam(value ="授权ID")
    private Long id;                            // ID
    @ApiParam(value ="授权应用ID")
    private Long appId;                         // APP_ID
    @ApiParam(value ="授权读取授权数据ID")
    private Integer licenseSourceId;            // 授权数据来源
    public String getLicenseSourceTxt() {
        return licenseSourceId == null ? null : CommonEnumUtil.LICENSE_SOURCE.valueOf(licenseSourceId);
    }

    /* 授权信息 */
    @ApiParam(value ="授权开始时间")
    private String licenseStartDateTime;          // 授权开始时间
    @ApiParam(value ="授权结束时间")
    private String licenseEndDateTime;                // 授权结束时间
    @ApiParam(value ="授权组织数量")
    private Integer licenseMaxOrganize;         // 授权组织数量
    @ApiParam(value ="授权仓库数量")
    private Integer licenseMaxWarehouse;        // 授权仓库数量
    @ApiParam(value ="授权产线数量")
    private Integer licenseMaxProductionLine;   // 授权产线数量
    @ApiParam(value ="授权用户数量")
    private Integer licenseMaxUser;             // 授权用户数量
    @ApiParam(value ="授权同时在线数量")
    private Integer licenseMaxUserSession;      // 授权在线用户数量
    @ApiParam(value ="授权描述信息")
    private String licenseDescription;          // 授权描述信息
    @ApiParam(value ="深思授权信息")
    private ImppLicenseSenseshield senseshield;

    /* 备用预留字段 */
    @ApiParam(value ="备用字段")
    private String licenseSpare01;
    @ApiParam(value ="备用字段")
    private String licenseSpare02;
    @ApiParam(value ="备用字段")
    private String licenseSpare03;
    @ApiParam(value ="备用字段")
    private String licenseSpare04;
    @ApiParam(value ="备用字段")
    private String licenseSpare05;
    @ApiParam(value ="备用字段")
    private String licenseSpare06;
    @ApiParam(value ="备用字段")
    private String licenseSpare07;
    @ApiParam(value ="备用字段")
    private String licenseSpare08;
    @ApiParam(value ="备用字段")
    private String licenseSpare09;
    @ApiParam(value ="备用字段")
    private String licenseSpare10;

    /* 业务人员信息 */
    @ApiParam(value ="授权创建时间")
    private Date licenseCreateDateTime;         // 授权时间
    @ApiParam(value ="授权创建人员ID")
    private Long licenseCreateUserId;           // 授权用户ID
    @ApiParam(value ="授权创建人员名称")
    private String licenseCreateUserName;       // 授权人名称
    @ApiParam(value ="授权更新人员ID")
    private Long licenseUpdateUserId;           // 授权更新人员ID
    @ApiParam(value ="授权更新人员名称")
    private String licenseUpdateUserName;       // 授权人名称
    @ApiParam(value ="授权更新人员时间")
    private Date licenseUpdateDateTime;         // 授权更新时间

    /* 授权锁信息(软锁,usb锁) */
    @ApiParam(value ="授权使用开始时间")
    private String lockStartTime;
    @ApiParam(value ="授权使用结束时间")
    private String lockEndTime;
    @ApiParam(value ="锁类型")
    private String lockType;


    /* 基础业务逻辑 */
    @ApiParam(value ="到期时间")
    private String maturityDateTime;

    public String getMaturityDateTime() {
        if(StringUtils.isBlank(maturityDateTime)){
            return StringUtils.isNotBlank(lockEndTime) ? lockEndTime : licenseEndDateTime;
        }
        return maturityDateTime;
    }

    public void setSenseshield(ImppLicenseSenseshield senseshield) {
        if(Objects.nonNull(senseshield)){
            try {
                Date lockEndTime = new Date((senseshield.getLast_update_timestamp() + senseshield.getSpan_time()) * 1000L);
                Date licenseEndDateTime = new Date(this.licenseEndDateTime);

                this.lockType = senseshield.getType();
                this.maturityDateTime = DateFormatUtils.format(lockEndTime.getTime() > licenseEndDateTime.getTime() ? licenseEndDateTime: lockEndTime,TIME_FORMAT);
            }catch (Exception e){
            }
        }
        this.senseshield = senseshield;
    }

}