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

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.actionscore.ActionScore;
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.Arrays;
import java.util.Calendar;
import java.util.List;

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

    /**
     * 公告列表查询
     *
     * @return
     */
    public String query_noticeList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fisontop desc,t1.checkdate desc"};
        String sort = getSort(sortfield, "t1.fisontop desc,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.ftype like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fsummary like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory noticehead = new SQLFactory(this, "公告列表查询", pageSize, pageNumber, sort);
        noticehead.addParameter("siteid", siteid);
        noticehead.addParameter("tenterprise_userid", userid);
        noticehead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(noticehead.getSQL());
        ArrayList<String> tnoticeidlist = rows.toArrayList("tnoticeid");

        SQLFactory noticeisread = new SQLFactory(this, "公告列表是否已读查询");
        noticeisread.addParameter("siteid", siteid);
        noticeisread.addParameter("tenterprise_userid", userid);
        noticeisread.addParameter_in("tnoticeid", tnoticeidlist);
        List tnoticeids = Arrays.asList(dbConnect.runSqlQuery(noticeisread.getSQL()).toArray("tnoticeid"));


        SQLFactory noticereadcount = new SQLFactory(this, "公告阅读量查询");
        noticereadcount.addParameter("siteid", siteid);
        noticereadcount.addParameter_in("tnoticeid", tnoticeidlist);
        RowsMap noticereadcountMap = dbConnect.runSqlQuery(noticereadcount.getSQL()).toRowsMap("tnoticeid");

        RowsMap tattachmentcountcountMap = getAttachmentQty("tnotice", tnoticeidlist);

        for (Row row : rows) {
            String tnoticeid = row.getString("tnoticeid");
            /**
             * 是否已读
             */
            if (tnoticeids.contains(tnoticeid)) {
                row.put("fisread", 1);
            } else {
                row.put("fisread", 0);
            }
            /**
             * 阅读量查询
             */
            if (noticereadcountMap.containsKey(tnoticeid)) {
                row.put("freadcount", noticereadcountMap.get(tnoticeid).get(0).getInteger("freadcount"));
            } else {
                row.put("freadcount", 0);
            }

            /**
             * 附件数量查询
             */
            if (tattachmentcountcountMap.containsKey(tnoticeid)) {
                row.put("fattachmentcount", tattachmentcountcountMap.get(tnoticeid).get(0).getInteger("fattachmentcount"));
            } else {
                row.put("fattachmentcount", 0);
            }
        }
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 公告主界面查询(公告阅读)
     *
     * @return
     */
    public String query_noticeMain() throws P2Exception {
        long tnoticeid = content.getLongValue("tnoticeid");
        SQLFactory noticehead = new SQLFactory(this, "公告主界面查询");
        noticehead.addParameter("siteid", siteid);
        noticehead.addParameter("tnoticeid", tnoticeid);
        Rows rows = dbConnect.runSqlQuery(noticehead.getSQL());

        if (!rows.isEmpty()) {
            Row row = rows.get(0);
            /**
             * 公告阅读量
             */
            SQLFactory noticereadcount = new SQLFactory(this, "公告阅读量查询");
            noticereadcount.addParameter("siteid", siteid);
            noticereadcount.addParameter_in("tnoticeid", String.valueOf(tnoticeid));
            Rows noticereadcountRows = dbConnect.runSqlQuery(noticereadcount.getSQL());
            if (!noticereadcountRows.isEmpty()) {
                row.put("freadcount", noticereadcountRows.get(0).getInteger("freadcount") + 1);
            } else {
                row.put("freadcount", 1);
            }

            /**
             * 评分及留言
             */
            SQLFactory noticeleavemessage = new SQLFactory(this, "公告评分及留言查询");
            noticeleavemessage.addParameter("siteid", siteid);
            noticeleavemessage.addParameter("tnoticeid", tnoticeid);
            noticeleavemessage.addParameter("tenterprise_userid", userid);
            Rows noticeleavemessageRows = dbConnect.runSqlQuery(noticeleavemessage.getSQL());
            if (!noticeleavemessageRows.isEmpty()) {
                row.put("fleavemessage", noticeleavemessageRows.get(0).getString("fleavemessage"));
                row.put("fscore", noticeleavemessageRows.get(0).getInteger("fscore"));
            } else {
                row.put("fleavemessage", "");
                row.put("fscore", 0);
            }
        }
        PaoSetRemote tnotice_userdataSet = getP2ServerSystemPaoSet("tnotice_userdata");
        PaoRemote tnotice_userdata = tnotice_userdataSet.addAtEnd();
        tnotice_userdata.setValue("siteid", siteid, 11L);
        tnotice_userdata.setValue("tnoticeid", tnoticeid, 11L);
        tnotice_userdata.setValue("tenterprise_userid", userid, 11L);
        tnotice_userdata.setValue("createby", username, 11L);
        Calendar calendar = Calendar.getInstance();
        tnotice_userdata.setValue("createdate", calendar.getTime(), 11L);
        calendar.add(Calendar.MINUTE, 5);
        tnotice_userdata.setValue("closedate", calendar.getTime(), 11L);
        tnotice_userdataSet.save();
        dbConnect.runSqlUpdate("update tnotice_users set flastreadtime=getdate() where siteid='" + siteid + "' and tnoticeid='" + tnoticeid + "' and tenterprise_userid='" + userid + "'");
        addActionScore(ActionScore.fsourcetype.阅读通告, tnoticeid, "阅读通告");

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

    public String noticeexit() {
        String tnoticeid = content.getString("tnoticeid");
        dbConnect.runSqlUpdate("update top(1) tnotice_userdata set closedate=getdate() where siteid='" + siteid + "' and tnoticeid='" + tnoticeid + "' and tenterprise_userid='" + userid + "'");
        return getSucReturnObject().toString();
    }

    /**
     * 公告评分及留言
     *
     * @return
     */
    public String notice_returnMessage() throws P2Exception {
        long tnoticeid = content.getLongValue("tnoticeid");
        String fleavemessage = content.getString("fleavemessage");
        int fscore = content.getIntValue("fscore");
        /**
         * 评分留言
         */
        PaoSetRemote tnotice_usersSet = getP2ServerSystemPaoSet("tnotice_users", "siteid='" + siteid + "' and tnoticeid='" + tnoticeid + " and tenterprise_userid='" + userid + "'");
        if (!tnotice_usersSet.isEmpty()) {
            PaoRemote tnotice_users = tnotice_usersSet.getPao(0);
            tnotice_users.setValue("fleavemessage", fleavemessage, 11L);
            tnotice_users.setValue("fscore", fscore, 11L);
            tnotice_users.setValue("changeby", username, 11L);
            tnotice_users.setValue("changedate", getDateTime(), 11L);
        } else {
            PaoRemote tnotice_users = tnotice_usersSet.addAtEnd();
            tnotice_users = tnotice_usersSet.getPao(0);
            tnotice_users.setValue("siteid", siteid);
            tnotice_users.setValue("tenterprise_userid", userid);
            tnotice_users.setValue("tnoticeid", tnoticeid);
            tnotice_users.setValue("fleavemessage", fleavemessage, 11L);
            tnotice_users.setValue("fscore", fscore, 11L);
            tnotice_users.setValue("changeby", username, 11L);
            tnotice_users.setValue("changedate", getDateTime(), 11L);
        }
        tnotice_usersSet.save();
        addActionScore(ActionScore.fsourcetype.通告评分, tnoticeid, "通告评分");
        return getSucReturnObject().toString();
    }


    /**
     * 公告主界面评分及留言查询
     *
     * @return
     */
    public String query_noticeLeavemessageList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate"};
        String sort = getSort(sortfield, "t1.createdate desc");

        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("fscore")) {
                where.append(" and(");
                where.append("t1.fscore ='").append(whereObject.getString("fscore")).append("' ");
                where.append(")");
            }
        }

        SQLFactory noticeleavemessage = new SQLFactory(this, "公告评分及留言列表查询", pageSize, pageNumber, sort);
        noticeleavemessage.addParameter("siteid", siteid);
        noticeleavemessage.addParameter("tnoticeid", content.getString("tnoticeid"));
        noticeleavemessage.addParameter_SQL("where", where);
        noticeleavemessage.addParameter_in("tenterprise_userid", getSubUsers());
        Rows noticeleavemessageRows = dbConnect.runSqlQuery(noticeleavemessage.getSQL());
        return getSucReturnObject().setDataByPaging(noticeleavemessageRows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 公告阅读分析
     *
     * @return
     */
    public String read_analysis() {
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告经销商阅读占比分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告业务员阅读占比分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 公告下载分析
     *
     * @return
     */
    public String download_analysis() {
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告经销商下载占比分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告业务员下载占比分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 公告评分分析
     *
     * @return
     */
    public String score_analysis() {
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告经销商评分分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 公告评分分析
     *
     * @return
     */
    public String score_analysis_saler() {
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告业务员评分分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 公告评分名单
     *
     * @return
     */
    public String scorelist_analysis() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告评分列表_经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 公告评分名单
     *
     * @return
     */
    public String scorelist_analysis_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告评分列表_业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        factory.addParameter_in("tenterprise_userid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 未浏览用户列表
     *
     * @return
     */
    public String havenoreaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告未浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告未浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");

        SQLFactory factory = new SQLFactory(this, "分析_公告已浏览经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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='tnotice' and t1.ownerid='" + tnoticeid + "' ");
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");

        SQLFactory factory = new SQLFactory(this, "分析_公告已浏览业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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='tnotice' and t1.ownerid='" + tnoticeid + "' ");
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告未下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告未下载业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");

        String tnoticeid = content.getString("tnoticeid");
        SQLFactory factory = new SQLFactory(this, "分析_公告已下载经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tnoticeid", tnoticeid);
        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 = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");

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