add:添加mock;

add:添加工单报工倒扣逻辑
This commit is contained in:
zhangzq
2026-06-12 14:59:37 +08:00
parent bd54c8057f
commit bf66aae879
34 changed files with 1123 additions and 72 deletions

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nl.common.annotation;
import org.nl.common.aspect.LimitType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author jacky
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Mock {
// 接口唯一标识(不填则默认 类名#方法名)
String value() default "";
// 接口描述
String desc() default "";
// 是否默认启用Mock
boolean enable() default false;
}

View File

@@ -0,0 +1,65 @@
package org.nl.common.aspect;
import com.alibaba.fastjson.JSON;
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.nl.common.annotation.Mock;
import org.nl.wms.system_manage.mock.service.IMockConfigService;
import org.nl.wms.system_manage.mock.service.dao.MockConfig;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
@Aspect
@Component
@Slf4j
public class MockAspect {
@Autowired
private IMockConfigService mockConfigService;
@Around("@annotation(org.nl.common.annotation.Mock)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method interfaceMethod = signature.getMethod();
Method method = AopUtils.getMostSpecificMethod(interfaceMethod, joinPoint.getTarget().getClass());
Mock mock = AnnotationUtils.findAnnotation(method, Mock.class);
if (mock == null) {
mock = AnnotationUtils.findAnnotation(interfaceMethod, Mock.class);
}
if (mock == null) {
return joinPoint.proceed();
}
MockConfig config = mockConfigService.createIfAbsent(method, mock);
if (config == null || !Boolean.TRUE.equals(config.getEnabled())) {
return joinPoint.proceed();
}
Integer delayMs = config.getDelay_ms();
if (delayMs != null && delayMs > 0) {
Thread.sleep(delayMs);
}
return buildMockResult(method, config.getResponse_data());
}
private Object buildMockResult(Method method, String responseData) {
Class<?> returnType = method.getReturnType();
if (Void.TYPE.equals(returnType)) {
return null;
}
if (String.class.equals(returnType)) {
return responseData;
}
if (responseData == null || responseData.trim().isEmpty()) {
return null;
}
Type genericReturnType = method.getGenericReturnType();
return JSON.parseObject(responseData, genericReturnType);
}
}