package com.cnd3b.restcontroller.enterprise.agentwebclient.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.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("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("'");
            }
            if (whereObject.containsKey("fmycollection") && whereObject.getBooleanValue("fmycollection")) {
                where.append(" and t2.tarchives_sc_colid is not null");
            }
            where.append(" and t1.fstatus='发布'");
            if (fisadministrator) {
                where.append(" and t1.fagentcanread=1");
            } else {
                where.append(" and t1.fagentsalercanread=1");
            }
        }
        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);
        }
        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();
    }

}
