JPTbMWater.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.shkpr.service.aimodelpower.jsonbean.zilaishui;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.shkpr.service.aimodelpower.bizmgr.KprAimWaterCollecationBizFun;
  5. import com.shkpr.service.aimodelpower.commtools.TimeTool;
  6. import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidList;
  7. import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidSS;
  8. import lombok.Data;
  9. import org.hibernate.validator.constraints.Range;
  10. import org.springframework.util.StringUtils;
  11. import java.time.LocalDateTime;
  12. import java.time.format.DateTimeFormatter;
  13. import java.time.temporal.TemporalAdjusters;
  14. /**
  15. * @ClassName JPTbMWater
  16. * @Description: TODO 查询日预测数据
  17. * @Author LX
  18. * @Date 2024/5/27
  19. * @Version V1.0
  20. **/
  21. @Data
  22. public class JPTbMWater {
  23. private Boolean isPage = false;//是否分页 默认false
  24. private String orgId;//水厂id
  25. @Range(min = 0, max = 65535, groups = {CommonParamValidList.class, CommonParamValidSS.class})
  26. private Integer limit = 20;//若分页 默认数为20
  27. @Range(min = 0, max = Integer.MAX_VALUE, groups = {CommonParamValidList.class, CommonParamValidSS.class})
  28. private Integer offset = 0;//若分页 默认起始值为0
  29. private Long startDate;//开始日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00)
  30. private Long endDate;//结束日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00)
  31. //业务字段
  32. private String forDateStr;
  33. public boolean checkValid(){
  34. if ("water_volume_prediction_jiangjin2".equals(KprAimWaterCollecationBizFun.databaseName)){
  35. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  36. if (startDate == null && endDate != null) {
  37. return false;
  38. }
  39. StringBuilder strSql = new StringBuilder("");
  40. if (!StringUtils.isEmpty(orgId)) {
  41. strSql.append(" AND \"zone_id\" = '" + orgId + "'");
  42. }
  43. if (startDate == null && endDate == null) {
  44. //TODO 说明前端没传,那么默认为当前月往前推到一号作为开始和结束条件
  45. LocalDateTime nowDate = LocalDateTime.now();//结束时间
  46. nowDate = nowDate.with(TemporalAdjusters.lastDayOfMonth());
  47. nowDate = nowDate.withHour(0);
  48. nowDate = nowDate.withMinute(0);
  49. nowDate = nowDate.withSecond(0);
  50. LocalDateTime startLocalDate = nowDate;
  51. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  52. String startLocalDateStr = startLocalDate.format(formatter);
  53. String endLocalDateStr = nowDate.format(formatter);
  54. strSql.append(" AND \"date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  55. forDateStr = strSql.toString();
  56. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  57. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  58. } else if (startDate != null && endDate != null) {
  59. try {
  60. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  61. LocalDateTime endLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(endDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  62. // long monthsBetween = ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
  63. if (startLocalDate.isAfter(endLocalDate)) {
  64. return false;
  65. } else if (endLocalDate.isBefore(startLocalDate)) {
  66. return false;
  67. } else if (startLocalDate.equals(endLocalDate) || startLocalDate.isBefore(endLocalDate)) {
  68. //TODO 说明符合可组成查询日期的条件
  69. //TODO 按照需求,特殊化结束时间再加4天
  70. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  71. endLocalDate = endLocalDate.minusDays(-4);
  72. String startLocalDateStr = startLocalDate.format(formatter);
  73. String endLocalDateStr = endLocalDate.format(formatter);
  74. strSql.append(" AND \"date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  75. forDateStr = strSql.toString();
  76. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  77. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  78. }
  79. } catch (Exception ex) {
  80. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, "JPCustomerMeterSS", "JPCustomerMeterSS"
  81. , String.format("errorMsg:%s====>"
  82. , ex.getLocalizedMessage()));
  83. return false;
  84. }
  85. } else if (startDate != null && endDate == null) {//警告没事,增加代码可读性
  86. //TODO 说明startDate传了 endDate没传 , 那么endDate为startDate月的最后一天
  87. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT))
  88. .with(TemporalAdjusters.firstDayOfMonth());
  89. LocalDateTime endLocalDate = startLocalDate.with(TemporalAdjusters.lastDayOfMonth());
  90. String startLocalDateStr = startLocalDate.format(formatter);
  91. String endLocalDateStr = endLocalDate.format(formatter);
  92. strSql.append(" AND \"date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  93. forDateStr = strSql.toString();
  94. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  95. }
  96. return true;
  97. }else {
  98. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  99. if (startDate == null && endDate != null) {
  100. return false;
  101. }
  102. StringBuilder strSql = new StringBuilder("");
  103. if (!StringUtils.isEmpty(orgId)) {
  104. strSql.append(" AND \"orgId\" = '" + orgId + "'");
  105. }
  106. if (startDate == null && endDate == null) {
  107. //TODO 说明前端没传,那么默认为当前月往前推到一号作为开始和结束条件
  108. LocalDateTime nowDate = LocalDateTime.now();//结束时间
  109. nowDate = nowDate.with(TemporalAdjusters.lastDayOfMonth());
  110. nowDate = nowDate.withHour(0);
  111. nowDate = nowDate.withMinute(0);
  112. nowDate = nowDate.withSecond(0);
  113. LocalDateTime startLocalDate = nowDate;
  114. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  115. String startLocalDateStr = startLocalDate.format(formatter);
  116. String endLocalDateStr = nowDate.format(formatter);
  117. strSql.append(" AND \"Date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  118. forDateStr = strSql.toString();
  119. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  120. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  121. } else if (startDate != null && endDate != null) {
  122. try {
  123. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  124. LocalDateTime endLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(endDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  125. // long monthsBetween = ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
  126. if (startLocalDate.isAfter(endLocalDate)) {
  127. return false;
  128. } else if (endLocalDate.isBefore(startLocalDate)) {
  129. return false;
  130. } else if (startLocalDate.equals(endLocalDate) || startLocalDate.isBefore(endLocalDate)) {
  131. //TODO 说明符合可组成查询日期的条件
  132. //TODO 按照需求,特殊化结束时间再加4天
  133. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  134. endLocalDate = endLocalDate.minusDays(-4);
  135. String startLocalDateStr = startLocalDate.format(formatter);
  136. String endLocalDateStr = endLocalDate.format(formatter);
  137. strSql.append(" AND \"Date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  138. forDateStr = strSql.toString();
  139. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  140. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  141. }
  142. } catch (Exception ex) {
  143. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, "JPCustomerMeterSS", "JPCustomerMeterSS"
  144. , String.format("errorMsg:%s====>"
  145. , ex.getLocalizedMessage()));
  146. return false;
  147. }
  148. } else if (startDate != null && endDate == null) {//警告没事,增加代码可读性
  149. //TODO 说明startDate传了 endDate没传 , 那么endDate为startDate月的最后一天
  150. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT))
  151. .with(TemporalAdjusters.firstDayOfMonth());
  152. LocalDateTime endLocalDate = startLocalDate.with(TemporalAdjusters.lastDayOfMonth());
  153. String startLocalDateStr = startLocalDate.format(formatter);
  154. String endLocalDateStr = endLocalDate.format(formatter);
  155. strSql.append(" AND \"Date\" BETWEEN '" + startLocalDateStr + "' " + "AND '" + endLocalDateStr + "'");
  156. forDateStr = strSql.toString();
  157. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  158. }
  159. return true;
  160. }
  161. }
  162. }