zhangherong
2025-03-21 1eab27b32154cbde5045569a5cbb5f464f4f9889
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.jeecg.modules.eam.aspect;
 
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.modules.eam.aspect.annotation.EquipmentHistoryLog;
import org.jeecg.modules.eam.constant.EquipmentOperationTagEnum;
import org.jeecg.modules.eam.entity.EamEquipment;
import org.jeecg.modules.eam.entity.EamEquipmentHistoryLog;
import org.jeecg.modules.eam.service.IEamEquipmentHistoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
/**
 * 设备履历 切面实现
 */
@Aspect
@Component
@Slf4j
public class EquipmentHistoryLogAspect {
 
    @Autowired
    private IEamEquipmentHistoryLogService equipmentHistoryLogService;
 
    @Pointcut("@annotation(org.jeecg.modules.eam.aspect.annotation.EquipmentHistoryLog)")
    public void logPointCut() {
 
    }
 
    // 环绕通知:计算执行时间
    @Around("logPointCut()")
    public Object logMethodExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start_time = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end_time = System.currentTimeMillis();
        saveLog(joinPoint, result);
        log.info("[请求耗时:{}ms]", end_time - start_time);
        return result;
    }
 
    private void saveLog(JoinPoint joinPoint, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
 
        EamEquipmentHistoryLog log = new EamEquipmentHistoryLog();
 
        EquipmentHistoryLog syslog = method.getAnnotation(EquipmentHistoryLog.class);
 
        if (syslog != null) {
            log.setBusinessTable(syslog.businessTable());
            log.setOperationTag(syslog.operationTag().name());
            convertResult(log, syslog.operationTag(), result);
            equipmentHistoryLogService.save(log);
        }
    }
 
 
    private void convertResult(EamEquipmentHistoryLog log, EquipmentOperationTagEnum operationTag, Object result) {
        switch (operationTag) {
            case ACCEPTANCE:
                if(result instanceof EamEquipment) {
                    EamEquipment equipment = (EamEquipment) result;
                    log.setEquipmentId(equipment.getId());
                    log.setBusinessId(equipment.getId());
                    log.setOperator(equipment.getCreateBy());
                    log.setDescription(equipment.getRemark());
                    log.setCreateTime(equipment.getAcceptanceCheckDate());
                }
                break;
            case POINT_INSPECTION:
                break;
            case SECOND_MAINTENANCE:
                break;
            case THIRD_MAINTENANCE:
                break;
            case REPORT_REPAIR:
                break;
            case REPAIRED:
                break;
            case MAJOR_REPAIR:
                break;
            case PARTIAL_REPAIR:
                break;
            case LEAN_OUT:
                break;
            case GIVE_BACK:
                break;
            case SEAL_UP:
                break;
            case UNSEALED:
                break;
            case TRANSFERRED:
                break;
            case SCRAPPED:
                break;
            default:
                break;
        }
    }
 
//    private long calculateExecutionTime(JoinPoint joinPoint) {
//        // 通过环绕通知获取精确时间
//        // (实际应使用环绕通知中的start_time和end_time计算)
//        return System.currentTimeMillis() - joinPoint.getArgs()[0].hashCode();
//        // 此处仅为示例,正确做法参见环绕通知实现
//    }
 
 
}