火点重构-完善防火季订阅

This commit is contained in:
liuchengqian 2023-04-12 14:53:47 +08:00
parent 2c0420d20b
commit 61725d63b6
9 changed files with 6 additions and 411 deletions

View File

@ -1,6 +1,6 @@
package com.xkrs.common;
import com.xkrs.helper.FirePointSubscribeManager;
import com.xkrs.fireseason.FireSeasonConfigService;
import com.xkrs.utilsnew.DateTimeUtils;
import com.xkrs.utilsnew.WeiXinMessageUtils;
import org.springframework.context.annotation.Configuration;
@ -18,12 +18,12 @@ import java.time.LocalDateTime;
public class StaticScheduleTask {
@Resource
private FirePointSubscribeManager firePointSubscribeManager;
private FireSeasonConfigService fireSeasonConfigService;
//每小时更新一次订阅
@Scheduled(cron = "0 0 * * * ?")
private void syncSubscribe() {
firePointSubscribeManager.autoSync();
fireSeasonConfigService.updateFireSeasonConfig();
}
//每天中午12点测试一次微信消息推送

View File

@ -4,7 +4,7 @@ import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.FirePointOrdinaryDao;
import com.xkrs.dao.StreetDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.helper.FirePointSubscribeManager;
import com.xkrs.fireseason.FireSeasonConfigService;
import com.xkrs.model.qo.AllFirePointQo;
import com.xkrs.model.qo.UpdateFirePointQo;
import com.xkrs.service.DispatchFirePointService;
@ -31,7 +31,7 @@ public class DispatchFirePointController {
@Resource
private DispatchFirePointService firePointService;
@Resource
private FirePointSubscribeManager firePointSubscribeManager;
private FireSeasonConfigService fireSeasonConfigService;
@Resource
private FirePointOrdinaryDao firePointOrdinaryDao;
@Resource
@ -79,7 +79,7 @@ public class DispatchFirePointController {
@GetMapping("/autoSync")
public String autoSync() {
return firePointSubscribeManager.autoSync();
return fireSeasonConfigService.updateFireSeasonConfig();
}
/**

View File

@ -1,10 +0,0 @@
package com.xkrs.dao;
import com.xkrs.model.entity.FirePointChannelConfigEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;
@Component
public interface FirePointChannelConfigDao extends JpaRepository<FirePointChannelConfigEntity, Long>, JpaSpecificationExecutor<FirePointChannelConfigEntity> {
}

View File

@ -1,7 +1,6 @@
package com.xkrs.fireseason;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.dao.FirePointChannelConfigDao;
import com.xkrs.dao.FirePointOrdinaryDao;
import com.xkrs.dao.StreetDao;
import com.xkrs.dao.SysUserDao;
@ -29,9 +28,6 @@ public class FireSeasonConfigServiceImpl implements FireSeasonConfigService {
private final Locale locale = LocaleContextHolder.getLocale();
@Resource
private FirePointChannelConfigDao channelConfigDao;
@Resource
private FirePointOrdinaryDao firePointOrdinaryDao;

View File

@ -1,136 +0,0 @@
package com.xkrs.helper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.dao.FirePointChannelConfigDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.bean.FirePointChannelConfigBean;
import com.xkrs.model.bean.LocalDateTimeRange;
import com.xkrs.model.entity.FirePointChannelConfigEntity;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.utilsnew.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import static com.xkrs.utilsold.HttpClientUtils.sendHttpPostTextPlain;
@Component
public class FirePointSubscribeManager {
private static final Logger log = LoggerFactory.getLogger(FirePointSubscribeManager.class);
@Resource
private FirePointChannelConfigDao channelConfigDao;
@Resource
private SysUserDao sysUserDao;
public String autoSync() {
long timeTag1 = System.currentTimeMillis();
log.info("开始更新订阅:");
List<String> subscribedCountyCodeList = obtainSubscribedCountyCodeList();
long timeTag2 = System.currentTimeMillis();
log.info("更新订阅计算耗时:" + ((timeTag2 - timeTag1) / 1000D) + "秒。");
String updateResult = updateSubscribedCountyCodeList(subscribedCountyCodeList);
long timeTag3 = System.currentTimeMillis();
log.info("更新订阅请求耗时:" + ((timeTag3 - timeTag2) / 1000D) + "秒。");
log.info("更新订阅结束,总耗时:" + ((timeTag3 - timeTag1) / 1000D) + "秒。");
return updateResult;
}
/**
* 获取订阅的区划编码集合
*/
private List<String> obtainSubscribedCountyCodeList() {
//查询防火季时间范围配置
List<FirePointChannelConfigEntity> channelConfigEntityList = channelConfigDao.findAll();
//获取到防火季时间格式集合
List<LocalDateTimeRange> fireSeasonTimeRangeList = obtainFireSeasonTimeRangeList(channelConfigEntityList);
//判断当前是否处于防火季
boolean checkIfIn = checkIfIn(fireSeasonTimeRangeList, LocalDateTime.now());
//不在防火季就返回空订阅
if (!checkIfIn) {
return new ArrayList<String>();
}
//在防火季就获取全部系统用户
List<String> countyCodeList = obtainCountyCodeList(sysUserDao.findAll());
List<String> mergedCountyCodeList = CountyCodeUtils.mergeCountyCodeList(countyCodeList);
mergedCountyCodeList.sort(Comparator.naturalOrder());
return mergedCountyCodeList;
}
private String updateSubscribedCountyCodeList(List<String> subscribedCountyCodeList) {
try {
String paramContent = ListUtils.fromStringList(subscribedCountyCodeList, ",");
String subscribeUrl = "http://118.24.27.47:5060/push-firepoint/ModifySystemMessages";
Map<String, Object> map = new HashMap<>();
map.put("name", "秸秆火系统正式版");
map.put("url", "http://118.24.27.47:6801/updateFirePoint");
map.put("param", paramContent);
map.put("typeOfLand", "耕地");
return sendHttpPostTextPlain(subscribeUrl, new ObjectMapper().writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
/**
* 获取到防火季时间格式集合
*/
private List<LocalDateTimeRange> obtainFireSeasonTimeRangeList(List<FirePointChannelConfigEntity> channelConfigEntityList) {
List<LocalDateTimeRange> timeRangeList = new ArrayList<>();
for (FirePointChannelConfigEntity channelConfigEntity : channelConfigEntityList) {
String jsonContent = channelConfigEntity.getJsonContent();
FirePointChannelConfigBean firePointChannelConfigBean = JsonUtils.deserialize(jsonContent, FirePointChannelConfigBean.class);
if (FirePointChannelConfigBean.Precise.equals(firePointChannelConfigBean.getChannelName())) {
List<FirePointChannelConfigBean.ChannelConfig> channelConfigList = firePointChannelConfigBean.getConfigList();
for (FirePointChannelConfigBean.ChannelConfig channelConfig : channelConfigList) {
LocalDateTime oldStartTime = LocalDateTime.parse(channelConfig.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1);
LocalDateTime newStartTime = oldStartTime.plusYears(LocalDateTime.now().getYear() - oldStartTime.getYear());
LocalDateTime oldEndTime = LocalDateTime.parse(channelConfig.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1);
LocalDateTime newEndTime = oldEndTime.plusYears(LocalDateTime.now().getYear() - oldEndTime.getYear());
timeRangeList.add(new LocalDateTimeRange(newStartTime, newEndTime));
}
}
}
return timeRangeList;
}
/**
* 判断targetTime是否属于timeRangeList范围内
* 实际应用也就是当前时间是否属于防火季
*/
private boolean checkIfIn(List<LocalDateTimeRange> timeRangeList, LocalDateTime targetTime) {
for (LocalDateTimeRange timeRange : timeRangeList) {
LocalDateTime startTime = timeRange.getStartTime();
LocalDateTime endTime = timeRange.getEndTime();
if (startTime.isBefore(targetTime) && endTime.isAfter(targetTime)) {
return true;
}
}
return false;
}
/**
* 获取全部VIP用户的区划编码
*/
private List<String> obtainCountyCodeList(List<SysUserEntity> sysUserEntityList) {
List<String> countyCodeList = new ArrayList<>();
for (SysUserEntity sysUserEntity : sysUserEntityList) {
if ("管理员".equals(sysUserEntity.getAccountType())) {//管理员用户不计入订阅
continue;
}
boolean inVipTimeRange = VipTimeRangeUtils.checkIfInVipTimeRange(sysUserEntity.getVipTimeRangeJson());
if (inVipTimeRange) {
countyCodeList.add(sysUserEntity.getCountyCode());
}
}
return countyCodeList;
}
}

View File

@ -1,67 +0,0 @@
package com.xkrs.model.bean;
import java.util.List;
public class FirePointChannelConfigBean {
public static final String Ordinary = "Ordinary";
public static final String Precise = "Precise";
/**
* 渠道名称
*/
private String channelName;
/**
* 渠道配置列表
*/
private List<ChannelConfig> configList;
/**
* 渠道配置
*/
public static class ChannelConfig {
/**
* 开始时间
*/
private String startTime;
/**
* 结束时间
*/
private String endTime;
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public List<ChannelConfig> getConfigList() {
return configList;
}
public void setConfigList(List<ChannelConfig> configList) {
this.configList = configList;
}
}

View File

@ -1,50 +0,0 @@
package com.xkrs.model.entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "fire_point_channel_config")
public class FirePointChannelConfigEntity implements Serializable {
/**
* 指定主键建立自增序列主键值取自序列
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "fire_point_ordinary_seq_gen")
@SequenceGenerator(name = "fire_point_ordinary_seq_gen", sequenceName = "fire_point_ordinary_id_seq", allocationSize = 1)
private Long id;
/**
* Json配置内容
*/
@Column(length = 1024, columnDefinition = "varchar(1024)")
private String jsonContent;
public FirePointChannelConfigEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getJsonContent() {
return jsonContent;
}
public void setJsonContent(String jsonContent) {
this.jsonContent = jsonContent;
}
@Override
public String toString() {
return "FirePointChannelConfigEntity{" +
"id=" + id +
", jsonContent='" + jsonContent + '\'' +
'}';
}
}

View File

@ -1,7 +1,6 @@
package com.xkrs.service.impl;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.FirePointChannelConfigDao;
import com.xkrs.dao.FirePointOrdinaryDao;
import com.xkrs.dao.StreetDao;
import com.xkrs.dao.SysUserDao;
@ -41,9 +40,6 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService {
private final Locale locale = LocaleContextHolder.getLocale();
@Resource
private FirePointChannelConfigDao channelConfigDao;
@Resource
private FirePointOrdinaryDao firePointOrdinaryDao;

View File

@ -1,134 +0,0 @@
package com.xkrs.utilsnew;
import com.xkrs.model.bean.FirePointChannelConfigBean;
import com.xkrs.model.entity.FirePointChannelConfigEntity;
import com.xkrs.service.impl.DispatchFirePointServiceImpl;
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 FirePointDispatchUtils {
public static Logger log = LoggerFactory.getLogger(DispatchFirePointServiceImpl.class);
private FirePointDispatchUtils() {
}
/**
* 判断此时是否是防火季节
* 如果是那么审核火点表中数据来源是审核火点
* 如果不是那么审核火点表中数据来源是普通火点
*/
public static boolean ifInFireSeason(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;
}
}