69 lines
2.6 KiB
Java
69 lines
2.6 KiB
Java
package com.evo.common.handler;
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
import com.evo.common.constant.Constants;
|
|
import com.evo.common.utils.DateUtils;
|
|
import com.evo.common.utils.SecurityUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
/**
|
|
* @desc: 修改和新增自定义注解
|
|
* @ClassName:InsertAndUpdateMybatisHandler
|
|
* @date: 2025年04月17日 9:03
|
|
* @author: andy.shi
|
|
* @contact: 17330188597
|
|
* @remark: 开发人员联系方式 1042025947@qq.com/微信同步
|
|
*/
|
|
@Component
|
|
@Slf4j
|
|
public class InsertAndUpdateMybatisHandler implements MetaObjectHandler {
|
|
@Override
|
|
public void insertFill(MetaObject metaObject) {
|
|
try {
|
|
String userName = SecurityUtils.getUsername();
|
|
if(StringUtils.isEmpty(userName)){
|
|
log.info("当前操作未登录, 无需执行");
|
|
return;
|
|
}
|
|
if(ObjectUtils.isNotEmpty(metaObject)){
|
|
mySetFieldValByName("createBy",SecurityUtils.getUsername(),metaObject);
|
|
mySetFieldValByName("createTime",DateUtils.getNowDate(),metaObject);
|
|
mySetFieldValByName("delFlag",Constants.DELETE_FLAG_0,metaObject);
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("自动填充拦截器异常=====insert,对象信息:{}, 异常信息:{}", JSONObject.toJSONString(metaObject), e.getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
private void mySetFieldValByName(String filedName, Object fieldVal, MetaObject metaObject) {
|
|
Object createType = getFieldValByName(filedName,metaObject);
|
|
if(ObjectUtils.isEmpty(createType)){
|
|
setFieldValByName(filedName,fieldVal,metaObject);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void updateFill(MetaObject metaObject) {
|
|
try {
|
|
String userName = SecurityUtils.getUsername();
|
|
if(StringUtils.isEmpty(userName)){
|
|
log.info("当前操作未登录, 无需执行");
|
|
return;
|
|
}
|
|
if(ObjectUtils.isNotEmpty(metaObject)){
|
|
mySetFieldValByName("updateBy",userName,metaObject);
|
|
mySetFieldValByName("updateTime",DateUtils.getNowDate(),metaObject);
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("自动填充拦截器异常=====update,对象信息:{}, 异常信息:{}", JSONObject.toJSONString(metaObject),e.getMessage());
|
|
}
|
|
}
|
|
}
|