package com.sooka.sponest.data.housingconstruction.handler; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.security.utils.DictUtils; import com.ruoyi.system.api.RemoteConfigService; import com.ruoyi.system.api.domain.SysDictData; import com.sooka.sponest.data.housingconstruction.domain.BO.SteelcylinderBO; import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionEnterprise; import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionEnterpriseproductionreport; import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionSteelcylinder; import com.sooka.sponest.data.housingconstruction.domain.eventEntity.GpsEntity; import com.sooka.sponest.data.housingconstruction.domain.eventEntity.SensorEventVo; import com.sooka.sponest.data.housingconstruction.domain.eventEntity.StaticEntity; import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionCarService; import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionEnterpriseService; import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionEnterpriseproductionreportService; import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionSteelcylinderService; import org.apache.commons.collections4.MapUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Optional; @RestController @RequestMapping("/taskHandler") public class TaskHandler { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private ICenterdataTHousingconstructionEnterpriseproductionreportService reportService; @Autowired private ICenterdataTHousingconstructionEnterpriseService enterpriseService; @Autowired private ICenterdataTHousingconstructionSteelcylinderService steelcylinderService; @Autowired private ICenterdataTHousingconstructionCarService carService; @Autowired private SendEvent sendEvent; /** * 企业生产报告运行预警 */ @GetMapping("/enterpriseProductionReportTask") // 每天午夜 12 点执行 // @Scheduled(cron = "0 0 0 * * ?") public void enterpriseProductionReportTask() { //字典列表 List reportCacheList = DictUtils.getDictCache("enterprise_production_report"); //企业列表 List enterpriseList = enterpriseService.selectCenterdataTHousingconstructionEnterpriseList(new CenterdataTHousingconstructionEnterprise()); //获取每个企业每种报告的最新记录 List dataList = reportService.getNewReport(new CenterdataTHousingconstructionEnterpriseproductionreport()); //遍历企业列表,获取企业信息 for (CenterdataTHousingconstructionEnterprise enterprise : enterpriseList) { StringBuilder enterpriseStr = new StringBuilder();//事故隐患排查治理制度,安全事故应急救援预案, //遍历字典集合,获取字典类型 for (SysDictData sysDictData : reportCacheList) { //设置标识,最终根据标识判断是否序号将当前类型的报告拼接到事件描述中 boolean flag = true; //遍历业务结果集 for (CenterdataTHousingconstructionEnterpriseproductionreport report : dataList) { //如果企业id相同 并且 文件类型相同 if (report.getEnterpriseId().equals(enterprise.getId()) && report.getReportType().equals(sysDictData.getDictValue())) { //获取文件上传时间 + 阈值30天 与当前时间做比较 //获取数据的上传时间 Date createTime = report.getCreateTime(); // 将 Date 转换为 LocalDateTime LocalDateTime localDateTime = convertToLocalDateTime(createTime); // 在 LocalDateTime 上加上 30 天 LocalDateTime increaseBy30Days = localDateTime.plus(30, ChronoUnit.DAYS); // 获取当前日期和时间 LocalDateTime now = LocalDateTime.now(); //文件上传时间没有超过设定的阈值, 不需要上报 if (increaseBy30Days.isAfter(now)) { //拼接 事件描述 flag = false; } break; } } if (flag) { enterpriseStr.append(sysDictData.getDictLabel()).append(","); } } if (enterpriseStr.length() > 0) { enterpriseStr.deleteCharAt(enterpriseStr.length() - 1); enterpriseStr.append(")发生%s, 超过30天未上传, 请及时处理!%s%s"); enterpriseStr.insert(0, enterprise.getEnterpriseName() + "的("); //创建事件实体类 SensorEventVo sensorEventVo = new SensorEventVo( enterprise.getLongitude(), enterprise.getLatitude(), enterprise.getId(), enterprise.getEnterpriseName(), new Date(), enterprise.getRegisteredAddress() ); //发送事件 sendEvent.sendEvent(enterpriseStr.toString(), StaticEntity.ENTERPRISE_NAME, "", "", sensorEventVo, StaticEntity.ENTERPRISE_CODE); } } // 每个企业每种类型的文件单独上报代码 /*//获取报告类型 reportCacheList.forEach(sysDictData -> { //获取企业信息 enterpriseList.forEach(enterprise -> { //设置事件标识, 当标识为true时, 发送事件 boolean flag = true; for (CenterdataTHousingconstructionEnterpriseproductionreport data : dataList) { if (sysDictData.getDictValue().equals(data.getReportType()) && enterprise.getId().equals(data.getEnterpriseId())) { //获取数据的上传时间 Date createTime = data.getCreateTime(); // 将 Date 转换为 LocalDateTime LocalDateTime localDateTime = convertToLocalDateTime(createTime); // 在 LocalDateTime 上加上 30 天 LocalDateTime increaseBy30Days = localDateTime.plus(30, ChronoUnit.DAYS); // 获取当前日期和时间 LocalDateTime now = LocalDateTime.now(); // 比较日期 if (increaseBy30Days.isAfter(now)) { flag = false; } break; } } //发送事件 if (flag) { //创建事件实体类 SensorEventVo sensorEventVo = new SensorEventVo( enterprise.getLongitude(), enterprise.getLatitude(), enterprise.getId(), enterprise.getEnterpriseName(), new Date(), enterprise.getRegisteredAddress() ); //设置基础模板 StringBuilder template = new StringBuilder(); template.append(enterprise.getEnterpriseName()).append("的").append(sysDictData.getDictLabel()).append("发生%s, 超过30天未上传, 请及时处理!%s%s"); //发送事件 sendEvent.sendEvent(template.toString(), StaticEntity.ENTERPRISE_NAME, "", "", sensorEventVo, StaticEntity.ENTERPRISE_CODE); } }); });*/ } // 将 Date 转换为 LocalDateTime public static LocalDateTime convertToLocalDateTime(Date date) { return date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); } /** * 1207 气瓶存疑预警 */ // 每天午夜 12 点执行 // @Scheduled(cron = "0 0 0 * * ?") @GetMapping("/performDailyTask") public void performDailyTask() { //获取告警数据 List datalist = steelcylinderService.selectTaskList(new CenterdataTHousingconstructionSteelcylinder()); //循环处理发送事件 datalist.forEach(data -> { //设置基础模板 StringBuilder template = new StringBuilder(); template.append(data.getEnterpriseName()).append("的").append(data.getName()).append("钢罐发生%s,").append("超过%s天未流转, 请及时处理!%s"); //创建事件实体类 SensorEventVo sensorEventVo = new SensorEventVo( data.getLongitude(),//实名用户经纬度 data.getLatitude(), data.getEnterpriseId(), data.getEnterpriseName(), new Date(), data.getGasAddress()//实名用户地址 ); //发送事件 sendEvent.sendEvent(template.toString(), StaticEntity.INSPECT_NAME, data.getThreshold(), "", sensorEventVo, StaticEntity.INSPECT_CODE); }); } @PostMapping("/sendGpsEvent") public AjaxResult sendGpsEvent(@RequestBody String jsonObj) { logger.info("接受的参数为:==》{}", jsonObj); if (jsonObj.isEmpty()) { return AjaxResult.error("发送数据为空"); } JSONObject jsonObject = JSONObject.parseObject(jsonObj); //获取设定的阈值 double speedThreshold = Double.parseDouble(SpringUtils.getBean(RemoteConfigService.class).remotegetConfigKey("speed_threshold").getData()); logger.info("设定的阈值{}", speedThreshold); String address = getAddressMap(jsonObject, speedThreshold); logger.info("事件发生地点信息{}",address); Map carInfo = carService.getCarInfoByDeviceId(jsonObject.getString("deviceid")); logger.info("车辆信息{}",carInfo); String eventSlatSlon = getEventSlatSlon(jsonObject); logger.info("事件起点经纬度{}",eventSlatSlon); //创建事件实体 SensorEventVo sensorEventVo = new SensorEventVo(); sensorEventVo.setEventType("8"); sensorEventVo.setLongitude("false".equals(eventSlatSlon)?"":eventSlatSlon.split("_")[0]); sensorEventVo.setLatitude("false".equals(eventSlatSlon)?"":eventSlatSlon.split("_")[1]); sensorEventVo.setSensorId(jsonObject.getString("deviceid")); sensorEventVo.setReportTime(new Date()); sensorEventVo.setAddress(address); sensorEventVo.setReportor(MapUtils.getString(carInfo, "carNumber")); StringBuilder template = new StringBuilder(); template.append(MapUtils.getString(carInfo, "enterpriseName")) .append("的").append(MapUtils.getString(carInfo, "driverName")) .append("驾驶").append(MapUtils.getString(carInfo, "carNumber")) .append("车辆").append("发生%s") .append(",设定的阈值为%skm/h"); System.out.println("事件推送实体 =》 " + sensorEventVo); //发送事件 sendEvent.sendEvent(template.toString(), StaticEntity.SPEED_NAME, String.valueOf(speedThreshold), "", sensorEventVo, StaticEntity.SPEED_CODE); return AjaxResult.success(); } private String getAddressMap(JSONObject jsonObject, double speedThreshold) { StringBuilder addressResult = new StringBuilder(); //获取车速集合 JSONArray totaltrips = jsonObject.getJSONArray("totaltrips"); //获取事件发生地点对象 JSONObject addressmap = jsonObject.getJSONObject("addressmap"); List gpsEntities = JSONArray.parseArray(totaltrips.toJSONString(), GpsEntity.class); gpsEntities.forEach(gps -> { System.out.println("gps = " + gps); double v = gps.getMaxspeed() / 1000; if (v > speedThreshold) { addressResult.append("在").append(getAddressStr(gps, addressmap, String.valueOf(v))).append(";"); } }); return addressResult.toString(); } //根据每条车速信息,获取发生地点范围 private String getAddressStr(GpsEntity entity, JSONObject addressmap, String maxSpeed) { //超速开始 String mapKeyStart = getStr(entity.getSlat()) + "_" + getStr(entity.getSlon()); //超速结束 String mapKeyEnd = getStr(entity.getElat()) + "_" + getStr(entity.getElon()); return "{" + addressmap.getString(mapKeyStart) + ">>>" + addressmap.getString(mapKeyEnd) + "}区间最高时速为" + maxSpeed +"km/h"; } /** * @param str 需要截取的数字 */ public String getStr(String str) { // 判断是整数还是小数,如果是整数,则小数点后用0补位 if (!str.contains(".")) { String b = "."; for (int a = 0; a < 5; a++) { b = b + "0"; } return str + b; } // 获取小数点的位置 int index = str.indexOf("."); String beforeNum = str.substring(0, index + 1); // 获取小数点后面是否有num位,不足用0补位 String afterNum = str.replace(beforeNum, ""); // 判断小数点后位数是否大于保留的位数 if (afterNum.length() <= 5) { for (int a = afterNum.length(); a < 5; a++) { afterNum = afterNum + "0"; } } else { afterNum = afterNum.substring(0, 5); } return beforeNum + afterNum; } private String getEventSlatSlon(JSONObject jsonObject){ JSONArray totaltrips = jsonObject.getJSONArray("totaltrips"); List gpsEntities = JSONArray.parseArray(totaltrips.toJSONString(), GpsEntity.class); // 找到 startTime 最小的 GpsEntity Optional minStartTimeEntity = gpsEntities.stream() .min((e1, e2) -> e1.getStarttime().compareTo(e2.getStarttime())); // 如果存在这样的实体,拼接 Slat 和 Slon if (minStartTimeEntity.isPresent()) { GpsEntity entity = minStartTimeEntity.get(); return entity.getSlat() + "_" + entity.getSlon(); } else { return "false"; } } }