diff --git a/src/main/java/com/swustoj/controller/ListController.java b/src/main/java/com/swustoj/controller/ListController.java index fbe7f234d5da1fc1360461908ac1f58f35b0f7a3..3dc022c228576763cd35a6b22f29cd93c68fa186 100644 --- a/src/main/java/com/swustoj/controller/ListController.java +++ b/src/main/java/com/swustoj/controller/ListController.java @@ -25,13 +25,12 @@ public class ListController { @Resource private ListService listService; - private HashMap data = new HashMap<>(); - @RequestMapping("/getMoreInfo/id/{id}") public Response getMoreInfo(@PathVariable String id) { Response response = new Response(); + HashMap data; try { - this.data = listService.searchById(id); + data = listService.searchById(id); response.put("code", 200); response.put("data", data); } catch (Exception e) { @@ -46,11 +45,12 @@ public class ListController { @AccessVerify(roles = {RoleEnum.ADMIN, RoleEnum.TEACHER, RoleEnum.STUDENT}) public Response getJoinList(HttpServletRequest request) { Response response = new Response(); + HashMap data; try { String accessToken = request.getHeader("Authorization").replace("Bearer ", ""); Claims claims = Utils.parse(accessToken); String userId = claims.get("userId").toString(); - this.data = listService.searchJoinList(userId); + data = listService.searchJoinList(userId); response.put("code", 200); response.put("data", data); } catch (Exception e) { @@ -64,11 +64,12 @@ public class ListController { @RequestMapping("/searchApplicationList/{filter}/page/{pageNumber}") public Response searchApplicationList(@PathVariable String filter, @PathVariable String pageNumber, HttpServletRequest request) { Response response = new Response(); + HashMap data; try { String accessToken = request.getHeader("Authorization").replace("Bearer ", ""); Claims claims = Utils.parse(accessToken); String userId = claims.get("userId").toString(); - this.data = listService.searchByKeyWord(filter, pageNumber, userId); + data = listService.searchByKeyWord(filter, pageNumber, userId); response.put("code", 200); response.put("data", data); } catch (Exception e) { @@ -82,11 +83,12 @@ public class ListController { @RequestMapping("/getApplicationList/{type}/page/{pageNumber}") public Response getApplicationList(@PathVariable String type, @PathVariable String pageNumber, HttpServletRequest request) { Response response = new Response(); + HashMap data; try { String accessToken = request.getHeader("Authorization").replace("Bearer ", ""); Claims claims = Utils.parse(accessToken); String userId = claims.get("userId").toString(); - this.data = listService.classifyByType(type, pageNumber, userId); + data = listService.classifyByType(type, pageNumber, userId); response.put("code", 200); response.put("data", data); } catch (Exception e) { diff --git a/src/main/java/com/swustoj/service/ListService.java b/src/main/java/com/swustoj/service/ListService.java index 05bb01b808bf17f1d8e9186aaf244ec8dd38d941..b3bcb78c03d31502070bd1ae24dd1cce3e1a0493 100644 --- a/src/main/java/com/swustoj/service/ListService.java +++ b/src/main/java/com/swustoj/service/ListService.java @@ -23,20 +23,16 @@ public class ListService { @Resource private SignUpMapper signUpMapper; - private List list = new ArrayList<>(); - private final List> result = new ArrayList<>(); - private List joinList = new ArrayList<>(); - /** * 获取一页的内容 * * @param page 需要获取的页数 * @param func 用于区分,func=2时要加isOver属性 */ - private void getOnePage(int page, int func) { - this.list.sort((o1, o2) -> -(o1.getId() - o2.getId())); - this.result.clear(); + private void getOnePage(int page, int func,List list,List> result,List joinList) { final int numPerPage = 9; + list.sort((o1, o2) -> -(o1.getId() - o2.getId())); + for (int i = (page - 1) * numPerPage; i < list.size() && i < (page - 1) * numPerPage + numPerPage; i++) { HashMap temp = new HashMap<>(6); if (func >= 1) { @@ -52,7 +48,7 @@ public class ListService { if (func >= 3) { temp.put("isOver", list.get(i).getDeadline().before(new Date())); } - this.result.add(temp); + result.add(temp); } } @@ -65,10 +61,12 @@ public class ListService { public HashMap searchById(String id) { try { Recruitment recruitment = recruitmentMapper.selectByPrimaryKey(Utils.stringToInt(id)); - HashMap data = new HashMap<>(4); - data.put("title", recruitment.getName()); - data.put("teacher", recruitment.getTeacher()); - data.put("description", recruitment.getDescription()); + HashMap data = new HashMap<>(); + if(recruitment != null) { + data.put("title", recruitment.getName()); + data.put("teacher", recruitment.getTeacher()); + data.put("description", recruitment.getDescription()); + } return data; } catch (Exception e) { e.printStackTrace(); @@ -83,11 +81,13 @@ public class ListService { * @return 返回值为包装好的data的json */ public HashMap searchJoinList(String userId) { + List list; + List> result = new ArrayList<>(); try { - this.list = recruitmentMapper.selectJoinListByUserId(Utils.stringToInt(userId)); - getOnePage(1, 1); - HashMap data = new HashMap<>(1); - data.put("joinList", this.result); + list = recruitmentMapper.selectJoinListByUserId(Utils.stringToInt(userId)); + getOnePage(1, 1,list,result,null); + HashMap data = new HashMap<>(); + data.put("joinList", result); return data; } catch (Exception e) { e.printStackTrace(); @@ -103,14 +103,17 @@ public class ListService { * @return 返回值为包装好的data的json */ public HashMap searchByKeyWord(String filter, String pageNumber, String userId) { + List list; + List> result = new ArrayList<>(); + List joinList; try { int page = Utils.stringToInt(pageNumber); - this.list = recruitmentMapper.selectByKeyWord(filter); - this.joinList = signUpMapper.selectJoin(Utils.stringToInt(userId)); - getOnePage(page, 2); - HashMap data = new HashMap<>(1); - data.put("itemArray", this.result); - data.put("listLength", this.list.size()); + list = recruitmentMapper.selectByKeyWord(filter); + joinList = signUpMapper.selectJoin(Utils.stringToInt(userId)); + getOnePage(page, 2,list,result,joinList); + HashMap data = new HashMap<>(); + data.put("itemArray", result); + data.put("listLength", list.size()); return data; } catch (Exception e) { e.printStackTrace(); @@ -126,6 +129,9 @@ public class ListService { * @return 返回值为包装好的data的json */ public HashMap classifyByType(String type, String pageNumber, String userId) { + List list; + List> result = new ArrayList<>(); + List joinList; try { int page = Utils.stringToInt(pageNumber); int flag; @@ -142,12 +148,12 @@ public class ListService { default: throw new RuntimeException("传入的type参数错误!"); } - this.list = recruitmentMapper.selectByType(flag); - this.joinList = signUpMapper.selectJoin(Utils.stringToInt(userId)); - this.getOnePage(page, 3); - HashMap data = new HashMap<>(4); - data.put("itemArray", this.result); - data.put("listLength", this.list.size()); + list = recruitmentMapper.selectByType(flag); + joinList = signUpMapper.selectJoin(Utils.stringToInt(userId)); + this.getOnePage(page, 3,list,result,joinList); + HashMap data = new HashMap<>(); + data.put("itemArray", result); + data.put("listLength", list.size()); return data; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/resources/com/swustoj/mapper/RecruitmentMapper.xml b/src/main/resources/com/swustoj/mapper/RecruitmentMapper.xml index 4224d706bd9dd9d3fd402d609afee2caa6440df2..c03e84985ad5aaaf154eb2ffd130b160e52ae98a 100644 --- a/src/main/resources/com/swustoj/mapper/RecruitmentMapper.xml +++ b/src/main/resources/com/swustoj/mapper/RecruitmentMapper.xml @@ -67,7 +67,9 @@ select from recruitment - where recruitment.status!=0 and recruitment.flag IS NOT NULL and ((recruitment.flag = #{flag,jdbcType=INTEGER}) or (recruitment.flag = 2 and #{flag,jdbcType=INTEGER} = 1) or (#{flag,jdbcType=INTEGER} = 2)) + where recruitment.status!=0 and recruitment.flag IS NOT NULL and ((recruitment.flag = #{flag,jdbcType=INTEGER}) or + (recruitment.flag = 2 and #{flag,jdbcType=INTEGER} = 1) or + (#{flag,jdbcType=INTEGER} = 2))