fire_point/src/main/java/com/xkrs/straw/utils/DispatchFirePointUtils.java
2023-02-14 10:12:02 +08:00

135 lines
8.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xkrs.straw.utils;
import com.xkrs.straw.model.bean.FirePointChannelConfigBean;
import com.xkrs.straw.model.entity.FirePointChannelConfigEntity;
import com.xkrs.straw.service.impl.DispatchFirePointServiceImpl;
import com.xkrs.utils.DateTimeUtils;
import com.xkrs.utils.SMSUtils;
import org.apache.hc.core5.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.util.List;
public class DispatchFirePointUtils {
public static Logger log = LoggerFactory.getLogger(DispatchFirePointServiceImpl.class);
private DispatchFirePointUtils() {
}
/**
* 检查火点卫星时间是否在精准渠道配置时间范围内
*/
public static boolean checkIfInPreciseTimeRange(String fireCode, LocalDateTime satelliteLocalDateTime, List<FirePointChannelConfigEntity> firePointChannelConfigEntityList) {
if (firePointChannelConfigEntityList == null || firePointChannelConfigEntityList.size() == 0) {
int resValue1 = 0;
String errorMessage = "星科瑞升-秸秆火后台系统报错-检查火点卫星时间是否在精准渠道配置时间范围内-数据库配置集合-为null或size=0-火点编码=" + fireCode;
try {
resValue1 = SMSUtils.sendSmsToUser(new String[]{"8615764226530"}, "1425995", new String[]{errorMessage});
} catch (Exception e) {
e.printStackTrace();
}
log.info(errorMessage + (resValue1 == 0 ? "--短信通知发送成功" : "--短信通知发送失败"));
return true;
}
for (FirePointChannelConfigEntity firePointChannelConfigEntity : firePointChannelConfigEntityList) {
try {
String jsonContent = firePointChannelConfigEntity.getJsonContent();
FirePointChannelConfigBean firePointChannelConfigBean = JsonUtils.deserialize(jsonContent, FirePointChannelConfigBean.class);
if (FirePointChannelConfigBean.Precise.equals(firePointChannelConfigBean.getChannelName())) {
List<FirePointChannelConfigBean.ChannelConfig> channelConfigList = firePointChannelConfigBean.getConfigList();
if (channelConfigList == null || channelConfigList.size() == 0) {
int resValue1 = 0;
String errorMessage = "星科瑞升-秸秆火后台系统报错-检查火点卫星时间是否在精准渠道配置时间范围内-数据库配置项-时间列表-为null或size=0-火点编码=" + fireCode;
try {
resValue1 = SMSUtils.sendSmsToUser(new String[]{"8615764226530"}, "1425995", new String[]{errorMessage});
} catch (Exception e) {
e.printStackTrace();
}
log.info(errorMessage + (resValue1 == 0 ? "--短信通知发送成功" : "--短信通知发送失败"));
return true;
}
for (FirePointChannelConfigBean.ChannelConfig channelConfig : channelConfigList) {
//预处理开始时间
String startTime = channelConfig.getStartTime();//获取精准火点渠道的开始时间
boolean needCheckStartTime = !TextUtils.isEmpty(startTime);
LocalDateTime sameYearStartTime = null;//计算得出精准渠道和传入火点同年的开始时间
if (needCheckStartTime) {
sameYearStartTime = obtainLocalDateTimeSameYear(satelliteLocalDateTime.getYear(), startTime);
needCheckStartTime = sameYearStartTime != null;
}
//预处理结束时间
String endTime = channelConfig.getEndTime();//获取精准火点渠道的结束时间
boolean needCheckEndTime = !TextUtils.isEmpty(endTime);
LocalDateTime sameYearEndTime = null;//计算得出精准渠道和传入火点同年的结束时间
if (needCheckEndTime) {
sameYearEndTime = obtainLocalDateTimeSameYear(satelliteLocalDateTime.getYear(), endTime);
needCheckEndTime = sameYearEndTime != null;
}
//排列组合4种情况
if (needCheckStartTime) {
if (needCheckEndTime) {
//开始时间、结束时间都需要比较
if (sameYearStartTime.isBefore(satelliteLocalDateTime) && sameYearEndTime.isAfter(satelliteLocalDateTime)) {
String time0 = DateTimeUtils.localDateTimeToString(satelliteLocalDateTime);
String time1 = DateTimeUtils.localDateTimeToString(sameYearStartTime);
String time2 = DateTimeUtils.localDateTimeToString(sameYearEndTime);
log.info("开始时间、结束时间都需要比较,比较结果:属于精准时间范围内,详情:火点时间=" + time0 + ",开始时间=" + time1 + ",结束时间=" + time2);
return true;
}
} else {
//开始时间需要比较,结束时间不需要比较
if (sameYearStartTime.isBefore(satelliteLocalDateTime)) {
String time0 = DateTimeUtils.localDateTimeToString(satelliteLocalDateTime);
String time1 = DateTimeUtils.localDateTimeToString(sameYearStartTime);
log.info("开始时间需要比较,结束时间不需要比较,比较结果:属于精准时间范围内,详情:火点时间=" + time0 + ",开始时间=" + time1);
return true;
}
}
} else {
if (needCheckEndTime) {
//开始时间不需要比较,结束时间需要比较
if (sameYearEndTime.isAfter(satelliteLocalDateTime)) {
String time0 = DateTimeUtils.localDateTimeToString(satelliteLocalDateTime);
String time2 = DateTimeUtils.localDateTimeToString(sameYearEndTime);
log.info("开始时间不需要比较,结束时间需要比较,比较结果:属于精准时间范围内,详情:火点时间=" + time0 + ",结束时间=" + time2);
return true;
}
} else {
//开始时间、结束时间都不需要比较
log.info("开始时间、结束时间都不需要比较,比较结果:属于精准时间范围内");
return true;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
log.info("检查渠道配置时间失败,错误=" + e.getMessage() + ",原始内容=" + firePointChannelConfigEntity.getJsonContent());
}
}
//没有因为遇到符合条件的情况提前退出,就认为不属于精准渠道配置的时间范围之内
return false;
}
/**
* 将formattedTime转化为LocalDateTime格式并将年字段设置为targetYear
* 转化失败返回null
*
* @param formattedTime 2022-01-01 00:00:00
*/
private static LocalDateTime obtainLocalDateTimeSameYear(int targetYear, String formattedTime) {
try {
LocalDateTime localDateTime = LocalDateTime.parse(formattedTime, DateTimeUtils.DATE_TIME_FORMATTER_1);
int dYear = targetYear - localDateTime.getYear();
return localDateTime.plusYears(dYear);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}