代码改造实录-- 分解大函数,并以新建函数名解释其功能

原来的代码:

 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、原来的函数更简洁易读。

 

posted @ 2020-08-11 10:54  GreenMountain  阅读(184)  评论(0编辑  收藏  举报