/**
 * Copyright 2013-present febit.org (support@febit.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.febit.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author zqq90
 */
public class ServiceResult<T> {

    public static final int SUCCESS = 0;
    public static final int ERROR_SYS = 100;
    public static final int REDIRECT = 3020;

    public static final int ERROR = 5000;
    //参数错误:
    public static final int ERROR_PARAM = 5100;
    //参数错误:不能为空
    public static final int ERROR_PARAM_REQUIRED = 5101;
    //参数错误:格式错误
    public static final int ERROR_PARAM_FORMAT = 5102;
    //文件上传错误
    public static final int ERROR_UPLOAD = 5200;
    //文件上传错误:文件太大
    public static final int ERROR_UPLOAD_TOOBIG = 5201;
    //文件上传错误:文件无法写入
    public static final int ERROR_UPLOAD_CANTWRITE = 5202;
    //文件上传错误:文件类型错误
    public static final int ERROR_UPLOAD_TYPE = 5203;
    //权限错误
    public static final int ERROR_RIGHT = 5400;
    //XSRF
    public static final int ERROR_XSRF = 5401;
    //验证码错误
    public static final int ERROR_VERCODE = 5402;

    public static final int ERROR_ADD = 6100;
    public static final int ERROR_DEL = 6200;
    public static final int ERROR_DEL_NOTFOUND = 6201;
    public static final int ERROR_DEL_UNABLE = 6202;
    public static final int ERROR_MODIFY = 6300;
    public static final int ERROR_MODIFY_NOTFOUND = 6301;
    public static final int ERROR_MODIFY_UNABLE = 6302;
    public static final int ERROR_QUERY = 6400;
    public static final int ERROR_QUERY_NOTFOUND = 6404;

    public static final ServiceResult SUCCESS_RESULT = new ServiceResult(SUCCESS);

    public final int code;
    public final String msg;
    public final Object[] args;
    public Map<Object, Object> map;
    public final T value;

    protected ServiceResult(T value) {
        this.code = SUCCESS;
        this.msg = null;
        this.args = null;
        this.value = value;
    }

    protected ServiceResult(int code, String message, Object[] arguments) {
        this.code = code;
        this.msg = message;
        this.args = arguments;
        this.value = null;
    }

    protected ServiceResult(int code) {
        this.code = code;
        this.msg = null;
        this.args = null;
        this.value = null;
    }

    public final boolean success() {
        return code == SUCCESS;
    }

    public final boolean failed() {
        return code != SUCCESS;
    }

    public ServiceResult put(Object key, Object value) {
        Map myMap = this.map;
        if (myMap == null) {
            this.map = myMap = new HashMap<>();
        }
        myMap.put(key, value);
        return this;
    }

    public Object get(Object key) {
        return map != null ? map.get(key) : null;
    }

    public Map<Object, Object> getMap() {
        return map;
    }

    @Override
    public String toString() {
        return "ServiceResult{" + "code=" + code + ", message=" + msg + ", arguments=" + Arrays.toString(args) + '}';
    }

    public static <T> ServiceResult<T> success(T val) {
        return new ServiceResult(val);
    }

    public static ServiceResult successResult() {
        return new ServiceResult(SUCCESS);
    }

    public static ServiceResult error(int code) {
        return new ServiceResult(code);
    }

    public static ServiceResult error(String msg) {
        return new ServiceResult(ERROR, msg, null);
    }

    public static ServiceResult error(String msg, Object... args) {
        return new ServiceResult(ERROR, msg, args);
    }

    public static ServiceResult error(int code, String msg, Object... args) {
        return new ServiceResult(code, msg, args);
    }
}

@@ -643,16 +643,45 @@
 rvice;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -770,16 +770,30 @@
 r zqq90%0A
+ * @param %3CT%3E%0A
  */%0Apubl
@@ -818,17 +818,95 @@
 sult%3CT%3E 
-%7B
+implements Serializable %7B%0A%0A    private static final long serialVersionUID = 1L;
 %0A%0A    pu
@@ -2372,16 +2372,43 @@
 c final 
+T value;%0A    private final 
 Object%5B%5D
@@ -2415,29 +2415,30 @@
  args;%0A    p
-ublic
+rivate
  Map%3CObject,
@@ -2450,37 +2450,13 @@
 ct%3E 
-map;%0A    public final T value
+datas
 ;%0A%0A 
@@ -2664,26 +2664,27 @@
 sage, Object
-%5B%5D
+...
  arguments) 
@@ -2958,38 +2958,32 @@
   %7D%0A%0A    public 
-final 
 boolean success(
@@ -3036,22 +3036,16 @@
  public 
-final 
 boolean 
@@ -3163,47 +3163,22 @@
     
-Map myMap = this.map;%0A        if (myMap
+if (this.datas
  == 
@@ -3206,19 +3206,13 @@
 his.
-map = myMap
+datas
  = n
@@ -3249,13 +3249,18 @@
     
-myMap
+this.datas
 .put
@@ -3352,19 +3352,21 @@
  return 
-map
+datas
  != null
@@ -3368,19 +3368,21 @@
  null ? 
-map
+datas
 .get(key
@@ -3432,19 +3432,21 @@
 ect%3E get
-Map
+Datas
 () %7B%0A   
@@ -3461,11 +3461,73 @@
 urn 
-map
+datas;%0A    %7D%0A%0A    public Object%5B%5D getArgs() %7B%0A        return args
 ;%0A  
@@ -4099,14 +4099,8 @@
  msg
-, null
 );%0A 

make ServiceResult Serializable