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

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;
import java.util.Date;
import java.util.Iterator;

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_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(noticehead.getSQL());
        String[] tnoticeids = rows.toArray("tnoticeid");

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

        SQLFactory tattachmentcount = new SQLFactory(this, "附件数量查询");
        tattachmentcount.addParameter("siteid", siteid);
        tattachmentcount.addParameter_in("tnoticeid", tnoticeids);
        RowsMap tattachmentcountcountMap = dbConnect.runSqlQuery(tattachmentcount.getSQL()).toRowsMap("tnoticeid");


        for (Row row : rows) {
            String tnoticeid = row.getString("tnoticeid");
            /**
             * 阅读量查询
             */
            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() {
        String tnoticeid = content.getString("tnoticeid");
        SQLFactory noticehead = new SQLFactory(this, "公告主界面查询");
        noticehead.addParameter("siteid", siteid);
        noticehead.addParameter("tnoticeid", tnoticeid);
        Rows rows = dbConnect.runSqlQuery(noticehead.getSQL());

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

        if (!rows.isEmpty()) {
            /**
             * 公告阅读量
             */
            if (!noticereadcountRows.isEmpty()) {
                rows.get(0).put("freadcount", noticereadcountRows.get(0).getInteger("freadcount") + 1);
            } else {
                rows.get(0).put("freadcount", 1);
            }
        }
        return getSucReturnObject().setData(rows).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);
        Rows noticeleavemessageRows = dbConnect.runSqlQuery(noticeleavemessage.getSQL());
        return getSucReturnObject().setDataByPaging(noticeleavemessageRows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    public String noticeontop() {
        String tnoticeid = content.getString("tnoticeid");
        String fisontop = content.getString("fisontop");//是否置顶
        String status = dbConnect.runSqlUpdate("update tnotice set fisontop=" + fisontop + ", changeby='" + username + "',changedate=getdate() where siteid='" + siteid + "' and tnoticeid='" + tnoticeid + "'");
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 公告新增、修改
     *
     * @return
     */
    public String insertOrModify() throws D3bException, P2Exception {
        long tnoticeid = content.getLongValue("tnoticeid"); //新增时默认传0
        String ftype = content.getString("ftype");//公告类型
        String ftitle = content.getString("ftitle", "tnotice.ftitle", "标题");//标题
        String fsummary = content.getString("fsummary", "tnotice.fsummary", "通告导语");//概述、导读
        String fcontent = content.getString("fcontent", "tnotice.fcontent", "正文");//公告内容
        boolean fissecret = content.getBooleanValue("fissecret");//是否私密
        boolean fisontop = content.getBooleanValue("fisontop");//是否置顶
        Date fbegdate = content.getDate("fbegdate");//开始时间
        Date fenddate = content.getDate("fenddate");//结束时间

        PaoSetRemote tnoticeSet = getP2ServerSystemPaoSet("tnotice", "siteid='" + siteid + "' and tnoticeid='" + tnoticeid + "'");
        PaoRemote tnotice = null;
        if (tnoticeid <= 0 || tnoticeSet.isEmpty()) {
            tnotice = tnoticeSet.addAtEnd();
            tnotice.setValue("siteid", siteid, 11L);//企业ID
            tnotice.setValue("createby", username, 11L);//录入人
            tnotice.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tnotice = tnoticeSet.getPao(0);
        }
        tnotice.setValue("ftype", ftype, 11L);//公告类型
        tnotice.setValue("ftitle", ftitle, 11L);//标题
        tnotice.setValue("fsummary", fsummary, 11L);//概述、导读
        tnotice.setValue("fcontent", fcontent, 11L);//公告内容
        tnotice.setValue("fissecret", fissecret, 11L);//是否私密
        tnotice.setValue("fisontop", fisontop, 11L);//是否置顶
        tnotice.setValue("fbegdate", fbegdate, 11L);//开始时间
        tnotice.setValue("fenddate", fenddate, 11L);//结束时间
        tnotice.setValue("changeby", username, 11L);//修改人
        tnotice.setValue("changedate", sysdate, 11L);//修改时间
        content.put("tnoticeid", tnotice.getUniqueIDValue());
        saveDataLog(tnotice);
        tnoticeSet.save();
        return query_noticeMain();
    }

    /**
     * 发布
     *
     * @return
     */
    public String check() {
        String tnoticeid = content.getString("tnoticeid");
        boolean fischeck = content.getBooleanValue("fischeck");//是否发布
        SQLFactory sqlFactory = null;
        if (fischeck) {
            sqlFactory = new SQLFactory(this, "公告发布");
            sqlFactory.addParameter("checkby", username);
        } else {
            sqlFactory = new SQLFactory(this, "公告取消发布");
        }
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tnoticeid", tnoticeid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 公告删除
     *
     * @return
     */
    public String delete() {
        long tnoticeid = content.getLongValue("tnoticeid");
        if (dbConnect.runSqlQuery("select * from tnotice where siteid='" + siteid + "' and fstatus='新建' and tnoticeid=" + tnoticeid).isEmpty()) {
            return getErrReturnObject().setErrMsg("当前状态不可删除").toString();
        }
        SQLFactory sqlFactory = new SQLFactory(this, "公告删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tnoticeid", tnoticeid);

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }


    /**
     * 查询公告人员
     *
     * @return
     */
    public String query_noticeuserList() {
        String tnoticeid = content.getString("tnoticeid");
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tnotice_usersid"};
        String sort = getSort(sortfield, "t1.tnotice_usersid");
        /**
         * 过滤条件设置
         */
        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("t2.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("t3.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fusertype") && !"".equals(whereObject.getString("fusertype"))) {
                where.append(" and(");
                where.append("t2.fusertype ='").append(whereObject.getString("fusertype")).append("' ");
                where.append(")");
            }
        }

        SQLFactory sqlFactory = new SQLFactory(this, "查询公告人员", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tnoticeid", tnoticeid);
        sqlFactory.addParameter_SQL("where", where);

        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 添加公告人员
     *
     * @return
     */
    public String adduser() {
        String tnoticeid = content.getString("tnoticeid");
        JSONArray idarray = content.getJSONArray("tenterprise_userid");
        Iterator<Object> it = idarray.iterator();
        ArrayList<String> list = new ArrayList<>();
        while (it.hasNext()) {
            SQLFactory sqlFactory = new SQLFactory(this, "添加公告人员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tenterprise_userid", (String) it.next());
            list.add(sqlFactory.getSQL());
        }
        if (!list.isEmpty()) {
            String status = dbConnect.runSqlUpdate(list);
            if ("true".equals(status)) {
                return getSucReturnObject().toString();
            } else {
                return getErrReturnObject().setErrMsg(status).toString();
            }
        }
        return getErrReturnObject().setErrMsg("没有选择添加人员").toString();
    }

    /**
     * 添加公告人员组
     *
     * @return
     */
    public String addusergroup() {
        String tnoticeid = content.getString("tnoticeid");
        long tusergroupid = content.getLongValue("tusergroupid");
        SQLFactory sqlFactory = null;
        if (tusergroupid == 1) {
            sqlFactory = new SQLFactory(this, "添加公告人员组_所有业务员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        } else if (tusergroupid == 2) {
            sqlFactory = new SQLFactory(this, "添加公告人员组_所有经销商主账号");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        } else if (tusergroupid == 3) {
            sqlFactory = new SQLFactory(this, "添加公告人员组_所有经销商员工账号");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        } else {
            sqlFactory = new SQLFactory(this, "添加公告人员组");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);

        }
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 删除提报人员
     *
     * @return
     */
    public String deleteuser() {
        String tnoticeid = content.getString("tnoticeid");
        JSONArray idarray = content.getJSONArray("tenterprise_userid");
        Iterator<Object> it = idarray.iterator();
        ArrayList<String> list = new ArrayList<>();
        while (it.hasNext()) {
            SQLFactory sqlFactory = new SQLFactory(this, "删除公告人员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tnoticeid", tnoticeid);
            sqlFactory.addParameter("tenterprise_userid", (String) it.next());
            list.add(sqlFactory.getSQL());
        }
        if (!list.isEmpty()) {
            String status = dbConnect.runSqlUpdate(list);
            if ("true".equals(status)) {
                return getSucReturnObject().toString();
            } else {
                return getErrReturnObject().setErrMsg(status).toString();
            }
        }
        return getErrReturnObject().setErrMsg("没有选择删除人员").toString();
    }

    /**
     * 公告分析
     *
     * @return
     */
    public String analysis() {
        String sort = content.getString("sort");
        if ("评分均值".equals(sort)) {
            sort = "AVG(t3.fscore) desc";
        } else if ("浏览数".equals(sort)) {
            sort = "count(distinct t2.tenterprise_userid) desc";
        } else if ("下载数".equals(sort)) {
            sort = "t4.fdownloadcount desc";
        } else if ("评分人数".equals(sort)) {
            sort = "count(distinct t3.tenterprise_userid) desc";
        } else if ("评分率".equals(sort)) {
            sort = "count(distinct t3.tenterprise_userid)/case when count(distinct t2.tenterprise_userid)=0 then 1 else count(distinct t2.tenterprise_userid) end desc";
        } else {
            sort = "count(distinct t2.tenterprise_userid) desc";
        }
        String where = " 1=1 ";
        String ftype = content.getString("ftype");
        if (!"".equals(ftype) && !"全部".equals(ftype)) {
            where = where + " and t1.ftype='" + ftype + "'";
        }
        String ftime = content.getString("ftime");
        if ("本年".equals(ftime)) {
            where = where + " and DATEPART(YEAR,t1.checkdate)=DATEPART(YEAR,getdate()) ";
        } else if ("本季".equals(ftime)) {
            where = where + " and DATEPART(QUARTER,t1.checkdate)=DATEPART(QUARTER,getdate()) ";
        } else if ("本月".equals(ftime)) {
            where = where + " and DATEPART(MONTH,t1.checkdate)=DATEPART(MONTH,getdate()) ";
        } else if ("本周".equals(ftime)) {
            where = where + " and DATEPART(WEEK,t1.checkdate)=DATEPART(WEEK,getdate()) ";
        }

        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).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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        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);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }
}
