增加客户端分页查询

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. 37
      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.android.VisitorPeople;
import com.community.pocket.entity.vo.Page;
import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.android.*;
@ -72,8 +73,17 @@ public class VisitorController {
return tokenService.checkToken(query, new DoCheck<VisitorReservationResponse, VisitorQuery>() {
@Override
public VisitorReservationResponse ok(VisitorQuery visitorQuery) {
List<Visitor> visitorList = visitorDao.loadMyReservation(query);
VisitorReservationResponse response = new VisitorReservationResponse(Result.OK, VisitorReservationResponse.Msg.ok);
Page<Visitor> visitorList = visitorDao.loadMyReservation(query);
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);
return response;
}
@ -93,8 +103,17 @@ public class VisitorController {
return tokenService.checkToken(query, new DoCheck<VisitorMyResponse, VisitorQuery>() {
@Override
public VisitorMyResponse ok(VisitorQuery visitorQuery) {
List<Visitor> visitorList = visitorDao.loadMyVisitor(query);
VisitorMyResponse response = new VisitorMyResponse(Result.OK, VisitorMyResponse.Msg.ok);
Page<Visitor> visitorList = visitorDao.loadMyVisitor(query);
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);
return response;
}

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

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

@ -11,6 +11,8 @@ public class VisitorQuery extends Token {
private Date startDate;
private Date endDate;
private Long currentPage;
public Date getStartDate() {
return startDate;
}
@ -26,4 +28,12 @@ public class VisitorQuery extends Token {
public void setEndDate(Date 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;
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.Result;
import java.util.List;
//我的预约信息
public class VisitorReservationResponse extends Response<VisitorReservationResponse.Msg> {
private List<Visitor> visitorList;
private Page<Visitor> visitorList;
public VisitorReservationResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public List<Visitor> getVisitorList() {
public Page<Visitor> getVisitorList() {
return visitorList;
}
public void setVisitorList(List<Visitor> visitorList) {
public void setVisitorList(Page<Visitor> visitorList) {
this.visitorList = visitorList;
}
public enum Msg implements CustomMessage {
ok,
ok_empty,
ok_more,
fail,
token
}

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

@ -19,6 +19,7 @@ import org.springframework.stereotype.Repository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
@ -31,6 +32,9 @@ public class VisitorDao extends BaseDao<Visitor> {
@Value("${visitor.dateformat}")
private String dateformat;
@Value("${visitor.page-size}")
private Integer androidPageSize;
//保存访客信息
public Visitor save(VisitorForm visitorForm) {
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) {
if (visitorQuery.getStartDate() != null && visitorQuery.getEndDate() != null) {
criteria = criteria.andOperator(Criteria.where("time").gte(visitorQuery.getStartDate().getTime()), Criteria.where("time").lte(visitorQuery.getEndDate().getTime()));
private Page<Visitor> load(com.community.pocket.entity.vo.android.VisitorQuery visitorQuery, Criteria criteria) {
List<Criteria> criteriaList = new ArrayList<>();
criteriaList.add(criteria);
if (visitorQuery.getStartDate() != null) {
criteriaList.add(Criteria.where("time").gte(visitorQuery.getStartDate().getTime()));
}
if (visitorQuery.getEndDate() != null) {
criteriaList.add(Criteria.where("time").lte(visitorQuery.getEndDate().getTime()));
}
Criteria c;
if (criteriaList.size() > 1) {
c = new Criteria().andOperator(criteriaList.toArray(new Criteria[]{}));
} else {
if (visitorQuery.getStartDate() != null) {
criteria = criteria.and("time").gte(visitorQuery.getStartDate().getTime());
}
if (visitorQuery.getEndDate() != null) {
criteria = criteria.and("time").lte(visitorQuery.getEndDate().getTime());
}
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());
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());
return load(visitorQuery, criteria);
}

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

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

Loading…
Cancel
Save