敏捷工具
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.

184 lines
6.5 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.model.OilPriceBean;
  6. import com.whn.hellospring.request.DeleteOilRecordRequest;
  7. import com.whn.hellospring.request.OilRecordListRequest;
  8. import com.whn.hellospring.service.OilService;
  9. import com.whn.hellospring.utils.DateUtils;
  10. import com.whn.hellospring.utils.StringUtil;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.hibernate.annotations.common.util.impl.Log;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.*;
  15. import javax.validation.Valid;
  16. import java.util.List;
  17. @Slf4j
  18. @RestController
  19. @RequestMapping(value = "/oil")
  20. public class OilController {
  21. @Autowired
  22. OilService service;
  23. /**
  24. * 市场油价
  25. */
  26. @PostMapping(value = "/price")
  27. public Status requestOilPrice(){
  28. try {
  29. OilPriceBean oilPriceBean = service.requestOilList();
  30. Status status = new Status(StateMessage.SUCCESS, "success");
  31. status.setData("list", oilPriceBean.getResult());
  32. return status;
  33. } catch (Exception e) {
  34. return new Status(StateMessage.UN_KNOW_REASON);
  35. }
  36. }
  37. /**
  38. * 平均油耗
  39. */
  40. @PostMapping(value = "/wear")
  41. public Status getOilWear(@RequestBody OilRecordListRequest request) {
  42. try {
  43. //平均油耗 = 加油总数量(num 数量相加 除第一次) / 区间里程总数(intervalmileage 相加 除第一次)* 100
  44. double numTotal = service.getNumTotal(request.getCustomer_id());
  45. double intervalMileageTotal = service.getIntervalMileageTotal(request.getCustomer_id());
  46. double oilWear = numTotal * 100.0 / intervalMileageTotal;
  47. String s = StringUtil.INSTANCE.doubleToString1(oilWear);
  48. Status status = new Status(StateMessage.SUCCESS, "success");
  49. status.setData("wear", s);
  50. return status;
  51. } catch (Exception e) {
  52. return new Status(StateMessage.UN_KNOW_REASON);
  53. }
  54. }
  55. /**
  56. * 删除记录
  57. */
  58. @PostMapping(value = "/delete")
  59. public Status deleteCoffeeById(@RequestBody DeleteOilRecordRequest request) {
  60. try {
  61. OilDO oilDO = service.deleteRecord(request.getId());
  62. Status status = new Status(StateMessage.SUCCESS, "success");
  63. return status;
  64. } catch (Exception e) {
  65. return new Status(StateMessage.UN_KNOW_REASON);
  66. }
  67. }
  68. /**
  69. * 获取油耗列表
  70. */
  71. @PostMapping(value = "/wearList")
  72. public Status getWearList(@RequestBody OilRecordListRequest request) {
  73. try {
  74. List<Double> wearList = service.getWearList(request.getCustomer_id());
  75. //删除第一条记录,一般为空
  76. wearList.remove(0);
  77. Status status = new Status(StateMessage.SUCCESS, "success");
  78. status.setData("list", wearList);
  79. return status;
  80. } catch (Exception e) {
  81. return new Status(StateMessage.UN_KNOW_REASON);
  82. }
  83. }
  84. /**
  85. * 获取一天的油费
  86. */
  87. @PostMapping(value = "/fuelOneDayList")
  88. public Status getFuelOneDayList(@RequestBody OilRecordListRequest request) {
  89. try {
  90. List<Double> list = service.getFuelOneDay(request.getCustomer_id());
  91. //删除第一条记录,一般为空
  92. list.remove(0);
  93. Status status = new Status(StateMessage.SUCCESS, "success");
  94. status.setData("list", list);
  95. return status;
  96. } catch (Exception e) {
  97. return new Status(StateMessage.UN_KNOW_REASON);
  98. }
  99. }
  100. /**
  101. * 获取记录列表
  102. */
  103. @PostMapping(value = "/recordList")
  104. public Status getRecordList(@RequestBody OilRecordListRequest request) {
  105. try {
  106. List<OilDO> recordList = service.getRecordList(request.getCustomer_id());
  107. Status status = new Status(StateMessage.SUCCESS, "success");
  108. status.setData("list", recordList);
  109. return status;
  110. } catch (Exception e) {
  111. return new Status(StateMessage.UN_KNOW_REASON);
  112. }
  113. }
  114. /**
  115. * 创建记录
  116. */
  117. @PostMapping(value = "/add")
  118. public Status createRecord(@Valid @RequestBody OilDO oilDO) {
  119. try {
  120. //上条有效记录的currentMileage
  121. OilDO recentlyRecord = service.getRecentlyRecord(oilDO.getCustomer_id());
  122. int recentlyMileage = 0;
  123. if (recentlyRecord != null) {
  124. recentlyMileage = recentlyRecord.getMileage();
  125. //区间里程intervalmileage = currentMileage - 上条有效记录的currentMileage
  126. int intervalMileage = oilDO.getMileage() - recentlyMileage;
  127. oilDO.setIntervalMileage(intervalMileage);
  128. //计算区间油耗 intervalOilWear = num / intervalmileage
  129. double intervalOilWear = oilDO.getNum() * 100.0 / intervalMileage;
  130. oilDO.setIntervalOilWear(intervalOilWear);
  131. //计算区间每天油费 fuelOneDay = price / (oilDO.Date - recentlyRecord.Date)
  132. long i = DateUtils.daysBetween(recentlyRecord.getTime(), oilDO.getTime());
  133. log.info("createRecord","间隔天数:"+i);
  134. oilDO.setBetweenDay(i);
  135. double fuelOneDay = oilDO.getPrice() / i;
  136. oilDO.setFuelOneDay(fuelOneDay);
  137. }else{
  138. oilDO.setIntervalOilWear(0.0);
  139. }
  140. oilDO.setStatus(1);
  141. oilDO.setTime(oilDO.getTime());
  142. OilDO oil = service.addUpdateRecord(oilDO);
  143. Status status = new Status(StateMessage.SUCCESS, "success");
  144. status.setData("bean", oil);
  145. return status;
  146. } catch (Exception e) {
  147. return new Status(StateMessage.UN_KNOW_REASON);
  148. }
  149. }
  150. // /**
  151. // * 更新记录
  152. // */
  153. // @PostMapping(value = "/update")
  154. // public Status updateRecord(@Valid @RequestBody OilDO oilDO){
  155. // try {
  156. // oilDO.setTime(oilDO.getTime());
  157. // OilDO oil = service.addUpdateRecord(oilDO);
  158. // Status status = new Status(StateMessage.SUCCESS, "success");
  159. // status.setData("bean",oil);
  160. // return status;
  161. // }catch (Exception e){
  162. // return new Status(StateMessage.UN_KNOW_REASON);
  163. // }
  164. // }
  165. }