package cn.estsh.i3plus.pojo.base.bean;

import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;
import java.util.Map;

/**
 * @Description : 基本返回结果
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-11-14 15:58
 * @Modify:
 **/
public class BaseResultBean<Obj> {

    @ApiModelProperty("是否成功")
    public boolean success;

    @ApiModelProperty("信息代码-国际化")
    public String code;

    @ApiModelProperty("操作消息")
    public String msg;

    @ApiModelProperty("错误消息")
    public String errorMsg;

    @ApiModelProperty("http路径")
    public String url;

    @ApiModelProperty("List结果集")
    public List<Obj> resultList;

    @ApiModelProperty("单个结果")
    public Obj resultObject;

    @ApiModelProperty("翻页和结果集")
    public ListPager<Obj> listPager;

    @ApiModelProperty("Map结果集")
    public Map<String, Object> resultMap;

    @ApiModelProperty("翻页操作")
    public Pager pager;

    @ApiModelProperty("总记录数")
    public int totalCount;

    public boolean isSuccess() {
        return success;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public String getUrl() {
        return url;
    }

    public List<Obj> getResultList() {
        return resultList;
    }

    public Obj getResultObject() {
        return resultObject;
    }

    public ListPager<Obj> getListPager() {
        return listPager;
    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    public Pager getPager() {
        return pager;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public BaseResultBean setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public BaseResultBean setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }

    public BaseResultBean setResultList(List<Obj> resultList) {
        this.resultList = resultList;
        return this;
    }

    public BaseResultBean setResultObject(Obj resultObject) {
        this.resultObject = resultObject;
        return this;
    }

    public BaseResultBean setListPager(ListPager<Obj> listPager) {
        this.listPager = listPager;
        return this;
    }

    public BaseResultBean setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
        return this;
    }

    public static BaseResultBean success(String msg){
        BaseResultBean rs =  new BaseResultBean(true);
        rs.setMsg(msg);
        return rs;
    }

    public static BaseResultBean success(){
        BaseResultBean rs =  new BaseResultBean(true);
        return rs;
    }

    public static BaseResultBean fail(String msg){
        BaseResultBean rs =  new BaseResultBean(false,msg);
        rs.setErrorMsg(msg);
        return rs;
    }

    public static BaseResultBean buildBaseResultBean(boolean success, String msg){
        BaseResultBean rs = new BaseResultBean();
        rs.success = success;
        if (success) {
            rs.msg = msg;
            rs.code = ResourceEnumUtil.MESSAGE.SUCCESS.getCode();
        } else {
            rs.code = ResourceEnumUtil.MESSAGE.FAIL.getCode();
            rs.errorMsg = msg;
        }

        return rs;
    }

    public BaseResultBean() {
    }

    public BaseResultBean(boolean isSuccess) {
        success = isSuccess;
        if(success){
            //国际化
            code = ResourceEnumUtil.MESSAGE.SUCCESS.getCode();
        }else{
            code = ResourceEnumUtil.MESSAGE.FAIL.getCode();
        }
    }

    public BaseResultBean(boolean success, List<Obj> resultList) {
        this.success = success;
        this.resultList = resultList;
    }

    public BaseResultBean(boolean success, Obj resultObject) {
        this.success = success;
        if(resultObject instanceof String){
            this.msg = resultObject.toString();
        }else{
            this.resultObject = resultObject;
        }
    }

    @Override
    public String toString() {
        return "BaseResultBean{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", errorMsg='" + errorMsg + '\'' +
                ", url='" + url + '\'' +
                ", resultList=" + resultList +
                ", resultObject=" + resultObject +
                ", listPager=" + listPager +
                ", resultMap=" + resultMap +
                ", pager=" + pager +
                ", totalCount=" + totalCount +
                '}';
    }
}
