package com.cnd3b.restcontroller.enterprise.agentclient.mainpage.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 java.text.SimpleDateFormat;
import java.util.Calendar;

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

    /**
     * 客户来源分析，基本饼图
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "客户来源分析").getTips()).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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "新增意向客户分析").getTips()).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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "销售动作分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 销售渠道分析，基本饼图
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "销售渠道分析").getTips()).saveToDataPool().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());
        }

        SQLFactory sqlFactory = new SQLFactory(this, "成交产品分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "客单值分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "小区占比分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "小区深度分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        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());
        }
        SQLFactory sqlFactory = new SQLFactory(this, "退单率分析");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "退单率分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 销售情况分析表，表格
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "销售情况分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 事件订单占比分析，基本饼图
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "事件订单占比分析").getTips()).saveToDataPool().toString();
    }

    /**
     * 月度杂项费用分析，基本饼图
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "月度杂项费用分析").getTips()).saveToDataPool().toString();
    }


    /**
     * 月度盈亏分析，面积图
     *
     * @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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        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());
        }
        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);
        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() {
        //如果年份为0，则默认取当前月至前12月的数据
        SQLFactory sqlFactory = new SQLFactory(this, "老客户维护系统生日提醒");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        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() {
        //如果年份为0，则默认取当前月至前12月的数据
        SQLFactory sqlFactory = new SQLFactory(this, "老客户维护系统周年提醒");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        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 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("tagentsid", tagentsid);
        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());
        returnObject.put("tips", new DataTips(rows, "意向客户信息表").getTips());
        DataPool.put(content.toString(), returnObject);
        return returnObject.toString();
    }

    /**
     * 意向客户跟进系统
     *
     * @return
     */
    public String newcustomertrack() {
        //如果年份为0，则默认取当前月至前12月的数据

        SQLFactory sqlFactory7 = new SQLFactory(this, "意向客户跟进系统7");
        sqlFactory7.addParameter("siteid", siteid);
        sqlFactory7.addParameter("tagentsid", tagentsid);
        Rows rows7 = dbConnect.runSqlQuery(sqlFactory7.getSQL());

        SQLFactory sqlFactory14 = new SQLFactory(this, "意向客户跟进系统14");
        sqlFactory14.addParameter("siteid", siteid);
        sqlFactory14.addParameter("tagentsid", tagentsid);
        Rows rows14 = dbConnect.runSqlQuery(sqlFactory14.getSQL());

        JSONObject object = new JSONObject();
        object.put("sevendays", rows7.toJsonArray());
        object.put("fourteendays", rows14.toJsonArray());

        returnObject = getSucReturnObject().setData(object);
        returnObject.put("tips", new DataTips("意向客户跟进系统").getTips());
        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("tagentsid", tagentsid);
        sqlFactory.addParameter("fbegmonth", fbegmonth);
        sqlFactory.addParameter("fendmonth", fendmonth);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        return getSucReturnObject().setData(rows).setTips(new DataTips(rows, "接待次数与成交分析").getTips()).saveToDataPool().toString();
    }

}
