package com.cnd3b.common;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.db.DataPool;

public class D3BReturnObject_Suc extends D3BReturnObject {

    boolean ispaging = false;

    public D3BReturnObject_Suc(Controller controller) {
        super(controller);
        this.put("msg", "成功");
        this.put("code", 1);
        this.put("data", "成功");
    }

    public D3BReturnObject_Suc setData(Rows rows) {
        if (!"".equals(rows.getErrMsg())) {
            this.put("msg", "失败");
            this.put("code", 0);
            this.put("data", rows.getErrMsg());
        } else {
            this.put("data", rows.toJsonArray());
        }
        return this;
    }

    public D3BReturnObject_Suc setDataByPaging(Rows rows) {
        ispaging = true;
        if (!"".equals(rows.getErrMsg())) {
            this.put("msg", "失败");
            this.put("code", 0);
            this.put("data", rows.getErrMsg());
        } else {
            this.put("data", rows.toJsonArray());
            this.put("pageNumber", controller.pageNumber);
            this.put("pageSize", controller.pageSize);
            this.put("total", rows.getTotalRows());
            this.put("pageTotal", rows.getTotalPage());
        }
        return this;
    }

    public D3BReturnObject_Suc setDataByPaging(Rows rows, String[] sortfield) {
        ispaging = true;
        if (!"".equals(rows.getErrMsg())) {
            this.put("msg", "失败");
            this.put("code", 0);
            this.put("data", rows.getErrMsg());
        } else {
            this.put("data", rows.toJsonArray());
            this.put("pageNumber", controller.pageNumber);
            this.put("pageSize", controller.pageSize);
            this.put("total", rows.getTotalRows());
            this.put("pageTotal", rows.getTotalPage());
            this.put("sortmsg", sortfield);
        }
        return this;
    }

    public D3BReturnObject_Suc setData(Row row) {
        this.put("data", row.toJsonObject());
        return this;
    }

    public D3BReturnObject_Suc setData(Object data) {
        this.put("data", data);
        return this;
    }

    public D3BReturnObject_Suc setData(String data) {
        this.put("data", data);
        return this;
    }

    public D3BReturnObject_Suc setTips(JSONObject tips) {
        this.put("tips", tips);
        return this;
    }

    public D3BReturnObject_Suc addAppHelpNotes(long tarchives_scid) {
        this.put("tarchives_scid", tarchives_scid);
        return this;
    }

    public D3BReturnObject_Suc addKeyValue(String key, Object value) {
        this.put(key, value);
        return this;
    }

    /**
     * 设置缓存及预加载
     *
     * @param pooldatalife    @default a   缓存有效时间，-1表示不进行缓存，0表示系统默认缓存时间，非0正数表示指定的缓存时间（分钟）
     * @param preloadingcount 预加载页数
     * @return
     */
    public String setFinalDo(int pooldatalife, int preloadingcount) {
        if (pooldatalife >= 0) {
            DataPool.put(controller.content.toJSONString(), this, pooldatalife);
        }
        if (ispaging) {
            controller.preloading(preloadingcount);
        }
        return this.toString();
    }


    public D3BReturnObject_Suc preloading(int preloadingcount) {
        if (ispaging) {
            controller.preloading(preloadingcount);
        }
        return this;
    }

    /**
     * 设置缓存及预加载
     *
     * @return
     */
    public D3BReturnObject_Suc saveToDataPool() {
        setFinalDo(DataPool.defaultdatalife, 0);
        return this;
    }

    /**
     * 设置缓存及预加载
     *
     * @param pooldatalife @default a   缓存有效时间，-1表示不进行缓存，0表示系统默认缓存时间，非0正数表示指定的缓存时间（分钟）
     * @return
     */
    public D3BReturnObject_Suc saveToDataPool(int pooldatalife) {
        setFinalDo(pooldatalife, 0);
        return this;
    }
}
