package com.sptg.device.api.entity;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import com.sptg.common.core.enums.Instruct;
import com.sptg.common.core.enums.InstructDH;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 *
 *
 * description: 指令推送日志
 */
@Setter
@Getter
@NoArgsConstructor
public class InstructPublishLog implements java.io.Serializable {

    @TableId(type = IdType.ID_WORKER)
    private Long id;

    /**
     * 机构位置
     */
    private String orgPosition;

    /**
     * 设备序列号
     */
    private String serialNumber;

    /**
     * 指令
     */
    private String instructCode;

    /**
     * 交换机
     */
    private String exchangeName;

    /**
     * 指令描述
     */
    private String instructDescribe;

    /**
     * 推送数据
     */
    private String publishData;

    /**
     * 状态：{ 1：待消费，2：已消费，3：异常 }
     */
    private Integer status;

    /**
     * 错误信息
     */
    private String errorMsg;

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

    //以下注释字段如果数据库表没有对应的话，会报错，建议注释
//    /**
//     * 回调请求状态码
//     */
//    @TableField(exist = false)
//    private String code;
//
//    /*
//     * 回调请求数据
//     */
//    @TableField(exist = false)
//    private String callbackData;

    /**
     * 最后修改时间
     */
    private Date lastModifyTime;

    /**
     * 最近重发时间
     */
    private Date lastResendTime;

    private String xqName;

    private String cardNo;

    private Date bizBeginTime;

    private Date bizEndTime;

    /**
     * 用于在列表展示的数据，比如卡号，人脸照片
     */
    private String showContent;

    /**
     * 设备业务阶段性状态
     */
    private Integer bizStatus;

    /**
     * 设备处理状态
     * 1、待处理
     * 2、已处理
     * 3、处理失败
     */
    private Integer handleStatus;

    /**
     * 指令是否已过期作废
     * 1、否
     * 2、过期作废
     */
    private Integer invalid;

    /**
     * 人脸图片地址
     */
    private String imgSrc;

    /**
     * 有效期开始时间
     */
    private Date beginTime;

    /**
     * 有效期结束时间
     */
    private Date endTime;

    /**
     * 版本号
     */
    private String version;

    /**
     * 用户名称
     */
    private String userName;

    /**
     * 单元名称
     */
    private String unitName;

    /**
     * 房间名称
     */
    private String roomName;

    /**
     * 密码
     */
    private String password;

    /**
     * 广告编号的格式用yyMMdd+2位排序号前面补0
     */
    @TableField(value = "s_ad_code")
    private String adCode;

    public InstructPublishLog(long id,Device device, Instruct instruct,String data, Map<String,String> map) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat();
        this.id = id;
        this.orgPosition = device.getOrgPosition();
        this.serialNumber = device.getSerialNumber();
        this.instructCode = instruct.getCode();
        this.exchangeName = instruct.getExchangeName();
        this.instructDescribe = instruct.getDescribe();
        this.publishData = data;
        this.status = 1;
        this.createTime = new Date();
        this.handleStatus = 1;
        this.invalid = 1;
        this.xqName = map.get("xqName");
        this.unitName = map.get("unitName");
        this.version = map.get("version");
        this.userName = map.get("userName");
        this.roomName = map.get("roomName");
        this.password = map.get("password");
        if (map.get("beginTime") != null){
            this.beginTime = format.parse(map.get("beginTime"));
        }else {
            this.beginTime = null;
        }
        if (map.get("endTime") != null){
            this.endTime = format.parse(map.get("endTime"));
        }else {
            this.endTime = null;
        }
        this.cardNo = map.get("cardNo");
        this.imgSrc = map.get("imgSrc");
    }

    public InstructPublishLog(long id, Device device, InstructDH instructDH, String data, Map<String,String> map) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat();
        this.id = id;
        this.orgPosition = device.getOrgPosition();
        this.serialNumber = device.getSerialNumber();
        this.instructCode = instructDH.getCode();
        this.exchangeName = instructDH.getExchangeName();
        this.instructDescribe = instructDH.getDescribe();
        this.publishData = data;
        this.status = 1;
        this.createTime = new Date();
        this.handleStatus = 1;
        this.invalid = 1;
        this.xqName = map.get("xqName");
        this.unitName = map.get("unitName");
        this.version = map.get("version");
        this.userName = map.get("userName");
        this.roomName = map.get("roomName");
        this.password = map.get("password");
        this.beginTime = format.parse(map.get("beginTime"));
        this.endTime = format.parse(map.get("endTime"));
        this.cardNo = map.get("cardNo");
        this.imgSrc = map.get("imgSrc");
    }

    /**
     * 排序条件
     */
    @TableField(exist = false)
    private String sortField;

    /**
     * 排序顺序
     */
    @TableField(exist = false)
    private String sortOrder;

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @TableField(exist = false)
    private Date beginDate;

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @TableField(exist = false)
    private Date endDate;
}
