From ea335163b66a8bbea51119feb02f46c14f8fd50a Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Sun, 5 Mar 2023 20:52:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../straw/model/bean/LocalDateTimeRange.java | 34 ++++ .../utils/FirePointSubscribeManager.java | 172 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 src/main/java/com/xkrs/straw/model/bean/LocalDateTimeRange.java create mode 100644 src/main/java/com/xkrs/straw/utils/FirePointSubscribeManager.java diff --git a/src/main/java/com/xkrs/straw/model/bean/LocalDateTimeRange.java b/src/main/java/com/xkrs/straw/model/bean/LocalDateTimeRange.java new file mode 100644 index 0000000..56515a5 --- /dev/null +++ b/src/main/java/com/xkrs/straw/model/bean/LocalDateTimeRange.java @@ -0,0 +1,34 @@ +package com.xkrs.straw.model.bean; + +import java.time.LocalDateTime; + +public class LocalDateTimeRange { + + private LocalDateTime startTime; + + private LocalDateTime endTime; + + public LocalDateTimeRange() { + } + + public LocalDateTimeRange(LocalDateTime startTime, LocalDateTime endTime) { + this.startTime = startTime; + this.endTime = endTime; + } + + public LocalDateTime getStartTime() { + return startTime; + } + + public void setStartTime(LocalDateTime startTime) { + this.startTime = startTime; + } + + public LocalDateTime getEndTime() { + return endTime; + } + + public void setEndTime(LocalDateTime endTime) { + this.endTime = endTime; + } +} diff --git a/src/main/java/com/xkrs/straw/utils/FirePointSubscribeManager.java b/src/main/java/com/xkrs/straw/utils/FirePointSubscribeManager.java new file mode 100644 index 0000000..986ec0e --- /dev/null +++ b/src/main/java/com/xkrs/straw/utils/FirePointSubscribeManager.java @@ -0,0 +1,172 @@ +package com.xkrs.straw.utils; + +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; +import com.xkrs.utils.DateTimeUtils; +import com.xkrs.utils.FirePointCodeUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +@Component +public class FirePointSubscribeManager { + + @Resource + private FirePointChannelConfigDao channelConfigDao; + + @Resource + private SysUserDao sysUserDao; + + public void autoSync() { + List subscribedCountyCodeList = obtainSubscribedCountyCodeList(); + updateSubscribedCountyCodeList(subscribedCountyCodeList); + } + + /** + * 获取订阅的区划编码集合 + */ + private List obtainSubscribedCountyCodeList() { + //查询防火季时间范围配置 + List channelConfigEntityList = channelConfigDao.findAll(); + //获取到防火季时间格式集合 + List fireSeasonTimeRangeList = obtainFireSeasonTimeRangeList(channelConfigEntityList); + //判断当前是否处于防火季 + boolean checkIfIn = checkIfIn(fireSeasonTimeRangeList, LocalDateTime.now()); + //不在防火季就返回空订阅 + if (!checkIfIn) { + return new ArrayList(); + } + //在防火季,就获取全部系统用户 + List countyCodeList = obtainCountyCodeList(sysUserDao.findAll()); + List mergedCountyCodeList = mergeCountyCodeList(countyCodeList); + mergedCountyCodeList.sort(Comparator.naturalOrder()); + return mergedCountyCodeList; + } + + private void updateSubscribedCountyCodeList(List subscribedCountyCodeList) { +// String url = "http://118.24.27.47:10721/winxin_api/msg"; +// Map map = new HashMap<>(); +// map.put("wxid", wxid); +// map.put("text", text); +// map.put("type", type); +// sendHttpPost(url, new ObjectMapper().writeValueAsString(map)); + } + + /** + * 获取到防火季时间格式集合 + */ + private List obtainFireSeasonTimeRangeList(List channelConfigEntityList) { + List 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 channelConfigList = firePointChannelConfigBean.getConfigList(); + for (FirePointChannelConfigBean.ChannelConfig channelConfig : channelConfigList) { + LocalDateTime startLocalDateTime = LocalDateTime.parse(channelConfig.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + startLocalDateTime.plusYears(LocalDateTime.now().getYear() - startLocalDateTime.getYear()); + LocalDateTime endLocalDateTime = LocalDateTime.parse(channelConfig.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + endLocalDateTime.plusYears(LocalDateTime.now().getYear() - endLocalDateTime.getYear()); + timeRangeList.add(new LocalDateTimeRange(startLocalDateTime, endLocalDateTime)); + } + } + } + return timeRangeList; + } + + private boolean checkIfIn(List timeRangeList, LocalDateTime targetTime) { + + return false; + } + + private List obtainCountyCodeList(List sysUserEntityList) { + List countyCodeList = new ArrayList<>(); + for (SysUserEntity sysUserEntity : sysUserEntityList) { + boolean inVipTimeRange = VipTimeRangeUtils.checkIfInVipTimeRange(sysUserEntity.getVipTimeRangeJson()); + if (inVipTimeRange) { + countyCodeList.add(sysUserEntity.getCountyCode()); + } + } + return countyCodeList; + } + + /** + * 合并区划编码 + */ + private List mergeCountyCodeList(List countyCodeList) { + List mergedCountyCodeList = new ArrayList<>(); + for (String countyCode : countyCodeList) { + DataWrapper4, String> checkMerged = checkMerged(mergedCountyCodeList, countyCode); + Boolean wasMerged = checkMerged.getData1(); + Boolean needReplace = checkMerged.getData2(); + List oldValueList = checkMerged.getData3(); + String newValue = checkMerged.getData4(); + if (!wasMerged) { + if (needReplace) { + mergedCountyCodeList.removeAll(oldValueList); + } + mergedCountyCodeList.add(newValue); + } + } + return mergedCountyCodeList; + } + + /** + * 检查是否被合并过了 + * DataWrapper4 + * 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, String> checkMerged(List mergedCountyCodeList, String countyCode) { + for (String mergedCountyCode : mergedCountyCodeList) { + try { + String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode); + String cutCountyCode = FirePointCodeUtils.getFormatCutCode(countyCode); + if (cutCountyCode.equals(cutMergedCountyCode)) { + continue; + } + if (cutCountyCode.startsWith(cutMergedCountyCode)) { + return new DataWrapper4<>(Boolean.TRUE, null, null, null); + } + if (cutMergedCountyCode.startsWith(cutCountyCode)) { + List 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 obtainRedundantMergedCountyCodeList(List mergedCountyCodeList, String cutCountyCode) { + List 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; + } + + +}