|
@@ -0,0 +1,53 @@
|
|
|
+package edu.travel.config;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
|
+import org.apache.ibatis.reflection.MetaObject;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class FieldMetaObjectHandler implements MetaObjectHandler {
|
|
|
+ private final static String UPDATE_USER_ID = "updateUserId";
|
|
|
+ private final static String UPDATE_TIME = "updateTime";
|
|
|
+ private final static String CREATE_USER_ID = "createUserId";
|
|
|
+ private final static String CREATE_TIME = "createTime";
|
|
|
+ private final static String PROJECT = "project";
|
|
|
+ private final static String DELETE_FLAG = "deleteFlag";
|
|
|
+ @Override
|
|
|
+ public void insertFill(MetaObject metaObject) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
|
+ Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(principal));
|
|
|
+ if (metaObject.hasSetter(CREATE_USER_ID)) {
|
|
|
+ this.strictInsertFill(metaObject, CREATE_USER_ID, Long.class, jsonObject.getLong("id"));
|
|
|
+ }
|
|
|
+ if (metaObject.hasSetter(CREATE_TIME)) {
|
|
|
+ this.strictInsertFill(metaObject, CREATE_TIME, Long.class, System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ if (metaObject.hasSetter(PROJECT)){
|
|
|
+ this.strictInsertFill(metaObject, PROJECT, String.class, request.getHeader("project"));
|
|
|
+ }
|
|
|
+ if (metaObject.hasSetter(DELETE_FLAG)) {
|
|
|
+ this.strictInsertFill(metaObject, DELETE_FLAG, Integer.class, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateFill(MetaObject metaObject) {
|
|
|
+ Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(principal));
|
|
|
+ if (metaObject.hasSetter(UPDATE_USER_ID)) {
|
|
|
+ this.strictInsertFill(metaObject, UPDATE_USER_ID, Long.class, jsonObject.getLong("id"));
|
|
|
+ }
|
|
|
+ if (metaObject.hasSetter(UPDATE_TIME)) {
|
|
|
+ this.strictInsertFill(metaObject, UPDATE_TIME, Long.class, System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|