zhangherong
2025-04-27 1007e0b552accd3288e7da6b47d8cc49a03bdf62
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.jeecg.modules.eam.aspect;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.constant.InspectionStatus;
import org.jeecg.modules.eam.constant.ReportRepairEnum;
import org.jeecg.modules.eam.constant.WeekMaintenanceStatusEnum;
import org.jeecg.modules.eam.entity.*;
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);
            if (StringUtils.isNotBlank(log.getEquipmentId())) {
                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:
                if (result instanceof EamInspectionOrder) {
                    EamInspectionOrder order = (EamInspectionOrder) result;
                    if (InspectionStatus.COMPLETE.name().equals(order.getInspectionStatus())) {
                        log.setEquipmentId(order.getEquipmentId());
                        log.setBusinessId(order.getId());
                        log.setOperator(order.getOperator());
                        log.setDescription(order.getConfirmComment());
                        log.setCreateTime(order.getOperateTime());
                    }
                }
                break;
            case WEEK_MAINTENANCE:
                if (result instanceof EamWeekMaintenanceOrder) {
                    EamWeekMaintenanceOrder order = (EamWeekMaintenanceOrder) result;
                    if (WeekMaintenanceStatusEnum.COMPLETE.name().equals(order.getMaintenanceStatus())) {
                        log.setEquipmentId(order.getEquipmentId());
                        log.setBusinessId(order.getId());
                        log.setOperator(order.getOperator());
                        log.setDescription(order.getFinalAcceptanceComment());
                        log.setCreateTime(order.getActualEndTime());
                    }
                }
                break;
            case SECOND_MAINTENANCE:
                break;
            case THIRD_MAINTENANCE:
                break;
            case REPORT_REPAIR:
                if (result instanceof EamReportRepair) {
                    EamReportRepair repair = (EamReportRepair) result;
                    log.setEquipmentId(repair.getEquipmentId());
                    log.setBusinessId(repair.getId());
                    log.setOperator(repair.getCreateBy());
                    log.setDescription(repair.getFaultDescription());
                    log.setCreateTime(repair.getFaultStartTime());
                }
                break;
            case REPAIRED:
                if (result instanceof EamRepairOrder) {
                    EamRepairOrder order = (EamRepairOrder) result;
                    if (ReportRepairEnum.COMPLETE.name().equals(order.getRepairStatus())) {
                        log.setEquipmentId(order.getEquipmentId());
                        log.setBusinessId(order.getId());
                        log.setOperator(order.getRepairer());
                        log.setDescription(order.getRepairDescription());
                        log.setCreateTime(order.getActualEndTime());
                    }
                }
                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();
//        // 此处仅为示例,正确做法参见环绕通知实现
//    }
 
 
}