package com.uas.platform.b2b.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.uas.platform.b2b.core.util.DateFormatUtils;
import com.uas.platform.core.logging.BufferedLogable;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.util.StringUtils;

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

/**
 * 系统一些操作的记录，不关联企业，方便针对异常进行查看
 *
 * Created by hejq on 2017-12-12.
 */
@Entity
@Table(name = "log$common")
public class CommunalLog extends BufferedLogable implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

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

    @Column(name = "log_enuu")
    private Long enUU;

    @OneToOne(cascade = { CascadeType.REFRESH })
    @JoinColumn(name = "log_useruu", insertable = false, updatable = false)
    private UserBaseInfo user;

    @Column(name = "log_useruu")
    private Long userUU;

    /**
     * 日志时间
     */
    @Column(name = "log_time")
    private Long time;

    /**
     * ip
     */
    @Column(name = "log_ip")
    private String ip;

    @Column(name = "log_title")
    private String title;

    @Column(name = "log_message")
    private String message;

    @Column(name = "log_detail", length = 1000)
    private String detail;

    public Long getId() {
        return id;
    }

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

    public Long getEnUU() {
        return enUU;
    }

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

    public UserBaseInfo getUser() {
        return user;
    }

    public void setUser(UserBaseInfo user) {
        this.user = user;
    }

    public Long getUserUU() {
        return userUU;
    }

    public void setUserUU(Long userUU) {
        this.userUU = userUU;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public String getIp() {
        return ip;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDetail() {
        return detail;
    }

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

    @JsonIgnore
    @JSONField(serialize = false)
    public String getTimeString() {
        return DateFormatUtils.DATETIME_FORMAT.format(getTime());
    }

    public CommunalLog() {

    }

    public CommunalLog(String title, String message, String detail, Long enUU) {
        this.enUU = enUU;
        this.title = title;
        this.message = message;
        this.time = System.currentTimeMillis();
        this.detail = detail;
    }

    public CommunalLog(String title, String message, String detail, Long userUU, Long enUU) {
        this.enUU = enUU;
        this.title = title;
        this.message = message;
        this.time = System.currentTimeMillis();
        this.detail = detail;
        this.userUU = userUU;
    }

    @Override
    public String bufferedMessage() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.time).append(separator);
        sb.append(this.ip).append(separator);
        sb.append(this.enUU).append(separator);
        sb.append(this.userUU).append(separator);
        sb.append(this.title).append(separator);
        sb.append(this.message).append(separator);
        sb.append(this.detail == null ? "" : this.detail).append(separator);
        return sb.toString();
    }

    @Override
    public void bufferedLog(String bufferedMessage) {
        Integer arraySize = 9;
        String[] strArray = bufferedMessage.split(separator);
        if (strArray.length == arraySize) {
            this.time = Long.parseLong(strArray[0]);
            this.ip = strArray[1];
            this.enUU = Long.parseLong(strArray[2]);
            this.userUU = Long.parseLong(strArray[3]);
            this.title = strArray[4];
            this.message = strArray[5];
            this.detail = StringUtils.hasText(strArray[6]) ? strArray[6] : null;
        }
    }

}
