package com.cnd3b.restcontroller.singleagent.webclient.data;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import com.cnd3b.restcontroller.enterprise.agentclient.mainpage.data.DataTips;

import java.text.SimpleDateFormat;
import java.util.Calendar;

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

    /**
     * 首页简报
     *
     * @return
     */
    public String query_briefreport() {

        String fbegdate = content.getString("fbegdate");
        String fenddate = content.getString("fenddate");

        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 factory = new SQLFactory(this, "数据统计看板");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tagentsid", tagentsid);
        factory.addParameter("fbegdate", fbegdate);
        factory.addParameter("fenddate", fenddate);
        factory.addParameter_SQL("where", where);

        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows.get(0)).saveToDataPool(1).toString();
    }

    /**
     * 成交产品分析，基本柱状图
     *
     * @return 查询结果
     */
    public String productoforders() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t2.tenterprise_userid is null or t2.tenterprise_userid='").append(userid).append("' or t2.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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "成交产品分析").getTips()).saveToDataPool().toString();
    }

    /**
     * 客单值分析，基本折线图
     *
     * @return 查询结果
     */
    public String amountoforders() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "客单值分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 小区占比分析，基本饼图
     *
     * @return 查询结果
     */
    public String communityoforders() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "小区占比分析").getTips()).saveToDataPool().toString();
    }

    /**
     * 小区深度分析，表格
     *
     * @return 查询结果
     */
    public String communitydepthoforders() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "小区深度分析").getTips()).saveToDataPool().toString();
    }

    /**
     * 退单率分析，基本折线图
     *
     * @return 查询结果
     */
    public String returncountoforders() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "退单率分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 老客户档案
     *
     * @return
     */
    public String oldcustomer() {
        //如果年份为0，则默认取当前月至前12月的数据
        String fbegmonth = content.getString("fbegmonth");
        String fendmonth = content.getString("fendmonth");
        if ("".equals(fbegmonth) || "".equals(fendmonth)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            fbegmonth = format.format(calendar.getTime());

            calendar.add(Calendar.MONTH, -11);
            fendmonth = format.format(calendar.getTime());
        }
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter("pageNumber", pageNumber);
        sqlFactory.addParameter("pageSize", pageSize);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        returnObject = getSucReturnObject().setData(rows);
        returnObject.put("pageNumber", pageNumber);
        returnObject.put("pageSize", pageSize);
        returnObject.put("total", rows.getTotalRows());
        returnObject.put("pageTotal", rows.getTotalPage());
        returnObject.put("tips", new DataTips(rows, "老客户信息表").getTips());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }

    /**
     * 老客户维护系统生日提醒
     *
     * @return
     */
    public String oldcustomerbless() {
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t2.tenterprise_userid is null or t2.tenterprise_userid='").append(userid).append("' or t2.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }

        //如果年份为0，则默认取当前月至前12月的数据
        SQLFactory sqlFactory = new SQLFactory(this, "老客户维护系统生日提醒");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        returnObject = getSucReturnObject().setData(rows);
        returnObject.put("tips", new DataTips(rows, "老客户维护系统生日提醒").getTips());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }

    /**
     * 老客户维护系统购买周年
     *
     * @return
     */
    public String oldcustomercycle() {
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (!fisadministrator) {
            where.append(" and (t2.tenterprise_userid is null or t2.tenterprise_userid='").append(userid).append("' or t2.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }

        //如果年份为0，则默认取当前月至前12月的数据
        SQLFactory sqlFactory = new SQLFactory(this, "老客户维护系统周年提醒");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        returnObject = getSucReturnObject().setData(rows);
        returnObject.put("tips", new DataTips(rows, "老客户维护系统周年提醒").getTips());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }


}
