fire_point/src/main/java/com/xkrs/fireseason/FireSeasonConfigServiceImpl.java

96 lines
4.6 KiB
Java

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<String> fireSeasonCountyCodeList = new ArrayList<>();//处于防火季的区划编码列表
List<FireSeasonConfigEntity> 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<String> 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<String, Object> 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();
}
}
}