diff --git a/src/main/java/com/ljsd/controller/GetNoticeController.java b/src/main/java/com/ljsd/controller/GetNoticeController.java index ead4d78..c5dd192 100644 --- a/src/main/java/com/ljsd/controller/GetNoticeController.java +++ b/src/main/java/com/ljsd/controller/GetNoticeController.java @@ -8,6 +8,7 @@ import com.ljsd.redis.RedisKey; import com.ljsd.util.BaseGlobal; import com.ljsd.util.MD5Util; import com.ljsd.util.StringUtils; +import com.ljsd.util.TimeUtil; import com.mongodb.DBObject; import javax.servlet.ServletException; @@ -26,14 +27,16 @@ public class GetNoticeController extends HttpServlet { private static Gson gson = new Gson(); @Override - protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(HttpServletRequest req, HttpServletResponse response){ response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); String timestamp = req.getParameter("timestamp"); String sign = req.getParameter("sign"); + String packageName = req.getParameter("packageName"); + ResMsg resMsg = new ResMsg(); resMsg.setCode(1); - try (PrintWriter out = response.getWriter();) { + try (PrintWriter out = response.getWriter()) { if (StringUtils.checkIsEmpty(timestamp) || StringUtils.checkIsEmpty(sign)) { resMsg.setMsg("参数不能为空"); out.print(gson.toJson(resMsg)); @@ -45,70 +48,54 @@ public class GetNoticeController extends HttpServlet { out.print(gson.toJson(resMsg)); return; } + if ("".equals(packageName) || packageName == null){ + resMsg.setMsg("包名不能为空"); + out.print(gson.toJson(resMsg)); + return; + } Map noticeInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.NOTICE, "", NoticeInfo.class, -1, false); - NoticeInfo send = noticeInfoMap.get("1"); + ArrayList list = new ArrayList<>(); - 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 (NoticeInfo noticeInfo : noticeInfoMap.values()) { - if (noticeInfo.getLeve() == 0) + HashSet remove = new HashSet<>(); + for (NoticeInfo info : noticeInfoMap.values()) { + // 获取时间 + long now = TimeUtil.nowInt(); + long start = TimeUtil.timeStrToSecond(info.getStartTime()); + long end = TimeUtil.timeStrToSecond(info.getEndTime()); + // 未开启公告 + if (start > now){ continue; - try { - - long start = noticeInfo.getStartTime() / 1000; - long end = noticeInfo.getEndTime() / 1000; - if (start > localtime) { - continue; - } - //移除当前时间以前的 - if (end < localtime) { - remove.add(noticeInfo.get_id()); - continue; - } - - int leve =noticeInfo.getLeve(); - 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(); + // 已过期公告 + if (now > end){ + remove.add(String.valueOf(info.getId())); + continue; } + // 包名不包含 + if (!info.getPackageName().contains(packageName)){ + continue; + } + // 最多展示十条 + if (list.size() > 10){ + break; + } + // 需要展示得公告列表 + list.add(info); } - resMsg.setCode(0); - resMsg.setCode(0); - resMsg.setMsg(""); - if (send != null) { - Map parms = new HashMap<>(); - parms.put("title", send.getTitle()); - parms.put("content", send.getContent()); - resMsg.setParms(parms); + // 删除过期公告 + for (String k : remove) { + BaseGlobal.getInstance().redisApp.hdel(RedisKey.NOTICE, "", k, false); } + + resMsg.setCode(0); + resMsg.setList(list); + System.out.println("1==="+gson.toJson(resMsg)); out.print(gson.toJson(resMsg)); } catch (Exception e) { + System.out.println("2==="+e.getMessage()); e.printStackTrace(); } } diff --git a/src/main/java/com/ljsd/pojo/NoticeInfo.java b/src/main/java/com/ljsd/pojo/NoticeInfo.java index a8d05be..9df05ec 100644 --- a/src/main/java/com/ljsd/pojo/NoticeInfo.java +++ b/src/main/java/com/ljsd/pojo/NoticeInfo.java @@ -1,11 +1,6 @@ package com.ljsd.pojo; - -import com.mongodb.DBObject; -import org.bson.BSONObject; - -import java.util.Map; -import java.util.Set; +import java.util.Objects; /** * @@ -15,13 +10,33 @@ import java.util.Set; public class NoticeInfo { + private long id; - private String _id; private String content; + private String title; - private int leve; - private long startTime; - private long endTime; + + private String packageName; + + /** + * 1 紧急 + * 2 最新 + * 3 最热 + * 4 常规 + */ + private int type; + + private String startTime; + + private String endTime; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } public String getContent() { return content; @@ -39,36 +54,48 @@ public class NoticeInfo { this.title = title; } - public String get_id() { - return _id; + public String getPackageName() { + return packageName; } - public void set_id(String _id) { - this._id = _id; + public void setPackageName(String packageName) { + this.packageName = packageName; } - public int getLeve() { - return leve; + public int getType() { + return type; } - public void setLeve(int leve) { - this.leve = leve; + public void setType(int type) { + this.type = type; } - public long getStartTime() { + public String getStartTime() { return startTime; } - public void setStartTime(long startTime) { + public void setStartTime(String startTime) { this.startTime = startTime; } - public long getEndTime() { + public String getEndTime() { return endTime; } - public void setEndTime(long endTime) { + public void setEndTime(String endTime) { this.endTime = endTime; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + NoticeInfo that = (NoticeInfo) o; + return Objects.equals(id, that.id); + } + + @Override + public int hashCode() { + return Objects.hash(id, content, title, packageName, type, startTime, endTime); + } } diff --git a/src/main/java/com/ljsd/pojo/ResMsg.java b/src/main/java/com/ljsd/pojo/ResMsg.java index 917a748..db0b50f 100644 --- a/src/main/java/com/ljsd/pojo/ResMsg.java +++ b/src/main/java/com/ljsd/pojo/ResMsg.java @@ -1,5 +1,6 @@ package com.ljsd.pojo; +import java.util.List; import java.util.Map; public class ResMsg { @@ -10,6 +11,11 @@ public class ResMsg { private Map parms; + private List list; + + public ResMsg() { + } + public int getCode() { return code; } @@ -33,4 +39,12 @@ public class ResMsg { public void setParms(Map parms) { this.parms = parms; } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } } diff --git a/src/main/java/com/ljsd/redis/RedisKey.java b/src/main/java/com/ljsd/redis/RedisKey.java index eaafeeb..dccc57d 100644 --- a/src/main/java/com/ljsd/redis/RedisKey.java +++ b/src/main/java/com/ljsd/redis/RedisKey.java @@ -13,7 +13,7 @@ public interface RedisKey { /** * 公告列表 */ - String NOTICE = "NOTICE"; + String NOTICE = "NOTICE_INFO"; String GLOBAL_SYS_PRO= "GLOBAL_SYS_PRO"; /** * 根据openId 封号 diff --git a/src/main/java/com/ljsd/util/TimeUtil.java b/src/main/java/com/ljsd/util/TimeUtil.java index 76a3abb..108c624 100644 --- a/src/main/java/com/ljsd/util/TimeUtil.java +++ b/src/main/java/com/ljsd/util/TimeUtil.java @@ -74,4 +74,27 @@ public class TimeUtil { int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; return (week == 0) ? 7 : week; } + + /** + * 字符串 时间格式 转秒时间戳 + * @param date + * @return + */ + public static long timeStrToSecond(String date){ + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return sdf.parse(date).getTime()/1000; + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + + /** + * 获取当前时间,秒 + * @return + */ + public static long nowInt(){ + return System.currentTimeMillis()/1000; + } } \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 7b27785..702862c 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> - + logbackConfigLocation @@ -47,7 +47,7 @@ getNotice - /getNotice + /jl_loginserver/getNotice