From d8eb22351c6d0bfe0c9f233edfd593538e6838c9 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期四, 24 四月 2025 13:58:49 +0800
Subject: [PATCH] 1、表阀一体机协议开阀类型变化; 2、表阀一体机协议开关阀报中增加水价数据;

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/AutoGenerateIdInterceptor.java |   85 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/AutoGenerateIdInterceptor.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/AutoGenerateIdInterceptor.java
new file mode 100644
index 0000000..f518814
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/AutoGenerateIdInterceptor.java
@@ -0,0 +1,85 @@
+package com.dy.common.mybatis;
+
+
+import com.alibaba.fastjson2.JSONObject;
+import com.alibaba.fastjson2.JSONWriter;
+import com.dy.common.po.BaseEntity;
+import com.dy.common.util.IDLongGenerator;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlCommandType;
+import org.apache.ibatis.plugin.*;
+
+import java.lang.reflect.Method;
+
+@Intercepts(
+    {
+        @Signature(
+            type = Executor.class,
+            method = "update",
+            args = { MappedStatement.class, Object.class }
+        )
+    }
+)
+public class AutoGenerateIdInterceptor implements Interceptor {
+
+    static int MAPPED_STATEMENT_INDEX = 0;
+    static int PARAMETER_INDEX = 1;
+    static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID = "setId";
+    static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID = "setUserId";
+    static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID = "setRoleId";
+
+    /**
+     * 鎷︽埅閫昏緫瀹炵幇
+     * @param invocation 浠g悊
+     * @return Object
+     * @throws Throwable 寮傚父
+     */
+    public Object intercept(Invocation invocation) throws Throwable {
+        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[MAPPED_STATEMENT_INDEX];
+        SqlCommandType commandType = mappedStatement.getSqlCommandType();
+        if (commandType.equals(SqlCommandType.INSERT)) {
+            Object entity = invocation.getArgs()[PARAMETER_INDEX];
+            if (entity instanceof BaseEntity) {
+                //Class<? extends Object> entityClass = entity.getClass();
+                Class<?> entityClass = entity.getClass();
+                Method setId = null ;
+                Method setUserId = null ;
+                Method setRoleId = null ;
+                try{
+                    /*
+                    // 娣诲姞鐢ㄦ埛鏃秛serId涓鸿嚜鍔ㄧ敓鎴愪富閿�俽oleId瀛樺湪鏃舵槸涓虹敤鎴风粦瑙掕壊
+                    String jsonString = JSONObject.toJSONString(entity, JSONWriter.Feature.WriteMapNullValue);
+                    JSONObject jsonObject = JSONObject.parseObject(jsonString);
+                    if(jsonObject.containsKey("userId") && !jsonObject.containsKey("roleId")) {
+                        setUserId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID, Long.class) ;
+                        setRoleId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID, Long.class) ;
+                    }else {
+                        //鏈変竴浜涘疄浣撴病鏈塱d锛屼緥濡備腑闂磋〃
+                        setId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID, Long.class) ;
+                    }
+                    */
+                    //鏈変竴浜涘疄浣撴病鏈塱d锛屼緥濡備腑闂磋〃
+                    setId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID, Long.class) ;
+                    // BaUser瀹炰綋涓槸setUserId, BaUserRole瀹炰綋涓寘鍚玸etUserId,鍜宻etRoleId
+                    setUserId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID, Long.class) ;
+                    setRoleId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID, Long.class) ;
+                }catch (Exception e){
+                    //褰揺ntityClass娌℃湁setId鏂规硶鏃讹紝浼氭姏鍑哄紓甯�
+                }
+                if(setUserId != null && setRoleId == null){
+                    setUserId.invoke(entity, new IDLongGenerator().generate());
+                }else if(setId != null){
+                    setId.invoke(entity, new IDLongGenerator().generate());
+                }
+                invocation.getArgs()[PARAMETER_INDEX] = entity;
+            }
+        }
+        return invocation.proceed();
+    }
+
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+}

--
Gitblit v1.8.0