From fa82dc7272741dcb71e61b2681067ca0839bf5be Mon Sep 17 00:00:00 2001 From: dearlin <1261008090@qq.com> Date: Thu, 28 Dec 2023 13:49:21 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B8=AF=E5=8A=A1=E5=B1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=90=8C=E6=AD=A5=E6=B3=A8=E8=A7=A3=E4=B8=8E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zcloud/aspect/DockAnnotation.java | 18 +++ .../java/com/zcloud/aspect/DockingAspect.java | 133 ++++++++++++++++++ .../service/mq/DockSendMessageService.java | 11 ++ .../mq/impl/DockSendMessageServiceImpl.java | 111 +++++++++++++++ src/main/resources/application-dev.properties | 7 + .../resources/application-local.properties | 5 + .../resources/application-master.properties | 5 + 7 files changed, 290 insertions(+) create mode 100644 src/main/java/com/zcloud/aspect/DockAnnotation.java create mode 100644 src/main/java/com/zcloud/aspect/DockingAspect.java create mode 100644 src/main/java/com/zcloud/service/mq/DockSendMessageService.java create mode 100644 src/main/java/com/zcloud/service/mq/impl/DockSendMessageServiceImpl.java diff --git a/src/main/java/com/zcloud/aspect/DockAnnotation.java b/src/main/java/com/zcloud/aspect/DockAnnotation.java new file mode 100644 index 00000000..920b2b78 --- /dev/null +++ b/src/main/java/com/zcloud/aspect/DockAnnotation.java @@ -0,0 +1,18 @@ +package com.zcloud.aspect; + +import java.lang.annotation.*; + +/** + * @Description: 对接切面注解 + * @Author: dearLin + * @Date: 2023/3/8 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface DockAnnotation { + // 是否有额外数据 + boolean isAdd() default false; + //是否有附件 + boolean hasAnnex() default false; +} diff --git a/src/main/java/com/zcloud/aspect/DockingAspect.java b/src/main/java/com/zcloud/aspect/DockingAspect.java new file mode 100644 index 00000000..171a8ee9 --- /dev/null +++ b/src/main/java/com/zcloud/aspect/DockingAspect.java @@ -0,0 +1,133 @@ +package com.zcloud.aspect; + +import com.alibaba.fastjson.JSON; +import com.zcloud.dto.TenCorpDto; +import com.zcloud.entity.PageData; +import com.zcloud.entity.system.User; +import com.zcloud.service.mq.DockSendMessageService; +import com.zcloud.util.Const; +import com.zcloud.util.Jurisdiction; +import com.zcloud.util.Tools; +import com.zcloud.util.UuidUtil; +import org.apache.shiro.session.Session; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +@Component +@Aspect +public class DockingAspect { + @Autowired + private DockSendMessageService sendMessageService; + @Value("${baseimgpath}") + public String baseimgpath; + @Pointcut("@annotation(com.zcloud.aspect.DockAnnotation)") + public void pointcut() { + } + + public static Map dockingRelationMap = new HashMap(); + + // 增强逻辑:在切点方法执行前打印日志 + @Around("pointcut()") + public Object beforeApi(ProceedingJoinPoint joinPoint) throws Throwable { + + try { + Object proceed = joinPoint.proceed(); + HashMap parseProceed = JSON.parseObject(JSON.toJSONString(proceed), HashMap.class); + if (parseProceed == null) { + return parseProceed; + } + // 数据同步 + dataSync(parseProceed, joinPoint); + return proceed; + } catch (Exception e) { + throw e; + } + } + + private void dataSync(HashMap proceed, ProceedingJoinPoint joinPoint) throws Exception { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes()); + DockAnnotation annotation = method.getAnnotation(DockAnnotation.class); + //获取资源映射路径:servlet路径 + String servletPath = getRequest().getServletPath(); + //验证该数据是否需要对接 + System.out.println("------------------------切面方法执行------------------------"); + PageData sendData = this.getPageData(); + // app 方法 不取session + PageData login_user = new PageData(); + if (!servletPath.contains("/app/")) { + login_user.put("USER_ID", Jurisdiction.getUSER_ID()); + login_user.put("USERNAME", Jurisdiction.getUsername()); + login_user.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); + login_user.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID()); + login_user.put("NAME", Jurisdiction.getName()); + login_user.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE()); + login_user.put("POST_ID", Jurisdiction.getPOST_ID()); + login_user.put("IS_MAIN", Jurisdiction.getIS_MAIN()); + Session session = Jurisdiction.getSession(); + User user = (User)session.getAttribute(Const.SESSION_USER); + login_user.put("user",JSON.toJSONString(user)); + sendData.put("LOGIN_USER", login_user); + } else { + // 手机app 会传递值。 + login_user.put("USER_ID", Tools.notEmpty(sendData.getString("USER_ID")) ? sendData.getString("USER_ID") : ""); + sendData.put("LOGIN_USER", login_user); + } + // 路径地址 + sendData.put("url", servletPath); + // 有存自己表里的图片 把自己图片服务器的前缀传过去 + if (annotation.hasAnnex()) { + sendData.put("BASEIMGPATH", baseimgpath); + // 自己表里的图片集合 + sendData.put("sendPicturesList", proceed.get("sendPicturesList")); + proceed.remove("sendPicturesList"); + } + TenCorpDto tenCorpDto = new TenCorpDto(); + tenCorpDto.setMessage("港务局数据同步消息"); + tenCorpDto.setData(sendData); + tenCorpDto.setId(UuidUtil.get32UUID()); + tenCorpDto.setTopic("docking"); + tenCorpDto.setProducer_name("qa-prevention-czks"); + // 有dockData + if (Tools.notEmpty(proceed.get("dockData"))) { + sendData.put("dockData", proceed.get("dockData")); + proceed.remove("dockData"); + sendMessageService.sendMessage(tenCorpDto); + } else { + System.out.println("------------------------无dockData不发消息------------------------"); + } + + System.out.println("------------------------切面方法结束------------------------"); + } + + + + private PageData getPageData() { + return new PageData(this.getRequest()); + } + + /** + * 得到request对象 + * + * @return + */ + private HttpServletRequest getRequest() { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) + .getRequest(); + return request; + } +} diff --git a/src/main/java/com/zcloud/service/mq/DockSendMessageService.java b/src/main/java/com/zcloud/service/mq/DockSendMessageService.java new file mode 100644 index 00000000..f56d0e22 --- /dev/null +++ b/src/main/java/com/zcloud/service/mq/DockSendMessageService.java @@ -0,0 +1,11 @@ +package com.zcloud.service.mq; + +import com.zcloud.dto.TenCorpDto; + +public interface DockSendMessageService { + + void sendMessage(TenCorpDto tenCorpDto) throws Exception; + void sendMessagePicture(TenCorpDto tenCorpDto) throws Exception; + void sendMessagePictureDelete(TenCorpDto tenCorpDto) throws Exception; + +} diff --git a/src/main/java/com/zcloud/service/mq/impl/DockSendMessageServiceImpl.java b/src/main/java/com/zcloud/service/mq/impl/DockSendMessageServiceImpl.java new file mode 100644 index 00000000..ca474cd1 --- /dev/null +++ b/src/main/java/com/zcloud/service/mq/impl/DockSendMessageServiceImpl.java @@ -0,0 +1,111 @@ +package com.zcloud.service.mq.impl; + +import com.alibaba.fastjson.JSONObject; +import com.zcloud.dto.TenCorpDto; +import com.zcloud.entity.PageData; +import com.zcloud.mapper.datasource.mq.MqErrorMessageLogMapper; +import com.zcloud.mapper.datasource.mq.MqMessageLogMapper; +import com.zcloud.service.mq.DockSendMessageService; +import com.zcloud.service.mq.LogService; +import com.zcloud.util.DateUtil; +import com.zcloud.util.Warden; +import com.zcloud.util.mq.MqUtil; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +public class DockSendMessageServiceImpl implements DockSendMessageService { + + @Value("${mq.gwj.data.topic}") + private String gwjDataTopic; + @Value("${mq.gwj.file.topic}") + private String gwjFileTopic; + @Resource + private RocketMQTemplate rocketMQTemplate; + + @Resource + private MqErrorMessageLogMapper mqErrorMessageLogMapper; + + @Resource + private LogService logService; + + @Resource + private MqMessageLogMapper mqMessageLogMapper; + + @Override + public void sendMessage(TenCorpDto tenCorpDto) throws Exception { + try { + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_LOG_ID", Warden.get32UUID()); + log.put("CREATE_TIME", DateUtil.getTime()); + mqMessageLogMapper.save(log); + System.out.println("生产者:" + tenCorpDto.toString()); + SendResult sendResult = rocketMQTemplate.syncSend(this.gwjDataTopic, tenCorpDto.toString()); + if (!sendResult.getSendStatus().equals(SendStatus.SEND_OK)) { + throw new RuntimeException("产品入栈失败"); + } + } catch (Exception e) { + e.printStackTrace(); + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_ERROR_LOG_ID", Warden.get32UUID()); + log.put("ERROR_MESSAGE", e.getMessage()); + log.put("TYPE", "0"); + log.put("TIME", DateUtil.getTime()); + mqErrorMessageLogMapper.save(log); + } + } + + @Override + public void sendMessagePicture(TenCorpDto tenCorpDto) throws Exception { + try { + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_LOG_ID", Warden.get32UUID()); + log.put("CREATE_TIME", DateUtil.getTime()); + mqMessageLogMapper.save(log); + System.out.println("生产者:" + tenCorpDto.toString()); + SendResult sendResult = rocketMQTemplate.syncSend(this.gwjFileTopic, tenCorpDto.toString()); + if (!sendResult.getSendStatus().equals(SendStatus.SEND_OK)) { + throw new RuntimeException("产品入栈失败"); + } + } catch (Exception e) { + e.printStackTrace(); + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_ERROR_LOG_ID", Warden.get32UUID()); + log.put("ERROR_MESSAGE", e.getMessage()); + log.put("TYPE", "0"); + log.put("TIME", DateUtil.getTime()); + mqErrorMessageLogMapper.save(log); + } + } + + @Override + public void sendMessagePictureDelete(TenCorpDto tenCorpDto) throws Exception { + try { + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_LOG_ID", Warden.get32UUID()); + log.put("CREATE_TIME", DateUtil.getTime()); + mqMessageLogMapper.save(log); + System.out.println("生产者:" + tenCorpDto.toString()); + SendResult sendResult = rocketMQTemplate.syncSend(this.gwjFileTopic, tenCorpDto.toString()); + if (!sendResult.getSendStatus().equals(SendStatus.SEND_OK)) { + throw new RuntimeException("产品入栈失败"); + } + } catch (Exception e) { + e.printStackTrace(); + PageData log = tenCorpDto.getPd(); + log.put("MESSAGE_ERROR_LOG_ID", Warden.get32UUID()); + log.put("ERROR_MESSAGE", e.getMessage()); + log.put("TYPE", "0"); + log.put("TIME", DateUtil.getTime()); + mqErrorMessageLogMapper.save(log); + } + } + + +} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index c8ee675a..2d5f8d76 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -89,3 +89,10 @@ mq.topic.info=info mq.topic.eightWork=eightWork mq.group.info=scheduled_tasks mq.group.eightWork=scheduled_tasks_eightWork + + +#数据同步topic +mq.gwj.data.topic=czks_docking +mq.gwj.file.topic=czks_dockingPicture +#港务局文件服务器前缀 +baseimgpath=https://qgqy.qhdsafety.com/file/ diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index 8d8cc1c0..8b0f52f4 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -93,3 +93,8 @@ mq.group.eightWork=scheduled_tasks_eightWork corp.default.pic-path=https://qgqy.qhdsafety.com/file/ corp.default.back-end-path=http://192.168.151.57:8092/ +#数据同步topic +mq.gwj.data.topic=czks_docking +mq.gwj.file.topic=czks_dockingPicture +#港务局文件服务器前缀 +baseimgpath=https://qgqy.qhdsafety.com/file/ diff --git a/src/main/resources/application-master.properties b/src/main/resources/application-master.properties index c6426837..d4811098 100644 --- a/src/main/resources/application-master.properties +++ b/src/main/resources/application-master.properties @@ -87,3 +87,8 @@ mq.group.eightWork=scheduled_tasks_eightWork corp.default.pic-path=https://qgqy.qhdsafety.com/file/ corp.default.back-end-path=https://skqhdg.porthebei.com:9004/file/ +#数据同步topic +mq.gwj.data.topic=czks_docking +mq.gwj.file.topic=czks_dockingPicture +#港务局文件服务器前缀 +baseimgpath=https://qgqy.qhdsafety.com/file/