程式碼改造實錄-- 分解大函式,並以新建函式名解釋其功能
阿新 • • 發佈:2020-08-11
原來的程式碼:
1 OrderInfo orderForm = this.orderInfoRepository.findByOrderId(orderNo); 2 if (orderForm != null && orderForm.getOrderStatus() == 0) { 3 MakeAnAppointment make = this.makeAnAppointmentRepository.findByOrderId(orderForm.getId()); 4 5 if (orderForm.getOrderType() == 0) {6 make.setOrderStatus(1); // 成功 7 String extra = orderForm.getExtra(); 8 OrderPlus doctorVisit = JSON.parseObject(extra, OrderPlus.class); 9 Integer id = doctorVisit.getId(); 10 OrderPlus d = this.doctorVisitRepository.findById(id).get(); 11 d.setMakeAnAppointment(doctorVisit.getMakeAnAppointment() + 1);12 this.doctorVisitRepository.save(d); 13 14 JSONObject params1 = new JSONObject(); 15 params1.put("title", "加號提醒"); 16 params1.put("content", orderForm.getUserName() + "申請了你的加號,請及時處理"); 17 JSONObject params2 = new JSONObject(); 18 DoctorInfo doctorDetail = this.doctorDetailRepository.findById(orderForm.getDoctorId()).get(); 19 UserAccount user2 = this.userRepository.findById(doctorDetail.getAccountId()).get(); 20 params2.put("userId", user2.getUserName()); 21 params2.put("msg", JSON.toJSONString(params1)); 22 HttpUtils.post(this.url + "/api/im/send", params2); 23 24 this.makeAnAppointmentRepository.save(make); 25 } else if (orderForm.getOrderType() == 1) { 26 make.setOrderStatus(1); 27 this.makeAnAppointmentRepository.save(make); 28 } else if (orderForm.getOrderType() == 2) { 29 make.setOrderStatus(1); 30 this.makeAnAppointmentRepository.save(make); 31 32 } else { 33 OrderAnnualFee fee = new OrderAnnualFee(); 34 BeanUtils.copyProperties(orderForm, fee); 35 annualFeeService.saveAnnualFee(fee); 36 orderService.saveLog(orderForm.getOrderId(), orderForm.getUserName(), orderForm.getUserName() + "會員繳費"); 37 } 38 orderForm.setPayType("wx"); 39 orderForm.setPayTime(Long.valueOf(System.currentTimeMillis())); 40 orderForm.setOrderStatus(1); 41 this.orderInfoRepository.save(orderForm); 42 }
修改後程式碼:
1 OrderInfo orderForm = this.orderInfoRepository.findByOrderId(orderNo); 2 if (orderForm != null && orderForm.getOrderStatus() == 0) { 3 4 if (orderForm.getOrderType() == 0) { 5 String extra = orderForm.getExtra(); 6 OrderPlus doctorVisit = JSON.parseObject(extra, OrderPlus.class); 7 orderPlusService.increaseAppointment(doctorVisit.getId()); 8 9 MessageSenderBiz.sendLovePlusToDoctor(orderForm.getUserName(), orderForm.getDoctorName(), this.url); 10 appointmentService.changeAppointmentStatus(orderForm.getId(), 1); 11 } else if (orderForm.getOrderType() == 1) { 12 appointmentService.changeAppointmentStatus(orderForm.getId(), 1); 13 } else if (orderForm.getOrderType() == 2) { 14 appointmentService.changeAppointmentStatus(orderForm.getId(), 1); 15 } else { 16 OrderAnnualFee fee = new OrderAnnualFee(); 17 BeanUtils.copyProperties(orderForm, fee); 18 annualFeeService.saveAnnualFee(fee); 19 orderService.saveLog(orderForm.getOrderId(), orderForm.getUserName(), orderForm.getUserName() + "會員繳費"); 20 } 21 orderForm.setPayType("wx"); 22 orderForm.setPayTime(Long.valueOf(System.currentTimeMillis())); 23 orderForm.setOrderStatus(1); 24 this.orderInfoRepository.save(orderForm); 25 }
修改後,將傳送資訊功能和更新預約狀態兩個功能獨立出來,並移交至各自的功能類中。這樣可以:1、增加程式碼複用,方法功能更清晰;2、符合類的封裝思想,自己的事情自己辦;3、原來的函式更簡潔易讀。