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.WeekMaintenanceStatusEnum; import org.jeecg.modules.eam.entity.EamEquipment; import org.jeecg.modules.eam.entity.EamEquipmentHistoryLog; import org.jeecg.modules.eam.entity.EamInspectionOrder; import org.jeecg.modules.eam.entity.EamWeekMaintenanceOrder; 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.getInspectionDate()); } } 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: 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(); // // 此处仅为示例,正确做法参见环绕通知实现 // } }