JPTbMWater.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.commtools.TimeTool;
  5. import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidList;
  6. import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidSS;
  7. import lombok.Data;
  8. import org.hibernate.validator.constraints.Range;
  9. import org.springframework.util.StringUtils;
  10. import java.time.LocalDateTime;
  11. import java.time.format.DateTimeFormatter;
  12. import java.time.temporal.TemporalAdjusters;
  13. /**
  14. * @ClassName JPTbMWater
  15. * @Description: TODO 查询日预测数据
  16. * @Author LX
  17. * @Date 2024/5/27
  18. * @Version V1.0
  19. **/
  20. @Data
  21. public class JPTbMWater {
  22. private Boolean isPage = false;//是否分页 默认false
  23. private String orgId;//水厂id
  24. @Range(min = 0, max = 65535, groups = {CommonParamValidList.class, CommonParamValidSS.class})
  25. private Integer limit = 20;//若分页 默认数为20
  26. @Range(min = 0, max = Integer.MAX_VALUE, groups = {CommonParamValidList.class, CommonParamValidSS.class})
  27. private Integer offset = 0;//若分页 默认起始值为0
  28. private Long startDate;//开始日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00)
  29. private Long endDate;//结束日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00)
  30. //业务字段
  31. private String forDateStr;
  32. public boolean checkValid(){
  33. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  34. if(startDate==null&&endDate!=null){
  35. return false;
  36. }
  37. StringBuilder strSql = new StringBuilder("");
  38. if(!StringUtils.isEmpty(orgId)){
  39. strSql.append(" AND orgId = '"+orgId+"'");
  40. }
  41. if(startDate==null&&endDate==null){
  42. //TODO 说明前端没传,那么默认为当前月往前推到一号作为开始和结束条件
  43. LocalDateTime nowDate = LocalDateTime.now();//结束时间
  44. nowDate = nowDate.with(TemporalAdjusters.lastDayOfMonth());
  45. nowDate = nowDate.withHour(0);
  46. nowDate = nowDate.withMinute(0);
  47. nowDate = nowDate.withSecond(0);
  48. LocalDateTime startLocalDate = nowDate;
  49. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  50. String startLocalDateStr = startLocalDate.format(formatter);
  51. String endLocalDateStr = nowDate.format(formatter);
  52. strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'");
  53. forDateStr = strSql.toString();
  54. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  55. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  56. }else if(startDate!=null&&endDate!=null){
  57. try {
  58. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  59. LocalDateTime endLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(endDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT));
  60. // long monthsBetween = ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
  61. if(startLocalDate.isAfter(endLocalDate)){
  62. return false;
  63. }else if(endLocalDate.isBefore(startLocalDate)){
  64. return false;
  65. }else if(startLocalDate .equals(endLocalDate)||startLocalDate.isBefore(endLocalDate)){
  66. //TODO 说明符合可组成查询日期的条件
  67. //TODO 按照需求,特殊化结束时间再加4天
  68. startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth());
  69. endLocalDate = endLocalDate.minusDays(-4);
  70. String startLocalDateStr = startLocalDate.format(formatter);
  71. String endLocalDateStr = endLocalDate.format(formatter);
  72. strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'");
  73. forDateStr = strSql.toString();
  74. startDate = TimeTool.convertDateStr2UTC(startLocalDateStr);
  75. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  76. }
  77. }catch(Exception ex){
  78. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, "JPCustomerMeterSS", "JPCustomerMeterSS"
  79. ,String.format("errorMsg:%s====>"
  80. ,ex.getLocalizedMessage()));
  81. return false;
  82. }
  83. }else
  84. if(startDate!=null&&endDate==null){//警告没事,增加代码可读性
  85. //TODO 说明startDate传了 endDate没传 , 那么endDate为startDate月的最后一天
  86. LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT))
  87. .with(TemporalAdjusters.firstDayOfMonth());
  88. LocalDateTime endLocalDate = startLocalDate.with(TemporalAdjusters.lastDayOfMonth());
  89. String startLocalDateStr = startLocalDate.format(formatter);
  90. String endLocalDateStr = endLocalDate.format(formatter);
  91. strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'");
  92. forDateStr = strSql.toString();
  93. endDate = TimeTool.convertDateStr2UTC(endLocalDateStr);
  94. }
  95. return true;
  96. }
  97. }