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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
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;

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("or t1.fclass1 like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fclass2 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("'");
            }
        }
        SQLFactory factory = new SQLFactory(this, "商学院课件列表查询", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());

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

    /**
     * 商学院课件主界面查询
     *
     * @return
     */
    public String query_archivesMain() {
        String tarchives_scid = content.getString("tarchives_scid");
        SQLFactory sqlFactory = new SQLFactory(this, "商学院课件主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        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);
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 商学院课件新增或修改
     *
     * @return
     */
    public String archivesInsertOrModify() throws D3bException, P2Exception {
        long tarchives_scid = content.getLongValue("tarchives_scid");//商学院课件ID，新增时默认传0
        if (tarchives_scid > 0) {
            if (dbConnect.runSqlQuery("select * from tarchives_sc where siteid='" + siteid + "' and fstatus='新建' and tarchives_scid=" + tarchives_scid).isEmpty()) {
                return getErrReturnObject().setErrMsg("当前状态不可修改！").toString();
            }
        }
        String fclass1 = content.getString("fclass1");//一集类别
        String fclass2 = content.getString("fclass2");//二集类别
        String ftitle = content.getString("ftitle", "tarchives_sc.ftitle", "标题");//商学院课件标题
        String fnotes = content.getString("fnotes", "tarchives_sc.fnotes", "备注");//商学院课件备注
        boolean fissecret = content.getBooleanValue("fissecret");//不允许下载
        String fenddate = nullToStr(content.getString("fenddate"));//课件有效期
        boolean fsalercanread = content.getBooleanValue("fsalercanread");//业务员是否可见
        boolean fagentcanread = content.getBooleanValue("fagentcanread");//经销商住账号是否可见
        boolean fagentsalercanread = content.getBooleanValue("fagentsalercanread");//经销商子账号是否可见
        JSONArray tag = content.getJSONArray("tag");//标签

        PaoSetRemote tarchives_scSet = getP2ServerSystemPaoSet("tarchives_sc", "siteid='" + siteid + "' and tarchives_scid='" + tarchives_scid + "'");
        PaoRemote tarchives_sc = null;
        if (tarchives_scid <= 0 || tarchives_scSet.isEmpty()) {
            tarchives_sc = tarchives_scSet.addAtEnd();
            tarchives_sc.setValue("siteid", siteid, 11L);//企业ID
            tarchives_sc.setValue("tenterprise_userid", userid, 11L);//用户ID
            tarchives_sc.setValue("createby", username, 11L);//录入人
            tarchives_sc.setValue("createdate", sysdate, 11L);//录入时间
            tarchives_sc.setValue("fstatus", "新建", 11L);//商学院课件状态
        } else {
            tarchives_sc = tarchives_scSet.getPao(0);
        }
        tarchives_sc.setValue("fclass1", fclass1, 11L);
        tarchives_sc.setValue("fclass2", fclass2, 11L);
        tarchives_sc.setValue("ftitle", ftitle, 11L);
        tarchives_sc.setValue("fnotes", fnotes, 11L);
        tarchives_sc.setValue("fissecret", fissecret, 11L);
        tarchives_sc.setValue("fenddate", fenddate, 11L);
        tarchives_sc.setValue("fsalercanread", fsalercanread, 11L);
        tarchives_sc.setValue("fagentcanread", fagentcanread, 11L);
        tarchives_sc.setValue("fagentsalercanread", fagentsalercanread, 11L);
        tarchives_sc.setValue("changeby", username);//修改人
        tarchives_sc.setValue("changedate", sysdate);//修改时间
        /**
         * 标签
         */
        dbConnect.runSqlUpdate("delete from tarchives_sc_tag where siteid='" + siteid + "' and tarchives_scid=" + tarchives_scid);
        PaoSetRemote tarchives_sc_tagSet = tarchives_sc.getPaoSet("$tarchives_sc_tag", "tarchives_sc_tag");
        for (Object o : tag) {
            String flable = (String) o;
            PaoRemote tarchives_sc_tag = tarchives_sc_tagSet.addAtEnd();
            tarchives_sc_tag.setValue("siteid", siteid, 11L);
            tarchives_sc_tag.setValue("flable", flable, 11L);
            tarchives_sc_tag.setValue("tarchives_scid", tarchives_scid, 11L);
        }
        content.put("tarchives_scid", tarchives_sc.getString("tarchives_scid"));
        saveDataLog(tarchives_sc);
        tarchives_scSet.save();

        return query_archivesMain();
    }

    /**
     * 商学院课件审核
     *
     * @return
     */
    public String archivesCheck() {
        long tarchives_scid = content.getLongValue("tarchives_scid");
        if (!dbConnect.runSqlQuery("select * from tarchives_sc where siteid='" + siteid + "' and fstatus='发布' and tarchives_scid=" + tarchives_scid).isEmpty()) {
            return getErrReturnObject().setErrMsg("商学院课件已发布，不可重复发布！").toString();
        }
        SQLFactory factory = new SQLFactory(this, "商学院课件发布");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        factory.addParameter("checkby", username);
        String status = dbConnect.runSqlUpdate(factory.getSQL());
        if (status.equals("true")) {
            SQLFactory sqlFactory = new SQLFactory(this, "商学院课件主界面查询");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tarchives_scid", content.getString("tarchives_scid"));
            Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
            if (!rows.isEmpty()) {
                String fclass1 = rows.get(0).getString("fclass1");
                String fclass2 = rows.get(0).getString("fclass2");
                String ftitle = rows.get(0).getString("ftitle");
                long tagentsid = rows.get(0).getLong("tagentsid");

                SQLFactory msgfac = new SQLFactory(this, "消息插入");
                msgfac.addParameter("siteid", siteid);
                msgfac.addParameter("objectname", "tarchives_sc");
                msgfac.addParameter("objectid", tarchives_scid);
                msgfac.addParameter("fmessage", "新发布了一条【" + fclass1 + "-" + fclass2 + "】类商学院课件：《" + ftitle + "》");
                dbConnect.runSqlUpdate(msgfac.getSQL());
            }
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }


    /**
     * 商学院课件取消发布
     *
     * @return
     */
    public String archivesUnCheck() {
        String tarchives_scid = content.getString("tarchives_scid");//商学院课件ID，新增时默认传0
        if (dbConnect.runSqlQuery("select * from tarchives_sc where siteid='" + siteid + "' and fstatus='发布' and tarchives_scid=" + tarchives_scid).isEmpty()) {
            return getErrReturnObject().setErrMsg("商学院课件不在发布状态，不可取消发布！").toString();
        }
        SQLFactory factory = new SQLFactory(this, "商学院课件取消发布");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        String status = dbConnect.runSqlUpdate(factory.getSQL());
        if (status.equals("true")) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).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 archiveDelete() {
        long tarchives_scid = content.getLongValue("tarchives_scid");
        if (tarchives_scid > 0) {
            if (dbConnect.runSqlQuery("select * from tarchives_sc where siteid='" + siteid + "' and fstatus='新建' and tarchives_scid=" + tarchives_scid).isEmpty()) {
                return getErrReturnObject().setErrMsg("当前状态不可删除！").toString();
            }
        }
        SQLFactory sqlFactory = new SQLFactory(this, "商学院课件删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tarchives_scid", tarchives_scid);
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }


    /**
     * 商学院课件经销商阅读分析
     *
     * @return
     */
    public String read_analysis() {
        long tarchives_scid = content.getLongValue("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件阅读占比分析_经销商");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

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

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

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

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

        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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");
        long tarchives_scid = content.getLongValue("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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_scid = content.getLongValue("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件未下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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_scid = content.getLongValue("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        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_scid = content.getLongValue("tarchives_scid");
        SQLFactory factory = new SQLFactory(this, "分析_商学院课件已下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tarchives_scid", tarchives_scid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }
}
