package com.ynfy.buss.exam.exam.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ynfy.buss.exam.exam.dto.QuestionTypeCountDTO;
import com.ynfy.buss.exam.exam.enums.ExamState;
import com.ynfy.buss.exam.paper.entity.Paper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;

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

/**
 * @Description: exam
 * @Author: jeecg-boot
 * @Date: 2023-02-23
 * @Version: V1.0
 */
@Data
@TableName("exam")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "exam对象", description = "exam")
public class Exam implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "id")
    private String id;
    /**
     * 考试标题
     */
    @Excel(name = "考试标题", width = 15)
    @ApiModelProperty(value = "考试标题")
    private String title;
    /**
     * 考试权限 1公开2部门3定员
     */
    @Excel(name = "1公开2部门3定员", width = 15)
    @ApiModelProperty(value = "1公开2部门3定员")
    @Dict(dicCode = "open_type")
    private Integer openType;
    /**
     * 开始时间
     */
    @Excel(name = "开始时间", width = 15, format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "开始时间")
    private Date startTime;
    /**
     * 结束时间
     */
    @Excel(name = "结束时间", width = 15, format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "结束时间")
    private Date endTime;
    /**
     * createTime
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "createTime")
    private Date createTime;
    /**
     * createBy
     */
    @ApiModelProperty(value = "createBy")
    private String createBy;
    /**
     * updateTime
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "updateTime")
    private Date updateTime;
    /**
     * updateBy
     */
    @ApiModelProperty(value = "updateBy")
    private String updateBy;
    /**
     * 试卷总分
     */
    @Excel(name = "试卷总分", width = 15)
    @ApiModelProperty(value = "试卷总分")
    private Integer totalScore;
    /**
     * 及格分
     */
    @Excel(name = "及格分", width = 15)
    @ApiModelProperty(value = "及格分")
    private Integer qualifyScore;
    /**
     * 考试时长
     */
    @Excel(name = "考试时长", width = 15)
    @ApiModelProperty(value = "考试时长")
    private Integer totalTime;
    /**
     * 试卷id
     */
    @Excel(name = "试卷id", width = 15)
    @ApiModelProperty(value = "试卷id")
    private String paperId;

    @Excel(name = "限考次数", width = 15)
    @ApiModelProperty(value = "考试时长")
    private Integer limitCount;

    @Excel(name = "考试者", width = 15)
    @ApiModelProperty(value = "考试者")
    private String examiner;

    @Excel(name = "是否切屏检测", width = 15)
    @ApiModelProperty(value = "是否切屏检测")
    private Boolean leaveOn;

    @Excel(name = "可以切屏的次数", width = 15)
    @ApiModelProperty(value = "可以切屏的次数")
    private Integer totalLeaveTimes;

    @Excel(name = "切换到其他页面多长时间判定为切屏", width = 15)
    @ApiModelProperty(value = "切换到其他页面多长时间判定为切屏")
    private Integer leaveTime;

    @Excel(name = "是否题目乱序", width = 15)
    @ApiModelProperty(value = "是否题目乱序")
    private Boolean questionDisorder;

    @Excel(name = "是否答案乱序", width = 15)
    @ApiModelProperty(value = "是否答案乱序")
    private Boolean answerDisorder;

    /**
     * 阅卷人类型  1:部门  2：人员
     */
    @Excel(name = "阅卷人类型", width = 15)
    @ApiModelProperty(value = "阅卷人类型")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Integer reviewerType;

    /**
     * 阅卷人
     */
    @Excel(name = "阅卷人", width = 15)
    @ApiModelProperty(value = "阅卷人")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String reviewer;

    @TableField(exist = false)
    private Paper paper;

    @TableField(exist = false)
    @Dict(dicCode = "exam_state")
    private Integer state;

    @TableField(exist = false)
    private List<QuestionTypeCountDTO> questionTypeCountList;

    /**
     * 用户考试次数
     */
    @TableField(exist = false)
    private Integer tryCount;

    /**
     * 图片
     */
    @Excel(name = "图片", width = 15)
    @ApiModelProperty(value = "图片")
    private String image;

    @ApiModelProperty(value = "考试结果展示类型 1：成绩 2：成绩+试卷明细")
    private Integer examResultShowtype;

    @ApiModelProperty(value = "展示截止时间")
    private Integer showDeadline;

    /**
     * 是否结束
     *
     * @return
     */
    public Integer getState() {
        if (!Objects.isNull(startTime)) {
            if (System.currentTimeMillis() < startTime.getTime()) {
                return ExamState.READY_START;
            }
        }
        if (!Objects.isNull(endTime)) {
            if (System.currentTimeMillis() > endTime.getTime()) {
                return ExamState.OVERDUE;
            }
        }
        if (!Objects.isNull(startTime) && !Objects.isNull(endTime)) {
            if (System.currentTimeMillis() > startTime.getTime()
                    && System.currentTimeMillis() < endTime.getTime()) {
                return ExamState.ENABLE;
            }
        }
        return null;
    }
}
