|  |  |  | 
|---|
|  |  |  | package com.xcong.excoin.configurations.interceptor; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.xcong.excoin.common.LoginUserUtils; | 
|---|
|  |  |  | import com.xcong.excoin.common.contants.AppContants; | 
|---|
|  |  |  | import com.xcong.excoin.common.system.base.BaseEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.member.entity.MemberEntity; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.ibatis.executor.Executor; | 
|---|
|  |  |  | import org.apache.ibatis.mapping.MappedStatement; | 
|---|
|  |  |  | import org.apache.ibatis.mapping.SqlCommandType; | 
|---|
|  |  |  | import org.apache.ibatis.plugin.*; | 
|---|
|  |  |  | import org.apache.ibatis.session.defaults.DefaultSqlSession; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.lang.reflect.Field; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @date 2020-05-13 | 
|---|
|  |  |  | **/ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | //@Component | 
|---|
|  |  |  | @Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) | 
|---|
|  |  |  | public class MybatisInterceptor implements Interceptor { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Object intercept(Invocation invocation) throws Throwable { | 
|---|
|  |  |  | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; | 
|---|
|  |  |  | String sqlId = mappedStatement.getId(); | 
|---|
|  |  |  | log.info("----sqlId----" + sqlId); | 
|---|
|  |  |  | SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); | 
|---|
|  |  |  | log.info("-------------->{}", sqlCommandType); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Object parameter = invocation.getArgs()[1]; | 
|---|
|  |  |  | if (parameter == null) { | 
|---|
|  |  |  | return invocation.proceed(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); | 
|---|
|  |  |  | if (SqlCommandType.INSERT == sqlCommandType) { | 
|---|
|  |  |  | Field[] fields = this.getAllFields(parameter); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for(Field field : fields) { | 
|---|
|  |  |  | log.info("----->{}", field.getName()); | 
|---|
|  |  |  | if ("createBy".equals(field.getName()) || "updateBy".equals(field.getName())) { | 
|---|
|  |  |  | byField(field, parameter, memberEntity); | 
|---|
|  |  |  | if (parameter instanceof DefaultSqlSession.StrictMap) { | 
|---|
|  |  |  | Map map = (Map) parameter; | 
|---|
|  |  |  | List list = (List) map.get("list"); | 
|---|
|  |  |  | for (Object o : list) { | 
|---|
|  |  |  | injectForInsert(o); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if ("createTime".equals(field.getName()) || "updateTime".equals(field.getName())) { | 
|---|
|  |  |  | timeField(field, parameter, memberEntity); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | injectForInsert(parameter); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (SqlCommandType.UPDATE == sqlCommandType) { | 
|---|
|  |  |  | Field[] fields = this.getAllFields(parameter); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (Field field : fields) { | 
|---|
|  |  |  | if ("updateBy".equals(field.getName())) { | 
|---|
|  |  |  | byField(field, parameter, memberEntity); | 
|---|
|  |  |  | if (parameter instanceof DefaultSqlSession.StrictMap) { | 
|---|
|  |  |  | Map map = (Map) parameter; | 
|---|
|  |  |  | List list = (List) map.get("list"); | 
|---|
|  |  |  | for (Object o : list) { | 
|---|
|  |  |  | injectForUpdate(o); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if ("updateTime".equals(field.getName())) { | 
|---|
|  |  |  | timeField(field, parameter, memberEntity); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | injectForUpdate(parameter); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Field[] getAllFields(Object object) { | 
|---|
|  |  |  | Class<?> clazz = object.getClass(); | 
|---|
|  |  |  | List<Field> fieldList = new ArrayList<>(); | 
|---|
|  |  |  | while (clazz != null) { | 
|---|
|  |  |  | fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()))); | 
|---|
|  |  |  | clazz = clazz.getSuperclass(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Field[] fields = new Field[fieldList.size()]; | 
|---|
|  |  |  | fieldList.toArray(fields); | 
|---|
|  |  |  | return fields; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void byField(Field field, Object parameter, MemberEntity memberEntity) throws IllegalAccessException { | 
|---|
|  |  |  | field.setAccessible(true); | 
|---|
|  |  |  | Object local = field.get(parameter); | 
|---|
|  |  |  | field.setAccessible(false); | 
|---|
|  |  |  | if (local != null) { | 
|---|
|  |  |  | field.setAccessible(true); | 
|---|
|  |  |  | field.set(parameter, memberEntity.getUsername()); | 
|---|
|  |  |  | field.setAccessible(false); | 
|---|
|  |  |  | public void injectForInsert(Object o) { | 
|---|
|  |  |  | MemberEntity member = LoginUserUtils.getUser(); | 
|---|
|  |  |  | if (o instanceof BaseEntity) { | 
|---|
|  |  |  | BaseEntity baseEntity = (BaseEntity) o; | 
|---|
|  |  |  | if (member != null) { | 
|---|
|  |  |  | String by = member.getPhone() != null ? member.getPhone() : member.getEmail(); | 
|---|
|  |  |  | baseEntity.setCreateBy(by); | 
|---|
|  |  |  | baseEntity.setUpdateBy(by); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | baseEntity.setCreateBy(AppContants.SYSTEM_USER); | 
|---|
|  |  |  | baseEntity.setUpdateBy(AppContants.SYSTEM_USER); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | baseEntity.setCreateTime(new Date()); | 
|---|
|  |  |  | baseEntity.setUpdateTime(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void timeField(Field field, Object parameter, MemberEntity memberEntity) throws IllegalAccessException { | 
|---|
|  |  |  | field.setAccessible(true); | 
|---|
|  |  |  | Object local_createBy = field.get(parameter); | 
|---|
|  |  |  | field.setAccessible(false); | 
|---|
|  |  |  | if (local_createBy != null) { | 
|---|
|  |  |  | field.setAccessible(true); | 
|---|
|  |  |  | field.set(parameter, new Date()); | 
|---|
|  |  |  | field.setAccessible(false); | 
|---|
|  |  |  | public void injectForUpdate(Object o) { | 
|---|
|  |  |  | MemberEntity member = LoginUserUtils.getUser(); | 
|---|
|  |  |  | if (o instanceof BaseEntity) { | 
|---|
|  |  |  | BaseEntity baseEntity = (BaseEntity) o; | 
|---|
|  |  |  | if (member != null) { | 
|---|
|  |  |  | String by = member.getPhone() != null ? member.getPhone() : member.getEmail(); | 
|---|
|  |  |  | baseEntity.setUpdateBy(by); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | baseEntity.setUpdateBy(AppContants.SYSTEM_USER); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | baseEntity.setUpdateTime(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|