liujie
2026-01-24 13392606c16362d23478e84e0d43e869813d3095
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
 
/**
 * 层次服务切面
 * 
 * <p>该切面用于在层次服务编辑、添加子节点、删除节点、删除节点时通知设备模型服务更新。
 * 主要用于确保层次服务变更后相关设备模型能够自动更新。</p>
 */
@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;
        });
 
    }
}