diff --git a/src/main/java/com/gis/xian/XianApplication.java b/src/main/java/com/gis/xian/XianApplication.java index 4311011..e274717 100644 --- a/src/main/java/com/gis/xian/XianApplication.java +++ b/src/main/java/com/gis/xian/XianApplication.java @@ -28,4 +28,4 @@ public class XianApplication { XianApplication app = context.getBean(XianApplication.class); System.out.println("后端服务启动成功!访问地址: http://localhost:" + app.port); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gis/xian/config/DataSourceAspect.java b/src/main/java/com/gis/xian/config/DataSourceAspect.java index b88e425..09d8967 100644 --- a/src/main/java/com/gis/xian/config/DataSourceAspect.java +++ b/src/main/java/com/gis/xian/config/DataSourceAspect.java @@ -12,9 +12,12 @@ import org.springframework.stereotype.Component; @Order(1) @Slf4j public class DataSourceAspect { - + @Around("@annotation(dataSource) || @within(dataSource)") public Object around(ProceedingJoinPoint point, DataSource dataSource) throws Throwable { + if (dataSource == null) { + return point.proceed(); + } try { String dsName = dataSource.value(); log.debug("切换数据源: {}", dsName); diff --git a/src/main/java/com/gis/xian/controller/DZEqEventController.java b/src/main/java/com/gis/xian/controller/DZEqEventController.java index 95acd5d..380638c 100644 --- a/src/main/java/com/gis/xian/controller/DZEqEventController.java +++ b/src/main/java/com/gis/xian/controller/DZEqEventController.java @@ -5,7 +5,9 @@ import com.gis.xian.domain.ApiResponse; import com.gis.xian.dto.pub.EqTriggerDTO; import com.gis.xian.query.EqQuery; import com.gis.xian.service.pub.IDZEqEventService; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.geolatte.geom.V; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -19,25 +21,19 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/open") public class DZEqEventController { - @Autowired + @Resource private IDZEqEventService idzEqEventService; @PostMapping("/eq/trigger") - public ApiResponse trigger(@RequestBody @Validated EqTriggerDTO trigger) { + public ApiResponse trigger(@RequestBody @Validated EqTriggerDTO trigger) { EqQuery query = idzEqEventService.trigger(trigger); - if (query == null) { - return ApiResponse.error(BaseConstants.RESULT_ERROR); - } return ApiResponse.ok(query); } @PostMapping("/eq/delete/{Id}") - public ApiResponse delete(@PathVariable Long Id) { + public ApiResponse delete(@PathVariable Long Id) { Boolean deleted = idzEqEventService.deletedById(Id); - if (!deleted) { - return ApiResponse.error("删除失败!"); - } - return ApiResponse.ok("删除成功!"); + return ApiResponse.ok(deleted); } } diff --git a/src/main/java/com/gis/xian/controller/DZInfluenceController.java b/src/main/java/com/gis/xian/controller/DZInfluenceController.java new file mode 100644 index 0000000..f3afa95 --- /dev/null +++ b/src/main/java/com/gis/xian/controller/DZInfluenceController.java @@ -0,0 +1,45 @@ +package com.gis.xian.controller; + +import com.gis.xian.constant.BaseConstants; +import com.gis.xian.domain.ApiResponse; +import com.gis.xian.dto.pub.EqAssessmentDTO; +import com.gis.xian.query.EqQuery; +import com.gis.xian.service.dzxx.IDZXXInfluenceService; +import com.gis.xian.service.pub.IDZInfluenceService; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * @author zzw + * @description: 影响场控制类 + * @date 2026/6/4 下午3:09 + */ +@Slf4j +@RestController +@RequestMapping("/open") +public class DZInfluenceController { + + @Resource + private IDZInfluenceService idzInfluenceService; + @Resource + private IDZXXInfluenceService idzxxInfluenceService; + + @PostMapping("/influence") + public ApiResponse> getInfluence(@RequestBody @Validated EqQuery query) { + Map influence = idzInfluenceService.getInfluence(query); + return ApiResponse.ok(influence); + } + + @PostMapping("/generate/influence") + public ApiResponse generateInfluence(@RequestBody @Validated EqAssessmentDTO assess) { + idzxxInfluenceService.handle(assess); + return ApiResponse.ok("地震影响场已生成!"); + } +} diff --git a/src/main/java/com/gis/xian/controller/DZProductController.java b/src/main/java/com/gis/xian/controller/DZProductController.java new file mode 100644 index 0000000..e103209 --- /dev/null +++ b/src/main/java/com/gis/xian/controller/DZProductController.java @@ -0,0 +1,28 @@ +package com.gis.xian.controller; + +import com.gis.xian.domain.ApiResponse; +import com.gis.xian.dto.pub.DZProductDTO; +import com.gis.xian.query.ProductQuery; +import com.gis.xian.service.pub.IDZProductService; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; + +/** + * @author zzw + * @description: 产出结果控制类 + * @date 2026/6/4 下午3:38 + */ +public class DZProductController { + + @Resource + private IDZProductService idzProductService; + + @PostMapping("/product") + public ApiResponse> getProducts(@RequestBody ProductQuery query) { + List products = idzProductService.getProducts(query); + return ApiResponse.ok(products); + } +} diff --git a/src/main/java/com/gis/xian/core/config/RabbitConfig.java b/src/main/java/com/gis/xian/core/config/RabbitConfig.java index fed4714..da4d12b 100644 --- a/src/main/java/com/gis/xian/core/config/RabbitConfig.java +++ b/src/main/java/com/gis/xian/core/config/RabbitConfig.java @@ -6,9 +6,14 @@ import org.springframework.amqp.core.*; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.amqp.support.converter.SimpleMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Arrays; + /** * @author zzw * @description: RabbitMq 配置 @@ -66,11 +71,22 @@ public class RabbitConfig { return BindingBuilder.bind(dlqQueue()).to(exchange()).with(BaseConstants.DLQ_QUEUE); } + @Bean + public MessageConverter messageConverter() { + SimpleMessageConverter converter = new SimpleMessageConverter(); + converter.setAllowedListPatterns(Arrays.asList( + "com.gis.xian.params.QgisArgs", + "com.gis.xian.domain.DlqMessage" + )); + return converter; + } + // 设置消息回调函数 自动确认消息 ack @Bean public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(); rabbitTemplate.setConnectionFactory(connectionFactory); + rabbitTemplate.setMessageConverter(messageConverter()); // 设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数 rabbitTemplate.setMandatory(true); diff --git a/src/main/java/com/gis/xian/listener/MapReceiveListener.java b/src/main/java/com/gis/xian/listener/MapReceiveListener.java new file mode 100644 index 0000000..ed3a3fc --- /dev/null +++ b/src/main/java/com/gis/xian/listener/MapReceiveListener.java @@ -0,0 +1,110 @@ +package com.gis.xian.listener; + +import com.gis.xian.config.DataSourceContextHolder; +import com.gis.xian.constant.BaseConstants; +import com.gis.xian.entity.pub.DZProduct; +import com.gis.xian.params.QgisArgs; +import com.gis.xian.service.ex.ServeException; +import com.gis.xian.service.pub.IDZProductService; +import com.rabbitmq.client.Channel; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.support.AmqpHeaders; +import org.springframework.messaging.handler.annotation.Header; +import org.springframework.stereotype.Component; + + +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; + +/** + * @author zzw + * @description: 专题图产出监听器 + * @date 2026/6/4 下午4:10 + */ +@Slf4j +@Component +public class MapReceiveListener { + + @Resource + private IDZProductService idzProductService; + + @PostConstruct + public void init() { + log.info("========================================"); + log.info("MapReceiveListener 监听器已初始化"); + log.info("正在监听队列: maps"); + log.info("监听方法: receive(QgisArgs args, Channel channel, ...)"); + log.info("ACK模式: MANUAL (手动确认)"); + log.info("========================================"); + } + + // rabbitmq 监听专题图队列 + @RabbitListener(queues = "maps", ackMode = "MANUAL") + public void receive(QgisArgs args, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Message message) throws IOException { + log.info("接收通知:{} 已生成!", args.getName()); + try { + // 获取路径 + File originFile = new File(args.getOutFile()); + if (!originFile.exists()) { + throw new ServeException(BaseConstants.FILE_NOT_FOUND_ERROR); + } + + // 直接使用本地文件路径保存到数据库 + handleData(args, originFile.getAbsolutePath()); + log.info("{} 本地路径已保存到数据库!", args.getName()); + + // 手动确认消息 + channel.basicAck(deliveryTag, false); + log.info("ack:消息确认成功!"); + + } catch (Exception ex) { + ex.printStackTrace(); + log.error("处理消息失败: {}", ex); + + /** + * 参数说明: + * 1. deliveryTag:消息唯一标识 + * 2. multiple:是否拒绝多条 + * 3. requeue:是否重新入队(false=直接丢弃,true=重新入队重试) + * 建议:非幂等业务设置为 false,避免死循环;幂等业务可设置为 true + */ + channel.basicNack(deliveryTag, false, false); + + // 抛出异常 + throw new ServeException(BaseConstants.THEMATIC_FAILED); + } + } + + private void handleData(QgisArgs args, String filePath) { + try { + DataSourceContextHolder.setDataSource("slave1"); + log.debug("切换数据源到: slave1"); + + DZProduct product = new DZProduct(); + product.setEqQueueId(args.getQueueId()); + product.setProTime(LocalDateTime.now()); + product.setCode(args.getMapLayout()); + product.setFileType("图片"); + product.setFileName(args.getName()); + product.setFilePath(args.getPath()); + product.setFileExtension(".jpg"); + // 地震/暴雨 专题图 + product.setProType(args.getDisaster()); + + // 将本地文件路径设置到源文件字段中 + product.setSourceFile(filePath); + + // 将图件信息插入到结果表中 + idzProductService.save(product); + log.info("{} 已保存到数据库!", args.getName()); + } finally { + DataSourceContextHolder.clearDataSource(); + log.debug("清除数据源上下文"); + } + } +} diff --git a/src/main/java/com/gis/xian/service/dzxx/impl/DZXXDistanceServiceImpl.java b/src/main/java/com/gis/xian/service/dzxx/impl/DZXXDistanceServiceImpl.java index 6d9da21..f0d5a9c 100644 --- a/src/main/java/com/gis/xian/service/dzxx/impl/DZXXDistanceServiceImpl.java +++ b/src/main/java/com/gis/xian/service/dzxx/impl/DZXXDistanceServiceImpl.java @@ -14,6 +14,7 @@ import com.gis.xian.service.base.IShanXiCountyService; import com.gis.xian.service.base.IShanXiTownsService; import com.gis.xian.service.dzxx.IDZXXDistanceService; import com.gis.xian.service.ex.ParmaException; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.locationtech.jts.geom.*; import org.springframework.beans.BeanUtils; @@ -35,11 +36,11 @@ import java.util.List; public class DZXXDistanceServiceImpl extends ServiceImpl implements IDZXXDistanceService { private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326); - @Autowired + @Resource private IShanXiCitiesService iShanXiCitiesService; - @Autowired + @Resource private IShanXiCountyService iShanXiCountyService; - @Autowired + @Resource private IShanXiTownsService iShanXiTownsService; // 处理所有乡镇表 diff --git a/src/main/java/com/gis/xian/service/dzxx/impl/DZXXInfluenceServiceImpl.java b/src/main/java/com/gis/xian/service/dzxx/impl/DZXXInfluenceServiceImpl.java index 157f680..ba8bceb 100644 --- a/src/main/java/com/gis/xian/service/dzxx/impl/DZXXInfluenceServiceImpl.java +++ b/src/main/java/com/gis/xian/service/dzxx/impl/DZXXInfluenceServiceImpl.java @@ -40,11 +40,11 @@ import java.util.List; public class DZXXInfluenceServiceImpl extends ServiceImpl implements IDZXXInfluenceService { - @Autowired + @Resource private IActiveFaultService faultService; - @Autowired + @Resource private EllipseToWktHandler ellipseToWktHandler; - @Autowired + @Resource private IDZInfluenceService idzInfluenceService; @Resource private EarthquakeHandler earthquakeHandler; diff --git a/src/main/java/com/gis/xian/service/pub/impl/DZEqEventServiceImpl.java b/src/main/java/com/gis/xian/service/pub/impl/DZEqEventServiceImpl.java index 9c8b360..93bc11b 100644 --- a/src/main/java/com/gis/xian/service/pub/impl/DZEqEventServiceImpl.java +++ b/src/main/java/com/gis/xian/service/pub/impl/DZEqEventServiceImpl.java @@ -17,6 +17,7 @@ import com.gis.xian.service.ex.ServeException; import com.gis.xian.service.pub.IDZEqEventService; import com.gis.xian.service.pub.IDZEqQueueService; import com.gis.xian.utils.BaseUtils; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -33,9 +34,9 @@ import org.springframework.transaction.annotation.Transactional; @DataSource("slave1") public class DZEqEventServiceImpl extends ServiceImpl implements IDZEqEventService { - @Autowired + @Resource private IDZXXCenterService idzxxCenterService; - @Autowired + @Resource private IDZEqQueueService idzEqQueueService; // 地震业务触发 diff --git a/src/main/java/com/gis/xian/service/pub/impl/DZEqQueueServiceImpl.java b/src/main/java/com/gis/xian/service/pub/impl/DZEqQueueServiceImpl.java index 780c774..34e8035 100644 --- a/src/main/java/com/gis/xian/service/pub/impl/DZEqQueueServiceImpl.java +++ b/src/main/java/com/gis/xian/service/pub/impl/DZEqQueueServiceImpl.java @@ -17,6 +17,7 @@ import com.gis.xian.service.ex.ServeException; import com.gis.xian.service.pub.IDZEqQueueService; import com.gis.xian.service.pub.IDZProductService; import com.gis.xian.utils.BaseUtils; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -35,11 +36,11 @@ import java.time.LocalDateTime; @DataSource("slave1") public class DZEqQueueServiceImpl extends ServiceImpl implements IDZEqQueueService { - @Autowired + @Resource private IDZXXDistanceService idzxxDistanceService; - @Autowired + @Resource private IDZXXInfluenceService idzxxInfluenceService; - @Autowired + @Resource private IDZProductService idzProductService; // 地震评估