package com.codvision.commoncore.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;


/**
 * @author codvision
 */
@ApiModel(description = "返回响应数据")
public class ResponseEntity<T> {

    @ApiModelProperty(value = "编码")
    private int code = 200;

    @ApiModelProperty(value = "基本信息")
    private String message = "成功";

    @ApiModelProperty(value = "返回对象")
    private T data;


    private static final HashMap<Integer, String> ERROR_CODE = new HashMap<Integer, String>() {
        {
            put(100, "暂无数据");
            put(200, "成功");
            put(10000, "通用错误");
            ///用户类
            put(10001, "用户名或密码错误");
            put(10002, "登录状态已过期");
            put(10003, "注册用户已存在");
            put(10004, "账号已被锁定,请在一小时后重试");

            ///操作权限类
            put(20001, "无操作权限");
            ///参数类
            put(30001, "非法参数");
            put(30002, "缺少必要参数");
            put(30003, "文件内容为空");
            put(30004, "不支持的文件格式");
            put(30005, "获取参数失败");
            ////数据操作类
            put(40001, "添加数据失败");
            put(40002, "更新数据失败");
            put(40003, "删除数据失败");
            put(50001, "不存在的对象");
            put(50002, "文件上传超过最大限制！");
            put(50003, "文件上传失败！");

            put(110000, "获取用户失败或用户不存在！");
            put(100001, "钉钉获取用户信息失败，请稍后再试");
            put(110002, "小程序授权获取手机号失败");

            put(990000, "系统错误");
        }
    };


    public ResponseEntity() {
    }

    public ResponseEntity(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public ResponseEntity setCode(int code) {
        this.code = code;
        if (ERROR_CODE.containsKey(code)) {
            setMessage(ERROR_CODE.get(code));
        }
        return this;
    }

    public String getMessage() {
        return message;
    }

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

    public T getData() {
        return data;
    }

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

    public static <T> ResponseEntity<T> def(Class<T> clazz) {
        return new ResponseEntity<>();
    }

    public ResponseEntity<T> ok() {
        setCode(200);
        return this;
    }

    public ResponseEntity<T> error(int code) {
        setCode(code);
        return this;
    }

    public ResponseEntity<T> error(int code, String message) {
        setCode(code);
        setMessage(message);
        return this;
    }

    public ResponseEntity<T> message(String message) {
        setMessage(message);
        return this;
    }

    public ResponseEntity<T> data(T data) {
        setData(data);
        return this;
    }

    public ResponseEntity<T> back(int code, String message, T data) {
        setCode(code);
        setMessage(message);
        setData(data);
        return this;
    }
}
