增加客户端分页查询

master
panqihua 5 years ago
parent f5a6ae9164
commit 6a19ea0ce2
  1. 27
      src/main/java/com/community/pocket/api/android/VisitorController.java
  2. 4
      src/main/java/com/community/pocket/entity/vo/Page.java
  3. 11
      src/main/java/com/community/pocket/entity/vo/android/VisitorMyResponse.java
  4. 10
      src/main/java/com/community/pocket/entity/vo/android/VisitorQuery.java
  5. 11
      src/main/java/com/community/pocket/entity/vo/android/VisitorReservationResponse.java
  6. 2
      src/main/java/com/community/pocket/repository/BaseDao.java
  7. 31
      src/main/java/com/community/pocket/repository/android/VisitorDao.java
  8. 7
      src/main/resources/application.yaml
  9. 10
      src/test/java/com/community/pocket/DemoApplicationTests.java

@ -2,6 +2,7 @@ package com.community.pocket.api.android;
import com.community.pocket.entity.po.Visitor; import com.community.pocket.entity.po.Visitor;
import com.community.pocket.entity.po.android.VisitorPeople; import com.community.pocket.entity.po.android.VisitorPeople;
import com.community.pocket.entity.vo.Page;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*; import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.android.*; import com.community.pocket.repository.android.*;
@ -72,8 +73,17 @@ public class VisitorController {
return tokenService.checkToken(query, new DoCheck<VisitorReservationResponse, VisitorQuery>() { return tokenService.checkToken(query, new DoCheck<VisitorReservationResponse, VisitorQuery>() {
@Override @Override
public VisitorReservationResponse ok(VisitorQuery visitorQuery) { public VisitorReservationResponse ok(VisitorQuery visitorQuery) {
List<Visitor> visitorList = visitorDao.loadMyReservation(query); Page<Visitor> visitorList = visitorDao.loadMyReservation(query);
VisitorReservationResponse response = new VisitorReservationResponse(Result.OK, VisitorReservationResponse.Msg.ok); VisitorReservationResponse response;
if (visitorList.isEmpty()) {
response = new VisitorReservationResponse(Result.OK, VisitorReservationResponse.Msg.ok_empty);
} else {
if (visitorList.getCurrentPage() > 1) {
response = new VisitorReservationResponse(Result.OK, VisitorReservationResponse.Msg.ok_more);
} else {
response = new VisitorReservationResponse(Result.OK, VisitorReservationResponse.Msg.ok);
}
}
response.setVisitorList(visitorList); response.setVisitorList(visitorList);
return response; return response;
} }
@ -93,8 +103,17 @@ public class VisitorController {
return tokenService.checkToken(query, new DoCheck<VisitorMyResponse, VisitorQuery>() { return tokenService.checkToken(query, new DoCheck<VisitorMyResponse, VisitorQuery>() {
@Override @Override
public VisitorMyResponse ok(VisitorQuery visitorQuery) { public VisitorMyResponse ok(VisitorQuery visitorQuery) {
List<Visitor> visitorList = visitorDao.loadMyVisitor(query); Page<Visitor> visitorList = visitorDao.loadMyVisitor(query);
VisitorMyResponse response = new VisitorMyResponse(Result.OK, VisitorMyResponse.Msg.ok); VisitorMyResponse response;
if (visitorList.isEmpty()) {
response = new VisitorMyResponse(Result.OK, VisitorMyResponse.Msg.ok_empty);
} else {
if (visitorList.getCurrentPage() > 1) {
response = new VisitorMyResponse(Result.OK, VisitorMyResponse.Msg.ok_more);
} else {
response = new VisitorMyResponse(Result.OK, VisitorMyResponse.Msg.ok);
}
}
response.setVisitorList(visitorList); response.setVisitorList(visitorList);
return response; return response;
} }

@ -17,9 +17,9 @@ public class Page<T> {
public Page(Long count, Long currentPage, Integer pageSize) { public Page(Long count, Long currentPage, Integer pageSize) {
this.count = count; this.count = count;
this.currentPage = currentPage;
this.pageSize = pageSize; this.pageSize = pageSize;
this.totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1; this.totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
this.currentPage = currentPage > this.totalPage ? this.totalPage : currentPage;
} }
public Integer getPageSize() { public Integer getPageSize() {
@ -51,6 +51,6 @@ public class Page<T> {
} }
public boolean isEmpty() { public boolean isEmpty() {
return this.count == 0; return this.list.isEmpty();
} }
} }

@ -1,23 +1,22 @@
package com.community.pocket.entity.vo.android; package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Visitor; import com.community.pocket.entity.po.Visitor;
import com.community.pocket.entity.vo.Page;
import com.community.pocket.entity.vo.Response; import com.community.pocket.entity.vo.Response;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
import java.util.List;
/** /**
* 我的访客信息 * 我的访客信息
*/ */
public class VisitorMyResponse extends Response<VisitorMyResponse.Msg> { public class VisitorMyResponse extends Response<VisitorMyResponse.Msg> {
private List<Visitor> visitorList; private Page<Visitor> visitorList;
public List<Visitor> getVisitorList() { public Page<Visitor> getVisitorList() {
return visitorList; return visitorList;
} }
public void setVisitorList(List<Visitor> visitorList) { public void setVisitorList(Page<Visitor> visitorList) {
this.visitorList = visitorList; this.visitorList = visitorList;
} }
@ -27,6 +26,8 @@ public class VisitorMyResponse extends Response<VisitorMyResponse.Msg> {
public enum Msg implements CustomMessage { public enum Msg implements CustomMessage {
ok, ok,
ok_empty,
ok_more,
fail, fail,
token token
} }

@ -11,6 +11,8 @@ public class VisitorQuery extends Token {
private Date startDate; private Date startDate;
private Date endDate; private Date endDate;
private Long currentPage;
public Date getStartDate() { public Date getStartDate() {
return startDate; return startDate;
} }
@ -26,4 +28,12 @@ public class VisitorQuery extends Token {
public void setEndDate(Date endDate) { public void setEndDate(Date endDate) {
this.endDate = endDate; this.endDate = endDate;
} }
public Long getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Long currentPage) {
this.currentPage = currentPage;
}
} }

@ -1,29 +1,30 @@
package com.community.pocket.entity.vo.android; package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Visitor; import com.community.pocket.entity.po.Visitor;
import com.community.pocket.entity.vo.Page;
import com.community.pocket.entity.vo.Response; import com.community.pocket.entity.vo.Response;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
import java.util.List;
//我的预约信息 //我的预约信息
public class VisitorReservationResponse extends Response<VisitorReservationResponse.Msg> { public class VisitorReservationResponse extends Response<VisitorReservationResponse.Msg> {
private List<Visitor> visitorList; private Page<Visitor> visitorList;
public VisitorReservationResponse(Result result, Msg message, Object... args) { public VisitorReservationResponse(Result result, Msg message, Object... args) {
super(result, message, args); super(result, message, args);
} }
public List<Visitor> getVisitorList() { public Page<Visitor> getVisitorList() {
return visitorList; return visitorList;
} }
public void setVisitorList(List<Visitor> visitorList) { public void setVisitorList(Page<Visitor> visitorList) {
this.visitorList = visitorList; this.visitorList = visitorList;
} }
public enum Msg implements CustomMessage { public enum Msg implements CustomMessage {
ok, ok,
ok_empty,
ok_more,
fail, fail,
token token
} }

@ -10,7 +10,7 @@ public abstract class BaseDao<T> {
@Autowired @Autowired
protected MongoTemplate mongoTemplate; protected MongoTemplate mongoTemplate;
@Value("${forum.page-size}") @Value("${page-size}")
protected Integer pageSize; protected Integer pageSize;
//增加 //增加

@ -19,6 +19,7 @@ import org.springframework.stereotype.Repository;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -31,6 +32,9 @@ public class VisitorDao extends BaseDao<Visitor> {
@Value("${visitor.dateformat}") @Value("${visitor.dateformat}")
private String dateformat; private String dateformat;
@Value("${visitor.page-size}")
private Integer androidPageSize;
//保存访客信息 //保存访客信息
public Visitor save(VisitorForm visitorForm) { public Visitor save(VisitorForm visitorForm) {
Visitor visitor = new Visitor(); Visitor visitor = new Visitor();
@ -67,29 +71,38 @@ public class VisitorDao extends BaseDao<Visitor> {
} }
//用户加载访客信息 //用户加载访客信息
private List<Visitor> load(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery, Criteria criteria) { private Page<Visitor> load(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery, Criteria criteria) {
if (visitorQuery.getStartDate() != null && visitorQuery.getEndDate() != null) { List<Criteria> criteriaList = new ArrayList<>();
criteria = criteria.andOperator(Criteria.where("time").gte(visitorQuery.getStartDate().getTime()), Criteria.where("time").lte(visitorQuery.getEndDate().getTime())); criteriaList.add(criteria);
} else {
if (visitorQuery.getStartDate() != null) { if (visitorQuery.getStartDate() != null) {
criteria = criteria.and("time").gte(visitorQuery.getStartDate().getTime()); criteriaList.add(Criteria.where("time").gte(visitorQuery.getStartDate().getTime()));
} }
if (visitorQuery.getEndDate() != null) { if (visitorQuery.getEndDate() != null) {
criteria = criteria.and("time").lte(visitorQuery.getEndDate().getTime()); criteriaList.add(Criteria.where("time").lte(visitorQuery.getEndDate().getTime()));
} }
Criteria c;
if (criteriaList.size() > 1) {
c = new Criteria().andOperator(criteriaList.toArray(new Criteria[]{}));
} else {
c = criteria;
} }
return mongoTemplate.find(new Query(criteria).with(Sort.by("time").ascending()), entityClass()); Query query = new Query(c).with(Sort.by("time").ascending());
long count = mongoTemplate.count(query, entityClass());
List<Visitor> visitorList = mongoTemplate.find(query.with(PageRequest.of(visitorQuery.getCurrentPage().intValue() - 1, androidPageSize)), entityClass());
Page<Visitor> visitorPage = new Page<>(count, visitorQuery.getCurrentPage(), androidPageSize);
visitorPage.setList(visitorList);
return visitorPage;
} }
//加载我的访客信息 //加载我的访客信息
public List<Visitor> loadMyVisitor(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery) { public Page<Visitor> loadMyVisitor(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery) {
Criteria criteria = Criteria.where("appointment").is(visitorQuery.getUsername()); Criteria criteria = Criteria.where("appointment").is(visitorQuery.getUsername());
return load(visitorQuery, criteria); return load(visitorQuery, criteria);
} }
//加载我的预约 //加载我的预约
public List<Visitor> loadMyReservation(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery) { public Page<Visitor> loadMyReservation(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery) {
Criteria criteria = Criteria.where("username").is(visitorQuery.getUsername()); Criteria criteria = Criteria.where("username").is(visitorQuery.getUsername());
return load(visitorQuery, criteria); return load(visitorQuery, criteria);
} }

@ -43,8 +43,6 @@ token:
forum: forum:
#帖子正文缩略长度 #帖子正文缩略长度
content-length: 10 content-length: 10
#响应客户端分页数据大小
page-size: 10
#注册配置 #注册配置
register: register:
@ -61,10 +59,15 @@ register:
- '16:00' - '16:00'
- '17:00' - '17:00'
#分页配置
page-size: 10
#访客配置 #访客配置
visitor: visitor:
#日期格式 #日期格式
dateformat: 'yyyy/MM/dd HH:mm' dateformat: 'yyyy/MM/dd HH:mm'
#客户端分页配置
page-size: 20
#废品回收配置 #废品回收配置
waste: waste:

@ -196,11 +196,9 @@ class DemoApplicationTests {
//加载访客信息 //加载访客信息
@Test @Test
void queryVisitor() throws Exception { void queryVisitor() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/visitor/reservation") MockHttpServletRequestBuilder builders = MockMvcRequestBuilders.get("/visitor/reservation")
.param("username", "panqihua") .param("currentPage", String.valueOf(1));
.param("startDate", "2020/04/18") mockMvc.perform(addToken(builders)).andDo(MockMvcResultHandlers.print()).andReturn();
.param("endDate", "2020/04/19"))
.andDo(MockMvcResultHandlers.print()).andReturn();
} }
//加载帖子信息 //加载帖子信息
@ -247,7 +245,7 @@ class DemoApplicationTests {
visitorForm.setChooseDate("2020/04/01"); visitorForm.setChooseDate("2020/04/01");
visitorForm.setChooseTime("11:00"); visitorForm.setChooseTime("11:00");
visitorForm.setNotes("备注"); visitorForm.setNotes("备注");
visitorForm.setUsername("eee" + i); visitorForm.setUsername("panqihua");
visitorDao.save(visitorForm); visitorDao.save(visitorForm);
} }
} }

Loading…
Cancel
Save