提交 28a1a95f 编写于 作者: tangyaolong's avatar tangyaolong

每五分钟进行一次分页,并将查询结果进行分页处理

上级 1b38824d
...@@ -37,6 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -37,6 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
...@@ -50,6 +51,7 @@ import java.sql.Timestamp; ...@@ -50,6 +51,7 @@ import java.sql.Timestamp;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
...@@ -105,8 +107,7 @@ public class NotifyCoreService { ...@@ -105,8 +107,7 @@ public class NotifyCoreService {
private static DingTalkClient createWorkRecordClient; private static DingTalkClient createWorkRecordClient;
private static DingTalkClient finishWorkRecordClient; private static DingTalkClient finishWorkRecordClient;
private static DingTalkClient getTokenClient; private static DingTalkClient getTokenClient;
private ConcurrentMap<String, Page<MsgBody>> msgMap = new ConcurrentHashMap<>(); private Map<String, List<MsgBody>> msgMap = new ConcurrentHashMap<>();
private ConcurrentMap<String, Long> expireMap = new ConcurrentHashMap<>();
static { static {
getTokenClient = new DefaultDingTalkClient(dingTalkTokenApi); getTokenClient = new DefaultDingTalkClient(dingTalkTokenApi);
sendMsgClient = new DefaultDingTalkClient(dingTalkSendMsgApi); sendMsgClient = new DefaultDingTalkClient(dingTalkSendMsgApi);
...@@ -114,8 +115,6 @@ public class NotifyCoreService { ...@@ -114,8 +115,6 @@ public class NotifyCoreService {
finishWorkRecordClient = new DefaultDingTalkClient(dingTalkFinishWorkRecordApi); finishWorkRecordClient = new DefaultDingTalkClient(dingTalkFinishWorkRecordApi);
} }
@Value("${ibiz.notify.expiretime:300000}")
private Long expireTime;
@Autowired @Autowired
@Lazy @Lazy
RestTemplate restTemplate; RestTemplate restTemplate;
...@@ -671,11 +670,13 @@ public class NotifyCoreService { ...@@ -671,11 +670,13 @@ public class NotifyCoreService {
* 获取某个用户分页存储催办消息 * 获取某个用户分页存储催办消息
* @return * @return
*/ */
public Page<MsgBody> getBacklogPageContent(MsgBodySearchContext context) { @Scheduled(fixedRate = 300000)
Page<MsgBody> result = msgBodyService.searchDefault(context); public void getBacklogPageContent() {
msgMap.put(context.getN_tousers_eq(),result); MsgBodySearchContext context = new MsgBodySearchContext();
expireMap.put(context.getN_tousers_eq(),System.currentTimeMillis()) ; context.setSize(Integer.MAX_VALUE);
return result;
Page<MsgBody> page = msgBodyService.searchDefault(context);
msgMap = page.getContent().stream().collect(Collectors.groupingBy(MsgBody::getToUsers));
} }
/** /**
...@@ -683,21 +684,31 @@ public class NotifyCoreService { ...@@ -683,21 +684,31 @@ public class NotifyCoreService {
* @return * @return
*/ */
public Page<MsgBody> getBacklogByPage(MsgBodySearchContext context) { public Page<MsgBody> getBacklogByPage(MsgBodySearchContext context) {
if(ObjectUtils.isEmpty(context)){ if (ObjectUtils.isEmpty(context)) {
throw new BadRequestAlertException("无效消息上下文","NotifyCoreService","getBacklogAllContent"); throw new BadRequestAlertException("无效消息上下文", "NotifyCoreService", "getBacklogAllContent");
} }
String toUserId = context.getN_tousers_eq(); String toUserId = context.getN_tousers_eq();
if(!msgMap.containsKey(toUserId) && !expireMap.containsKey(toUserId)){ if (!msgMap.containsKey(toUserId)) {
return getBacklogPageContent(context); return Page.empty();
}else { } else {
if(msgMap.get(toUserId).getSize() != context.getSize()){ com.baomidou.mybatisplus.extension.plugins.pagination.Page<MsgBody> pages = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
return getBacklogPageContent(context); int current = context.getPages().getCurrent() < 0 ? 0 : (int) context.getPages().getCurrent();
} int size = context.getPages().getCurrent() < 0 ? 0 : (int) context.getPages().getSize();
if(System.currentTimeMillis() - expireMap.get(toUserId) <= expireTime){ List<MsgBody> pageList = new ArrayList<>();
return msgMap.get(toUserId); List<MsgBody> msgBodyList = msgMap.get(toUserId);
}else {
return getBacklogPageContent(context); //计算当前页第一条数据的下标
int currId = context.getPages().getCurrent() > 1 ? (current - 1) * size : 0;
for (int i = 0; i < size && i < msgBodyList.size() - currId; i++) {
pageList.add(msgBodyList.get(currId + i));
} }
pages.setSize(size);
pages.setCurrent(current);
pages.setTotal(msgMap.get(toUserId).size());
pages.setRecords(pageList);
return new PageImpl<>(pages.getRecords(), context.getPageable(), pages.getTotal());
} }
} }
} }
...@@ -4,6 +4,7 @@ import cn.ibizlab.api.dto.MsgBodyDTO; ...@@ -4,6 +4,7 @@ import cn.ibizlab.api.dto.MsgBodyDTO;
import cn.ibizlab.api.mapping.MsgBodyMapping; import cn.ibizlab.api.mapping.MsgBodyMapping;
import cn.ibizlab.core.extensions.domain.Template; import cn.ibizlab.core.extensions.domain.Template;
import cn.ibizlab.core.extensions.service.NotifyCoreService; import cn.ibizlab.core.extensions.service.NotifyCoreService;
import cn.ibizlab.core.notify.domain.MsgBody;
import cn.ibizlab.core.notify.filter.MsgBodySearchContext; import cn.ibizlab.core.notify.filter.MsgBodySearchContext;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -92,13 +93,9 @@ public class NotifyCoreResource { ...@@ -92,13 +93,9 @@ public class NotifyCoreResource {
* @return * @return
*/ */
@RequestMapping(method = RequestMethod.GET,value = "/notify/msgbody/getbacklogbypage") @RequestMapping(method = RequestMethod.GET,value = "/notify/msgbody/getbacklogbypage")
public ResponseEntity<List<MsgBodyDTO>> getBacklogByPage(MsgBodySearchContext context) { public ResponseEntity<Page> getBacklogByPage(MsgBodySearchContext context) {
Page domains = notifyCoreService.getBacklogByPage(context); Page page = notifyCoreService.getBacklogByPage(context);
List<MsgBodyDTO> list = mapping.toDto(domains.getContent()); return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(mapping.toDto(page.getContent()), page.getPageable(), page.getContent().size()));
return ResponseEntity.status(HttpStatus.OK)
.header("x-page", String.valueOf(context.getPageable().getPageNumber()))
.header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
.header("x-total", String.valueOf(domains.getTotalElements()))
.body(list);
} }
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册