更新
This commit is contained in:
@@ -1,43 +1,84 @@
|
||||
package quant.rich.emoney.controller.common;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import quant.rich.emoney.exception.RException;
|
||||
import quant.rich.emoney.pojo.dto.R;
|
||||
|
||||
@Slf4j
|
||||
public abstract class UpdateBoolController<T, M extends BaseMapper<T>, S extends ServiceImpl<M, T>> extends BaseController {
|
||||
public abstract class UpdateBoolController<T> extends BaseController {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
private Map<Class<?>, IService<?>> serviceMap;
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
serviceMap = new HashMap<>();
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map<String, IService> beans = ctx.getBeansOfType(IService.class);
|
||||
for (IService<?> service : beans.values()) {
|
||||
ResolvableType type = ResolvableType.forClass(service.getClass()).as(IService.class);
|
||||
Class<?> entityType = type.getGeneric(0).resolve();
|
||||
if (entityType != null) {
|
||||
serviceMap.put(entityType, service);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public IService<T> getService(Class<T> entityClass) {
|
||||
return (IService<T>) serviceMap.get(entityClass);
|
||||
}
|
||||
|
||||
@PostMapping("/updateBool")
|
||||
@ResponseBody
|
||||
protected
|
||||
R<?> updateBool(S service, Class<T> clazz, SFunction<T, ?> idName, Object idValue, String field, Boolean value) {
|
||||
R<?> updateBool(String id, String field, Boolean value) {
|
||||
|
||||
|
||||
ResolvableType type = ResolvableType.forClass(this.getClass()).as(UpdateBoolController.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<T> clazz = (Class<T>) type.getGeneric(0).resolve();
|
||||
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
||||
Object converted = mapper.convertValue(idValue, tableInfo.getKeyType());
|
||||
Object converted = mapper.convertValue(id, tableInfo.getKeyType());
|
||||
|
||||
// 获取 Service
|
||||
IService<T> s = getService((Class<T>) clazz);
|
||||
|
||||
try {
|
||||
String idField = tableInfo.getKeyColumn();
|
||||
Field declaredField = clazz.getDeclaredField(field);
|
||||
Optional<TableFieldInfo> fieldInfo = tableInfo.getFieldList().stream()
|
||||
.filter(f -> f.getProperty().equals(field))
|
||||
.findFirst();
|
||||
if (declaredField.getType().equals(Boolean.class)) {
|
||||
return R.judge(service.update(
|
||||
return R.judge(s.update(
|
||||
new UpdateWrapper<T>()
|
||||
.set(fieldInfo.get().getColumn(), value)
|
||||
.lambda().eq(idName, converted)
|
||||
.eq(idField, converted)
|
||||
), "更新失败,请查看日志");
|
||||
}
|
||||
}
|
||||
@@ -47,8 +88,4 @@ public abstract class UpdateBoolController<T, M extends BaseMapper<T>, S extends
|
||||
throw RException.badRequest().setLogRequest(true);
|
||||
}
|
||||
|
||||
@PostMapping("/updateBool")
|
||||
@ResponseBody
|
||||
abstract protected R<?> updateBool(String id, String field, Boolean value);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user