package com.uas.platform.b2b.model;

import com.uas.platform.b2b.support.SystemSession;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 记录平台接口报错异常
 *
 * Created by hejq on 2018-06-20.
 */
@Table(name = "log$error")
@Entity
public class ErrorLog implements Serializable {

    /**
     * default serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "log_id")
    private Long id;

    /**
     * 请求地址
     */
    @Column(name = "log_url")
    private String url = "";

    /**
     * 错误消息
     */
    @Length(max = 2000)
    @Column(name = "log_msg")
    private String msg = "";

    /**
     * 错误详情
     */
    @Lob
    @Column(name = "log_detail", columnDefinition = "text")
    private String detail = "";

    /**
     * 记录时间
     */
    @Column(name = "log_date")
    private Date date = new Date(System.currentTimeMillis());

    /**
     * 企业UU
     */
    @Column(name = "log_enuu")
    private Long enUU = 0L;

    /**
     * 企业名称
     */
    @Column(name = "log_enname")
    private String enName = "";

    /**
     * 标识
     */
    @Column(name = "log_uuid")
    private String uuid = "";

    public Long getId() {
        return id;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Long getEnUU() {
        return enUU;
    }

    public void setEnUU(Long enUU) {
        this.enUU = enUU;
    }

    public String getEnName() {
        return enName;
    }

    public void setEnName(String enName) {
        this.enName = enName;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public ErrorLog() {}

    /**
     * 新增错误日志
     *
     * @param detail 错误详情
     * @param msg 错误消息
     * @param url 访问路径
     */
    public ErrorLog(String detail, String msg, String url, String uuid) {
        User user = SystemSession.getUser();
        if (null != user) {
            this.enUU = user.getEnterprise().getUu();
            this.enName = user.getEnterprise().getEnName();
        }
        this.detail = detail;
        this.msg = msg;
        this.url = url;
        this.uuid = uuid;
    }
}
