From 0f4d11968b18594937a96e885136fcf1cde45607 Mon Sep 17 00:00:00 2001 From: jingkaimori Date: Thu, 10 Mar 2022 22:36:51 +0800 Subject: [PATCH] [refactor] use new return type for existing api --- .../AppointmentManagerController.java | 180 ++++++++++-------- 1 file changed, 105 insertions(+), 75 deletions(-) diff --git a/service/src/main/java/cn/edu/bit/ruixin/community/controller/AppointmentManagerController.java b/service/src/main/java/cn/edu/bit/ruixin/community/controller/AppointmentManagerController.java index fd144bf..31c5164 100644 --- a/service/src/main/java/cn/edu/bit/ruixin/community/controller/AppointmentManagerController.java +++ b/service/src/main/java/cn/edu/bit/ruixin/community/controller/AppointmentManagerController.java @@ -1,7 +1,6 @@ package cn.edu.bit.ruixin.community.controller; -import cn.edu.bit.ruixin.base.common.CommonResult; -import cn.edu.bit.ruixin.base.common.ResultCode; +import cn.edu.bit.ruixin.base.common.exp.CommonResult; import cn.edu.bit.ruixin.community.annotation.MsgSecCheck; import cn.edu.bit.ruixin.community.domain.Appointment; import cn.edu.bit.ruixin.community.domain.Room; @@ -12,7 +11,9 @@ import cn.edu.bit.ruixin.community.service.ScheduleService; import cn.edu.bit.ruixin.community.service.UserService; import cn.edu.bit.ruixin.community.vo.AppointmentInfoVo; +import cn.edu.bit.ruixin.community.vo.PageVo; import lombok.Getter; +import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -64,31 +65,25 @@ private AppointmentInfoVo getAppointmentInfoVo(Appointment appointment) { @PreAuthorize("hasAuthority('appointCheck')") @GetMapping("") - public CommonResult lookupAppointmentById(@RequestParam("id") Integer id) { + public CommonResult lookupAppointmentById(@RequestParam("id") Integer id) { Appointment appointment = appointmentService.getAppointmentById(id); AppointmentInfoVo infoVo = getAppointmentInfoVo(appointment); - return CommonResult.ok(ResultCode.SUCCESS).data("appointment", infoVo); + return CommonResult.ok() + .data(new AppointmentInfoReturnVo(infoVo)); } @PreAuthorize("hasAuthority('appointCheck')") @GetMapping("/{current}/{limit}/{schoolId}") - public CommonResult lookupAppointmentBySchoolId(@PathVariable("current") int current, @PathVariable("limit") int limit, @PathVariable("schoolId") String schoolId) { + public CommonResult> lookupAppointmentBySchoolId(@PathVariable("current") int current, @PathVariable("limit") int limit, @PathVariable("schoolId") String schoolId) { // 构造分页对象 Pageable pageable = PageRequest.of(current, limit); Page page = appointmentService.getAppointmentsBySchoolId(pageable, schoolId); - List list = page.getContent(); - List infoVos = new ArrayList<>(); - list.forEach(appointment -> infoVos.add(getAppointmentInfoVo(appointment))); - - Map map = new HashMap<>(); - map.put("totalElements", page.getTotalElements()); - map.put("totalPages", page.getTotalPages()); - map.put("hasNext", page.hasNext()); - map.put("hasPrevious", page.hasPrevious()); - map.put("items", infoVos); - return CommonResult.ok(ResultCode.SUCCESS).data(map); + PageVo res = PageVo.convertToVo(page) + .foreach(appointment -> getAppointmentInfoVo(appointment)); + return CommonResult.>ok() + .data(res); } /** @@ -98,12 +93,13 @@ public CommonResult lookupAppointmentBySchoolId(@PathVariable("current") int cur */ @PreAuthorize("hasAuthority('appointCheck')") @GetMapping("all") - public CommonResult getAllAppointment(@RequestParam(required = false, name = "status") String status) { + public CommonResult getAllAppointment(@RequestParam(required = false, name = "status") String status) { List list = appointmentService.getAllAppointment(status); List infoVos = new ArrayList<>(); list.forEach(appointment -> infoVos.add(getAppointmentInfoVo(appointment))); - return CommonResult.ok(ResultCode.SUCCESS).data("appointments", infoVos); + return CommonResult.ok() + .data(new AppointmentListReturnVo(infoVos)); } /** @@ -115,35 +111,27 @@ public CommonResult getAllAppointment(@RequestParam(required = false, name = "st */ @PreAuthorize("hasAuthority('appointCheck')") @GetMapping("/{current}/{limit}") - public CommonResult getAppointmentPages(@PathVariable("current") int current, @PathVariable("limit") int limit, @RequestParam(required = false, name = "status") String status) { + public CommonResult> getAppointmentPages(@PathVariable("current") int current, @PathVariable("limit") int limit, @RequestParam(required = false, name = "status") String status) { // 构造排序对象 // Sort sort = Sort.by(Sort.Direction.DESC, "launchDate", "execDate", "launchTime"); // 构造分页对象 Pageable pageable = PageRequest.of(current, limit); Page page = appointmentService.getAppointmentPages(pageable, status); - List list = page.getContent(); - - List infoVos = new ArrayList<>(); - list.forEach(appointment -> infoVos.add(getAppointmentInfoVo(appointment))); + PageVo res = PageVo.convertToVo(page) + .foreach(appointment -> getAppointmentInfoVo(appointment)); - Map map = new HashMap<>(); - map.put("totalElements", page.getTotalElements()); - map.put("totalPages", page.getTotalPages()); - map.put("hasNext", page.hasNext()); - map.put("hasPrevious", page.hasPrevious()); - map.put("items", infoVos); - return CommonResult.ok(ResultCode.SUCCESS).data(map); + return CommonResult.>ok().data(res); } @PreAuthorize("hasAuthority('appointCheck')") @MsgSecCheck({"conductor", "checkNote"}) @PutMapping("/check/{appointmentId}") - public CommonResult check(@PathVariable(name = "appointmentId") Integer id, + public CommonResult check(@PathVariable(name = "appointmentId") Integer id, @RequestParam(required = true, name = "status") String status, @RequestParam(required = true, name = "conductor") String conductor, @RequestParam("checkNote") String checkNote) { appointmentService.checkOutAppointment(id, status, conductor, checkNote); - return CommonResult.ok(ResultCode.SUCCESS).msg("审批操作成功!"); + return CommonResult.ok().msg("审批操作成功!"); } /** @@ -153,17 +141,19 @@ public CommonResult check(@PathVariable(name = "appointmentId") Integer id, */ @PreAuthorize("hasAuthority('appoint')") @PostMapping("/appoint") - public CommonResult appoint(@RequestBody(required = true) AppointmentInfoVo[] infoVos) { + public CommonResult appoint(@RequestBody(required = true) AppointmentInfoVo[] infoVos) { List appointments = Arrays.stream(infoVos) .map(infoVo -> AppointmentInfoVo.convertToPo(infoVo)) .collect(Collectors.toList()); List deletedAppointments = appointmentService.addNewAppointmentsByAdmin(appointments); + AppointmentConfilctingReturnVo res = new AppointmentConfilctingReturnVo( + deletedAppointments.stream() + .map(appointment -> getAppointmentInfoVo(appointment)) + .collect(Collectors.toList())); - return CommonResult.ok(ResultCode.SUCCESS).msg("管理员预约成功").data("conflictingAppointments", - deletedAppointments.stream() - .map(appointment -> getAppointmentInfoVo(appointment)) - .collect(Collectors.toList())); + return CommonResult.ok() + .msg("管理员预约成功").data(res); } /** @@ -173,9 +163,9 @@ public CommonResult appoint(@RequestBody(required = true) AppointmentInfoVo[] in */ @PreAuthorize("hasAuthority('appoint')") @PutMapping("/cancel") - public CommonResult cancel(@RequestBody(required = true) Integer[] ids) { + public CommonResult cancel(@RequestBody(required = true) Integer[] ids) { appointmentService.cancelAppointmentsByAdminThroughIds(ids); - return CommonResult.ok(ResultCode.SUCCESS).msg("撤销预约成功!"); + return CommonResult.ok().msg("撤销预约成功!"); } /** @@ -184,15 +174,16 @@ public CommonResult cancel(@RequestBody(required = true) Integer[] ids) { */ @PreAuthorize("hasAuthority('appoint')") @GetMapping("/allByAdmin") - public CommonResult getAllAppointmentAppointedByAdmin() { + public CommonResult getAllAppointmentAppointedByAdmin() { List appointments = appointmentService.getAllAppointmentsAppointedByAdmin(); - return CommonResult.ok(ResultCode.SUCCESS) - .data("appointments", appointments.stream() - .map(appointment -> { - AppointmentInfoVo appointmentInfoVo = getAppointmentInfoVo(appointment); - return appointmentInfoVo; - }) - .collect(Collectors.toList())); + return CommonResult.ok() + .data(new AppointmentListReturnVo( + appointments.stream() + .map(appointment -> { + AppointmentInfoVo appointmentInfoVo = getAppointmentInfoVo(appointment); + return appointmentInfoVo; + }) + .collect(Collectors.toList()))); } /** @@ -201,9 +192,9 @@ public CommonResult getAllAppointmentAppointedByAdmin() { */ @PreAuthorize("hasAuthority('appoint')") @GetMapping("/availablePeriod") - public CommonResult getAvailablePeriodByRoomId(@RequestParam(name = "roomId") Integer roomId, @RequestParam(name = "conductor") String conductor, @RequestParam(name = "date") String date) { - Map result = roomService.getRoomFreeTimeByAdmin(roomId, conductor, date); - return CommonResult.ok(ResultCode.SUCCESS) + public CommonResult>> getAvailablePeriodByRoomId(@RequestParam(name = "roomId") Integer roomId, @RequestParam(name = "conductor") String conductor, @RequestParam(name = "date") String date) { + Map> result = roomService.getRoomFreeTimeByAdmin(roomId, conductor, date); + return CommonResult.>>ok() .data(result); } @@ -213,37 +204,76 @@ public CommonResult getAvailablePeriodByRoomId(@RequestParam(name = "roomId") In */ @PreAuthorize("hasAuthority('appointCheck')") @GetMapping("/allTime") - public CommonResult getAllTimeByAdmin() { + public CommonResult getAllTimeByAdmin() { List scheduleList = scheduleService.getAllTime(); + return CommonResult.ok() + .data(ScheduleListVo.getFrom(scheduleList)); + } +} - @Getter - class scheduleBeginTime { - private Integer id; - private String beginTime; - - public scheduleBeginTime(Integer id, String beginTime) { - this.id = id; - this.beginTime = beginTime; - } +@Getter +@NoArgsConstructor +class ScheduleListVo { + + @Getter + class scheduleBeginTime { + private Integer id; + private String beginTime; + + public scheduleBeginTime(Schedule schedule) { + this.id = schedule.getId(); + this.beginTime = schedule.getBegin(); } + } - @Getter - class scheduleEndTime { - private Integer id; - private String endTime; + @Getter + class scheduleEndTime { + private Integer id; + private String endTime; - public scheduleEndTime(Integer id, String endTime) { - this.id = id; - this.endTime = endTime; - } + public scheduleEndTime(Schedule schedule) { + this.id = schedule.getId(); + this.endTime = schedule.getEnd(); } + } + private List beginTimes; + private List endTimes; + + public static ScheduleListVo getFrom(List scheduleList) { + ScheduleListVo res = new ScheduleListVo(); + res.beginTimes = scheduleList.stream() + .map(schedule -> res.new scheduleBeginTime(schedule)) + .collect(Collectors.toList()); + res.endTimes = scheduleList.stream() + .map(schedule -> res.new scheduleEndTime(schedule)) + .collect(Collectors.toList()); + return res; + } +} + +@Getter +class AppointmentInfoReturnVo { + private AppointmentInfoVo appointment; + + AppointmentInfoReturnVo(AppointmentInfoVo infovo){ + this.appointment = infovo; + } +} + +@Getter +class AppointmentListReturnVo { + private List appointments; + + AppointmentListReturnVo(List infovo){ + this.appointments = infovo; + } +} - return CommonResult.ok(ResultCode.SUCCESS) - .data("beginTimes", scheduleList.stream() - .map(schedule -> new scheduleBeginTime(schedule.getId(), schedule.getBegin())) - .collect(Collectors.toList())) - .data("endTimes", scheduleList.stream() - .map(schedule -> new scheduleEndTime(schedule.getId(), schedule.getEnd())) - .collect(Collectors.toList())); +@Getter +class AppointmentConfilctingReturnVo { + private List conflictingAppointments; + + AppointmentConfilctingReturnVo(List infovo){ + this.conflictingAppointments = infovo; } }