FactWellServiceImpl.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.bowintek.practice.services.impl;
  2. import com.bowintek.practice.mapper.cquery.FactWellCQuery;
  3. import com.bowintek.practice.model.EsQueryLog;
  4. import com.bowintek.practice.services.service.FactWellService;
  5. import com.bowintek.practice.util.multipdb.DBTypeEnum;
  6. import com.bowintek.practice.util.multipdb.SwitchDataSource;
  7. import com.bowintek.practice.vo.EsQueryLogVo;
  8. import com.bowintek.practice.vo.FactWell.FactWellGroupList;
  9. import com.github.pagehelper.PageHelper;
  10. import com.github.pagehelper.PageInfo;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Component;
  13. import java.text.Collator;
  14. import java.text.SimpleDateFormat;
  15. import java.util.*;
  16. import java.util.stream.Collectors;
  17. @Component
  18. public class FactWellServiceImpl implements FactWellService {
  19. @Autowired
  20. private FactWellCQuery factWellCQuery;
  21. @Override
  22. @SwitchDataSource(DBTypeEnum.POSTGRE)
  23. public List<FactWellGroupList> getFactWellList(String wellId, String timeType){
  24. if(timeType.equals("day")) return getDayGroupList(wellId);
  25. if(timeType.equals("month")) return getMonthGroupList(wellId);
  26. else return getYearGroupList(wellId);
  27. }
  28. @Override
  29. @SwitchDataSource(DBTypeEnum.POSTGRE)
  30. public List<FactWellGroupList> getFactWellListByTime(String wellId, String timeType, Date startTime, Date endTime){
  31. if(timeType.equals("day")) return getDayGroupList(wellId, startTime, endTime);
  32. if(timeType.equals("month")) return getMonthGroupList(wellId, startTime, endTime);
  33. else return getYearGroupList(wellId, startTime, endTime);
  34. }
  35. public List<FactWellGroupList> getDayGroupList(String wellId){
  36. Calendar calendar = Calendar.getInstance();
  37. calendar.setTime(getToDay()); // 设置当前天日期
  38. calendar.set(Calendar.DAY_OF_MONTH, 1);
  39. calendar.add(Calendar.DAY_OF_MONTH, -1);
  40. //上月最后一天
  41. Date endTime = calendar.getTime();
  42. calendar.add(Calendar.DAY_OF_MONTH, -29);
  43. Date startTime = calendar.getTime();
  44. return getDayGroupList(wellId, startTime, endTime);
  45. }
  46. public List<FactWellGroupList> getDayGroupList(String wellId, Date startTime, Date endTime){
  47. //重新计算当前30天日期
  48. if(startTime==null || endTime==null) return getDayGroupList(wellId);
  49. List<FactWellGroupList> rtnList = factWellCQuery
  50. .getDayGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
  51. //补充列表数据
  52. List<String> dayList = new ArrayList<>();
  53. for(Date time = startTime;time.getTime()<=endTime.getTime();){
  54. Calendar calendarTime = Calendar.getInstance();
  55. calendarTime.setTime(time);
  56. dayList.add(dateToDayString(calendarTime.getTime()));
  57. //下一天
  58. calendarTime.add(Calendar.DAY_OF_MONTH, 1);
  59. time = calendarTime.getTime();
  60. }
  61. rtnList = supplementList(wellId, dayList, rtnList);
  62. return rtnList;
  63. }
  64. public Date getToDay(){
  65. Calendar calendar = Calendar.getInstance();
  66. calendar.set(Calendar.HOUR_OF_DAY, 0);
  67. calendar.set(Calendar.MINUTE, 0);
  68. calendar.set(Calendar.SECOND, 0);
  69. calendar.set(Calendar.MILLISECOND, 0);
  70. return calendar.getTime();
  71. }
  72. public List<FactWellGroupList> getMonthGroupList(String wellId){
  73. Calendar calendar = Calendar.getInstance();
  74. calendar.setTime(getToDay());
  75. calendar.set(Calendar.DAY_OF_MONTH, 1);
  76. calendar.add(Calendar.DAY_OF_MONTH, -1);
  77. //上一月最后一天
  78. Date endTime = calendar.getTime();
  79. //11月前的1号
  80. calendar.add(Calendar.MONTH, -11);
  81. calendar.set(Calendar.DAY_OF_MONTH, 1);
  82. Date startTime = calendar.getTime();
  83. return getMonthGroupList(wellId, startTime, endTime);
  84. }
  85. public List<FactWellGroupList> getMonthGroupList(String wellId, Date startTime, Date endTime){
  86. //重新计算当前12月
  87. if(startTime==null || endTime==null) return getMonthGroupList(wellId);
  88. System.out.println("getMonthGroupList "+dateToDayString(startTime)+" "+dateToDayString(endTime));
  89. List<FactWellGroupList> rtnList = factWellCQuery
  90. .getMonthGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
  91. //补充列表数据
  92. List<String> monthList = new ArrayList<>();
  93. for(Date time = startTime;time.getTime()<=endTime.getTime();){
  94. Calendar calendarTime = Calendar.getInstance();
  95. calendarTime.setTime(time);
  96. monthList.add(dateToMonthString(calendarTime.getTime()));
  97. //下一月
  98. calendarTime.add(Calendar.MONTH, 1);
  99. time = calendarTime.getTime();
  100. }
  101. rtnList = supplementList(wellId, monthList, rtnList);
  102. return rtnList;
  103. }
  104. public List<FactWellGroupList> getYearGroupList(String wellId){
  105. Calendar calendar = Calendar.getInstance();
  106. calendar.setTime(getToDay());
  107. calendar.set(Calendar.MONTH, 12);
  108. calendar.set(Calendar.DAY_OF_MONTH, 1);
  109. calendar.add(Calendar.MONTH, 1);
  110. calendar.add(Calendar.DAY_OF_MONTH, -1);
  111. //当年12月30日
  112. Date endTime = calendar.getTime();
  113. //5年前1月1日
  114. calendar.add(Calendar.YEAR, -4);
  115. calendar.set(Calendar.MONTH, 1);
  116. calendar.set(Calendar.DAY_OF_MONTH, 1);
  117. Date startTime = calendar.getTime();
  118. return getYearGroupList(wellId, startTime, endTime);
  119. }
  120. public List<FactWellGroupList> getYearGroupList(String wellId, Date startTime, Date endTime){
  121. //重新计算5年时间
  122. if(startTime==null || endTime==null) return getYearGroupList(wellId);
  123. System.out.println("getYearGroupList "+dateToDayString(startTime)+" "+dateToDayString(endTime));
  124. List<FactWellGroupList> rtnList = factWellCQuery
  125. .getYearGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
  126. //补充列表数据
  127. List<String> monthList = new ArrayList<>();
  128. for(Date time = startTime;time.getTime()<=endTime.getTime();){
  129. Calendar calendarTime = Calendar.getInstance();
  130. calendarTime.setTime(time);
  131. monthList.add(dateToYearString(calendarTime.getTime()));
  132. //下一年
  133. calendarTime.add(Calendar.YEAR, 1);
  134. time = calendarTime.getTime();
  135. }
  136. rtnList = supplementList(wellId, monthList, rtnList);
  137. return rtnList;
  138. }
  139. public List<FactWellGroupList> supplementList(String wellId, List<String> timeList, List<FactWellGroupList> source){
  140. //if(source.stream().filter(r->r.getTime_str().equals()))
  141. for(int i=0;i<timeList.size();i++){
  142. final String timeStr = timeList.get(i);
  143. if(source.stream().filter(r->r.getTime_str().equals(timeStr))
  144. .collect(Collectors.toList()).size()==0){
  145. source.add(FactWellGroupList.gen(wellId, timeStr));
  146. }
  147. }
  148. Collections.sort(source, new Comparator<FactWellGroupList>() {
  149. public int compare(FactWellGroupList o1, FactWellGroupList o2) {
  150. Collator instance = Collator.getInstance(Locale.CHINA);
  151. return instance.compare(o1.getTime_str(), o2.getTime_str());
  152. }
  153. });
  154. return source;
  155. }
  156. public String dateToDayString(Date time){
  157. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  158. return sdf.format(time);
  159. }
  160. public String dateToMonthString(Date time){
  161. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
  162. return sdf.format(time);
  163. }
  164. public String dateToYearString(Date time){
  165. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  166. return sdf.format(time);
  167. }
  168. }