package com.cnd3b.restcontroller.enterprise.webclient.mainpage;

import com.cnd3b.common.Controller;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.Rows;
import com.cnd3b.utility.obs.Bucket;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.utility.Math;

public class mainpage extends Controller {
    public mainpage(JSONObject content) {
        super(content);
    }

    /**
     * 首页简报
     *
     * @return
     */
    public String query_briefreport() {
        SQLFactory factory = new SQLFactory(this, "专业版首页简报");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows.get(0)).saveToDataPool().toString();
    }

    /**
     * 标准版首页简报
     *
     * @return
     */
    public String query_simplebriefreport() {
        SQLFactory factory = new SQLFactory(this, "标准版首页简报");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows.get(0)).saveToDataPool().toString();
    }


    public String query_onlineuserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.flastrequestdate desc"};
        String sort = getSort(sortfield, "t1.flastrequestdate desc");

        SQLFactory factory = new SQLFactory(this, "在线用户列表", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
    }


    /**
     * 今日访问列表
     *
     * @return
     */
    public String query_todayrequestagentlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tagentsid"};
        String sort = getSort(sortfield, "t1.tagentsid");

        SQLFactory factory = new SQLFactory(this, "今日访问列表", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
    }

    /**
     * 今日未访问列表
     *
     * @return
     */
    public String query_todayhaventrequestagentlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tagentsid"};
        String sort = getSort(sortfield, "t1.tagentsid");

        SQLFactory factory = new SQLFactory(this, "今日未访问列表", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
    }

    /**
     * 今日下载列表
     *
     * @return
     */
    public String query_todaydownloadlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate desc"};
        String sort = getSort(sortfield, "t1.createdate desc");

        SQLFactory factory = new SQLFactory(this, "今日下载列表", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
    }

    /**
     * 云存储用量查询
     *
     * @return
     */
    public String query_obsVolume() {
        int fmaxvolume = 0;
        Rows rows = dbConnect.runSqlQuery("select fmaxvolume from tobsmag where siteid='" + siteid + "'");
        if (!rows.isEmpty()) {
            fmaxvolume = rows.get(0).getInteger("fmaxvolume");
        }
        Bucket bucket = new Bucket(siteid);
        double fusevolume = bucket.getVolume();
        JSONObject object = new JSONObject();
        object.put("fmaxvolume", fmaxvolume);
        if ((fusevolume / 1024d) < 1024d) {
            object.put("fusevolume", Math.div(bucket.getVolume(), 1024d, 2) + "KB");
        } else if ((fusevolume / (1024d * 1024d)) < 1024d) {
            object.put("fusevolume", Math.div(bucket.getVolume(), (1024d * 1024d), 2) + "MB");
        } else {
            object.put("fusevolume", Math.div(bucket.getVolume(), (1024d * 1024d * 1024d), 2) + "GB");
        }
        return getSucReturnObject().setData(object).saveToDataPool(60).toString();
    }

    /**
     * 短信用量查询
     *
     * @return
     */
    public String query_smsVolume() {
        SQLFactory sqlFactory = new SQLFactory(this, "企业短信使用量查询");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        int fsmsvolume = rows.get(0).getInteger("fsmsvolume");
        int fusevolume = rows.get(0).getInteger("fusevolume");
        JSONObject object = new JSONObject();
        object.put("fmaxvolume", fsmsvolume);
        object.put("fusevolume", fusevolume);
        object.put("fcanusevolume", fsmsvolume - fusevolume);
        return getSucReturnObject().setData(object).saveToDataPool(60).toString();
    }
}
