package com.estate.expert.admin.component; import com.estate.expert.admin.service.HierarchyService; import com.estate.expert.admin.service.model.DeviceModelService; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Resource; /** * 层次服务切面 * *
该切面用于在层次服务编辑、添加子节点、删除节点、删除节点时通知设备模型服务更新。 * 主要用于确保层次服务变更后相关设备模型能够自动更新。
*/ @Aspect @Component @Slf4j public class HierachyServiceAspect { @Resource private HierarchyService hierarchyService; @Resource private DeviceModelService deviceModelService; @Resource private TransactionTemplate transactionTemplate; @AfterReturning("execution(* com.estate.expert.admin.service.HierarchyService.edit(..))") public void afterEdit(JoinPoint joinPoint) { // 获取方法参数 Integer hierarchyId = (Integer) joinPoint.getArgs()[0]; notifyHierarchyChange(hierarchyId); } @AfterReturning("execution(* com.estate.expert.admin.service.HierarchyService.addChild(..))") public void afterAddChild(JoinPoint joinPoint) { // 获取方法参数 Integer hierarchyId = (Integer) joinPoint.getArgs()[0]; notifyHierarchyChange(hierarchyId); } @AfterReturning("execution(* com.estate.expert.admin.service.HierarchyService.deleteNode(..))") public void afterDeleteNode(JoinPoint joinPoint) { // 获取方法参数 Integer hierarchyId = (Integer) joinPoint.getArgs()[0]; notifyHierarchyChange(hierarchyId); } @AfterReturning("execution(* com.estate.expert.admin.service.HierarchyService.deleteNodes(..))") public void afterDeleteNodes(JoinPoint joinPoint) { // 获取方法参数 Integer hierarchyId = (Integer) joinPoint.getArgs()[0]; notifyHierarchyChange(hierarchyId); } // 通知设备模型服务更新, 主要是更新模型版本 private void notifyHierarchyChange(Integer hierarchyId) { transactionTemplate.execute(status -> { log.info("Hierarchy changed: {}", hierarchyId); deviceModelService.notifyHierarchyChange(hierarchyId); return null; }); } }