package com.crunii.micro.domain.entitysql.main;

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.domain.common.jdbc.dto.AbsSetProps;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

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

@Entity
@Table(name = "t_busi_log")
@DynamicInsert
@DynamicUpdate
@Getter
@ToString
@EqualsAndHashCode(of = { "id" })
@ApiModel(description = "业务日志表")
public class TBusiLogS extends AbsSetProps {

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@ApiModelProperty(value = "主键")
	private long id;

	@Column(name = "call_result")
	@ApiModelProperty(value = "SUCCESS|FAILURE 调用结果成功或者失败")
	private String callResult;

	@Column(name = "error_msg")
	@ApiModelProperty(value = "错误描述")
	private String errorMsg;

	@Column(name = "method_info")
	@ApiModelProperty(value = "方法信息，自动定义扩展信息")
	private String methodInfo;

	@Column(name = "login_id")
	@ApiModelProperty(value = "登录id")
	private int loginId;

	@Column(name = "login_code")
	@ApiModelProperty(value = "登录工号")
	private String loginCode;

	@Column(name = "worker_name")
	@ApiModelProperty(value = "员工姓名")
	private String workerName;

	@Column(name = "app_name")
	@ApiModelProperty(value = "spring.application.name,调用的应用名称")
	private String appName;

	@Column(name = "log_type")
	@ApiModelProperty(value = "CLIENT|SERVER 我们作为客户端或者服务端")
	private String logType;

	@Column(name = "trade_no")
	@ApiModelProperty(value = "有可能一个业务方法多次调用接口，多次调用的时候每次调用日志的交易编号一样,可以通过NanoIdUtil.randomLowerNanoId()生成")
	private String tradeNo;

	@Column(name = "busi_type")
	@ApiModelProperty(value = "业务类型，如果xx接口，xx平台")
	private String busiType;

	@Column(name = "busi_no")
	@ApiModelProperty(value = "业务编号如定单号")
	private String busiNo;

	@Column(name = "client_addr")
	@ApiModelProperty(value = "客服端地址")
	private String clientAddr;

	@Column(name = "server_addr")
	@ApiModelProperty(value = "服务端地址")
	private String serverAddr;

	@Column(name = "send_data")
	@ApiModelProperty(value = "发送的报文json字符串或者xml等,java对象可以转json后存储，总之通过文本存储")
	private String sendData;

	@Column(name = "recv_data")
	@ApiModelProperty(value = "接收的报文json字符串或者xml等,java对象可以转json后存储，总之通过文本存储")
	private String recvData;

	@Column(name = "use_time")
	@ApiModelProperty(value = "调用耗时，单位毫秒")
	private long useTime;

	@Column(name = "call_time")
	@ApiModelProperty(value = "send或者是recv的开始时间")
	private Date callTime;

	@Column(name = "create_time")
	@ApiModelProperty(value = "日志记录创建时间,后台写数据库的时候才填写")
	private Date createTime;

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

	public void setCallResult(String callResult) {
		this.callResult = callResult;
		fireSet("callResult", callResult);
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
		fireSet("errorMsg", errorMsg);
	}

	public void setMethodInfo(String methodInfo) {
		this.methodInfo = methodInfo;
		fireSet("methodInfo", methodInfo);
	}

	public void setLoginId(int loginId) {
		this.loginId = loginId;
		fireSet("loginId", loginId);
	}

	public void setLoginCode(String loginCode) {
		this.loginCode = loginCode;
		fireSet("loginCode", loginCode);
	}

	public void setWorkerName(String workerName) {
		this.workerName = workerName;
		fireSet("workerName", workerName);
	}

	public void setAppName(String appName) {
		this.appName = appName;
		fireSet("appName", appName);
	}

	public void setLogType(String logType) {
		this.logType = logType;
		fireSet("logType", logType);
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
		fireSet("tradeNo", tradeNo);
	}

	public void setBusiType(String busiType) {
		this.busiType = busiType;
		fireSet("busiType", busiType);
	}

	public void setBusiNo(String busiNo) {
		this.busiNo = busiNo;
		fireSet("busiNo", busiNo);
	}

	public void setClientAddr(String clientAddr) {
		this.clientAddr = clientAddr;
		fireSet("clientAddr", clientAddr);
	}

	public void setServerAddr(String serverAddr) {
		this.serverAddr = serverAddr;
		fireSet("serverAddr", serverAddr);
	}

	public void setSendData(String sendData) {
		this.sendData = sendData;
		fireSet("sendData", sendData);
	}

	public void setRecvData(String recvData) {
		this.recvData = recvData;
		fireSet("recvData", recvData);
	}

	public void setUseTime(long useTime) {
		this.useTime = useTime;
		fireSet("useTime", useTime);
	}

	public void setCallTime(Date callTime) {
		this.callTime = callTime;
		fireSet("callTime", callTime);
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
		fireSet("createTime", createTime);
	}

	public static TBusiLogS getBusiClient(String busiType, String serverAddr, String sendData) {
		TBusiLogS busiLog = new TBusiLogS();
		busiLog.setBusiType(busiType);
		busiLog.setServerAddr(serverAddr);
		busiLog.setSendData(sendData);
		busiLog.setLogType(CommonConstants.TYPE_CLIENT);
		busiLog.setCallTime(new Date());
		return busiLog;
	}

	public static TBusiLogS getBusiServer(String busiType, String recvData) {
		TBusiLogS busiLog = new TBusiLogS();
		busiLog.setBusiType(busiType);
		busiLog.setRecvData(recvData);
		busiLog.setLogType(CommonConstants.TYPE_SERVER);
		busiLog.setCallTime(new Date());
		return busiLog;
	}

	public static TBusiLogS getBusiServer(String busiType, String clientAddr, String recvData) {
		TBusiLogS busiLog = new TBusiLogS();
		busiLog.setBusiType(busiType);
		busiLog.setClientAddr(clientAddr);
		busiLog.setRecvData(recvData);
		busiLog.setLogType(CommonConstants.TYPE_SERVER);
		busiLog.setCallTime(new Date());
		return busiLog;
	}

	/**
	 * <pre>有些情况不需要打印数据（send_data、recv_data）日志太多了，</pre>
	 *
	 * @return
	 */
	public String getNoDataStr() {
		return "TBusiLogS{" +
				"id=" + id +
				", callResult='" + callResult + '\'' +
				", errorMsg='" + errorMsg + '\'' +
				", methodInfo='" + methodInfo + '\'' +
				", loginId=" + loginId +
				", loginCode='" + loginCode + '\'' +
				", workerName='" + workerName + '\'' +
				", appName='" + appName + '\'' +
				", logType='" + logType + '\'' +
				", tradeNo='" + tradeNo + '\'' +
				", busiType='" + busiType + '\'' +
				", busiNo='" + busiNo + '\'' +
				", clientAddr='" + clientAddr + '\'' +
				", serverAddr='" + serverAddr + '\'' +
				", useTime=" + useTime +
				", callTime=" + callTime +
				", createTime=" + createTime +
				'}';
	}

}
