80 lines
3.1 KiB
Java
80 lines
3.1 KiB
Java
|
|
package com.klp.wms.aspect;
|
|||
|
|
|
|||
|
|
import com.klp.wms.annotation.CoilWarehouseOperationLog;
|
|||
|
|
import com.klp.domain.WmsCoilWarehouseOperationLog;
|
|||
|
|
import com.klp.mapper.WmsCoilWarehouseOperationLogMapper;
|
|||
|
|
import lombok.RequiredArgsConstructor;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|||
|
|
import org.aspectj.lang.annotation.Around;
|
|||
|
|
import org.aspectj.lang.annotation.Aspect;
|
|||
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|||
|
|
import org.springframework.stereotype.Component;
|
|||
|
|
|
|||
|
|
import java.lang.reflect.Method;
|
|||
|
|
|
|||
|
|
@Slf4j
|
|||
|
|
@Aspect
|
|||
|
|
@Component
|
|||
|
|
@RequiredArgsConstructor
|
|||
|
|
public class CoilWarehouseOperationLogAspect {
|
|||
|
|
|
|||
|
|
private final WmsCoilWarehouseOperationLogMapper wmsCoilWarehouseOperationLogMapper;
|
|||
|
|
|
|||
|
|
@Around("@annotation(com.klp.wms.annotation.CoilWarehouseOperationLog)")
|
|||
|
|
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
|||
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|||
|
|
Method method = signature.getMethod();
|
|||
|
|
CoilWarehouseOperationLog annotation = method.getAnnotation(CoilWarehouseOperationLog.class);
|
|||
|
|
|
|||
|
|
Long coilId = getParamValue(joinPoint, annotation.coilIdParam(), Long.class);
|
|||
|
|
Long warehouseId = getParamValue(joinPoint, annotation.warehouseIdParam(), Long.class);
|
|||
|
|
Long oldWarehouseId = null;
|
|||
|
|
if (!annotation.oldWarehouseIdParam().isEmpty()) {
|
|||
|
|
oldWarehouseId = getParamValue(joinPoint, annotation.oldWarehouseIdParam(), Long.class);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Object result = joinPoint.proceed();
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
if (coilId != null) {
|
|||
|
|
WmsCoilWarehouseOperationLog operationLog = new WmsCoilWarehouseOperationLog();
|
|||
|
|
operationLog.setCoilId(coilId);
|
|||
|
|
operationLog.setActualWarehouseId(oldWarehouseId != null ? oldWarehouseId : warehouseId);
|
|||
|
|
operationLog.setOperationType(annotation.operationType());
|
|||
|
|
operationLog.setInOutType(annotation.inOutType());
|
|||
|
|
operationLog.setRemark(annotation.remark());
|
|||
|
|
wmsCoilWarehouseOperationLogMapper.insert(operationLog);
|
|||
|
|
log.info("记录钢卷库区操作日志:coilId={}, operationType={}, inOutType={}",
|
|||
|
|
coilId, annotation.operationType(), annotation.inOutType());
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.warn("记录钢卷库区操作日志失败:{}", e.getMessage());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private <T> T getParamValue(ProceedingJoinPoint joinPoint, String paramName, Class<T> type) {
|
|||
|
|
if (paramName == null || paramName.isEmpty()) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
Object[] args = joinPoint.getArgs();
|
|||
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|||
|
|
String[] parameterNames = signature.getParameterNames();
|
|||
|
|
|
|||
|
|
if (parameterNames == null) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (int i = 0; i < parameterNames.length; i++) {
|
|||
|
|
if (paramName.equals(parameterNames[i]) && args[i] != null) {
|
|||
|
|
if (type.isInstance(args[i])) {
|
|||
|
|
return type.cast(args[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|