package yem.base.common.module.iscb.helper;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import yem.base.common.utils.YEM;

import java.util.Date;

/**
 * 接口日志实体
 *
 * @author longh
 * @description
 * @date 2023年9月7日
 */
public class ApiLog {
    private static final String KEY_APILOG = "yem_base_apilog";
    private static final String SUCCESS = "success";
    private static final String ERROR = "fail";
    private Api logApi;
    private boolean success_s;
    private String message;
    private String errorCode;
    private String errorMsg;
    private String requestData;
    private String responseData;

    public ApiLog(Api api) {
        if (api == null) {
            api = new Api(null);
        }
        this.logApi = api;
    }

    /**
     * 日志写入
     */
    public void write() {
        DynamicObject logBill = BusinessDataServiceHelper.newDynamicObject(KEY_APILOG);
        logBill.set("yem_user", YEM.getCurrentUserId());
        logBill.set("yem_opdate", new Date());
        logBill.set("yem_iscb_api", logApi.getDyObj());
        logBill.set("yem_state", errorCode);
        logBill.set("yem_message", YEM.isEmpty(this.message) ? this.errorCode : this.message);
        logBill.set("yem_message_tag", errorMsg);
        logBill.set("yem_source_data_tag", requestData);
        logBill.set("yem_target_data_tag", responseData);
        SaveServiceHelper.save(new DynamicObject[]{logBill});
    }

    /**
     * 是否成功
     *
     * @return
     */
    public boolean isSuccess() {
        return this.success_s;
    }

    /**
     * 是否成功
     *
     * @param success
     */
    public void setSuccess(boolean success) {
        if (YEM.isEmpty(this.errorCode)) {
            if (success) {
                this.errorCode = SUCCESS;
            } else {
                this.errorCode = ERROR;
            }
        }
        this.success_s = success;
    }

    /**
     * 信息
     *
     * @return
     */
    public String getMessage() {
        return message;
    }

    /**
     * 信息
     *
     * @param message
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 异常信息
     *
     * @return
     */
    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * 异常信息
     *
     * @param errorMsg
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    /**
     * 执行状态
     *
     * @return
     */
    public String getErrorCode() {
        return errorCode;
    }

    /**
     * 执行状态
     *
     * @param errorCode
     */
    /*
     * public void setErrorCode(String errorCode) { this.errorCode = errorCode; }
     */

    /**
     * 请求体JSONString
     *
     * @return
     */
    public String getRequestData() {
        return requestData;
    }

    /**
     * 请求体JSONString
     *
     * @param requestData
     */
    public void setRequestData(String requestData) {
        this.requestData = requestData;
    }

    /**
     * 响应体JSONString
     *
     * @return
     */
    public String getResponseData() {
        return responseData;
    }

    /**
     * 响应体JSONString
     *
     * @param responseData
     */
    public void setResponseData(String responseData) {
        this.responseData = responseData;
    }

}
