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_sc extends Controller {
    public archives_sc(JSONObject content) {
        super(content);
    }

    /**
     * 商学院课件列表查询
     *
     * @return
     */
    public String query_archivesList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate", "t1.fvisitors"};
        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_sc_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());

        SQLFactory docfactory = new SQLFactory(this, "商学院课件列表封面查询");
        docfactory.addParameter("siteid", siteid);
        docfactory.addParameter_in("ownerid", rows.toArrayList("tarchives_scid"));
        RowsMap map = dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("tarchives_scid");
        for (Row row : rows) {
            row.put("docinfos", map.get(row.getString("tarchives_scid")));
        }

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

    /**
     * 商学院课件主界面查询
     *
     * @return
     */
    public String query_archivesMain() throws P2Exception {
        long tarchives_scid = content.getLongValue("tarchives_scid");
        SQLFactory sqlFactory = new SQLFactory(this, "商学院课件主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        sqlFactory.addParameter("tarchives_scid", tarchives_scid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            //标签
            String[] flable = dbConnect.runSqlQuery("select flable from tarchives_sc_tag where siteid='" + siteid + "' and tarchives_scid='" + tarchives_scid + "'").toArray("flable");
            row.put("tag", flable);

            SQLFactory agentsql = new SQLFactory(this, "商学院课件经销商学习信息查询");
            agentsql.addParameter("siteid", siteid);
            agentsql.addParameter("tarchives_scid", tarchives_scid);
            agentsql.addParameter_in("fsaleruserid", getSubUsers());

            Rows agentRows = dbConnect.runSqlQuery(agentsql.getSQL());
            RowsMap freadstatusMap = agentRows.toRowsMap("freadstatus");

            //总人数
            row.put("ftotalusercount", agentRows.size());
            //已学习人数
            if (freadstatusMap.containsKey("已学习")) {
                row.put("freadusercount", freadstatusMap.get("已学习").size());
            } else {
                row.put("freadusercount", 0);
            }

            RowsMap salerRowsMap = getUser(agentRows.toArray("fsaleruserid"));
            /**
             * 未学习列表
             */
            JSONArray unreadArray = new JSONArray();
            Rows unreadrows = freadstatusMap.get("未学习");
            Rows readrows = freadstatusMap.get("已学习");
            RowsMap unreadrowsMap = unreadrows.toRowsMap("fsaleruserid");
            RowsMap readrowsMap = readrows.toRowsMap("fsaleruserid");
            for (String fsaleruserid : unreadrowsMap.keySet()) {
                JSONObject object = new JSONObject();
                object.put("fsaler", salerRowsMap.get(fsaleruserid).get(0).getString("fname"));
                object.put("ftotalusercount", unreadrowsMap.get(fsaleruserid).size());
                object.put("fdoneusercount", readrowsMap.get(fsaleruserid).size());
                object.put("userlist", unreadrowsMap.get(fsaleruserid));
                unreadArray.add(object);
            }
            row.put("unreadmsg", unreadArray);
        }
        dbConnect.runSqlUpdate("update tarchives_sc set fvisitors=isnull(fvisitors,0)+1 where siteid='" + siteid + "' and tarchives_scid=" + content.getString("tarchives_scid"));

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

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

    /**
     * 商学院课件收藏
     *
     * @return
     */
    public String archivesCollection() throws P2Exception {
        long tarchives_scid = content.getLongValue("tarchives_scid");//商学院课件ID
        boolean iscollection = content.getBooleanValue("iscollection");//是否收藏，1收藏，0取消收藏


        PaoSetRemote tarchives_sc_collectionSet = getP2ServerSystemPaoSet("tarchives_sc_collection", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tarchives_scid='" + tarchives_scid + "'");
        PaoRemote tarchives_sc_collection = null;
        if (tarchives_sc_collectionSet.isEmpty() && iscollection) {
            tarchives_sc_collection = tarchives_sc_collectionSet.addAtEnd();
            tarchives_sc_collection.setValue("siteid", siteid, 11L);//企业ID
            tarchives_sc_collection.setValue("tenterprise_userid", userid, 11L);//用户ID
            tarchives_sc_collection.setValue("fcollectiondate", sysdate, 11L);//录入时间
            tarchives_sc_collection.setValue("tarchives_scid", tarchives_scid, 11L);
        } else if (!tarchives_sc_collectionSet.isEmpty() && !iscollection) {
            tarchives_sc_collectionSet.deleteAll();
        }
        tarchives_sc_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() {
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件阅读占比分析_经销商");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 商学院课件业务员阅读分析
     *
     * @return
     */
    public String read_analysis_saler() {
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件阅读占比分析_业务员");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 商学院课件经销商下载分析
     *
     * @return
     */
    public String download_analysis() {
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件下载占比分析_经销商");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 商学院课件业务员下载分析
     *
     * @return
     */
    public String download_analysis_saler() {
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件下载占比分析_业务员");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");
        String tarchives_scid = content.getString("tarchives_scid");

        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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_sc' and t1.ownerid='" + tarchives_scid + "' ");
        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");
        String tarchives_scid = content.getString("tarchives_scid");

        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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_sc' and t1.ownerid='" + tarchives_scid + "' ");
        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");
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");

        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");

        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }
}
