package com.tiesheng.util.pojos;

import com.tiesheng.util.exception.ApiRespEnum;

/**
 * @author huang
 * @ProjectName health
 * @Copyright Hangzhou ShuoChuang Technology Co.,Ltd All Right Reserved
 * @Description 这里是对文件的描述
 * @data 2017/9/4
 * @note 这里写文件的详细功能和改动
 * @note
 */
public class ApiResp<T> {

    public static int CODE_OK = 200;

    private int code;
    private String message;
    private T data;
    private long recordsTotal = 0;
    private boolean encrypt = false;

    /**
     * 请求成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respOK(T data) {
        ApiRespEnum okResp = ApiRespEnum.OK;
        ApiResp<T> result = new ApiResp<>();
        result.code = okResp.getCode();
        result.message = okResp.getMessage();
        result.data = data;
        return result;
    }


    /**
     * 请求成功，返回总条数
     *
     * @param data
     * @param recordsTotal
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respOK(T data, long recordsTotal) {
        ApiRespEnum okResp = ApiRespEnum.OK;
        ApiResp<T> result = new ApiResp<>();
        result.code = okResp.getCode();
        result.message = okResp.getMessage();
        result.data = data;
        result.recordsTotal = recordsTotal;
        return result;
    }


    /**
     * 自定义的错误
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respCust(int code, String msg) {
        ApiResp<T> result = new ApiResp<>();
        result.code = code;
        result.message = msg;
        return result;
    }

    /**
     * 自定义的错误
     *
     * @param respEnum
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respCust(ApiRespEnum respEnum) {
        ApiResp<T> result = new ApiResp<>();
        result.code = respEnum.getCode();
        result.message = respEnum.getMessage();
        return result;
    }

    /**
     * 用户未登录
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respNeedLogin(String msg) {
        ApiResp<T> result = new ApiResp<>();
        result.code = 110;
        result.message = msg;
        return result;
    }

    /**
     * 操作失败
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respDoFail() {
        ApiResp<T> result = new ApiResp<>();
        result.code = 121;
        result.message = "操作失败";
        return result;
    }


    /**
     * 根据bool返回数据
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResp<T> respBool(T data, boolean isOk) {
        ApiResp<T> result = respOK(data);
        if (!isOk) {
            result = respDoFail();
        }
        return result;
    }


    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////

    public int getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

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

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public boolean isEncrypt() {
        return encrypt;
    }

    public void setEncrypt(boolean encrypt) {
        this.encrypt = encrypt;
    }
}
