xiebin
2022-09-20 689f6b7f55519874daca1a1ffb52dae6f456e360
增加工具类解决重写save方法没有给技术字段赋值的问题
1 files modified
1 files added
75 ■■■■■ changed files
src/main/java/org/springblade/common/tool/EnhancementServicesTool.java 63 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/taskinfo/service/impl/TaskInfoServiceImpl.java 12 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/tool/EnhancementServicesTool.java
New file
@@ -0,0 +1,63 @@
package org.springblade.common.tool;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
/**
 * @ClassName EnhancementServicesTool
 * @Description TODO 给框架重写save方法导致没有默认技术字段赋值的解决方法
 * @Author aix
 * @Date 2022/9/20 16:16
 * @Version 1.0
 */
public class EnhancementServicesTool <T extends BaseEntity> {
    public void resolveEntity(T entity) {
        try {
            BladeUser user = AuthUtil.getUser();
            Date now = DateUtil.now();
            if (entity.getId() == null) {
                if (user != null) {
                    entity.setCreateUser(user.getUserId());
                    entity.setCreateDept(Func.firstLong(user.getDeptId()));
                    entity.setUpdateUser(user.getUserId());
                }
                if (entity.getStatus() == null) {
                    entity.setStatus(1);
                }
                entity.setCreateTime(now);
            } else if (user != null) {
                entity.setUpdateUser(user.getUserId());
            }
            entity.setUpdateTime(now);
            entity.setIsDeleted(0);
            Field field = ReflectUtil.getField(entity.getClass(), "tenantId");
            if (ObjectUtil.isNotEmpty(field)) {
                Method getTenantId = ClassUtil.getMethod(entity.getClass(), "getTenantId", new Class[0]);
                String tenantId = String.valueOf(getTenantId.invoke(entity));
                if (ObjectUtil.isEmpty(tenantId)) {
                    Method setTenantId = ClassUtil.getMethod(entity.getClass(), "setTenantId", new Class[]{String.class});
                    setTenantId.invoke(entity, null);
                }
            }
        } catch (Throwable var8) {
            try {
                throw var8;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
src/main/java/org/springblade/modules/taskinfo/service/impl/TaskInfoServiceImpl.java
@@ -18,6 +18,10 @@
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springblade.common.tool.EnhancementServicesTool;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.*;
import org.springblade.modules.taskinfo.entity.TaskInfoEntity;
import org.springblade.modules.taskinfo.vo.TaskInfoVO;
import org.springblade.modules.taskinfo.mapper.TaskInfoMapper;
@@ -25,6 +29,11 @@
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
/**
 * 日常巡检 服务实现类
@@ -42,6 +51,8 @@
    @Override
    public boolean save(TaskInfoEntity entity) {
        EnhancementServicesTool tool = new EnhancementServicesTool<TaskInfoEntity>();
        tool.resolveEntity(entity);
        return SqlHelper.retBool(baseMapper.saveTask(entity));
    }
@@ -54,4 +65,5 @@
    public boolean updateById(TaskInfoEntity entity) {
        return SqlHelper.retBool(baseMapper.updateTask(entity));
    }
}