fire_point/src/main/java/com/xkrs/straw/helper/FirePointSubscribeManager.java

195 lines
9.2 KiB
Java
Raw Normal View History

2023-03-08 11:50:21 +08:00
package com.xkrs.straw.helper;
2023-03-05 20:52:34 +08:00
2023-03-06 17:29:46 +08:00
import com.fasterxml.jackson.databind.ObjectMapper;
2023-03-05 20:52:34 +08:00
import com.xkrs.straw.dao.FirePointChannelConfigDao;
import com.xkrs.straw.dao.SysUserDao;
import com.xkrs.straw.model.bean.DataWrapper4;
import com.xkrs.straw.model.bean.FirePointChannelConfigBean;
import com.xkrs.straw.model.bean.LocalDateTimeRange;
import com.xkrs.straw.model.entity.FirePointChannelConfigEntity;
import com.xkrs.straw.model.entity.SysUserEntity;
2023-03-08 11:21:05 +08:00
import com.xkrs.straw.utilsnew.JsonUtils;
import com.xkrs.straw.utilsnew.VipTimeRangeUtils;
import com.xkrs.straw.utilsnew.DateTimeUtils;
2023-03-08 11:59:44 +08:00
import com.xkrs.straw.utilsnew.FirePointCodeUtils;
2023-03-08 11:21:05 +08:00
import com.xkrs.straw.utilsnew.ListUtils;
2023-03-05 20:52:34 +08:00
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
2023-03-06 17:29:46 +08:00
import java.util.*;
2023-03-08 11:21:59 +08:00
import static com.xkrs.straw.utilsold.HttpClientUtils.sendHttpPost;
2023-03-05 20:52:34 +08:00
@Component
public class FirePointSubscribeManager {
@Resource
private FirePointChannelConfigDao channelConfigDao;
@Resource
private SysUserDao sysUserDao;
2023-03-06 17:29:46 +08:00
public String autoSync() {
2023-03-05 20:52:34 +08:00
List<String> subscribedCountyCodeList = obtainSubscribedCountyCodeList();
2023-03-06 17:29:46 +08:00
return updateSubscribedCountyCodeList(subscribedCountyCodeList);
2023-03-05 20:52:34 +08:00
}
/**
* 获取订阅的区划编码集合
*/
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;
}
2023-03-06 17:29:46 +08:00
private String updateSubscribedCountyCodeList(List<String> subscribedCountyCodeList) {
try {
String paramContent = ListUtils.fromStringList(subscribedCountyCodeList, ",");
System.out.println(paramContent);
String subscribeUrl = "http://192.168.2.46:8800/push-firepoint/ModifySystemMessages";
Map<String, Object> map = new HashMap<>();
map.put("name", "秸秆火系统");
map.put("url", "http://192.168.2.36:6821/insertFirePointChannelPrecise");
map.put("param", paramContent);
map.put("typeOfLand", "耕地");
return sendHttpPost(subscribeUrl, new ObjectMapper().writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
2023-03-05 20:52:34 +08:00
}
/**
* 获取到防火季时间格式集合
*/
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) {
2023-03-06 17:29:46 +08:00
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));
2023-03-05 20:52:34 +08:00
}
}
}
return timeRangeList;
}
2023-03-05 21:00:32 +08:00
/**
* 判断targetTime是否属于timeRangeList范围内
* 实际应用也就是当前时间是否属于防火季
*/
2023-03-05 20:52:34 +08:00
private boolean checkIfIn(List<LocalDateTimeRange> timeRangeList, LocalDateTime targetTime) {
2023-03-05 21:00:32 +08:00
for (LocalDateTimeRange timeRange : timeRangeList) {
LocalDateTime startTime = timeRange.getStartTime();
LocalDateTime endTime = timeRange.getEndTime();
if (startTime.isBefore(targetTime) && endTime.isAfter(targetTime)) {
return true;
}
}
2023-03-05 20:52:34 +08:00
return false;
}
2023-03-05 21:00:32 +08:00
/**
* 获取全部VIP用户的区划编码
*/
2023-03-05 20:52:34 +08:00
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;
}
/**
* 合并区划编码
2023-03-05 21:00:32 +08:00
* 将获取的全部VIP用户的区划编码进行合并去除冗余
2023-03-05 20:52:34 +08:00
*/
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;
}
/**
2023-03-05 21:00:32 +08:00
* 在mergedCountyCodeList中检查传入的countyCode是否被合并过了
2023-03-05 20:52:34 +08:00
* 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);
2023-03-05 21:00:32 +08:00
if (cutCountyCode.equals(cutMergedCountyCode) || cutCountyCode.startsWith(cutMergedCountyCode)) {
2023-03-05 20:52:34 +08:00
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;
}
}