package com.qmth.themis.business.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.themis.business.enums.ExamRecordStatusEnum;
import com.qmth.themis.business.enums.MessageTypeEnum;
import com.qmth.themis.business.util.UidUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.jetbrains.annotations.NotNull;

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

/**
 * @Description: 考生轨迹
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@ApiModel(value = "t_e_exam_student_log", description = "考生轨迹")
public class TEExamStudentLog implements Serializable, Comparable<TEExamStudentLog> {

    private static final long serialVersionUID = 1L;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "主键")
    @TableId(value = "id")
    private Long id;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "学生id")
    @TableField(value = "student_id")
    private Long studentId;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "考生id")
    @TableField(value = "exam_student_id")
    private Long examStudentId;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "考试记录id")
    @TableField(value = "exam_record_id")
    private Long examRecordId;

    @ApiModelProperty(value = "类型")
    @TableField(value = "type")
    private String type;

    @ApiModelProperty(value = "描述")
    @TableField(value = "info")
    private String info;

    @ApiModelProperty(value = "备注")
    @TableField(value = "remark")
    private String remark;

    @ApiModelProperty(value = "创建时间")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Long createTime;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "业务id")
    @TableField(value = "obj_id")
    private Long objId;

    @ApiModelProperty(value = "更新时间")
    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    private Long updateTime;

    @ApiModelProperty(value = "日志标题")
    @TableField(value = "title")
    private String title;

    @ApiModelProperty(value = "ip地址")
    private String ip;

    @ApiModelProperty(value = "国家")
    private String country;

    @ApiModelProperty(value = "地区")
    private String region;

    @ApiModelProperty(value = "省份")
    private String province;

    @ApiModelProperty(value = "城市")
    private String city;

    @ApiModelProperty(value = "运营商")
    private String isp;

    @ApiModelProperty(value = "ip是否改变")
    @TableField(exist = false)
    private Boolean ipChange;

    @ApiModelProperty(value = "消息类型")
    @TableField(exist = false)
    private MessageTypeEnum msgType;

    @ApiModelProperty(value = "消息类型")
    @TableField(exist = false)
    private String msgTypeStr;

    @ApiModelProperty(value = "消息内容")
    @TableField(exist = false)
    private String content;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "发送人id")
    @TableField(exist = false)
    private Long formUserId;

    @ApiModelProperty(value = "发送人名称")
    @TableField(exist = false)
    private String formUserName;

    public TEExamStudentLog() {

    }

    public TEExamStudentLog(Long studentId, Long examStudentId, Long examRecordId, MessageTypeEnum msgType, String content, Long formUserId, String formUserName, Long createTime) {
        this.studentId = studentId;
        this.examStudentId = examStudentId;
        this.examRecordId = examRecordId;
        this.msgType = msgType;
        this.msgTypeStr = msgType.getCode();
        this.content = content;
        this.formUserId = formUserId;
        this.formUserName = formUserName;
        this.createTime = createTime;
    }

    public TEExamStudentLog(Long studentId, Long examStudentId, Long examRecordId, MessageTypeEnum msgType, String content, Long createTime) {
        this.studentId = studentId;
        this.examStudentId = examStudentId;
        this.examRecordId = examRecordId;
        this.msgType = msgType;
        this.msgTypeStr = msgType.getCode();
        this.content = content;
        this.createTime = createTime;
    }

    public TEExamStudentLog(String type, String info, String remark, Long studentId, String ip,
                            String country, String region, String province, String city, String isp) {
        this.id = UidUtil.nextId();
        this.type = type;
        this.info = info;
        this.remark = remark;
        this.studentId = studentId;
        this.ip = ip;
        this.country = country;
        this.region = region;
        this.province = province;
        this.city = city;
        this.isp = isp;
    }

    public TEExamStudentLog(String type, String info, String remark, Long studentId, Long examStudentId,
                            Long examRecordId, String title, String ip,
                            String country, String region, String province, String city, String isp) {
        this.id = UidUtil.nextId();
        this.type = type;
        this.info = info;
        this.remark = remark;
        this.studentId = studentId;
        this.examStudentId = examStudentId;
        this.examRecordId = examRecordId;
        this.title = title;
        this.ip = ip;
        this.country = country;
        this.region = region;
        this.province = province;
        this.city = city;
        this.isp = isp;
    }

    public TEExamStudentLog(String type, String info, String remark, Long studentId, Long examStudentId,
                            Long examRecordId, Long objId, String title, String ip,
                            String country, String region, String province, String city, String isp) {
        this.id = UidUtil.nextId();
        this.type = type;
        this.info = info;
        this.remark = remark;
        this.studentId = studentId;
        this.examStudentId = examStudentId;
        this.examRecordId = examRecordId;
        this.objId = objId;
        this.title = title;
        this.ip = ip;
        this.country = country;
        this.region = region;
        this.province = province;
        this.city = city;
        this.isp = isp;
    }

    public TEExamStudentLog(Long studentId, Long examStudentId, Long examRecordId, String type, String info, String remark) {
        this.id = UidUtil.nextId();
        this.type = type;
        this.info = info;
        this.remark = remark;
        this.studentId = studentId;
        this.examStudentId = examStudentId;
        this.examRecordId = examRecordId;
        this.createTime = createTime;
    }

    public String getFormUserName() {
        return formUserName;
    }

    public void setFormUserName(String formUserName) {
        this.formUserName = formUserName;
    }

    public MessageTypeEnum getMsgType() {
        return msgType;
    }

    public void setMsgType(MessageTypeEnum msgType) {
        this.msgType = msgType;
    }

    public String getMsgTypeStr() {
        if (Objects.nonNull(msgType)) {
            return msgType.getCode();
        } else {
            return msgTypeStr;
        }
    }

    public void setMsgTypeStr(String msgTypeStr) {
        this.msgTypeStr = msgTypeStr;
    }

    public List<TEExamStudentLog> ipChange(List<TEExamStudentLog> teExamStudentLogList) {
        String currentIp = teExamStudentLogList.get(0).getIp();
        for (int i = 1; i < teExamStudentLogList.size(); i++) {
            String ip = teExamStudentLogList.get(i).getIp();
            if (Objects.isNull(currentIp) || Objects.equals(currentIp.trim(), "")) {
                currentIp = ip;
                continue;
            } else if (Objects.nonNull(currentIp) && Objects.nonNull(ip) && !Objects.equals(currentIp.trim(), "") && !Objects.equals(currentIp, ip)) {
                if (!Objects.equals(teExamStudentLogList.get(i).getType(), ExamRecordStatusEnum.FINISHED.name())
                        || (Objects.equals(teExamStudentLogList.get(i).getType(), ExamRecordStatusEnum.FINISHED.name()) && Objects.nonNull(ip))) {
                    teExamStudentLogList.get(i).setIpChange(true);
                    currentIp = ip;
                }
            }
        }
        return teExamStudentLogList;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getFormUserId() {
        return formUserId;
    }

    public void setFormUserId(Long formUserId) {
        this.formUserId = formUserId;
    }

    public Boolean getIpChange() {
        return ipChange;
    }

    public void setIpChange(Boolean ipChange) {
        this.ipChange = ipChange;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getIsp() {
        return isp;
    }

    public void setIsp(String isp) {
        this.isp = isp;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
    }

    public Long getObjId() {
        return objId;
    }

    public void setObjId(Long objId) {
        this.objId = objId;
    }

    public Long getExamStudentId() {
        return examStudentId;
    }

    public void setExamStudentId(Long examStudentId) {
        this.examStudentId = examStudentId;
    }

    public Long getExamRecordId() {
        return examRecordId;
    }

    public void setExamRecordId(Long examRecordId) {
        this.examRecordId = examRecordId;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Long getStudentId() {
        return studentId;
    }

    public void setStudentId(Long studentId) {
        this.studentId = studentId;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    @Override
    public int compareTo(@NotNull TEExamStudentLog o) {
        if (o.getCreateTime() < this.getCreateTime()) {
            return 1;
        } else if (o.getCreateTime() > this.getCreateTime()) {
            return -1;
        } else {
            return 0;
        }
    }
}
