package com.cnd3b.restcontroller.enterprise.salerwebclient.mainpage.archives;

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.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.ArrayList;
import java.util.Calendar;

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

    /**
     * 推广素材列表查询
     *
     * @return
     */
    public String query_archivesList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate", "t1.fvisitors", "t1.fshares"};
        String sort = getSort(sortfield, "t1.checkdate desc");

        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t1.ftitle like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fnotes like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fclass1") && !"".equals(whereObject.getString("fclass1"))) {
                where.append(" and fclass1=").append("'").append(whereObject.getString("fclass1")).append("'");
            }
            if (whereObject.containsKey("fclass2") && !"".equals(whereObject.getString("fclass2"))) {
                where.append(" and fclass2=").append("'").append(whereObject.getString("fclass2")).append("'");
            }
            if (whereObject.containsKey("fmycollection") && whereObject.getBooleanValue("fmycollection")) {
                where.append(" and t2.tarchives_ad_colid is not null");
            }
        }
        SQLFactory factory = new SQLFactory(this, "推广素材列表查询", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tenterprise_userid", userid);
        factory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());

        String[] tarchives_adids = rows.toArray("tarchives_adid");

        SQLFactory contentfactory = new SQLFactory(this, "推广素材列表查询_富文本内容查询");
        contentfactory.addParameter("siteid", siteid);
        contentfactory.addParameter_in("tarchives_adid", tarchives_adids);
        RowsMap contentrowsmap = dbConnect.runSqlQuery(factory.getSQL()).toRowsMap("tarchives_adid");
        for (Row row : rows) {
            String tarchives_adid = row.getString("tarchives_adid");
            if (contentrowsmap.containsKey(tarchives_adid)) {
                row.put("fcontent", contentrowsmap.get(tarchives_adid).get(0).getString("fcontent"));
            } else {
                row.put("fcontent", "");
            }
        }


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

    /**
     * 推广素材主界面查询
     *
     * @return
     */
    public String query_archivesMain() throws P2Exception {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory sqlFactory = new SQLFactory(this, "推广素材主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        sqlFactory.addParameter("tarchives_adid", tarchives_adid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            //标签
            String[] flable = dbConnect.runSqlQuery("select flable from tarchives_ad_tag where siteid='" + siteid + "' and tarchives_adid='" + tarchives_adid + "'").toArray("flable");
            row.put("tag", flable);
            Rows fcontent = dbConnect.runSqlQuery("select fcontent from tarchives_ad where siteid='" + siteid + "' and tarchives_adid='" + tarchives_adid + "'");
            row.put("fcontent", fcontent.get(0).getString("fcontent"));
        }
        dbConnect.runSqlUpdate("update tarchives_ad set fvisitors=isnull(fvisitors,0)+1 where siteid='" + siteid + "' and tarchives_adid=" + content.getString("tarchives_adid"));

        PaoSetRemote tarchives_ad_userdataSet = getP2ServerSystemPaoSet("tarchives_ad_userdata");
        PaoRemote tarchives_ad_userdata = tarchives_ad_userdataSet.addAtEnd();
        tarchives_ad_userdata.setValue("siteid", siteid, 11L);
        tarchives_ad_userdata.setValue("tarchives_adid", tarchives_adid, 11L);
        tarchives_ad_userdata.setValue("tenterprise_userid", userid, 11L);
        tarchives_ad_userdata.setValue("createby", username, 11L);
        Calendar calendar = Calendar.getInstance();
        tarchives_ad_userdata.setValue("createdate", calendar.getTime(), 11L);
        calendar.add(Calendar.MINUTE, 5);
        tarchives_ad_userdata.setValue("closedate", calendar.getTime(), 11L);
        tarchives_ad_userdataSet.save();

        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 推广素材标签查询
     *
     * @return
     */
    public String query_tagList() {
        SQLFactory sql = new SQLFactory(this, "推广素材系统标签查询");
        sql.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 推广素材收藏
     *
     * @return
     */
    public String archivesCollection() throws P2Exception {
        long tarchives_adid = content.getLongValue("tarchives_adid");//推广素材ID
        boolean iscollection = content.getBooleanValue("iscollection");//是否收藏，1收藏，0取消收藏

        PaoSetRemote tarchives_ad_collectionSet = getP2ServerSystemPaoSet("tarchives_ad_collection", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tarchives_adid='" + tarchives_adid + "'");
        PaoRemote tarchives_ad_collection = null;
        if (tarchives_ad_collectionSet.isEmpty() && iscollection) {
            tarchives_ad_collection = tarchives_ad_collectionSet.addAtEnd();
            tarchives_ad_collection.setValue("siteid", siteid, 11L);//企业ID
            tarchives_ad_collection.setValue("tenterprise_userid", userid, 11L);//用户ID
            tarchives_ad_collection.setValue("fcollectiondate", sysdate, 11L);//录入时间
            tarchives_ad_collection.setValue("tarchives_adid", tarchives_adid, 11L);
        } else if (!tarchives_ad_collectionSet.isEmpty() && !iscollection) {
            tarchives_ad_collectionSet.deleteAll();
        }
        tarchives_ad_collectionSet.save();

        return getSucReturnObject().toString();
    }

    /**
     * 推广素材类型查询
     *
     * @return
     */
    public String getArchivesClass() {
        SQLFactory sqlFactory = new SQLFactory(this, "推广素材分类查询");
        sqlFactory.addParameter("siteid", siteid);
        RowsMap rowsmap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap("ftypemx");
        JSONArray array = new JSONArray();
        for (String ftypemx : rowsmap.keySet()) {
            JSONObject object = new JSONObject();
            object.put("fclass1", ftypemx);
            object.put("fclass2", rowsmap.get(ftypemx).toArray("fvalue"));
            array.add(object);
        }
        return getSucReturnObject().setData(array).saveToDataPool().toString();
    }


    /**
     * 推广素材阅读分析
     *
     * @return
     */
    public String read_analysis() {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材阅读占比分析_经销商");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 推广素材阅读分析
     *
     * @return
     */
    public String read_analysis_saler() {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材阅读占比分析_业务员");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 推广素材下载分析
     *
     * @return
     */
    public String download_analysis() {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材下载占比分析_经销商");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 推广素材下载分析
     *
     * @return
     */
    public String download_analysis_saler() {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材下载占比分析_业务员");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 未浏览用户列表
     *
     * @return
     */
    public String havenoreaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材未浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 未浏览用户列表
     *
     * @return
     */
    public String havenoreaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材未浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已浏览用户列表
     *
     * @return
     */
    public String havereaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");

        SQLFactory factory = new SQLFactory(this, "分析_推广素材已浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());

        Rows downloaduserid = dbConnect.runSqlQuery("select t2.tenterprise_userid from tattachment t1 inner join tattachment_downlog t2 on t1.siteid=t2.siteid and t1.tattachmentid=t2.tattachmentid where  t1.siteid='" + siteid + "' and t1.ownertable='tarchives_ad' and t1.ownerid='" + tarchives_adid + "' ");
        ArrayList<String> userlist = downloaduserid.toArrayList("tenterprise_userid");
        for (Row row : rows) {
            row.put("fisdownloadfile", userlist.contains(row.getString("tenterprise_userid")));
        }
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已浏览用户列表
     *
     * @return
     */
    public String havereaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");

        SQLFactory factory = new SQLFactory(this, "分析_推广素材已浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());

        Rows downloaduserid = dbConnect.runSqlQuery("select t2.tenterprise_userid from tattachment t1 inner join tattachment_downlog t2 on t1.siteid=t2.siteid and t1.tattachmentid=t2.tattachmentid where  t1.siteid='" + siteid + "' and t1.ownertable='tarchives_ad' and t1.ownerid='" + tarchives_adid + "' ");
        ArrayList<String> userlist = downloaduserid.toArrayList("tenterprise_userid");
        for (Row row : rows) {
            row.put("fisdownloadfile", userlist.contains(row.getString("tenterprise_userid")));
        }
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 未下载用户列表
     *
     * @return
     */
    public String havenodownloaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材未下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 未下载用户列表
     *
     * @return
     */
    public String havenodownloaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材未下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已下载用户列表
     *
     * @return
     */
    public String havedownloaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");

        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材已下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已下载用户列表
     *
     * @return
     */
    public String havedownloaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t2.tenterprise_userid"};
        String sort = getSort(sortfield, "t2.tenterprise_userid");

        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "分析_推广素材已下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 分享统计
     * 分享人数、分享名单、分享次数等
     *
     * @return
     */
    public String shareStatistics() {
        String tarchives_adid = content.getString("tarchives_adid");
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tarchives_adid"};
        String sort = getSort(sortfield, "t1.tarchives_adid");

        SQLFactory factory = new SQLFactory(this, "推广素材分享统计", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 分享统计,拉新名单
     *
     * @return
     */
    public String shareStatistics_custlist() {
        String tarchives_adid = content.getString("tarchives_adid");
        String tenterprise_userid = content.getString("tenterprise_userid");
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate desc"};
        String sort = getSort(sortfield, "t1.createdate desc");

        SQLFactory factory = new SQLFactory(this, "推广素材分享统计_拉新名单", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        factory.addParameter("tenterprise_userid", tenterprise_userid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 分享渠道统计
     *
     * @return
     */
    public String shareChannelStatistics() {
        String tarchives_adid = content.getString("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "推广素材分享渠道统计");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 分享拉新渠道统计
     *
     * @return
     */
    public String shareCustChannelStatistics() {
        String tarchives_adid = content.getString("tarchives_adid");
        SQLFactory factory = new SQLFactory(this, "推广素材拉新渠道统计");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_adid", tarchives_adid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}
