From 6160d3da6abe0dd3c22e4bb6077841e972ba22a3 Mon Sep 17 00:00:00 2001 From: jun Date: Thu, 27 Jun 2024 10:23:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=B3=BB=E7=BB=9F=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=A0=87=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mes/apiservice/aspect/MesRepositoryAspect.java | 130 +++++++++++++++++++++ .../src/main/resources/application.properties | 4 +- 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/aspect/MesRepositoryAspect.java diff --git a/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/aspect/MesRepositoryAspect.java b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/aspect/MesRepositoryAspect.java new file mode 100644 index 0000000..ba27a9e --- /dev/null +++ b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/aspect/MesRepositoryAspect.java @@ -0,0 +1,130 @@ +package cn.estsh.i3plus.ext.mes.apiservice.aspect; + +import cn.estsh.i3plus.platform.common.tool.TimeTool; +import cn.estsh.i3plus.pojo.base.bean.BaseBean; +import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.Configuration; + +import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + +/** + * @Description : 更新系统同步标志 + * @Reference : + * @Author : junsheng.li + * @CreateDate 2024/6/27 10:10 + * @Modify: + **/ +@ConditionalOnExpression("'${mes.aspect.repository:false}' == 'true'") +@Aspect +@Configuration +public class MesRepositoryAspect { + + @Before("controllerPointcut()") + public void before(JoinPoint joinPoint) { + String methodName = joinPoint.getSignature().getName(); + if (methodName.equals("save") || methodName.equals("update")) { + Object[] args = joinPoint.getArgs(); + for (Object arg : args) { + if (arg.getClass().isArray()) { + int len = Array.getLength(arg); + for (int i = 0; i < len; ++i) { + Object item = Array.get(arg, i); + if (BaseBean.class.isAssignableFrom(item.getClass())) { + updateBeanSync((BaseBean) item); + } + } + } else if (BaseBean.class.isAssignableFrom(arg.getClass())) { + updateBeanSync((BaseBean) arg); + } + } + } else if (methodName.equals("insert")) { + Object[] args = joinPoint.getArgs(); + if (args.length > 0) { + Object item = args[0]; + if (BaseBean.class.isAssignableFrom(item.getClass())) { + updateBeanSync((BaseBean) item); + } + } + } else if (methodName.equals("saveAll")) { + Object[] args = joinPoint.getArgs(); + if (args.length > 0) { + Object arg = args[0]; + if (arg instanceof List) { + List items = (List) arg; + for (Object item : items) { + if (BaseBean.class.isAssignableFrom(item.getClass())) { + updateBeanSync((BaseBean) item); + } + } + } else if (BaseBean.class.isAssignableFrom(arg.getClass())) { + updateBeanSync((BaseBean) arg); + } + } + } + } + + @Pointcut("execution(* cn.estsh.*..*.repository.*..*(..))") + public void controllerPointcut() { + } + + @After("controllerPointcut()") + public void after(JoinPoint joinPoint) { + String methodName = joinPoint.getSignature().getName(); + if (methodName.startsWith("updateByProperties")) { + try { + Object[] args = joinPoint.getArgs(); + Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); + if (args.length == 4) { + if (args[2].getClass().isArray()) { + method.invoke(joinPoint.getTarget(), args[0], args[1], new String[]{"modifyDatetime", "systemSyncStatus"}, + new Object[]{TimeTool.getNowTime(true), CommonEnumUtil.FALSE}); + } else { + method.invoke(joinPoint.getTarget(), args[0], args[1], "modifyDatetime", TimeTool.getNowTime(true)); + method.invoke(joinPoint.getTarget(), args[0], args[1], "systemSyncStatus", CommonEnumUtil.FALSE); + } + } else if (args.length == 3) { + if (args[0].getClass().isArray()) { + method.invoke(joinPoint.getTarget(), new String[]{"modifyDatetime", "systemSyncStatus"}, + new Object[]{TimeTool.getNowTime(true), CommonEnumUtil.FALSE}, args[2]); + } else { + method.invoke(joinPoint.getTarget(), "modifyDatetime", TimeTool.getNowTime(true), args[2]); + method.invoke(joinPoint.getTarget(), "systemSyncStatus", CommonEnumUtil.FALSE, args[2]); + } + } + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } else if (methodName.equals("updateByHqlWhere")) { + Object[] args = joinPoint.getArgs(); + try { + if (args.length == 3) { + Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); + if (args[1].getClass().isArray()) { + method.invoke(joinPoint.getTarget(), args[0], new String[]{"modifyDatetime", "systemSyncStatus"}, + new Object[]{TimeTool.getNowTime(true), CommonEnumUtil.FALSE}); + } else { + method.invoke(joinPoint.getTarget(), "modifyDatetime", TimeTool.getNowTime(true), args[2]); + method.invoke(joinPoint.getTarget(), "systemSyncStatus", CommonEnumUtil.FALSE, args[2]); + } + } + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } + + private void updateBeanSync(BaseBean bean) { + bean.setSystemSyncStatus(CommonEnumUtil.FALSE); + bean.setModifyDatetime(TimeTool.getNowTime(true)); + } +} diff --git a/modules/i3plus-ext-mes-apiservice/src/main/resources/application.properties b/modules/i3plus-ext-mes-apiservice/src/main/resources/application.properties index a08c0d8..4ffa841 100644 --- a/modules/i3plus-ext-mes-apiservice/src/main/resources/application.properties +++ b/modules/i3plus-ext-mes-apiservice/src/main/resources/application.properties @@ -70,4 +70,6 @@ huaweiobs.bucketName=mes-wms-obs #访问的key huaweiobs.ak=TPNXQ2LUMRHNYYOBO8QO #访问的秘钥 -huaweiobs.sk=ppTtbisjdBxQsU124mFnubSojUsB6Wvp9KSaUAeb \ No newline at end of file +huaweiobs.sk=ppTtbisjdBxQsU124mFnubSojUsB6Wvp9KSaUAeb +#更新系统同步标识 +mes.aspect.repository = true \ No newline at end of file