fire_point/src/main/java/com/xkrs/helper/FirePointSubscribeManager.java
2023-03-08 21:39:52 +08:00

202 lines
9.6 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.helper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.dao.FirePointChannelConfigDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.bean.DataWrapper4;
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 = 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:6802/insertFirePointChannelPrecise");
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) {
boolean inVipTimeRange = VipTimeRangeUtils.checkIfInVipTimeRange(sysUserEntity.getVipTimeRangeJson());
if (inVipTimeRange) {
countyCodeList.add(sysUserEntity.getCountyCode());
}
}
return countyCodeList;
}
/**
* 合并区划编码
* 将获取的全部VIP用户的区划编码进行合并去除冗余
*/
private List<String> mergeCountyCodeList(List<String> countyCodeList) {
List<String> mergedCountyCodeList = new ArrayList<>();
for (String countyCode : countyCodeList) {
DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged = checkMerged(mergedCountyCodeList, countyCode);
Boolean wasMerged = checkMerged.getData1();
Boolean needReplace = checkMerged.getData2();
List<String> oldValueList = checkMerged.getData3();
String newValue = checkMerged.getData4();
if (!wasMerged) {
if (needReplace) {
mergedCountyCodeList.removeAll(oldValueList);
}
mergedCountyCodeList.add(newValue);
}
}
return mergedCountyCodeList;
}
/**
* 在mergedCountyCodeList中检查传入的countyCode是否被合并过了
* DataWrapper4<Boolean, Boolean, String, String>
* Boolean 是否被合并了
* Boolean 如果1==true则2失效如果1==false则2表示这个未被合并的数据是否需要替换如果2==true则将3替换成4如果2==false则只添加4忽略3
* String 如果1==false并且2==true这个数据没有被合并并且它需要替换添加3是待替换的旧值
* String 如果1==false并且2==true这个数据没有被合并并且它需要替换添加4是要替换成的新值如果1==false并且2==false这个数据没有被合并并且它不需要替换添加4是要添加的
*/
private DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged(List<String> mergedCountyCodeList, String countyCode) {
for (String mergedCountyCode : mergedCountyCodeList) {
try {
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
String cutCountyCode = FirePointCodeUtils.getFormatCutCode(countyCode);
if (cutCountyCode.equals(cutMergedCountyCode) || cutCountyCode.startsWith(cutMergedCountyCode)) {
return new DataWrapper4<>(Boolean.TRUE, null, null, null);
}
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
List<String> list = obtainRedundantMergedCountyCodeList(mergedCountyCodeList, cutCountyCode);
return new DataWrapper4<>(Boolean.FALSE, Boolean.TRUE, list, countyCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return new DataWrapper4<>(Boolean.FALSE, Boolean.FALSE, null, countyCode);
}
/**
* 在发现了一个新的、范围更大的区划时,获取这个区划下边已经包含的、冗余的、范围小一点的区划
*/
private List<String> obtainRedundantMergedCountyCodeList(List<String> mergedCountyCodeList, String cutCountyCode) {
List<String> redundantMergedCountyCodeList = new ArrayList<>();
for (String mergedCountyCode : mergedCountyCodeList) {
try {
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
redundantMergedCountyCodeList.add(mergedCountyCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return redundantMergedCountyCodeList;
}
}