package cn.wizzer.framework.base;

import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;

/**
 * Created by wizzer on 2016/12/21.
 */
public class Result {

    private int code;
    private String msg;
    private Object data;

    public Result() {

    }

    public static Result NEW() {
        return new Result();
    }


    public Result addCode(int code) {
        this.code = code;
        return this;
    }

    public Result addMsg(String msg) {
        this.msg = Strings.isBlank(msg) ? "" : Mvcs.getActionContext().getRequest() == null ? msg : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        return this;
    }

    public Result addData(Object data) {
        this.data = data;
        return this;
    }

    public Result(int code, String msg, Object data) {
        this.code = code;
        this.msg = Strings.isBlank(msg) ? "" : Mvcs.getActionContext().getRequest() == null ? msg : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        this.data = data;
    }

    public static Result success(String content) {
        return new Result(0, content, null);
    }

    public static Result success(String content, Object data) {
        return new Result(0, content, data);
    }

    public static Result error(int code, String content) {
        return new Result(code, content, null);
    }

    public static Result error(String content) {
        return new Result(1, content, null);
    }

    public static Result success() {
        return new Result(0, "globals.result.success", null);
    }

    public static Result error() {
        return new Result(1, "globals.result.error", null);
    }

    @Override
    public String toString() {
        return Json.toJson(this, JsonFormat.compact());
    }

}

@@ -883,35 +883,28 @@
 de;%0A        
-this.msg = 
+if (
 Strings.isBl
@@ -904,38 +904,69 @@
 gs.isBlank(msg) 
-? %22%22 :
+%7C%7C Mvcs.getActionContext() == null %7C%7C
  Mvcs.getActionC
@@ -987,39 +987,171 @@
 quest() == null 
-? msg :
+%7C%7C Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg) == null) %7B%0A            this.msg = %22%22;%0A        %7D else %7B%0A            this.msg =
  Mvcs.getMessage
@@ -1187,32 +1187,42 @@
 equest(), msg);%0A
+        %7D%0A
         this.dat
@@ -1874,24 +1874,202 @@
 null);%0A    %7D
+%0A    %0A    %0A    public int getCode() %7B%0A        return code;%0A    %7D%0A%0A    public String getMsg() %7B%0A        return msg;%0A    %7D%0A%0A    public Object getData() %7B%0A        return data;%0A    %7D
 %0A%0A    @Overr

fix Result方法在测试报NPE的问题并增加获取Result对象数据的公开发方法