package com.zhongshu.card.server.core.domain.devices;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.microservice.types.deviceUse.DeviceType;
import com.github.microservice.types.deviceUse.DeviceUseState;
import com.github.microservice.types.deviceUse.OperateState;
import com.github.microservice.types.deviceUse.OperateType;
import com.github.microservice.types.user.DevicePermissFromType;
import com.github.microservice.types.user.UserFromType;
import com.zhongshu.card.client.model.devices.DeviceInfoStoreModel;
import com.zhongshu.card.client.model.org.UserCountSimpleModel;
import com.zhongshu.card.client.model.school.CardInfoStoreModel;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.domain.base.SuperMain;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;

/**
 * 设备使用日志
 *
 * @author TRX
 * @date 2024/10/21
 */
@Data
@Document
@NoArgsConstructor
@AllArgsConstructor
public class DeviceUseRecords extends SuperMain {

    @Indexed(unique = true, sparse = true)
    @Schema(description = "mqtt推送生成的业务数据ID")
    private String mqttDataId;

    @Schema(description = "关联的项目名称")
    private String projectName;

    @Schema(description = "设备使用状态")
    private DeviceUseState useState = DeviceUseState.Finish;

    @Schema(description = "权限来自哪里的配置")
    private DevicePermissFromType permissFromType = DevicePermissFromType.SystemConfig;

    @Schema(description = "用户类型")
    private UserFromType userFromType = UserFromType.SystemUser;

    @Schema(description = "处理的service名称")
    private String serviceBeanName;

    @Schema(description = "关联的数据id")
    private String aboutDataId;

    //----------------------------设备信息start ---------------------

    @Schema(description = "操作模式，卡片、人脸、密码、指纹...")
    private OperateType operateType;

    @Schema(description = "操作记录结果, 成功、失败、未知")
    private OperateState operateState;

    @Schema(description = "使用开始时间")
    private Long startTime;

    @Schema(description = "使用结束时间")
    private Long endTime;

    @Schema(description = "设备ID")
    private String deviceId;

    @Schema(description = "设备名称")
    private String deviceName;

    @Schema(description = "设备类型")
    private DeviceType deviceType;

    @Schema(description = "设备信息")
    private DeviceInfoStoreModel deviceInfo;

    @Schema(description = "网关ID")
    private String gateWayId;

    @Schema(description = "设备所属机构oid")
    private String beLongOid;

    @Schema(description = "设备所属机构name")
    private String beLongName;

    //----------------------卡片信息 start-------------------

    @Schema(description = "10进制卡序列号（实体卡号或虚拟卡号）")
    private String cardNo;

    @Schema(description = "卡片信息")
    private CardInfoStoreModel cardInfo;

    @Schema(description = "人脸信息")
    private String pic = "";

    //------------------用户信息 start-------------------

    @Schema(description = "用户userId")
    private String userId;

    @Schema(description = "用户名称")
    private String userName;

    @Schema(description = "用户信息")
    public UserCountSimpleModel userAccount;

    @Schema(description = "使用时间 2024-09-12 20:40:00")
    private String time;

    @Schema(description = "时间")
    private Long timestamp;

    @Schema(description = "耗时，毫秒")
    private Long useTime;

    @Schema(description = "年份，如：2024")
    private Integer year;

    @Schema(description = "月份，如：6")
    private Integer month;

    @Schema(description = "当前月的第几天")
    private Integer dayOfMonth;

    @Schema(description = "今年第几周,如：32")
    private Integer week;

    @Schema(description = "当前年的第几天")
    private Integer dayOfYear;

    @Schema(description = "当前的第几小时")
    private Integer hourOfDay;

    public void setTimes() {
        this.year = DateUtils.getCurrentYear();
        this.month = DateUtils.getCurrentMonthInYear();
        this.dayOfMonth = DateUtils.getCurrentDayInMonth();
        this.hourOfDay = DateUtils.getCurrentHourOfDay();

        this.dayOfYear = DateUtils.getCurrentDayInYear();
        this.week = DateUtils.getCurrentWeekInYear();
    }

    //-----------------------验证是否通过

    @Schema(description = "创建时间，可阅读的")
    private String createTimeStr;

    @Schema(description = "提示信息")
    private String msg = "";

    @JsonProperty("IsOffLine")
    @Schema(description = "是否是离线使用")
    private Boolean isOffLine = Boolean.FALSE;

    @Schema(description = "过期时间")
    @Indexed(expireAfterSeconds = 0)
    private Date ttl;

    @Schema(description = "关联的数据")
    private Object data;

    @Schema(description = "操作类型")
    private String operator;
}
