package com.qianwen.core.mp.base;
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Date;
|
import java.util.List;
|
import javax.validation.constraints.NotEmpty;
|
import com.qianwen.core.mp.base.BaseEntity;
|
import com.qianwen.core.secure.BladeUser;
|
import com.qianwen.core.secure.utils.AuthUtil;
|
import com.qianwen.core.tool.utils.BeanUtil;
|
import com.qianwen.core.tool.utils.ClassUtil;
|
import com.qianwen.core.tool.utils.DateUtil;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.ObjectUtil;
|
import com.qianwen.core.tool.utils.ReflectUtil;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
@Validated
|
/* loaded from: blade-starter-mybatis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/mp/base/BaseServiceImpl.class */
|
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
|
public boolean save(T entity) {
|
resolveEntity(entity);
|
return super.save(entity);
|
}
|
|
public boolean saveBatch(Collection<T> entityList, int batchSize) {
|
entityList.forEach(this::resolveEntity);
|
return super.saveBatch(entityList, batchSize);
|
}
|
|
public boolean updateById(T entity) {
|
resolveEntity(entity);
|
return super.updateById(entity);
|
}
|
|
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
|
entityList.forEach(this::resolveEntity);
|
return super.updateBatchById(entityList, batchSize);
|
}
|
|
public boolean saveOrUpdate(T entity) {
|
if (entity.getId() == null)
|
return save(entity);
|
return updateById(entity);
|
}
|
|
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
|
entityList.forEach(this::resolveEntity);
|
return super.saveOrUpdateBatch(entityList, batchSize);
|
}
|
|
@Override // org.springblade.core.mp.base.BaseService
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean deleteLogic(@NotEmpty List<Long> ids) {
|
BladeUser user = AuthUtil.getUser();
|
List<T> list = new ArrayList<>();
|
ids.forEach(id -> {
|
//BaseEntity baseEntity = (BaseEntity)BeanUtil.newInstance(currentModelClass());
|
T baseEntity = (T) BeanUtil.newInstance(currentModelClass());
|
if (user != null)
|
baseEntity.setUpdateUser(user.getUserId());
|
baseEntity.setUpdateTime(DateUtil.now());
|
baseEntity.setId(id);
|
list.add(baseEntity);
|
});
|
return (updateBatchById(list) && removeByIds(ids));
|
}
|
|
@Override // org.springblade.core.mp.base.BaseService
|
public boolean changeStatus(@NotEmpty List<Long> ids, Integer status) {
|
BladeUser user = AuthUtil.getUser();
|
List<T> list = new ArrayList<>();
|
ids.forEach(id -> {
|
//BaseEntity baseEntity = (BaseEntity) BeanUtil.newInstance(currentModelClass());
|
T baseEntity = (T) BeanUtil.newInstance(currentModelClass());
|
if (user != null) {
|
baseEntity.setUpdateUser(user.getUserId());
|
}
|
baseEntity.setUpdateTime(DateUtil.now());
|
baseEntity.setId(id);
|
baseEntity.setStatus(status);
|
list.add(baseEntity);
|
});
|
return super.updateBatchById(list);
|
}
|
|
private void resolveEntity(T entity) {
|
try {
|
BladeUser user = AuthUtil.getUser();
|
Date now = DateUtil.now();
|
if (entity.getId() == null) {
|
if (user != null) {
|
entity.setCreateUser(user.getUserId());
|
if (entity.getCreateDept() == null) {
|
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, new Object[0]));
|
// String tenantId = String.valueOf(getTenantId.invoke(entity, new Object[0]));
|
if (ObjectUtil.isEmpty(tenantId)) {
|
Method setTenantId = ClassUtil.getMethod(entity.getClass(), "setTenantId",
|
new Class[] { String.class });
|
|
setTenantId.invoke(entity, new Object[] { null });
|
}
|
}
|
} catch (Throwable ex) {
|
throw new RuntimeException(ex);
|
}
|
}
|
}
|