敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

2 years ago
  1. package com.whn.hellospring.controller;
  2. import com.whn.hellospring.common.StateMessage;
  3. import com.whn.hellospring.common.Status;
  4. import com.whn.hellospring.model.OilDO;
  5. import com.whn.hellospring.request.DeleteOilRecordRequest;
  6. import com.whn.hellospring.request.OilRecordListRequest;
  7. import com.whn.hellospring.service.OilService;
  8. import com.whn.hellospring.utils.StringUtil;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.validation.Valid;
  12. import java.util.List;
  13. @RestController
  14. @RequestMapping(value = "/oil")
  15. public class OilController {
  16. @Autowired
  17. OilService service;
  18. /**
  19. * 平均油耗
  20. *
  21. * @return
  22. */
  23. @PostMapping(value = "/wear")
  24. public Status getOilWear(@RequestBody OilRecordListRequest request) {
  25. try {
  26. //平均油耗 = 加油总数量(num 数量相加 除第一次) / 区间里程总数(intervalmileage 相加 除第一次)* 100
  27. double numTotal = service.getNumTotal(request.getCustomer_id());
  28. double intervalMileageTotal = service.getIntervalMileageTotal(request.getCustomer_id());
  29. double oilWear = numTotal * 100.0 / intervalMileageTotal;
  30. String s = StringUtil.INSTANCE.doubleToString1(oilWear);
  31. Status status = new Status(StateMessage.SUCCESS, "success");
  32. status.setData("wear", s);
  33. return status;
  34. } catch (Exception e) {
  35. return new Status(StateMessage.UN_KNOW_REASON);
  36. }
  37. }
  38. /**
  39. * 删除记录
  40. */
  41. @PostMapping(value = "/delete")
  42. public Status deleteCoffeeById(@RequestBody DeleteOilRecordRequest request) {
  43. try {
  44. OilDO oilDO = service.deleteRecord(request.getId());
  45. Status status = new Status(StateMessage.SUCCESS, "success");
  46. return status;
  47. } catch (Exception e) {
  48. return new Status(StateMessage.UN_KNOW_REASON);
  49. }
  50. }
  51. /**
  52. * 获取油耗列表
  53. */
  54. @PostMapping(value = "/wearList")
  55. public Status getWearList(@RequestBody OilRecordListRequest request) {
  56. try {
  57. List<Double> wearList = service.getWearList(request.getCustomer_id());
  58. Status status = new Status(StateMessage.SUCCESS, "success");
  59. status.setData("list", wearList);
  60. return status;
  61. } catch (Exception e) {
  62. return new Status(StateMessage.UN_KNOW_REASON);
  63. }
  64. }
  65. /**
  66. * 获取记录列表
  67. */
  68. @PostMapping(value = "/recordList")
  69. public Status getRecordList(@RequestBody OilRecordListRequest request) {
  70. try {
  71. List<OilDO> recordList = service.getRecordList(request.getCustomer_id());
  72. Status status = new Status(StateMessage.SUCCESS, "success");
  73. status.setData("list", recordList);
  74. return status;
  75. } catch (Exception e) {
  76. return new Status(StateMessage.UN_KNOW_REASON);
  77. }
  78. }
  79. /**
  80. * 创建记录
  81. */
  82. @PostMapping(value = "/add")
  83. public Status createRecord(@Valid @RequestBody OilDO oilDO) {
  84. try {
  85. //上条有效记录的currentMileage
  86. OilDO recentlyRecord = service.getRecentlyRecord(oilDO.getCustomer_id());
  87. Double recentlyMileage = 0.0;
  88. if (recentlyRecord != null&& recentlyRecord.getMileage()!=null) {
  89. recentlyMileage = recentlyRecord.getMileage();
  90. //区间里程intervalmileage = currentMileage - 上条有效记录的currentMileage
  91. double intervalMileage = oilDO.getMileage() - recentlyMileage;
  92. oilDO.setIntervalMileage(intervalMileage);
  93. //计算区间油耗 intervalOilWear = num / intervalmileage
  94. double intervalOilWear = oilDO.getNum() * 100.0 / intervalMileage;
  95. oilDO.setIntervalOilWear(intervalOilWear);
  96. }else{
  97. oilDO.setIntervalOilWear(0.0);
  98. }
  99. oilDO.setStatus(1);
  100. oilDO.setTime(oilDO.getTime());
  101. OilDO oil = service.addUpdateRecord(oilDO);
  102. Status status = new Status(StateMessage.SUCCESS, "success");
  103. status.setData("bean", oil);
  104. return status;
  105. } catch (Exception e) {
  106. return new Status(StateMessage.UN_KNOW_REASON);
  107. }
  108. }
  109. // /**
  110. // * 更新记录
  111. // */
  112. // @PostMapping(value = "/update")
  113. // public Status updateRecord(@Valid @RequestBody OilDO oilDO){
  114. // try {
  115. // oilDO.setTime(oilDO.getTime());
  116. // OilDO oil = service.addUpdateRecord(oilDO);
  117. // Status status = new Status(StateMessage.SUCCESS, "success");
  118. // status.setData("bean",oil);
  119. // return status;
  120. // }catch (Exception e){
  121. // return new Status(StateMessage.UN_KNOW_REASON);
  122. // }
  123. // }
  124. }