package org.jeecg.module.custom.ocr.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.module.custom.ocr.common.enums.ReturnCode;
import org.jeecg.module.custom.ocr.utils.ApiHelper;

import java.io.Serializable;
import java.util.Map;

/**
 * @ClassName ResultData
 * @Description TODO
 * @Author wazngzezhen
 * @Date 6:20 AM 3/6/22
 * @Version 1.0
 **/
@Data
@ApiModel(value = "统一返回结果封装",description = "接口返回统一结果")
public class ResultData<T> implements Serializable {
    /**
     * 结果状态
     */
    @ApiModelProperty(value = "状态码")
    private int status;

    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息")
    private String message;

    /**
     * 后端返回结果，加密后为Base64字符串：{@link String}
     * @see ApiHelper#encrypt(String, String, String)
     */
    @ApiModelProperty(value = "后端返回结果")
    private T data;

    /**
     * 后端响应状态
     */
    @ApiModelProperty(value = "后端响应状态")
    private boolean success;
    /**
     * 响应时间戳，作为签名、加密的随机盐（Salt）
     * @see ApiHelper#combineSalt(String, String)
     */
    @ApiModelProperty(value = "响应时间戳")
    private long timestamp ;
    /**
     * 数据签名
     * @see ApiHelper#getSignature(String, String, Map)
     */
    @ApiModelProperty(value = "数据签名")
    private String signature;

    /**
     * （预留）http状态
     */
    private int httpStatus;

    public ResultData(){
        this.timestamp = System.currentTimeMillis();
    }


    public static <T> ResultData<T> success(String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC100.getCode());
        resultData.setSuccess(true);
        resultData.setMessage(message);
        return resultData;
    }

    public static <T> ResultData<T> success() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC100.getCode());
        resultData.setSuccess(true);
        resultData.setMessage(ReturnCode.RC100.getMessage());
        return resultData;
    }

    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC100.getCode());
        resultData.setSuccess(true);
        resultData.setMessage(ReturnCode.RC100.getMessage());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> success(String message, T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC100.getCode());
        resultData.setSuccess(true);
        resultData.setMessage(message);
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> fail(String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC999.getCode());
        resultData.setSuccess(false);
        resultData.setMessage(message);
        return resultData;
    }

    public static <T> ResultData<T> fail(int code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setSuccess(false);
        resultData.setMessage(message);
        return resultData;
    }

    public static <T>ResultData<T> fail() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC999.getCode());
        resultData.setSuccess(false);
        resultData.setMessage(ReturnCode.RC999.getMessage());
        return resultData;
    }


    @JsonIgnore
    public int getHttpStatus() {
        return httpStatus;
    }
}
