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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import p2.util.P2Exception;

import java.text.SimpleDateFormat;
import java.util.Calendar;
public class analysis extends Controller {
    public analysis(JSONObject content) {
        super(content);
    }

    /**
     * 工作数据
     * <p>
     * 工作结果评定，下属业务员工作结果评定
     *
     * @return
     */
    public String businesstripGrade() {
        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());
        }
        SQLFactory factory = new SQLFactory(this, "工作数据_业务员工作结果评定");
        factory.addParameter("siteid", siteid);
        factory.addParameter("fbegmonth", fbegmonth);
        factory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 工作数据
     * <p>
     * 方法收集排名
     *
     * @return
     */
    public String businesstripGetKnowledge() {
        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());
        }
        SQLFactory factory = new SQLFactory(this, "工作数据_经销商方法收集排名");
        factory.addParameter("siteid", siteid);
        factory.addParameter("fbegmonth", fbegmonth);
        factory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 销售数据
     * <p>
     * 同比增幅分析
     *
     * @return
     */
    public String saleAmountGrowthRateAnalysis() {
        int fyear = content.getIntValue("fyear");
        int fmonth = content.getIntValue("fmonth");

        SQLFactory factory = new SQLFactory(this, "销售数据_同比增幅分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("fyear", fyear);
        factory.addParameter("fmonth", fmonth);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        RowsMap map = rows.toRowsMap("fclass");

        String[] fclasses = {"-20%以下", "20%-0", "0-10%", "10-20%", "20%以上"};
        JSONArray array = new JSONArray();
        for (String fclass : fclasses) {
            JSONObject object = new JSONObject();
            object.put("fclass", fclass);
            object.put("fcount", map.get(fclass).size());
            object.put("fdata", map.get(fclass));
            array.add(object);
        }
        return getSucReturnObject().setData(array).saveToDataPool().toString();
    }

    /**
     * 销售数据
     * <p>
     * 月销售平均分析
     *
     * @return
     */
    public String saleAmountAverageAnalysis() {
        int fyear = content.getIntValue("fyear");
        SQLFactory factory = new SQLFactory(this, "销售数据_月销售平均分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("fyear", fyear);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        RowsMap map = rows.toRowsMap("fclass");

        String[] fclasses = {"1w以下", "1-3w", "3-5w", "5-10w", "10-20w", "20-50w", "50-100w", "100w以上"};
        JSONArray array = new JSONArray();
        for (String fclass : fclasses) {
            JSONObject object = new JSONObject();
            object.put("fclass", fclass);
            object.put("fcount", map.get(fclass).size());
            object.put("fdata", map.get(fclass));
            array.add(object);
        }
        return getSucReturnObject().setData(array).saveToDataPool().toString();
    }

    /**
     * 销售数据
     * <p>
     * 销售数据分析
     *
     * @return
     */
    public String saleAmountAnalysis() {
        int fyear = content.getIntValue("fyear");
        int fmonth = content.getIntValue("fmonth");
        SQLFactory factory = new SQLFactory(this, "销售数据_销售数据分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("fyear", fyear);
        factory.addParameter("fmonth", fmonth);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 店铺数据
     * <p>
     * 面积占比分析
     *
     * @return
     */
    public String storeAreaAnalysis() {
        SQLFactory factory = new SQLFactory(this, "店铺数据_面积占比分析");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        RowsMap map = rows.toRowsMap("fclass");

        String[] fclasses = {"<60", "60-80", "80-100", "100-150", "150-200", "200-300", ">300"};
        JSONArray array = new JSONArray();
        for (String fclass : fclasses) {
            JSONObject object = new JSONObject();
            object.put("fclass", fclass);
            object.put("fcount", map.get(fclass).size());
            object.put("fdata", map.get(fclass));
            array.add(object);
        }
        JSONObject object = new JSONObject();
        object.put("data", array);

        SQLFactory factory2 = new SQLFactory(this, "店铺数据_平均店铺面积");
        factory2.addParameter("siteid", siteid);
        Rows rows2 = dbConnect.runSqlQuery(factory2.getSQL());
        if (rows2.isEmpty()) {
            object.put("favgarea", 0);
        } else {
            object.put("favgarea", rows2.get(0).getDouble("farea"));
        }
        return getSucReturnObject().setData(object).saveToDataPool().toString();
    }

    /**
     * 店铺数据
     * <p>
     * 改建分析
     *
     * @return
     */
    public String storeRebuildAnalysis() {
        SQLFactory factory = new SQLFactory(this, "店铺数据_店铺改建分析");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        RowsMap map = rows.toRowsMap("fclass");
        String[] fclasses = {"<3年", "<2年", "<1年"};
        JSONArray array = new JSONArray();
        for (String fclass : fclasses) {
            JSONObject object = new JSONObject();
            object.put("fclass", fclass);
            object.put("fcount", map.get(fclass).size());
            object.put("fdata", map.get(fclass));
            array.add(object);
        }

        JSONObject object = new JSONObject();
        object.put("data", array);

        SQLFactory factory2 = new SQLFactory(this, "店铺数据_平均翻新时长");
        factory2.addParameter("siteid", siteid);
        Rows rows2 = dbConnect.runSqlQuery(factory2.getSQL());
        if (rows2.isEmpty()) {
            object.put("favgintervaltime", 0);
        } else {
            object.put("favgintervaltime", rows2.get(0).getDouble("intervaltime"));
        }
        return getSucReturnObject().setData(object).saveToDataPool().toString();
    }

    /**
     * 店铺数据
     * <p>
     * 店铺卖场分析
     *
     * @return
     */
    public String storeMarketAnalysis() {
        SQLFactory factory = new SQLFactory(this, "店铺数据_店铺卖场分析");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        RowsMap map = rows.toRowsMap("fclass");

        String[] fclasses = dbConnect.runSqlQuery("select fvalue from ttypedetail where ftype='卖场'").toArray("fvalue");
        JSONArray array = new JSONArray();
        for (String fclass : fclasses) {
            JSONObject object = new JSONObject();
            object.put("fclass", fclass);
            object.put("fcount", map.get(fclass).size());
            object.put("fdata", map.get(fclass));
            array.add(object);
        }
        return getSucReturnObject().setData(array).saveToDataPool().toString();
    }

    /**
     * 店铺数据
     * <p>
     * 经销商档案表
     *
     * @return
     */
    public String agentList() {
        SQLFactory factory = new SQLFactory(this, "店铺数据_经销商档案表", pageSize, pageNumber, "fagentnum");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 店铺数据
     * <p>
     * 经销商档案表,店铺列表
     *
     * @return
     */
    public String agentStoreList() {
        SQLFactory factory = new SQLFactory(this, "店铺数据_经销商档案店铺列表");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tagentsid", content.getString("tagentsid"));
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 意向客户分析
     * 客户来源分析，基本饼图
     *
     * @return
     */
    public String channelofcustomers() {
        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());
        }

        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_客户来源分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 意向客户分析
     * 新增意向客户分析，双值柱状图
     *
     * @return
     */
    public String newcustomers() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_新增意向客户分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 意向客户分析
     * 销售动作分析，漏斗图
     *
     * @return
     */
    public String customeraction() {
        //如果年份为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());
        }

        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_销售动作分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 销售渠道分析，基本饼图
     *
     * @return
     */
    public String channeloforders() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_销售渠道分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 成交产品分析，基本柱状图
     *
     * @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());
        }

        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_成交产品分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 客单值分析，基本折线图
     *
     * @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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_客单值分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 小区占比分析，基本饼图
     *
     * @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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_小区占比分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 经销商订单数占比分析
     *
     * @return
     */
    public String agentsoforders() {
        String sort = "count(t1.tagentsid) desc";
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_经销商订单数占比分析", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 退单率分析，基本折线图
     *
     * @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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_退单率分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 意向客户分析
     * <p>
     * 销售情况分析表，表格
     *
     * @return
     */
    public String customeroforders() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fdate"};
        String sort = getSort(sortfield, "t1.fdate desc");

        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_销售情况分析", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        RowsMap docRowsmap = getAttachmentQty("tcustomer_reception", rows.toArray("tcustomer_receptid"));
        for(Row row:rows){
            String tcustomer_receptid=row.getString("tcustomer_receptid");
            if(docRowsmap.containsKey(tcustomer_receptid)){
                row.put("docqty",docRowsmap.get(tcustomer_receptid).get(0).getInteger("fattachmentcount"));
            }else{
                row.put("docqty",0);
            }
        }

        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 获取excel下载地址
     * @return
     */
    public String customerofordersXls()throws P2Exception {
        String fbegdate=content.getString("fbegdate");
        String fenddate=content.getString("fenddate");
        long datakey= getRequestAuthorDataKey();
        String url=hostipport+ "/p2report/frameset?__report=REPORT/agentorderlist.rptdesign&__format=xls&__asattachment=true&&siteid="+siteid+"&fbegdate="+fbegdate+"&fenddate="+fenddate+"&datakey="+datakey;
        return getSucReturnObject().setData(url).toString();
    }

    /**
     * 终端大数据
     * 事件分析
     * <p>
     * 事件订单占比分析，基本饼图
     *
     * @return
     */
    public String eventoforders() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_事件分析_事件订单占比分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 终端大数据
     * 事件分析
     * <p>
     * 经销商累计事件统计分析，基本饼图
     *
     * @return
     */
    public String eventofagentorders() {
        String sort = "count(distinct tsaleeventid) desc";
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_事件分析_经销商累计事件统计分析", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 终端大数据
     * 月度费用分析
     * <p>
     * 月度杂项费用分析，基本饼图
     *
     * @return
     */
    public String costnameofmonth() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_月度费用分析_月度杂项费用分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 月度费用分析
     * <p>
     * 月度盈亏分析，面积图
     *
     * @return
     */
    public String costandordersofmonth() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_月度费用分析_月度盈亏分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 终端大数据
     * 客户档案分析
     * <p>
     * 老客户档案表
     *
     * @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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_客户档案分析_老客户档案表");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter("pageNumber", pageNumber);
        sqlFactory.addParameter("pageSize", pageSize);
        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());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }


    /**
     * 终端大数据
     * 客户档案分析
     * <p>
     * 意向客户档案表
     *
     * @return
     */
    public String newcustomer() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_客户档案分析_意向客户档案表");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        sqlFactory.addParameter("pageNumber", pageNumber);
        sqlFactory.addParameter("pageSize", pageSize);
        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());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }

    /**
     * 接待次数与首单成交分析，饼图
     *
     * @return
     */
    public String receptiontimesoffirstorder() {
        //如果年份为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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "终端大数据_意向客户分析_接待次数与成交分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 用户访问数据分析
     *
     * @return
     */
    public String userrequestcount() {
        String fbegdate = content.getString("fbegdate");
        String fenddate = content.getString("fenddate");

        SQLFactory sqlFactory = new SQLFactory(this, "系统_用户访问数据分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegdate", fbegdate);
        sqlFactory.addParameter("fenddate", fenddate);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 用户下载数据分析
     *
     * @return
     */
    public String userdownloadcount() {
        String fbegdate = content.getString("fbegdate");
        String fenddate = content.getString("fenddate");

        SQLFactory sqlFactory = new SQLFactory(this, "系统_用户下载数据分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegdate", fbegdate);
        sqlFactory.addParameter("fenddate", fenddate);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 模块应用使用次数数据分析
     *
     * @return
     */
    public String apprequesttimesbyday() {
        String fbegdate = content.getString("fbegdate");
        String fenddate = content.getString("fenddate");
        String fappname = content.getString("fappname");

        SQLFactory sqlFactory = new SQLFactory(this, "系统_模块应用数据分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegdate", fbegdate);
        sqlFactory.addParameter("fenddate", fenddate);
        sqlFactory.addParameter("fappname", fappname);

        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 模块应用使用次数占比数据分析
     *
     * @return
     */
    public String apprequesttimes() {
        String fbegdate = content.getString("fbegdate");
        String fenddate = content.getString("fenddate");
        SQLFactory sqlFactory = new SQLFactory(this, "系统_模块应用占比分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fbegdate", fbegdate);
        sqlFactory.addParameter("fenddate", fenddate);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}