| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package com.github.jfcloud.gene.handler;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
- import com.github.jfcloud.common.security.util.SecurityUtils;
- import com.github.jfcloud.gene.common.util.UserUtil;
- import org.apache.ibatis.reflection.MetaObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.util.ClassUtils;
- import java.nio.charset.Charset;
- import java.time.LocalDateTime;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- public class BaseEntityMetaObjectHandler implements MetaObjectHandler {
- private static final Logger log = LoggerFactory.getLogger(BaseEntityMetaObjectHandler.class);
- private static void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
- if (fieldVal != null) {
- if (metaObject.hasSetter(fieldName)) {
- Object userSetValue = metaObject.getValue(fieldName);
- String setValueStr = StrUtil.str(userSetValue, Charset.defaultCharset());
- if (!StrUtil.isNotBlank(setValueStr) || isCover) {
- Class<?> getterType = metaObject.getGetterType(fieldName);
- if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
- metaObject.setValue(fieldName, fieldVal);
- }
- }
- }
- }
- }
- public void insertFill(MetaObject metaObject) {
- log.debug("mybatis plus start insert fill ....");
- Map<String, Object> fieldsToFill = new HashMap<>();
- //判断创建时间类型
- setTime("createTime", metaObject, fieldsToFill);
- setTime("create_time", metaObject, fieldsToFill);
- setTime("updateTime", metaObject, fieldsToFill);
- setTime("update_time", metaObject, fieldsToFill);
- fieldsToFill.put("createBy", UserUtil.getUserName());
- fieldsToFill.put("delFlag", "0");
- fieldsToFill.put("deleted", "0");
- fieldsToFill.put("deptId", UserUtil.getDeptId());
- fieldsToFill.put("groupIndex", this.getGroupIndex());
- fieldsToFill.put("groupPi", this.getGroupPi());
- fieldsToFill.put("tenantId", UserUtil.getTenantId());
- this.fillFieldsIfExist(metaObject, fieldsToFill, false);
- }
- private void setTime(String propertyName, MetaObject metaObject, Map<String, Object> fillMap) {
- if (metaObject.hasGetter(propertyName)) {
- Class<?> createTimeType = metaObject.getGetterType(propertyName);
- if (createTimeType.equals(Date.class)) {
- fillMap.put(propertyName, new Date());
- } else if (createTimeType.equals(LocalDateTime.class)) {
- fillMap.put(propertyName, LocalDateTime.now());
- }
- }
- }
- private void fillFieldsIfExist(MetaObject metaObject, Map<String, Object> fieldsToFill, boolean isCover) {
- for(Map.Entry<String, Object> entry : fieldsToFill.entrySet()) {
- String fieldName = entry.getKey();
- Object defaultValue = entry.getValue();
- if (metaObject.hasGetter(fieldName)) {
- fillValIfNullByName(fieldName, defaultValue, metaObject, isCover);
- }
- }
- }
- public void updateFill(MetaObject metaObject) {
- Map<String, Object> fieldsToFill = new HashMap<>();
- fieldsToFill.put("updateTime", new Date());
- fieldsToFill.put("update_time", new Date());
- fieldsToFill.put("updateBy", UserUtil.getUserName());
- this.fillFieldsIfExist(metaObject, fieldsToFill, false);
- }
- private Integer getGroupIndex() {
- return SecurityUtils.getGroupIndex();
- }
- private Long getGroupPi() {
- return SecurityUtils.getGroupPi();
- }
- }
|