package com.uas.finance.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.uas.finance.util.SystemSession;
import com.uas.platform.core.logging.BufferedLogable;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * 金融项目使用日志
 *
 * @author yingp
 */
@Entity
@Table(name = "log$usage", indexes = {@Index(name = "log$usage_enuu_idx", columnList = "log_enuu")})
public class UsageLog extends BufferedLogable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "log$erp_gen", sequenceName = "log$erp_seq", allocationSize = 1)
    @Column(name = "log_id")
    private Long id;

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

    @NotNull
    @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;

    /**
     * 相关单据号
     */
    @Column(name = "rel_code")
    private String code;

    /**
     * 相关单据主键
     */
    @Column(name = "rel_key")
    private Long key;

    @JsonIgnore
    @JSONField(serialize = false)
    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 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;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }

    public UsageLog() {

    }

    public UsageLog(String title, String message) {
        this.enUU = SystemSession.getUserAccount().getSpaceUU();
        this.title = title;
        this.message = message;
        this.time = new Date().getTime();
        this.userUU = SystemSession.getUserAccount().getUserUU();
    }

    public UsageLog(String title, String message, Long userUU, String ip,
                    String code, Long key) {
        this.enUU = 0L;
        this.title = title;
        this.message = message;
        this.code = code;
        this.key = key;
        this.time = new Date().getTime();
        this.userUU = userUU;
        this.ip = ip;
    }

    public void bufferedLog(String bufferedMessage) {
        String[] strArray = bufferedMessage.split(separator);
        if (strArray.length == 9) {
            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;
            this.code = StringUtils.hasText(strArray[7]) ? strArray[7] : null;
            this.key = StringUtils.hasText(strArray[8]) ? Long
                    .parseLong(strArray[8]) : null;
        }
    }

    @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);
        sb.append(this.code == null ? "" : this.code).append(separator);
        sb.append(this.key == null ? " " : this.key);
        return sb.toString();
    }

}
