|
|
@@ -1,11 +1,21 @@
|
|
|
package cn.tr.core.config;
|
|
|
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.core.pojo.LoginUser;
|
|
|
import cn.tr.core.strategy.LoginUserStrategy;
|
|
|
import cn.tr.core.constant.MybatisConstant;
|
|
|
+import cn.tr.core.utils.SecurityUtil;
|
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
import java.util.Date;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
|
|
|
|
|
|
/**
|
|
|
* MP注入处理器
|
|
|
@@ -19,38 +29,54 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
|
|
|
@Override
|
|
|
public void insertFill(MetaObject metaObject) {
|
|
|
try {
|
|
|
- String localUserId = LoginUserStrategy.tr.getCurrentUserId();
|
|
|
+ LoginUser loginUser =null;
|
|
|
+ try {
|
|
|
+ loginUser= SecurityUtil.getLoginUser();
|
|
|
+ }catch (Exception __){
|
|
|
+
|
|
|
+ }
|
|
|
if (metaObject.hasGetter(MybatisConstant.CREATE_TIME) && metaObject.getValue(MybatisConstant.CREATE_TIME) == null) {
|
|
|
- this.strictInsertFill(metaObject,MybatisConstant. CREATE_TIME, Date.class, new Date());
|
|
|
+ this.strictInsertFill(metaObject, MybatisConstant.CREATE_TIME, Date.class, new Date());
|
|
|
+ }
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.IS_DELETE) && metaObject.getValue(MybatisConstant.IS_DELETE) == null) {
|
|
|
+ this.strictInsertFill(metaObject, MybatisConstant.IS_DELETE, Integer.class,0);
|
|
|
}
|
|
|
if (metaObject.hasGetter(MybatisConstant.CREATE_BY) && metaObject.getValue(MybatisConstant.CREATE_BY) == null) {
|
|
|
|
|
|
- this.strictInsertFill(metaObject, MybatisConstant.CREATE_BY, String.class,localUserId);
|
|
|
+ this.strictInsertFill(metaObject, MybatisConstant.CREATE_BY, String.class, Objects.isNull(loginUser) ? "1" : loginUser.getId().toString());
|
|
|
}
|
|
|
- if (metaObject.hasGetter(MybatisConstant.DELETED) && metaObject.getValue(MybatisConstant.DELETED) == null) {
|
|
|
- this.strictInsertFill(metaObject, MybatisConstant.DELETED, Integer.class,0);
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.UPDATE_TIME) && metaObject.getValue(MybatisConstant.UPDATE_TIME) == null) {
|
|
|
+ this.strictUpdateFill(metaObject, MybatisConstant.UPDATE_TIME, Date.class, new Date());
|
|
|
+ }
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.UPDATE_BY) && metaObject.getValue(MybatisConstant.UPDATE_BY) == null) {
|
|
|
+ this.strictUpdateFill(metaObject, MybatisConstant.UPDATE_BY, String.class, Objects.isNull(loginUser) ? "1" : loginUser.getId().toString());
|
|
|
+ }
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.TENANT_ID) && metaObject.getValue(MybatisConstant.TENANT_ID) == null) {
|
|
|
+ ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ String tenantId ="1";
|
|
|
+ if(request!=null){
|
|
|
+ tenantId = String.valueOf(request.getAttribute("tenantId", SCOPE_REQUEST));
|
|
|
+ }
|
|
|
+ this.strictInsertFill(metaObject, MybatisConstant.TENANT_ID, String.class, tenantId);
|
|
|
}
|
|
|
- updateFill(metaObject);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.warn("CreateAndUpdateMetaObjectHandler insertFill confront a error,{}",e.getMessage());
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001.getErrMsg());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void updateFill(MetaObject metaObject) {
|
|
|
try {
|
|
|
- String localUserId = LoginUserStrategy.tr.getCurrentUserId();
|
|
|
- if (metaObject.hasGetter(MybatisConstant.UPDATE_TIME) ) {
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.UPDATE_TIME) && metaObject.getValue(MybatisConstant.UPDATE_TIME) == null) {
|
|
|
this.strictUpdateFill(metaObject, MybatisConstant.UPDATE_TIME, Date.class, new Date());
|
|
|
}
|
|
|
- if (metaObject.hasGetter(MybatisConstant.UPDATE_BY) ) {
|
|
|
- this.strictUpdateFill(metaObject,MybatisConstant. UPDATE_BY, String.class,localUserId);
|
|
|
+ if (metaObject.hasGetter(MybatisConstant.UPDATE_BY) && metaObject.getValue(MybatisConstant.UPDATE_BY) == null) {
|
|
|
+ LoginUser loginUser = SecurityUtil.getLoginUser();
|
|
|
+ this.strictUpdateFill(metaObject, MybatisConstant.UPDATE_BY, String.class, Objects.isNull(loginUser) ? null : loginUser.getId().toString());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("CreateAndUpdateMetaObjectHandler updateFill confront a error,{}",e.getMessage());
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001.getErrMsg());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|