package com.shkpr.service.aimodelpower.jsonbean.zilaishui; import com.global.base.log.LogLevelFlag; import com.global.base.log.LogPrintMgr; import com.shkpr.service.aimodelpower.commtools.TimeTool; import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidList; import com.shkpr.service.aimodelpower.controllervalid.CommonParamValidSS; import lombok.Data; import org.hibernate.validator.constraints.Range; import org.springframework.util.StringUtils; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; /** * @ClassName JPTbMWater * @Description: TODO 查询日预测数据 * @Author LX * @Date 2024/5/27 * @Version V1.0 **/ @Data public class JPTbMWater { private Boolean isPage = false;//是否分页 默认false private String orgId;//水厂id @Range(min = 0, max = 65535, groups = {CommonParamValidList.class, CommonParamValidSS.class}) private Integer limit = 20;//若分页 默认数为20 @Range(min = 0, max = Integer.MAX_VALUE, groups = {CommonParamValidList.class, CommonParamValidSS.class}) private Integer offset = 0;//若分页 默认起始值为0 private Long startDate;//开始日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00) private Long endDate;//结束日期 (实际时间为 年 月 日(就为当月的一号),时分秒为 00:00:00,如2023-03-01 00:00:00) //业务字段 private String forDateStr; public boolean checkValid(){ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if(startDate==null&&endDate!=null){ return false; } StringBuilder strSql = new StringBuilder(""); if(!StringUtils.isEmpty(orgId)){ strSql.append(" AND orgId = '"+orgId+"'"); } if(startDate==null&&endDate==null){ //TODO 说明前端没传,那么默认为当前月往前推到一号作为开始和结束条件 LocalDateTime nowDate = LocalDateTime.now();//结束时间 nowDate = nowDate.with(TemporalAdjusters.lastDayOfMonth()); nowDate = nowDate.withHour(0); nowDate = nowDate.withMinute(0); nowDate = nowDate.withSecond(0); LocalDateTime startLocalDate = nowDate; startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth()); String startLocalDateStr = startLocalDate.format(formatter); String endLocalDateStr = nowDate.format(formatter); strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'"); forDateStr = strSql.toString(); startDate = TimeTool.convertDateStr2UTC(startLocalDateStr); endDate = TimeTool.convertDateStr2UTC(endLocalDateStr); }else if(startDate!=null&&endDate!=null){ try { LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT)); LocalDateTime endLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(endDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT)); // long monthsBetween = ChronoUnit.MONTHS.between(startLocalDate, endLocalDate); if(startLocalDate.isAfter(endLocalDate)){ return false; }else if(endLocalDate.isBefore(startLocalDate)){ return false; }else if(startLocalDate .equals(endLocalDate)||startLocalDate.isBefore(endLocalDate)){ //TODO 说明符合可组成查询日期的条件 //TODO 按照需求,特殊化结束时间再加4天 startLocalDate = startLocalDate.with(TemporalAdjusters.firstDayOfMonth()); endLocalDate = endLocalDate.minusDays(-4); String startLocalDateStr = startLocalDate.format(formatter); String endLocalDateStr = endLocalDate.format(formatter); strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'"); forDateStr = strSql.toString(); startDate = TimeTool.convertDateStr2UTC(startLocalDateStr); endDate = TimeTool.convertDateStr2UTC(endLocalDateStr); } }catch(Exception ex){ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, "JPCustomerMeterSS", "JPCustomerMeterSS" ,String.format("errorMsg:%s====>" ,ex.getLocalizedMessage())); return false; } }else if(startDate!=null&&endDate==null){//警告没事,增加代码可读性 //TODO 说明startDate传了 endDate没传 , 那么endDate为startDate月的最后一天 LocalDateTime startLocalDate = LocalDateTime.parse(TimeTool.convertUTC2DateStr(startDate, TimeTool.TIMESTAMP_FORMAT), DateTimeFormatter.ofPattern(TimeTool.TIMESTAMP_FORMAT)) .with(TemporalAdjusters.firstDayOfMonth()); LocalDateTime endLocalDate = startLocalDate.with(TemporalAdjusters.lastDayOfMonth()); String startLocalDateStr = startLocalDate.format(formatter); String endLocalDateStr = endLocalDate.format(formatter); strSql.append(" AND \"Date\" BETWEEN '"+startLocalDateStr+"' "+"AND '"+endLocalDateStr+"'"); forDateStr = strSql.toString(); endDate = TimeTool.convertDateStr2UTC(endLocalDateStr); } return true; } }