BaseController转为接口,追加导入导出通用方式

This commit is contained in:
yuejiajun 2025-10-15 17:01:18 +08:00
parent 3d15dfdc17
commit 091b281544
2 changed files with 95 additions and 23 deletions

View File

@ -7,10 +7,15 @@ import com.example.demo.common.domain.BaseVO;
import com.example.demo.common.exception.BusinessException;
import com.example.demo.common.wrapper.WrapMapper;
import com.example.demo.common.wrapper.Wrapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.slf4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 统一 Controller 接口
@ -24,8 +29,7 @@ import java.util.List;
* @version 1.0.0
*/
@SuppressWarnings("all")
@Slf4j
public abstract class BaseController<
public interface BaseController<
SERVICE extends BaseService,
DTO extends BaseDTO,
QueryDTO extends BaseQueryDTO,
@ -39,7 +43,7 @@ public abstract class BaseController<
*
* @return 服务层实例继承自BaseService
*/
abstract protected SERVICE getService();
public SERVICE getService();
/**
* 获取操作名称
@ -49,7 +53,17 @@ public abstract class BaseController<
*
* @return 操作名称"用户""角色"
*/
abstract protected String getOperationName();
public String getOperationName();
/**
* 获取操作名称
* <p>
* 子类必须实现此方法返回当前操作的中文名称用于日志记录和错误信息提示
* </p>
*
* @return 操作名称"用户""角色"
*/
public Logger getLog();
/**
* 创建对象的通用方法
@ -57,14 +71,14 @@ public abstract class BaseController<
* @param dto 数据传输对象
* @return 操作结果包装对象
*/
protected Wrapper<VO> create(DTO dto) {
default public Wrapper<VO> create(DTO dto) {
try {
return (Wrapper<VO>) WrapMapper.ok(getService().create(dto));
} catch (BusinessException e) {
log.error("{}创建失败: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}创建失败: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(e.getMessage());
} catch (Exception e) {
log.error("{}创建异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}创建异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(getOperationName() + "创建失败");
}
}
@ -75,17 +89,17 @@ public abstract class BaseController<
* @param dto 数据传输对象
* @return 操作结果包装对象
*/
protected Wrapper<VO> update(DTO dto) {
default public Wrapper<VO> update(DTO dto) {
try {
getService().update(dto);
// 假设DTO有getId方法
VO result = (VO) getService().detail(getIdFromDto(dto));
return WrapMapper.ok(result);
} catch (BusinessException e) {
log.error("{}更新失败: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}更新失败: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(e.getMessage());
} catch (Exception e) {
log.error("{}更新异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}更新异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(getOperationName() + "更新失败");
}
}
@ -96,15 +110,15 @@ public abstract class BaseController<
* @param id 对象ID
* @return 操作结果包装对象
*/
protected Wrapper<Void> delete(String id) {
default public Wrapper<Void> delete(String id) {
try {
getService().delete(id);
return WrapMapper.ok();
} catch (BusinessException e) {
log.error("{}删除失败: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}删除失败: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(e.getMessage());
} catch (Exception e) {
log.error("{}删除异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("{}删除异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(getOperationName() + "删除失败");
}
}
@ -115,15 +129,15 @@ public abstract class BaseController<
* @param id 对象ID
* @return 操作结果包装对象
*/
protected Wrapper<VO> detail(String id) {
default public Wrapper<VO> detail(String id) {
try {
VO result = (VO) getService().detail(id);
return WrapMapper.ok(result);
} catch (BusinessException e) {
log.error("获取{}详情失败: {}", getOperationName(), e.getMessage(), e);
getLog().error("获取{}详情失败: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error(e.getMessage());
} catch (Exception e) {
log.error("获取{}详情异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("获取{}详情异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error("获取" + getOperationName() + "详情失败");
}
}
@ -134,12 +148,12 @@ public abstract class BaseController<
* @param queryDTO 查询数据传输对象
* @return 操作结果包装对象
*/
protected Wrapper<SearchResult<VO>> pageList(QueryDTO queryDTO) {
default public Wrapper<SearchResult<VO>> pageList(QueryDTO queryDTO) {
try {
SearchResult<VO> result = getService().pageList(queryDTO);
return WrapMapper.ok(result);
} catch (Exception e) {
log.error("查询{}列表异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("查询{}列表异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error("查询" + getOperationName() + "列表失败");
}
}
@ -150,16 +164,47 @@ public abstract class BaseController<
* @param queryDTO 查询数据传输对象
* @return 操作结果包装对象
*/
protected Wrapper<List<VO>> optionsList(QueryDTO queryDTO) {
default public Wrapper<List<VO>> optionsList(QueryDTO queryDTO) {
try {
List<VO> result = getService().option(queryDTO);
return WrapMapper.ok(result);
} catch (Exception e) {
log.error("获取所有{}异常: {}", getOperationName(), e.getMessage(), e);
getLog().error("获取所有{}异常: {}", getOperationName(), e.getMessage(), e);
return WrapMapper.error("获取所有" + getOperationName() + "失败");
}
}
/**
* 导入JSONL数据的通用方法
*
* @param file JSONL文件
* @return 操作结果包装对象
*/
default public Map<String, String> importJSONLData(MultipartFile file) {
Map<String, String> result = new HashMap<>();
try {
// 读取文件内容
String content = new String(file.getBytes(), StandardCharsets.UTF_8);
// 调用服务层导入数据
result.putAll(getService().importJSONLData(content));
} catch (Exception e) {
result.put("error", "导入失败");
}
return result;
}
/**
* 导出JSONL数据的通用方法
*
* @param ids 数据主键ID
* @return 操作结果包装对象
*/
default Map<String, String> exportJSONLData(HttpServletResponse response, List<String> ids) {
// 调用服务层导入数据
getService().exportJSONLData(response, ids);
return null;
}
/**
* 从DTO中获取ID
* 默认实现子类可以重写此方法以适应不同的DTO结构
@ -167,13 +212,13 @@ public abstract class BaseController<
* @param dto 数据传输对象
* @return 对象ID
*/
protected String getIdFromDto(Object dto) {
default public String getIdFromDto(Object dto) {
try {
// 尝试通过反射获取id属性
java.lang.reflect.Method getIdMethod = dto.getClass().getMethod("getId");
return (String) getIdMethod.invoke(dto);
} catch (Exception e) {
log.warn("无法从DTO中获取ID使用默认实现", e);
getLog().warn("无法从DTO中获取ID使用默认实现", e);
return null;
}
}

View File

@ -6,8 +6,10 @@ import com.example.demo.common.domain.BaseEntity;
import com.example.demo.common.domain.BaseQueryDTO;
import com.example.demo.common.domain.BaseVO;
import com.mybatisflex.core.service.IService;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
* 统一 Service 接口
@ -60,9 +62,34 @@ public interface BaseService<Entity extends BaseEntity, VO extends BaseVO, DTO e
*/
VO detail(String id);
/**
* 导入细节用于导入 jsonl
* @param line 导入行内容
* @return 导入处理后信息
*/
Object importDetail(String line);
/**
* 导出详情用于生成 jsonl
* @param id ID
* @return 导出处理后信息
*/
Object exportDetail(String id);
/**
* 刷新缓存
*/
void refreshCache();
/**
* 导入JSONL数据
*/
Map<String, String> importJSONLData(String content);
/**
* 导出JSONL数据
* @param ids ID列表可以导出多条数据
*/
void exportJSONLData(HttpServletResponse response, List<String> ids);
}