diff --git a/src/main/java/com/xkrs/dao/FireSeasonConfigDao.java b/src/main/java/com/xkrs/fireseason/FireSeasonConfigDao.java similarity index 83% rename from src/main/java/com/xkrs/dao/FireSeasonConfigDao.java rename to src/main/java/com/xkrs/fireseason/FireSeasonConfigDao.java index 204af60..83dc981 100644 --- a/src/main/java/com/xkrs/dao/FireSeasonConfigDao.java +++ b/src/main/java/com/xkrs/fireseason/FireSeasonConfigDao.java @@ -1,6 +1,5 @@ -package com.xkrs.dao; +package com.xkrs.fireseason; -import com.xkrs.model.entity.FireSeasonConfigEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Component; diff --git a/src/main/java/com/xkrs/model/entity/FireSeasonConfigEntity.java b/src/main/java/com/xkrs/fireseason/FireSeasonConfigEntity.java similarity index 83% rename from src/main/java/com/xkrs/model/entity/FireSeasonConfigEntity.java rename to src/main/java/com/xkrs/fireseason/FireSeasonConfigEntity.java index 3b58c3d..37821dc 100644 --- a/src/main/java/com/xkrs/model/entity/FireSeasonConfigEntity.java +++ b/src/main/java/com/xkrs/fireseason/FireSeasonConfigEntity.java @@ -1,4 +1,4 @@ -package com.xkrs.model.entity; +package com.xkrs.fireseason; import javax.persistence.*; import java.io.Serializable; @@ -101,13 +101,6 @@ public class FireSeasonConfigEntity implements Serializable { @Override public String toString() { - return "FireSeasonConfigEntity{" + - "id=" + id + - ", configLevel=" + configLevel + - ", startTime='" + startTime + '\'' + - ", endTime='" + endTime + '\'' + - ", countyCode='" + countyCode + '\'' + - ", countyName='" + countyName + '\'' + - '}'; + return "FireSeasonConfigEntity{" + "id=" + id + ", configLevel=" + configLevel + ", startTime='" + startTime + '\'' + ", endTime='" + endTime + '\'' + ", countyCode='" + countyCode + '\'' + ", countyName='" + countyName + '\'' + '}'; } } diff --git a/src/main/java/com/xkrs/fireseason/FireSeasonConfigService.java b/src/main/java/com/xkrs/fireseason/FireSeasonConfigService.java new file mode 100644 index 0000000..b371562 --- /dev/null +++ b/src/main/java/com/xkrs/fireseason/FireSeasonConfigService.java @@ -0,0 +1,7 @@ +package com.xkrs.fireseason; + +public interface FireSeasonConfigService { + + String updateFireSeasonConfig(); + +} diff --git a/src/main/java/com/xkrs/fireseason/FireSeasonConfigServiceImpl.java b/src/main/java/com/xkrs/fireseason/FireSeasonConfigServiceImpl.java new file mode 100644 index 0000000..834bd60 --- /dev/null +++ b/src/main/java/com/xkrs/fireseason/FireSeasonConfigServiceImpl.java @@ -0,0 +1,95 @@ +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; +import com.xkrs.helper.FirePointPushManager; +import com.xkrs.helper.FirePointQueryManager; +import com.xkrs.utilsnew.CountyCodeUtils; +import com.xkrs.utilsnew.DateTimeUtils; +import com.xkrs.utilsnew.ListUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; + +import static com.xkrs.utilsold.HttpClientUtils.sendHttpPostTextPlain; + +@Service +public class FireSeasonConfigServiceImpl implements FireSeasonConfigService { + + public static Logger log = LoggerFactory.getLogger(FireSeasonConfigServiceImpl.class); + + private final Locale locale = LocaleContextHolder.getLocale(); + + @Resource + private FirePointChannelConfigDao channelConfigDao; + + @Resource + private FirePointOrdinaryDao firePointOrdinaryDao; + + @Resource + private FirePointQueryManager firePointQueryManager; + + @Resource + private FirePointPushManager firePointPushManager; + + @Resource + private SysUserDao sysUserDao; + + @Resource + private StreetDao streetDao; + + private FireSeasonConfigDao fireSeasonConfigDao; + + @Override + public String updateFireSeasonConfig() { + try { + long timeTag1 = System.currentTimeMillis(); + log.info("开始更新订阅:"); + LocalDateTime now = LocalDateTime.now();//当前时间,用来判断是否处于防火季 + List fireSeasonCountyCodeList = new ArrayList<>();//处于防火季的区划编码列表 + List fireSeasonConfigList = fireSeasonConfigDao.findAll();//获取全部防火季配置项 + for (FireSeasonConfigEntity fireSeasonConfig : fireSeasonConfigList) { + String startTime = fireSeasonConfig.getStartTime();//开始时间 + String endTime = fireSeasonConfig.getEndTime();//结束时间 + String countyCode = fireSeasonConfig.getCountyCode();//区划编码 + DateTimeUtils.checkDateTimeFormat(startTime);//校验开始时间格式 + DateTimeUtils.checkDateTimeFormat(endTime);//校验结束时间格式 + LocalDateTime oldStartLocalDateTime = DateTimeUtils.formatToLocalDateTime(startTime);//解析原始的开始时间 + LocalDateTime oldEndLocalDateTime = DateTimeUtils.formatToLocalDateTime(endTime);//解析原始的结束时间 + LocalDateTime newStartLocalDateTime = oldStartLocalDateTime.plusYears(LocalDateTime.now().getYear() - oldStartLocalDateTime.getYear());//同步到今年的开始时间 + LocalDateTime newEndLocalDateTime = oldEndLocalDateTime.plusYears(LocalDateTime.now().getYear() - oldEndLocalDateTime.getYear());//同步到今年的结束时间 + if (newStartLocalDateTime.isBefore(now) && newEndLocalDateTime.isAfter(now)) { + fireSeasonCountyCodeList.add(countyCode);//这个区划现在正处于防火季,加入到订阅列表中 + } + } + System.out.println("区划信息列表(收集完毕):" + CountyCodeUtils.obtainLogInfo(fireSeasonConfigList, fireSeasonCountyCodeList)); + List collectList = CountyCodeUtils.mergeCountyCodeList(fireSeasonCountyCodeList).stream().sorted(String::compareTo).collect(Collectors.toList()); + System.out.println("区划信息列表(收集、合并、排序完毕):" + CountyCodeUtils.obtainLogInfo(fireSeasonConfigList, collectList)); + //发送订阅请求 + String subscribeUrl = "http://118.24.27.47:5060/push-firepoint/ModifySystemMessages"; + Map map = new HashMap<>(); + map.put("name", "秸秆火系统正式版"); + map.put("url", "http://118.24.27.47:6801/updateFirePoint"); + map.put("param", ListUtils.fromStringList(collectList, ",")); + map.put("typeOfLand", "耕地"); + String updateResult = sendHttpPostTextPlain(subscribeUrl, new ObjectMapper().writeValueAsString(map)); + long timeTag3 = System.currentTimeMillis(); + log.info("更新订阅结束,总耗时:" + ((timeTag3 - timeTag1) / 1000D) + "秒。"); + return updateResult; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + + } + +} diff --git a/src/main/java/com/xkrs/helper/FirePointSubscribeManager.java b/src/main/java/com/xkrs/helper/FirePointSubscribeManager.java index a97b3b9..c62e7e1 100644 --- a/src/main/java/com/xkrs/helper/FirePointSubscribeManager.java +++ b/src/main/java/com/xkrs/helper/FirePointSubscribeManager.java @@ -3,7 +3,6 @@ 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; @@ -59,7 +58,7 @@ public class FirePointSubscribeManager { } //在防火季,就获取全部系统用户 List countyCodeList = obtainCountyCodeList(sysUserDao.findAll()); - List mergedCountyCodeList = mergeCountyCodeList(countyCodeList); + List mergedCountyCodeList = CountyCodeUtils.mergeCountyCodeList(countyCodeList); mergedCountyCodeList.sort(Comparator.naturalOrder()); return mergedCountyCodeList; } @@ -134,71 +133,4 @@ public class FirePointSubscribeManager { return countyCodeList; } - /** - * 合并区划编码 - * 将获取的全部VIP用户的区划编码,进行合并,去除冗余 - */ - 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; - } - - /** - * 在mergedCountyCodeList中检查传入的countyCode是否被合并过了 - * 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) || 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; - } - } diff --git a/src/main/java/com/xkrs/service/FireSeasonConfigService.java b/src/main/java/com/xkrs/service/FireSeasonConfigService.java deleted file mode 100644 index d8f175c..0000000 --- a/src/main/java/com/xkrs/service/FireSeasonConfigService.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.xkrs.service; - -public interface FireSeasonConfigService { - -} diff --git a/src/main/java/com/xkrs/service/impl/FireSeasonConfigServiceImpl.java b/src/main/java/com/xkrs/service/impl/FireSeasonConfigServiceImpl.java deleted file mode 100644 index 735e390..0000000 --- a/src/main/java/com/xkrs/service/impl/FireSeasonConfigServiceImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.xkrs.service.impl; - -import com.xkrs.dao.FirePointChannelConfigDao; -import com.xkrs.dao.FirePointOrdinaryDao; -import com.xkrs.dao.StreetDao; -import com.xkrs.dao.SysUserDao; -import com.xkrs.helper.FirePointPushManager; -import com.xkrs.helper.FirePointQueryManager; -import com.xkrs.service.FireSeasonConfigService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; -import java.util.Locale; - -@Service -public class FireSeasonConfigServiceImpl implements FireSeasonConfigService { - - public static Logger log = LoggerFactory.getLogger(FireSeasonConfigServiceImpl.class); - - private final Locale locale = LocaleContextHolder.getLocale(); - - @Resource - private FirePointChannelConfigDao channelConfigDao; - - @Resource - private FirePointOrdinaryDao firePointOrdinaryDao; - - @Resource - private FirePointQueryManager firePointQueryManager; - - @Resource - private FirePointPushManager firePointPushManager; - - @Resource - private SysUserDao sysUserDao; - - @Resource - private StreetDao streetDao; - -} diff --git a/src/main/java/com/xkrs/utilsnew/CountyCodeUtils.java b/src/main/java/com/xkrs/utilsnew/CountyCodeUtils.java new file mode 100644 index 0000000..061d012 --- /dev/null +++ b/src/main/java/com/xkrs/utilsnew/CountyCodeUtils.java @@ -0,0 +1,105 @@ +package com.xkrs.utilsnew; + +import com.xkrs.fireseason.FireSeasonConfigEntity; +import com.xkrs.model.bean.DataWrapper4; + +import java.util.ArrayList; +import java.util.List; + +public class CountyCodeUtils { + + private CountyCodeUtils() { + } + + /** + * 合并区划编码 + * 将获取的全部VIP用户的区划编码,进行合并,去除冗余 + */ + public static 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; + } + + /** + * 在mergedCountyCodeList中检查传入的countyCode是否被合并过了 + * 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 static 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) || 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 static 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; + } + + /** + * 打印countyCodeList的信息 + */ + public static String obtainLogInfo(List fireSeasonConfigList, List countyCodeList) { + try { + List list = new ArrayList<>(); + for (String countyCode : countyCodeList) { + list.add(countyCode + "(" + getCountyName(fireSeasonConfigList, countyCode) + ")"); + } + return "[" + ListUtils.fromStringList(list, ",") + "]"; + } catch (Exception e) { + e.printStackTrace(); + } + return "null"; + } + + private static String getCountyName(List fireSeasonConfigList, String countyCode) { + for (FireSeasonConfigEntity fireSeasonConfig : fireSeasonConfigList) { + if (fireSeasonConfig.getCountyCode().equals(countyCode)) { + return fireSeasonConfig.getCountyName(); + } + } + return "null"; + } +} diff --git a/src/main/java/com/xkrs/utilsnew/DateTimeUtils.java b/src/main/java/com/xkrs/utilsnew/DateTimeUtils.java index 0881490..c0ef08b 100644 --- a/src/main/java/com/xkrs/utilsnew/DateTimeUtils.java +++ b/src/main/java/com/xkrs/utilsnew/DateTimeUtils.java @@ -1,8 +1,12 @@ package com.xkrs.utilsnew; +import org.apache.hc.core5.util.TextUtils; + import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class DateTimeUtils { @@ -13,6 +17,11 @@ public class DateTimeUtils { return DATE_TIME_FORMATTER_1.format(localDateTime); } + public static LocalDateTime formatToLocalDateTime(String formattedLocalDateTime) { + checkDateTimeFormat(formattedLocalDateTime); + return LocalDateTime.parse(formattedLocalDateTime, DateTimeUtils.DATE_TIME_FORMATTER_1); + } + public static LocalDateTime stringToLocalDateTime(String localDateTimeString) { try { return LocalDateTime.parse(localDateTimeString, DATE_TIME_FORMATTER_1); @@ -21,4 +30,33 @@ public class DateTimeUtils { return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0); } } + + /** + * 判断时间格式 格式必须为 "yyyy-MM-dd HH:mm:ss" + */ + public static boolean isValidDateTime(String formattedLocalDateTime) { + int validLength = 19; + if (TextUtils.isEmpty(formattedLocalDateTime) || formattedLocalDateTime.length() != validLength) { + return false; + } + String regex = "^([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(formattedLocalDateTime); + return matcher.matches(); + } + + public static void checkDateTimeFormat(String formattedLocalDateTime) { + boolean valid = isValidDateTime(formattedLocalDateTime); + if (!valid) { + throw new RuntimeException("日期时间格式校验失败:(" + formattedLocalDateTime + ")"); + } + } + + public static void main(String[] args) { + + boolean validDateTime = isValidDateTime("2222-01-21 09:09:59"); + System.out.println("validDateTime = " + validDateTime); + + } + } diff --git a/src/main/java/com/xkrs/utilsold/DateTimeUtil.java b/src/main/java/com/xkrs/utilsold/DateTimeUtil.java index f1c8cfc..e009765 100644 --- a/src/main/java/com/xkrs/utilsold/DateTimeUtil.java +++ b/src/main/java/com/xkrs/utilsold/DateTimeUtil.java @@ -1,13 +1,9 @@ package com.xkrs.utilsold; -import org.apache.hc.core5.util.TextUtils; - import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * 日期时间工具 @@ -76,25 +72,4 @@ public class DateTimeUtil { return COMMON_FORMATTER_DATETIME.format(LocalDateTime.now()); } - /** - * 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss" - */ - public static boolean isValidDateTime(String formattedDateTime) { - int validLength = 19; - if (TextUtils.isEmpty(formattedDateTime) || formattedDateTime.length() != validLength) { - return false; - } - String regex = "^([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(formattedDateTime); - return matcher.matches(); - } - - public static void main(String[] args) { - -// boolean validDateTime = isValidDateTime("2222-01-21 19:59:59"); -// System.out.println("validDateTime = " + validDateTime); - - } - }