package com.cnd3b.restcontroller.enterprise.wechatclient.promotersactivities;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
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 com.cnd3b.common.data.Row;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.ArrayList;
import java.util.HashMap;

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

    /**
     * 数据统计（首页简报）
     *
     * @return
     */
    public String query_analysis() {
        SQLFactory sqlFactory = new SQLFactory(this, "数据统计");
        sqlFactory.addParameter("tenterprise_userid", userid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    private static HashMap<String, String> activitiesSiteidMap = new HashMap<>();

    /**
     * 获取活动所属站点
     *
     * @param tagentactivitiesid
     * @return
     */
    public String getActivitiesSiteid(String tagentactivitiesid) {
        if (activitiesSiteidMap.containsKey(tagentactivitiesid)) {
            return activitiesSiteidMap.get(tagentactivitiesid);
        } else {
            Rows rows = dbConnect.runSqlQuery("select siteid from tagentactivities where tagentactivitiesid=" + tagentactivitiesid);
            if (!rows.isEmpty()) {
                String siteid = rows.get(0).getString("siteid");
                activitiesSiteidMap.put(tagentactivitiesid, siteid);
                return siteid;
            }
        }
        return null;
    }


    private static HashMap<String, String> activitiesAgentidMap = new HashMap<>();

    /**
     * 获取活动所属经销商ID
     *
     * @param tagentactivitiesid
     * @return
     */
    public String getActivitiesAgentid(String tagentactivitiesid) {
        if (activitiesAgentidMap.containsKey(tagentactivitiesid)) {
            return activitiesAgentidMap.get(tagentactivitiesid);
        } else {
            Rows rows = dbConnect.runSqlQuery("select tagentsid from tagentactivities where tagentactivitiesid=" + tagentactivitiesid);
            if (!rows.isEmpty()) {
                String tagentsid = rows.get(0).getString("tagentsid");
                activitiesAgentidMap.put(tagentactivitiesid, tagentsid);
                return tagentsid;
            }
        }
        return null;
    }


    /**
     * 经销商活动（找客户）列表查询
     *
     * @return
     */
    public String query_agentactivitiesList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate"};
        String sort = getSort(sortfield, "t1.checkdate desc");
        SQLFactory activitiesSQl = new SQLFactory(this, "活动列表查询", pageSize, pageNumber, sort);
        activitiesSQl.addParameter("tenterprise_userid", userid);
        Rows rows = dbConnect.runSqlQuery(activitiesSQl.getSQL());
        ArrayList<String> tagentactivitiesidlist = rows.toArrayList("tagentactivitiesid");
        ArrayList<String> siteidlist = rows.toArrayList("siteid");

        SQLFactory coverfactory = new SQLFactory(this, "找客户分享页封面图查询");
        coverfactory.addParameter_in("siteid", siteidlist);
        coverfactory.addParameter_in("tagentactivitiesid", tagentactivitiesidlist);
        RowsMap coverrowsmap = dbConnect.runSqlQuery(coverfactory.getSQL()).toRowsMap("tagentactivitiesid");

        for (Row row : rows) {
            String tagentactivitiesid = row.getString("tagentactivitiesid");
            String siteid = row.getString("siteid");
            if (coverrowsmap.containsKey(tagentactivitiesid)) {
                row.put("cover", coverrowsmap.get(tagentactivitiesid).get(0).getString("fobsurl"));
            } else {
                row.put("cover", "");
            }
            activitiesSiteidMap.put(tagentactivitiesid, siteid);
        }
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商活动（找客户）主界面查询
     *
     * @return
     */
    public String query_agentactivitiesMain() {
        String tagentactivitiesid = content.getString("tagentactivitiesid");//活动ID
        String siteid = getActivitiesSiteid(tagentactivitiesid);
        SQLFactory activitiesSQl = new SQLFactory(this, "活动详情页查询_他人");
        activitiesSQl.addParameter("siteid", siteid);
        activitiesSQl.addParameter("tagentactivitiesid", tagentactivitiesid);
        activitiesSQl.addParameter("tenterprise_userid", userid);

        Rows rows = dbConnect.runSqlQuery(activitiesSQl.getSQL());
        for (Row row : rows) {
            SQLFactory coverfactory = new SQLFactory(this, "找客户分享页封面图查询");
            coverfactory.addParameter_in("siteid", siteid);
            coverfactory.addParameter_in("tagentactivitiesid", tagentactivitiesid);
            Rows coverrows = dbConnect.runSqlQuery(coverfactory.getSQL());
            if (!coverrows.isEmpty()) {
                row.put("cover", coverrows.get(0).getString("fobsurl"));
            } else {
                row.put("cover", "");
            }
            SQLFactory docfactory = new SQLFactory(this, "找客户分享页详情图查询");
            docfactory.addParameter("siteid", siteid);
            docfactory.addParameter("tagentactivitiesid", tagentactivitiesid);
            Rows docrows = dbConnect.runSqlQuery(docfactory.getSQL());
            row.put("docinfos", docrows);
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 经销商活动（找客户）主界面查询_拉新统计
     *
     * @return
     */
    public String query_agentactivitiesCustList() {
        String tagentactivitiesid = content.getString("tagentactivitiesid");//活动ID
        String siteid = getActivitiesSiteid(tagentactivitiesid);

        SQLFactory activitiesSQl = new SQLFactory(this, "活动详情页拉新统计查询_他人", pageSize, pageNumber, "t1.createdate desc");
        activitiesSQl.addParameter("siteid", siteid);
        activitiesSQl.addParameter("tagentactivitiesid", tagentactivitiesid);
        activitiesSQl.addParameter("tenterprise_userid", userid);

        Rows rows = dbConnect.runSqlQuery(activitiesSQl.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商活动（找客户）主界面查询_线索统计
     *
     * @return
     */
    public String query_agentactivitiesFormList() {
        String tagentactivitiesid = content.getString("tagentactivitiesid");//活动ID
        String siteid = getActivitiesSiteid(tagentactivitiesid);

        SQLFactory activitiesSQl = new SQLFactory(this, "活动详情页线索数据查询_他人", pageSize, pageNumber, "t1.createdate desc");
        activitiesSQl.addParameter("siteid", siteid);
        activitiesSQl.addParameter("tagentactivitiesid", tagentactivitiesid);
        activitiesSQl.addParameter("tenterprise_userid", userid);

        Rows rows = dbConnect.runSqlQuery(activitiesSQl.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 经销商活动分享记录
     *
     * @return
     */
    public String agentactivitiesShare() throws P2Exception {
        String tagentactivitiesid = content.getString("tagentactivitiesid");//活动ID

        String siteid = getActivitiesSiteid(tagentactivitiesid);
        String tagentsid = getActivitiesAgentid(tagentactivitiesid);

        String fsaler1id = "";
        String fsaler1 = "";
        Rows rows = dbConnect.runSqlQuery("select fsaler1id from tpromoters where siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and fstatus=1");
        if (!rows.isEmpty()) {
            fsaler1id = rows.get(0).getString("fsaler1id");
            fsaler1 = getUser(fsaler1id, siteid).getString("fname");
        }
        PaoSetRemote tagentactivities_shareSet = getP2ServerSystemPaoSet("tagentactivities_share");
        PaoRemote tagentactivities_share = tagentactivities_shareSet.addAtEnd();
        tagentactivities_share.setValue("siteid", siteid, 11L);//企业ID
        tagentactivities_share.setValue("fsaler1id", fsaler1id, 11L);//一级推广人ID
        tagentactivities_share.setValue("fsaler1", fsaler1, 11L);//一级推广人姓名
        tagentactivities_share.setValue("fsaler2id", userid, 11L);//二级推广人ID
        tagentactivities_share.setValue("fsaler2", username, 11L);//二级推广人姓名
        tagentactivities_share.setValue("tagentactivitiesid", tagentactivitiesid, 11L);
        tagentactivities_share.setValue("createdate", getDateTime(), 11L);//推广时间
        long tagentactivities_shareid = tagentactivities_share.getUniqueIDValue();
        tagentactivities_shareSet.save();
        tagentactivities_shareSet.close();
        
        JSONObject dataobject = new JSONObject();
        dataobject.put("tagentactivities_shareid", tagentactivities_shareid);
        return getSucReturnObject().setData(dataobject).toString();
    }


}
