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

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;

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

    /**
     * 首页简报
     *
     * @return
     */
    public String query_briefreport() {
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t1.tenterprise_userid is null or t1.tenterprise_userid='").append(userid).append("' or t1.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }

        String fdate = content.getString("fdate");

        SQLFactory factory = new SQLFactory(this, "首页简报");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tagentsid", tagentsid);
        factory.addParameter("fdate", fdate.substring(0, 7));
        factory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows.get(0)).saveToDataPool(1).toString();
    }

    /**
     * 今日动态
     *
     * @return
     */
    public String query_newcustmsg() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate asc", "t1.createdate desc"};
        String sort = getSort(sortfield, "t1.createdate desc");
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t1.tenterprise_userid is null or t1.tenterprise_userid='").append(userid).append("' or t1.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }

        String fdate = content.getString("fdate");
        SQLFactory factory = new SQLFactory(this, "今日动态", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tagentsid", tagentsid);
        factory.addParameter("fdate", fdate);
        factory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows,sortfield).setFinalDo(1,1);
    }

    /**
     * 最新访问
     *
     * @return
     */
    public String query_latestvisit() {
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t1.tenterprise_userid is null or t1.tenterprise_userid='").append(userid).append("' or t1.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }
        SQLFactory sqlFactory = new SQLFactory(this, "最新访问查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool(2).toString();
    }

}
