|
@@ -3,11 +3,15 @@ package shop.alien.store.config;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import shop.alien.util.common.JwtUtil;
|
|
import shop.alien.util.common.JwtUtil;
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Mybatis日期填充
|
|
* Mybatis日期填充
|
|
@@ -17,6 +21,7 @@ import java.util.Date;
|
|
* @date 2024/12/6 10:19
|
|
* @date 2024/12/6 10:19
|
|
*/
|
|
*/
|
|
@Component
|
|
@Component
|
|
|
|
+@Slf4j
|
|
public class MyBatisFieldHandler implements MetaObjectHandler {
|
|
public class MyBatisFieldHandler implements MetaObjectHandler {
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -26,17 +31,17 @@ public class MyBatisFieldHandler implements MetaObjectHandler {
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public void insertFill(MetaObject metaObject) {
|
|
public void insertFill(MetaObject metaObject) {
|
|
- // 设置创建时间
|
|
|
|
- setFieldValByName("createdTime", new Date(), metaObject);
|
|
|
|
- // 设置更新时间
|
|
|
|
- setFieldValByName("updatedTime", new Date(), metaObject);
|
|
|
|
-
|
|
|
|
- Integer currentUserId = getUserId();
|
|
|
|
- if (currentUserId != null) {
|
|
|
|
- // 设置创建人 ID
|
|
|
|
- setFieldValByName("createdUserId", currentUserId, metaObject);
|
|
|
|
- // 设置修改人 ID
|
|
|
|
- setFieldValByName("updatedUserId", currentUserId, metaObject);
|
|
|
|
|
|
+ log.info("=================================insertFill=========================================");
|
|
|
|
+ System.out.println(metaObject.getOriginalObject());
|
|
|
|
+ //字段为实体类名, 不是表字段名
|
|
|
|
+ this.setFieldValByName("createdTime", new Date(), metaObject);
|
|
|
|
+ this.setFieldValByName("updatedTime", new Date(), metaObject);
|
|
|
|
+ if (JwtUtil.hasToken()) {
|
|
|
|
+ this.setFieldValByName("createdUserId", Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId"), metaObject);
|
|
|
|
+ this.setFieldValByName("updatedUserId", Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId"), metaObject);
|
|
|
|
+ } else {
|
|
|
|
+ this.setFieldValByName("createdUserId", 0, metaObject);
|
|
|
|
+ this.setFieldValByName("updatedUserId", 0, metaObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -46,24 +51,14 @@ public class MyBatisFieldHandler implements MetaObjectHandler {
|
|
* @param metaObject 元对象
|
|
* @param metaObject 元对象
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public void updateFill(MetaObject metaObject) { //填充 更新时间
|
|
|
|
- // 设置更新时间
|
|
|
|
- setFieldValByName("updatedTime", new Date(), metaObject);
|
|
|
|
-
|
|
|
|
- Integer currentUserId = getUserId();
|
|
|
|
- if (currentUserId != null) {
|
|
|
|
- // 设置修改人 ID
|
|
|
|
- setFieldValByName("updatedUserId", currentUserId, metaObject);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Integer getUserId() {
|
|
|
|
- String token = JwtUtil.getToken();
|
|
|
|
- String userId = "0";
|
|
|
|
- if (StringUtils.isNotEmpty(token)) {
|
|
|
|
- JSONObject tokenInfo = JwtUtil.getTokenInfo(token);
|
|
|
|
- userId = tokenInfo.getString("userId");
|
|
|
|
|
|
+ public void updateFill(MetaObject metaObject) {
|
|
|
|
+ log.info("=================================updateFill=========================================");
|
|
|
|
+ //字段为实体类名, 不是表字段名
|
|
|
|
+ this.setFieldValByName("updatedTime", new Date(), metaObject);
|
|
|
|
+ if (JwtUtil.hasToken()) {
|
|
|
|
+ this.setFieldValByName("updatedUserId", Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId"), metaObject);
|
|
|
|
+ } else {
|
|
|
|
+ this.setFieldValByName("updatedUserId", 0, metaObject);
|
|
}
|
|
}
|
|
- return Integer.parseInt(userId);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|