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

212 lines
5.7 KiB

2 years ago
  1. package com.whn.hellospring.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.whn.hellospring.model.OilDO;
  5. import com.whn.hellospring.model.OilPriceBean;
  6. import com.whn.hellospring.repository.OilRepository;
  7. import com.whn.hellospring.utils.HttpUtils;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.util.EntityUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. @Slf4j
  18. @Service
  19. public class OilService {
  20. @Autowired
  21. private OilRepository oilRepository;
  22. /**
  23. * 获取三方油价列表
  24. * @return
  25. */
  26. public OilPriceBean requestOilList() throws Exception {
  27. String host = "http://apis.juhe.cn";
  28. String path = "/gnyj/query";
  29. String key = "212b6526c4f8a3899ca406a1173bafa8";
  30. JSONObject requestObj = new JSONObject();
  31. // requestObj.put("key", key);
  32. String bodys = requestObj.toJSONString();
  33. JSONObject resObj = new JSONObject();
  34. // resObj.put("key",key);
  35. try {
  36. Map<String, String> headers = new HashMap<String, String>();
  37. Map<String, String> querys = new HashMap<String, String>();
  38. querys.put("key",key);
  39. HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
  40. int stat = response.getStatusLine().getStatusCode();
  41. if (stat != 200) {
  42. System.out.println("Http code: " + stat);
  43. System.out.println("http header error msg: " + response.getFirstHeader("X-Ca-Error-Message"));
  44. String bodyErr = EntityUtils.toString(response.getEntity());
  45. System.out.println("Http body error msg:" + bodyErr);
  46. resObj.put("errorMsg",bodyErr);
  47. }else{
  48. String res = EntityUtils.toString(response.getEntity());
  49. resObj.put("status","SUCCESS");
  50. resObj.putAll(JSON.parseObject(res));
  51. }
  52. } catch (Exception e) {
  53. resObj.put("status","FAIL");
  54. e.printStackTrace();
  55. } finally {
  56. if(!"SUCCESS".equals(resObj.getString("status"))){
  57. throw new Exception();
  58. }
  59. log.info("resObj={}",JSONObject.toJSONString(resObj,true));
  60. OilPriceBean oilPriceBean = JSONObject.parseObject(String.valueOf(resObj), OilPriceBean.class);
  61. log.info("bean"+oilPriceBean.toString());
  62. return oilPriceBean;
  63. }
  64. }
  65. //获取id,里程,区间里程
  66. //计算区间里程
  67. //根据id,将区间里程赋值到数据库
  68. /**
  69. * test
  70. * 计算区间里程
  71. */
  72. public void calculationIntervalMileage(Long customer_id) {
  73. List<OilDO> list = oilRepository.finAllSortByTime(customer_id);
  74. log.info("list:{}", list);
  75. for (int i = 1; i < list.size(); i++) {
  76. list.get(i).setMileage(list.get(i-1).getMileage()-list.get(i).getIntervalMileage());
  77. }
  78. }
  79. /**
  80. * 油耗列表
  81. */
  82. public List<Double> getWearList(Long customer_id) {
  83. return oilRepository.findWearList(customer_id);
  84. }
  85. /**
  86. * 一天油费
  87. */
  88. public List<Double> getFuelOneDay(Long customer_id) {
  89. return oilRepository.findFuelOneDay(customer_id);
  90. }
  91. /**
  92. * 获取有效数量num之和
  93. *
  94. * @return
  95. */
  96. public double getNumTotal(Long customer_id) {
  97. return oilRepository.countNum(customer_id);
  98. }
  99. /**
  100. * 获取有效区间里程和
  101. *
  102. * @return
  103. */
  104. public double getIntervalMileageTotal(Long customer_id) {
  105. return oilRepository.countIntervalMileage(customer_id);
  106. }
  107. /**
  108. * 获取最新的一条有效记录
  109. */
  110. public OilDO getRecentlyRecord(Long customer_id) {
  111. List<OilDO> recordList = oilRepository.finAllSortByTime(customer_id);
  112. if (recordList == null || recordList.size() == 0) return null;
  113. int index = -1;
  114. for (int i = 0; i < recordList.size(); i++) {
  115. OilDO oilDO = recordList.get(i);
  116. if (oilDO.getStatus() == 1) {
  117. index = i;
  118. break;
  119. }
  120. }
  121. if (index == -1) {
  122. return null;
  123. } else {
  124. return recordList.get(index);
  125. }
  126. }
  127. /**
  128. * 获取有效记录列表
  129. */
  130. public List<OilDO> getRecordList(Long customer_id) {
  131. log.info("customer_id:{}", customer_id);
  132. List<OilDO> recordList = oilRepository.finAllSortByTime(customer_id);
  133. log.info("recordList:{}", recordList);
  134. // Collections.reverse(recordList);
  135. List<OilDO> list = new ArrayList<>();
  136. for (int i = 0; i < recordList.size(); i++) {
  137. if (recordList.get(i).getStatus() == 1) {
  138. list.add(recordList.get(i));
  139. }
  140. }
  141. log.info("list:{}", list);
  142. return list;
  143. }
  144. /**
  145. * 获取全部记录列表
  146. * 包括被删除的status = 0
  147. */
  148. public List<OilDO> getRecordAllList(Long customer_id) {
  149. List<OilDO> recordList = oilRepository.finAllSortByTime(customer_id);
  150. // Collections.reverse(recordList);
  151. log.info("recordList:{}", recordList);
  152. return recordList;
  153. }
  154. /**
  155. * 新增更新
  156. */
  157. public OilDO addUpdateRecord(OilDO oilDO) {
  158. OilDO save = oilRepository.save(oilDO);
  159. return save;
  160. }
  161. /**
  162. * 删除
  163. *
  164. * @return
  165. */
  166. public OilDO deleteRecord(Long id) {
  167. OilDO oilDO = oilRepository.getOne(id);
  168. oilDO.setStatus(0);
  169. return oilRepository.save(oilDO);
  170. }
  171. }