fire_point/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java

459 lines
21 KiB
Java
Raw Normal View History

2021-07-13 10:18:08 +08:00
package com.xkrs.service.impl;
2023-01-05 11:27:02 +08:00
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.common.encapsulation.PromptMessageEnum;
2021-07-16 10:47:24 +08:00
import com.xkrs.dao.*;
import com.xkrs.model.entity.*;
2021-07-13 10:18:08 +08:00
import com.xkrs.model.qo.FirePointQo;
2022-05-07 15:20:28 +08:00
import com.xkrs.model.vo.GaoDeIgGeocodeVo;
2021-07-13 10:18:08 +08:00
import com.xkrs.service.FirePointService;
import com.xkrs.service.GlobalConfigService;
import com.xkrs.service.StreetService;
import com.xkrs.sms.PushHelper;
2022-07-06 15:43:26 +08:00
import com.xkrs.sms.SMSHelper;
2021-07-16 10:47:24 +08:00
import com.xkrs.utils.*;
2021-07-14 17:35:10 +08:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.i18n.LocaleContextHolder;
2021-07-13 10:18:08 +08:00
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
2021-07-16 10:47:24 +08:00
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
2021-07-15 15:22:23 +08:00
import java.time.LocalDate;
2021-07-13 10:18:08 +08:00
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.utils.DateTimeUtil.dateTimeToString;
import static com.xkrs.utils.XlsxUtil.downloadXlsx;
2021-07-13 10:18:08 +08:00
/**
* @author XinYi Song
*/
@CacheConfig(cacheNames = "FirePointServiceCache")
2021-07-13 10:18:08 +08:00
@Service
public class FirePointServiceImpl implements FirePointService {
2021-07-14 17:35:10 +08:00
public static Logger log = LoggerFactory.getLogger(FirePointServiceImpl.class);
2022-08-23 14:42:31 +08:00
private final Locale locale = LocaleContextHolder.getLocale();
2021-07-13 10:18:08 +08:00
@Resource
private FirePointDao firePointDao;
@Resource
private SysUserDao sysUserDao;
2022-05-07 15:31:25 +08:00
@Resource
private StreetDao streetDao;
@Resource
private CountyCodeWeiXinDao countyCodeWeiXinDao;
@Resource
private RelRoleAuthorityDao relRoleAuthorityDao;
2022-06-07 16:24:00 +08:00
@Resource
private FirePointQueryHelper firePointQueryHelper;
2022-06-16 11:27:42 +08:00
@Resource
private GlobalConfigService globalConfigService;
2022-06-07 16:24:00 +08:00
@Resource
private StreetService streetService;
@Resource
2022-07-06 15:43:26 +08:00
private AgentOrgDao agentOrgDao;
@Resource
private SMSHelper smsHelper;
@Resource
private PushHelper pushHelper;
2021-07-13 10:18:08 +08:00
/**
* 添加火点信息
*/
2022-05-06 17:49:37 +08:00
@CacheEvict(value = "FirePointServiceCache", allEntries = true)
2021-07-13 10:18:08 +08:00
@Override
2022-08-23 11:06:30 +08:00
public boolean insertFirePoint(FirePointQo firePointQo) {
if ("水体".equals(firePointQo.getLandtype())) {
return false;
}
2022-08-23 11:06:30 +08:00
if ("Himawari 8".equals(firePointQo.getSatelliteType())) {
if ("N".equals(firePointQo.getConfidence()) || "L".equals(firePointQo.getConfidence())) {
return false;
}
}
FirePointEntity firePointEntity = new FirePointEntity();
firePointEntity.setFireCode(firePointQo.getFireCode());
firePointEntity.setCountyCode(firePointQo.getCountyCode().toString());
firePointEntity.setCountyName(firePointQo.getCountyName());
firePointEntity.setSatelliteTime(DateTimeUtil.timeMillisToString(firePointQo.getSatelliteTimeTs().longValue()));
firePointEntity.setLongitude(firePointQo.getLongitude());
firePointEntity.setLatitude(firePointQo.getLatitude());
firePointEntity.setSatelliteType(firePointQo.getSatelliteType());
firePointEntity.setLandType(firePointQo.getLandtype());
firePointEntity.setConfidence(firePointQo.getConfidence());
firePointEntity.setAddTime(dateTimeToString(LocalDateTime.now()));
firePointEntity.setFireType("0");
firePointEntity.setFireImage(firePointQo.getFireImage());
firePointEntity.setSatelliteImage(firePointQo.getSatelliteImage());
2022-06-02 10:53:48 +08:00
firePointEntity.setAuditFireType("0");//初始化火点为未审核状态
2023-01-05 08:47:49 +08:00
bindAddress(firePointEntity);
2022-08-23 13:40:47 +08:00
if (checkDuplicatedData(firePointEntity)) {
2023-01-05 11:12:02 +08:00
log.info("-------该火点和历史火点重复,不入库");
2022-08-23 11:06:30 +08:00
return false;
}
2023-01-05 11:12:02 +08:00
log.info("-------发现新火点");
firePointDao.save(firePointEntity);
2023-01-05 11:27:02 +08:00
forwardPoint(firePointEntity);
2023-01-05 11:12:02 +08:00
//发送消息通知
sendBroadcast(firePointEntity);
return true;
// if (firePointEntity.getCountyCode().startsWith("37")) {
// //如果开关已打开,就只将未审核的火点添加到山东临时表
// log.info("-------发现新山东火点");
// ShanDongFirePointEntity shanDongFirePointByFirePoint = getShanDongFirePointByFirePoint(firePointEntity);
// ShanDongFirePointEntity savedShanDongFirePoint = shanDongFirePointDao.save(shanDongFirePointByFirePoint);
// //微信消息通知火点审核工作组
// try {
// String messageContent = getMessageContent(savedShanDongFirePoint);
// WDWxSendMsgUtil.sendMsg("18447024917@chatroom", messageContent, 0);
// } catch (Exception e) {
// e.printStackTrace();
// }
// return true;
// }
2021-07-13 10:18:08 +08:00
}
2021-07-15 15:22:23 +08:00
2023-01-05 11:27:02 +08:00
private void forwardPoint(FirePointEntity firePointEntity) {
try {
String url = "http://121.36.229.60:6811/insertfirepointchannelThree";
HttpClientUtils.sendHttpPostTextPlain(url, new ObjectMapper().writeValueAsString(firePointEntity));
} catch (Exception e) {
e.printStackTrace();
}
}
2022-08-23 13:40:47 +08:00
/**
* 检查重复数据
*
* @param firePointEntity 待入库实体类
* @return true-是重复数据拒绝入库false-不是重复数据可以入库
*/
private boolean checkDuplicatedData(FirePointEntity firePointEntity) {
List<FirePointEntity> duplicatedDataList = firePointDao.findDuplicatedData(firePointEntity.getLongitude(), firePointEntity.getLatitude(), firePointEntity.getStreetCode(), firePointEntity.getSatelliteTime(), firePointEntity.getSatelliteType(), firePointEntity.getLandType());
2023-01-06 19:32:34 +08:00
return duplicatedDataList != null && duplicatedDataList.size() > 0;
2022-08-23 13:40:47 +08:00
}
/**
* 为实体类绑定地址编号名称
*/
2023-01-05 08:47:49 +08:00
private void bindAddress(FirePointEntity firePointEntity) {
List<String> locationList = new ArrayList<>();
locationList.add(firePointEntity.getLongitude() + "," + firePointEntity.getLatitude());
GaoDeIgGeocodeVo geocode = GaoDeApiUtil.geocode(locationList);
GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0);
String formattedAddress = reGeoCode.getFormatted_address();
firePointEntity.setFirePointAddress(formattedAddress);
GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0);
firePointEntity.setCountyCode(addressComponent.getAdcode());
firePointEntity.setCountyName(addressComponent.getDistrict());
String townCode = addressComponent.getTowncode();
String fixedTownCode = townCode.length() > 9 ? townCode.substring(0, 9) : townCode;
firePointEntity.setStreetCode(fixedTownCode);
firePointEntity.setStreetName(addressComponent.getTownship());
}
private void sendBroadcast(FirePointEntity firePointEntity) {
//发送微信群聊消息
sendWeChatGroupMessage(firePointEntity);
if ("耕地".equals(firePointEntity.getLandType())) {
//过滤短信通知收件人
List<SysUserEntity> sysUserList = sysUserDao.findAll();
List<RelRoleAuthorityEntity> adminList = relRoleAuthorityDao.selectByAuthorityId(1);
boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L);
boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L);
List<SysUserEntity> smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable);
// //推送
// try {
// List<String> userAccountList = pushHelper.obtainUserAccountList(smsReceiverList);
// pushHelper.dispatchPushMessage(userAccountList, firePointEntity);
// } catch (Exception e) {
// e.printStackTrace();
// }
//对短信通知收件人进行分组
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll();
Map<String, List<SysUserEntity>> agentOrgNameGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, agentOrgList);
//分发短信通知
smsHelper.dispatchSMSMessage(agentOrgNameGroupMap, new String[]{firePointEntity.getFirePointAddress()}, new FirePointEntity[]{firePointEntity});
}
}
/**
* 发送微信消息
*
* @param firePointEntity
*/
private void sendWeChatGroupMessage(FirePointEntity firePointEntity) {
List<CountyCodeWeiXinEntity> countyCodeWeiXinList = countyCodeWeiXinDao.findAll();
if (countyCodeWeiXinList.isEmpty()) {
return;
}
List<String> weixinIdList = new ArrayList<>();
for (CountyCodeWeiXinEntity countyCodeWeiXin : countyCodeWeiXinList) {
2022-06-08 09:23:33 +08:00
try {
2022-08-19 09:22:33 +08:00
if ((countyCodeWeiXin.getSendState() != null) && (countyCodeWeiXin.getSendState() != 0)) {
String formatCutCode = FirePointCodeUtils.getFormatCutCode(countyCodeWeiXin.getCountyCode());
if (firePointEntity.getStreetCode().startsWith(formatCutCode)) {
2022-08-19 09:22:33 +08:00
weixinIdList.add(countyCodeWeiXin.getWeixinId());
}
2022-06-08 09:23:33 +08:00
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (weixinIdList.size() > 0) {
StringBuilder stringBuilder = new StringBuilder("发送微信消息通知:\n");
String messageContent = getMessageContent(firePointEntity);
for (String weixinId : weixinIdList) {
stringBuilder.append(weixinId).append("\n");
try {
WDWxSendMsgUtil.sendMsg(weixinId, messageContent, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
stringBuilder.append("火情信息:").append(messageContent);
log.info(stringBuilder.toString());
}
}
private String getMessageContent(FirePointEntity firePointEntity) {
java.text.DecimalFormat decimalFormat = new DecimalFormat("#.000000");
String satelliteTime = firePointEntity.getSatelliteTime();
String formatLongitude = decimalFormat.format(firePointEntity.getLongitude());
String formatLatitude = decimalFormat.format(firePointEntity.getLatitude());
String countyName = firePointEntity.getCountyName();
String streetName = firePointEntity.getStreetName();
2022-10-24 15:59:01 +08:00
String landType = firePointEntity.getLandType();
String content = firePointEntity.getSatelliteType() + "发现1个火点。\n卫星时间" + satelliteTime + "\nlongitude" + formatLongitude + "\nlatitude" + formatLatitude + "\ncountyName" + countyName + "\nstreetName" + streetName + "\nlandType" + landType;
return content;
}
2022-06-08 09:23:33 +08:00
/**
* 下载VIP用户火点数据
*/
@Override
public String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response) {
LocalDate now = LocalDate.now();
List<Map<String, String>> list = firePointDao.downloadFirePointByVip(dateTimeToString(LocalDateTime.of(now.minusDays(1), LocalTime.MIN)));
if (list.size() == 0) {
2022-08-23 14:42:31 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "无数据!", locale);
}
int year = now.getYear();
int month = now.getMonth().getValue();
int day = now.getDayOfMonth();
String fileName = String.format("%s_%s_%s_VIP用户火点信息列表.xlsx", year, month, day);
response.setContentType("application/octet-stream; charset=UTF8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
try {
downloadXlsx(list, response.getOutputStream());
} catch (IOException e) {
log.error("download vip user's fire point fail: {}", e.getMessage());
}
2022-08-23 14:42:31 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", locale);
}
@Override
public String downloadFirePoint(HttpServletRequest request, HttpServletResponse response, String ids) {
List<String> idList = ListUtils.toStringList(ids, ",");
if (idList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "无数据!", locale);
}
List<Map<String, String>> list = new ArrayList<>();
List<FirePointEntity> sourceList = firePointDao.findAllById(ListUtils.stringListToLongList(idList));
for (FirePointEntity source : sourceList) {
Map<String, String> record = new HashMap<>();
record.put("所在区县", source.getCountyName());
record.put("所在街道", source.getStreetName());
record.put("发现时间", source.getSatelliteTime());
record.put("地物类型", source.getLandType());
record.put("经度", String.valueOf(source.getLongitude()));
record.put("纬度", String.valueOf(source.getLatitude()));
record.put("详细地址", source.getFirePointAddress());
list.add(record);
}
LocalDate now = LocalDate.now();
String fileName = String.format("%s_%s_%s_火点信息列表.xlsx", now.getYear(), now.getMonth().getValue(), now.getDayOfMonth());
response.setContentType("application/octet-stream; charset=UTF8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
try {
downloadXlsx(list, response.getOutputStream());
} catch (IOException e) {
log.error("download fire point fail: {}", e.getMessage());
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", locale);
}
/**
* 动态多条件查询火点列表
*
* @param code 区划编码可以是省市区县街道任意编码如果传需要至少两位不传代表查询全国火点
* @param startTime 过滤火点开始卫星时间不传代表不指定开始时间后台会有限制
* @param endTime 过滤火点结束卫星时间不传代表不指定结束时间
* @param satelliteType 过滤传感器类型不传代表查询全部类型的传感器
* @param landType 过滤地物类型不传代表查询全部地物类型
* @return 符合条件的火点列表
*/
@Override
public List<FirePointEntity> queryFirePoint(String code, String startTime, String endTime, String satelliteType, String landType) {
return firePointQueryHelper.queryFirePoint(code, startTime, endTime, satelliteType, landType);
}
2021-07-15 15:22:23 +08:00
/**
* 查询今天的火点信息
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
2021-07-15 15:22:23 +08:00
@Override
public List<FirePointEntity> selectTodayFirePoint(String countyCode) {
2022-06-08 09:23:33 +08:00
LocalDateTime startTime1 = LocalDateTime.now();
LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0);
String startTimeString = DateTimeUtils.localDateTimeToString(startTime2);
LocalDateTime endTime1 = LocalDateTime.now().plusDays(1);
LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth(), 0, 0, 0, 0);
2022-06-08 09:23:33 +08:00
String endTimeString = DateTimeUtils.localDateTimeToString(endTime2);
return firePointQueryHelper.queryFirePoint(countyCode, startTimeString, endTimeString, null, null);
}
/**
* 查询近一周的火点信息
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
2022-06-08 09:23:33 +08:00
@Override
public List<FirePointEntity> selectFirePointBetweenSeven() {
LocalDateTime startTime1 = LocalDateTime.now().minusDays(7);
LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0);
String startTimeString = DateTimeUtils.localDateTimeToString(startTime2);
LocalDateTime endTime1 = LocalDateTime.now().plusDays(1);
LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth(), 0, 0, 0, 0);
2022-06-08 09:23:33 +08:00
String endTimeString = DateTimeUtils.localDateTimeToString(endTime2);
return firePointQueryHelper.queryFirePoint(null, startTimeString, endTimeString, null, null);
}
/**
* 查询近一个月的火点信息
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
2022-06-08 09:23:33 +08:00
@Override
public List<FirePointEntity> selectFirePointByMonth(String countyCode) {
2022-08-31 10:54:22 +08:00
// LocalDateTime startTime1 = LocalDateTime.now().minusMonths(1);
// LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0);
// String startTimeString = DateTimeUtils.localDateTimeToString(startTime2);
//
// LocalDateTime endTime1 = LocalDateTime.now().plusDays(1);
// LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth(), 0, 0, 0, 0);
// String endTimeString = DateTimeUtils.localDateTimeToString(endTime2);
//
// return firePointQueryHelper.queryFirePoint(countyCode, startTimeString, endTimeString, null, null);
List<FirePointEntity> firePointEntities = firePointQueryHelper.queryFirePoint(countyCode, null, null, null, null);
return firePointEntities;
2021-07-15 15:22:23 +08:00
}
/**
* 查询近一个月的火点数量
2022-05-06 17:49:37 +08:00
*
* @return
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override
public String selectFirePointNumMonth() {
Map map = new HashMap(3);
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = localDateTime.plusDays(1);
String endTime = dateTimeToString(localDateTime2);
// 当前时间减30
LocalDateTime localDateTime1 = localDateTime.minusDays(30);
String startTime = dateTimeToString(localDateTime1);
// 查询近一个月各植被类型的火点数量
List<Map<String, Object>> maps = firePointDao.selectNumByLandType(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("land", maps);
// 查询近一个月各地区火点数量
List<Map<String, Object>> maps1 = firePointDao.selectNumByArea(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("area", maps1);
2022-05-06 17:49:37 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale);
}
/**
* 查询近一天的火点数量
2022-05-06 17:49:37 +08:00
*
* @return
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override
public String selectFirePointNumDay() {
Map map = new HashMap(3);
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = localDateTime.plusDays(1);
String endTime = dateTimeToString(localDateTime2);
// 当前时间减1
LocalDateTime localDateTime1 = localDateTime.minusDays(1);
String startTime = dateTimeToString(localDateTime1);
// 查询近一个月各植被类型的火点数量
List<Map<String, Object>> maps = firePointDao.selectNumByLandType(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("land", maps);
// 查询近一个月各地区火点数量
List<Map<String, Object>> maps1 = firePointDao.selectNumByArea(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("area", maps1);
2022-05-06 17:49:37 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale);
}
/**
* 查询近一周的火点数量
2022-05-06 17:49:37 +08:00
*
* @return
*/
2022-08-31 10:54:22 +08:00
// @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override
public String selectFirePointNumWeek() {
Map map = new HashMap(3);
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = localDateTime.plusDays(1);
String endTime = dateTimeToString(localDateTime2);
// 当前时间减7
LocalDateTime localDateTime1 = localDateTime.minusDays(7);
String startTime = dateTimeToString(localDateTime1);
// 查询近一个月各植被类型的火点数量
List<Map<String, Object>> maps = firePointDao.selectNumByLandType(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("land", maps);
// 查询近一个月各地区火点数量
List<Map<String, Object>> maps1 = firePointDao.selectNumByArea(startTime, endTime);
2022-05-06 17:49:37 +08:00
map.put("area", maps1);
2022-05-06 17:49:37 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale);
}
2021-07-22 11:54:01 +08:00
2021-07-13 10:18:08 +08:00
}