All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jladder.data.RequestResult Maven / Gradle / Ivy

There is a newer version: 5.0.44
Show newest version
package com.jladder.data;

import com.jladder.Ladder;
import com.jladder.lang.Json;
import com.jladder.lang.Regex;
import com.jladder.lang.Times;

import java.util.*;

/***
 * 请求返回结果类
 */
public class RequestResult{

        /***
         * 初始化开始时间
         */
        private transient long _dtstart = 0;
        /***
         * 是否处理成功
         */
        public boolean Success;


        /***
         * 状态码
         */
        public int Code;

        /***
         * 消息正文
         */
        public String Message;
        /// 
        /// 资源索引
        /// 
        public String Rel;
        /// 
        /// 返回结果
        /// 
        public Object Result;

        /// 
        /// 返回时间
        /// 
        public long ReTime = Times.getTS();
        /// 
        /// 处理时长
        /// 
        public String duration  = "0ms";

        /// 
        /// 节点标识
        /// 

        public String watchPoint = Ladder.Settings().getSite();
        /// 
        /// 未定义
        /// 
        public String datatype = "undefined";

        private Map config = null;

        /// 
        /// 基本构造
        /// 
        public RequestResult()
        {
            _dtstart = Times.getTS();
            Code = 200;
            Message = AjaxResultCode.getMessage(200);
        }
        /// 
        /// 基本构造
        /// 
        /// 返回情况
        public RequestResult(Object result){
            _dtstart =Times.getTS();;
            if (result == null){
                this.Code = 400;
                Message = AjaxResultCode.Undfind.name();
                return;
            }
            else if (result instanceof Integer){
                this.Code = (int)result;
                if (Code != 200) Message = AjaxResultCode.getMessage(Code);
                else this.Message = "";
                return;
            }
            else if (result instanceof String){
                if (Regex.isMatch(result.toString(), "^\\d+$")){
                    this.Code = Integer.valueOf(result.toString());
                    return;
                }
            }
             else if (result instanceof AjaxResult){
                 AjaxResult ret = (AjaxResult)result;
                 Code = ret.getStatusCode();
                 Message = ret.getMessage();
                 Result = ret.getData();
                 Rel = ret.getRel();
                 duration = ret.getDuration();
                 datatype = ret.getDatatype();
                 return;
             }
            else if (result instanceof Boolean){
                boolean re = (Boolean)result;
                if (re){
                    Code = 200;
                }
                else{
                    Code = 500;
                    Message = AjaxResultCode.getMessage(500);
                }
                return;
            }
            Code = 200;
            Message = "操作成功";
            Result = result;
        }

        /// 
        /// 基本构造
        /// 
        /// 状态码
        /// 消息文本
        public RequestResult(int code, String message)
        {
            _dtstart = Times.getTS();
            Message = message;
            Code = code;
        }

        /// 
        /// 设置
        /// 
        /// 状态码
        /// 消息文本
        /// 
        public RequestResult set(int code, String msg)
        {
            _dtstart = Times.getTS();
            Code = code;
            Message = msg;
            return this;
        }
    /***
     * 设置消息文本
     * @param message 消息文本
     * @return
     */
        public RequestResult setMessage(String message)
        {
            _dtstart = Times.getTS();
            Message = message;
            return this;
        }

        /// 
        /// 设置处理时长
        /// 
        /// 开始时间
        /// 
        public RequestResult SetDuration(Date startTime)
        {
            long time = new Date().getTime() - startTime.getTime();
            duration = time+"ms";
            return this;
        }
        /// 
        /// 设置处理时长
        /// 
        /// 开始时间
        /// 
        public RequestResult setDuration(String duration)
        {
            _dtstart = Times.getTS();
            duration = duration;
            return this;
        }
        /// 
        /// 设置返回数据
        /// 
        /// 数据
        /// 
        public RequestResult setData(Object data)
        {
            _dtstart = Times.getTS();
            Result = data;
            return this;
        }
        /// 
        /// 推入数据
        /// 
        /// 数据
        /// 
        public RequestResult pushData(Object data)
        {
            _dtstart = Times.getTS();
            if (Result == null)
            {
                Result = new ArrayList();
            }
            if (data instanceof List)
            {
                ((java.util.List)this.Result).add(data);
                return this;
            }
            List li = new ArrayList();
            li.add(this.Result);
            li.add(data);
            this.Result = li;
            return this;
        }
        /// 
        /// 设置资源索引
        /// 
        /// 
        /// 
        public RequestResult setRel(String rel)
        {
            _dtstart = Times.getTS();
            Rel = rel;
            return this;
        }
        /// 
        /// 设置映射节点
        /// 
        /// 
        /// 
        /// 
        public RequestResult setMapping(String node, String name)
        {
            _dtstart = Times.getTS();
            if (config == null) config = new HashMap<>();
            config.put(node, name);
            return this;
        }
        /// 
        /// 文本格式化
        /// 
        /// 
        public String ToString()
        {
            if (config == null) return Json.toJson(this);
            else
            {
                Record record = Record.parse(this);
                config.forEach((x,y)->record.re(x,y));
                return Json.toJson(record);
            }
        }
    }