lvxinran 2021-05-18 11:27:12 +08:00
commit 2da1dd7c2d
6 changed files with 130 additions and 79 deletions

View File

@ -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<String, NoticeInfo> noticeInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.NOTICE, "", NoticeInfo.class, -1, false);
NoticeInfo send = noticeInfoMap.get("1");
ArrayList<NoticeInfo> list = new ArrayList<>();
Comparator<NoticeInfo> comparator = (o1, o2) -> {
if (o1.getStartTime() > o2.getStartTime()) {
return -1;
} else if (o1.getStartTime() == o2.getStartTime()) {
return 0;
} else {
return 1;
}
};
TreeSet<NoticeInfo> normalList = new TreeSet<>(comparator);
TreeSet<NoticeInfo> highList = new TreeSet<>(comparator);
Set<String> remove = new HashSet<>();
long localtime = System.currentTimeMillis() / 1000;
for (NoticeInfo noticeInfo : noticeInfoMap.values()) {
if (noticeInfo.getLeve() == 0)
continue;
try {
long start = noticeInfo.getStartTime() / 1000;
long end = noticeInfo.getEndTime() / 1000;
if (start > localtime) {
HashSet<String> 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;
}
//移除当前时间以前的
if (end < localtime) {
remove.add(noticeInfo.get_id());
// 已过期公告
if (now > end){
remove.add(String.valueOf(info.getId()));
continue;
}
// 包名不包含
if (!info.getPackageName().contains(packageName)){
continue;
}
// 最多展示十条
if (list.size() > 10){
break;
}
// 需要展示得公告列表
list.add(info);
}
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();
}
// 删除过期公告
for (String k : remove) {
BaseGlobal.getInstance().redisApp.hdel(RedisKey.NOTICE, "", k, false);
}
resMsg.setCode(0);
resMsg.setCode(0);
resMsg.setMsg("");
if (send != null) {
Map<String, String> parms = new HashMap<>();
parms.put("title", send.getTitle());
parms.put("content", send.getContent());
resMsg.setParms(parms);
}
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();
}
}

View File

@ -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);
}
}

View File

@ -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<String,String> parms;
private List<NoticeInfo> list;
public ResMsg() {
}
public int getCode() {
return code;
}
@ -33,4 +39,12 @@ public class ResMsg {
public void setParms(Map<String, String> parms) {
this.parms = parms;
}
public List<NoticeInfo> getList() {
return list;
}
public void setList(List<NoticeInfo> list) {
this.list = list;
}
}

View File

@ -13,7 +13,7 @@ public interface RedisKey {
/**
*
*/
String NOTICE = "NOTICE";
String NOTICE = "NOTICE_INFO";
String GLOBAL_SYS_PRO= "GLOBAL_SYS_PRO";
/**
* openId

View File

@ -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;
}
}

View File

@ -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">
<!-- 本地路劲需要加 jl_loginserver -->
<context-param>
<param-name>logbackConfigLocation</param-name>
@ -47,7 +47,7 @@
<servlet-mapping>
<servlet-name>getNotice</servlet-name>
<url-pattern>/getNotice</url-pattern>
<url-pattern>/jl_loginserver/getNotice</url-pattern>
</servlet-mapping>
<servlet>