From bf8b75448796f14f32d9db11753c7871b2a696de Mon Sep 17 00:00:00 2001 From: zhangshanxue Date: Wed, 9 Oct 2019 19:55:23 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E6=97=B6=E4=BC=98=E5=85=88=E7=BA=A7?= =?UTF-8?q?=E5=85=AC=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ljsd/controller/GetNoticeController.java | 91 +++++++++++++++++-- .../ljsd/controller/GetUserController.java | 12 +++ src/main/java/com/ljsd/pojo/NoticeInfo.java | 74 +++++++++++++++ .../java/com/ljsd/util/MyMongoDBPool.java | 12 +++ 4 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/ljsd/pojo/NoticeInfo.java diff --git a/src/main/java/com/ljsd/controller/GetNoticeController.java b/src/main/java/com/ljsd/controller/GetNoticeController.java index ff9ab6e..3c61896 100644 --- a/src/main/java/com/ljsd/controller/GetNoticeController.java +++ b/src/main/java/com/ljsd/controller/GetNoticeController.java @@ -1,6 +1,7 @@ package com.ljsd.controller; import com.google.gson.Gson; +import com.ljsd.pojo.NoticeInfo; import com.ljsd.pojo.ResMsg; import com.ljsd.util.BaseGlobal; import com.ljsd.util.MD5Util; @@ -13,9 +14,12 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Map; +import java.util.*; + +/** + * 常驻 中 高优先级 同等优先级按最新发布的 + */ public class GetNoticeController extends HttpServlet { private static Gson gson = new Gson(); @@ -39,16 +43,86 @@ public class GetNoticeController extends HttpServlet { out.print(gson.toJson(resMsg)); return; } + + + List notices = BaseGlobal.getInstance().mongoDBPool.findAll("notice_info"); + + + NoticeInfo send = new NoticeInfo(); DBObject notice = BaseGlobal.getInstance().mongoDBPool.findOne("notice_info", "1"); + if(notice!=null){ + Object content = notice.get("content"); + Object title = notice.get("title"); + send.setContent(content.toString()); + send.setTitle(title.toString()); + } + + Comparator comparator = (o1, o2) -> { + if (o1.getStartTime() > o2.getStartTime()) { + return -1; + } else if (o1.getStartTime()==o2.getStartTime()) { + return 0; + } else { + return 1; + } + }; + TreeSet normalList = new TreeSet<>(comparator); + TreeSet highList = new TreeSet<>(comparator); + + Set remove = new HashSet<>(); + long localtime = System.currentTimeMillis() / 1000; + for (DBObject dbObject:notices) { + String id = dbObject.get("_id").toString(); + if(id.equals("1")) + continue; + try { + NoticeInfo noticeInfo = new NoticeInfo(); + noticeInfo.set_id(id); + Object content = dbObject.get("content"); + Object title = dbObject.get("title"); + Object start_time = dbObject.get("start_time"); + Object end_time = dbObject.get("end_time"); + noticeInfo.setContent(content.toString()); + noticeInfo.setTitle(title.toString()); + + + long start = (long)start_time/1000; + long end = (long)end_time/1000; + if (start > localtime) { + continue; + } + //移除当前时间以前的 + if (end < localtime) { + remove.add(id); + continue; + } + + int leve = (int)dbObject.get("leve"); + if(leve==1) + normalList.add(noticeInfo); + else if(leve==2){ + highList.add(noticeInfo); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + if(highList.size()!=0){ + send = highList.last(); + }else { + if(normalList.size()!=0){ + send = normalList.last(); + } + } + resMsg.setCode(0); resMsg.setCode(0); resMsg.setMsg(""); - if(notice!=null){ + + if(send!=null){ Map parms = new HashMap<>(); - Object content = notice.get("content"); - Object title = notice.get("title"); - parms.put("title",title.toString()); - parms.put("content",content.toString()); + parms.put("title",send.getTitle()); + parms.put("content",send.getContent()); resMsg.setParms(parms); } out.print(gson.toJson(resMsg)); @@ -56,4 +130,7 @@ public class GetNoticeController extends HttpServlet { e.printStackTrace(); } } + + + } diff --git a/src/main/java/com/ljsd/controller/GetUserController.java b/src/main/java/com/ljsd/controller/GetUserController.java index bfbe9ea..5acfe45 100644 --- a/src/main/java/com/ljsd/controller/GetUserController.java +++ b/src/main/java/com/ljsd/controller/GetUserController.java @@ -59,6 +59,10 @@ public class GetUserController extends HttpServlet { return; } String admin = request.getParameter("admin"); //平台类型 + + String gid = request.getParameter("gid"); //gid + String pid = request.getParameter("pid"); //pid + /*if (StringUtils.checkIsEmpty(admin)) { response.sendError(400, "platform is empety"); return; @@ -86,6 +90,14 @@ public class GetUserController extends HttpServlet { dbObject.put("openId", openId); dbObject.put("serverId", serverId); dbObject.put("platform", platform); + if (gid != null && !gid.isEmpty()) { + dbObject.put("gid", gid); + } + + if (pid != null && !pid.isEmpty()) { + dbObject.put("pid", pid); + } + int uid = 0; List userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject); diff --git a/src/main/java/com/ljsd/pojo/NoticeInfo.java b/src/main/java/com/ljsd/pojo/NoticeInfo.java new file mode 100644 index 0000000..a8d05be --- /dev/null +++ b/src/main/java/com/ljsd/pojo/NoticeInfo.java @@ -0,0 +1,74 @@ +package com.ljsd.pojo; + + +import com.mongodb.DBObject; +import org.bson.BSONObject; + +import java.util.Map; +import java.util.Set; + +/** + * + * @author Administrator + * @date 2015/8/13 + */ + +public class NoticeInfo { + + + private String _id; + private String content; + private String title; + private int leve; + private long startTime; + private long endTime; + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String get_id() { + return _id; + } + + public void set_id(String _id) { + this._id = _id; + } + + public int getLeve() { + return leve; + } + + public void setLeve(int leve) { + this.leve = leve; + } + + public long getStartTime() { + return startTime; + } + + public void setStartTime(long startTime) { + this.startTime = startTime; + } + + public long getEndTime() { + return endTime; + } + + public void setEndTime(long endTime) { + this.endTime = endTime; + } + +} diff --git a/src/main/java/com/ljsd/util/MyMongoDBPool.java b/src/main/java/com/ljsd/util/MyMongoDBPool.java index 17c639b..81c6250 100644 --- a/src/main/java/com/ljsd/util/MyMongoDBPool.java +++ b/src/main/java/com/ljsd/util/MyMongoDBPool.java @@ -41,6 +41,18 @@ public class MyMongoDBPool { } + public List findAll(String collectionName) throws Exception { + DBCollection coll = db.getCollection(collectionName); + DBCursor cursor = coll.find(); + List docs = new ArrayList<>(); + + while (cursor.hasNext()) { + DBObject doc = cursor.next(); + docs.add(doc); + } + return docs; + } + public DBObject findOne(String collectionName, String id) throws Exception { return findOne2(collectionName, id); }