mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-11-05 08:57:12 +08:00
优化:🔥 深度优化后端 CRUD 公共组件,并抽取前端下载功能到 CRUD 公共组件
1. 后端抽取导出功能到 CRUD 公共组件 2. 查询列表及导出接口支持排序参数 3. 深度优化 BaseServiceImpl 中的 CRUD 公共实现 4. 前端抽取公共下载组件 5. 优化部分细节并修复部分错误
This commit is contained in:
@@ -18,6 +18,8 @@ package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -29,6 +31,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.query.SortQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
|
||||
@@ -76,13 +79,15 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q,
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param sortQuery
|
||||
* 排序查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
@Operation(summary = "查询列表")
|
||||
@ResponseBody
|
||||
@GetMapping("/all")
|
||||
protected R<List<V>> list(@Validated Q query) {
|
||||
List<V> list = baseService.list(query);
|
||||
@GetMapping("/list")
|
||||
protected R<List<V>> list(@Validated Q query, @Validated SortQuery sortQuery) {
|
||||
List<V> list = baseService.list(query, sortQuery);
|
||||
return R.ok(list);
|
||||
}
|
||||
|
||||
@@ -147,4 +152,20 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q,
|
||||
baseService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param sortQuery
|
||||
* 排序查询条件
|
||||
* @param response
|
||||
* 响应对象
|
||||
*/
|
||||
@Operation(summary = "导出数据")
|
||||
@GetMapping("/export")
|
||||
protected void export(@Validated Q query, @Validated SortQuery sortQuery, HttpServletResponse response) {
|
||||
baseService.export(query, sortQuery, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.query.SortQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
|
||||
/**
|
||||
@@ -53,9 +56,11 @@ public interface BaseService<V, D, Q, C extends BaseRequest> {
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param sortQuery
|
||||
* 排序查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
List<V> list(Q query);
|
||||
List<V> list(Q query, SortQuery sortQuery);
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
@@ -90,4 +95,16 @@ public interface BaseService<V, D, Q, C extends BaseRequest> {
|
||||
* ID 列表
|
||||
*/
|
||||
void delete(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 导出
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param sortQuery
|
||||
* 排序查询条件
|
||||
* @param response
|
||||
* 响应对象
|
||||
*/
|
||||
void export(Q query, SortQuery sortQuery, HttpServletResponse response);
|
||||
}
|
||||
|
||||
@@ -16,20 +16,35 @@
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.query.SortQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
import top.charles7c.cnadmin.common.service.CommonUserService;
|
||||
import top.charles7c.cnadmin.common.util.ExcelUtils;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
@@ -65,32 +80,39 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
public PageDataVO<V> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
return PageDataVO.build(page, voClass);
|
||||
PageDataVO<V> pageDataVO = PageDataVO.build(page, voClass);
|
||||
pageDataVO.getList().forEach(this::fill);
|
||||
return pageDataVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<V> list(Q query) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return BeanUtil.copyToList(entityList, voClass);
|
||||
public List<V> list(Q query, SortQuery sortQuery) {
|
||||
List<V> list = this.list(query, sortQuery, voClass);
|
||||
list.forEach(this::fill);
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public D get(Long id) {
|
||||
T entity = this.getById(id);
|
||||
return BeanUtil.copyProperties(entity, detailVoClass);
|
||||
D detailVO = BeanUtil.copyProperties(entity, detailVoClass);
|
||||
this.fillDetail(detailVO);
|
||||
return detailVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public abstract Long create(C request);
|
||||
public Long create(C request) {
|
||||
if (request == null) {
|
||||
return 0L;
|
||||
}
|
||||
// 保存信息
|
||||
T entity = BeanUtil.copyProperties(request, entityClass);
|
||||
baseMapper.insert(entity);
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
|
||||
Object idValue = tableInfo.getPropertyValue(entity, this.currentEntityIdName());
|
||||
return Convert.toLong(idValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@@ -105,6 +127,35 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
|
||||
List<D> list = this.list(query, sortQuery, detailVoClass);
|
||||
list.forEach(this::fillDetail);
|
||||
ExcelUtils.export(list, "导出数据", detailVoClass, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param sortQuery
|
||||
* 排序查询条件
|
||||
* @param targetClass
|
||||
* 指定类型
|
||||
* @return 列表信息
|
||||
*/
|
||||
protected <E> List<E> list(Q query, SortQuery sortQuery, Class<E> targetClass) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
// 设置排序
|
||||
Sort sort = sortQuery.getSort();
|
||||
for (Sort.Order order : sort) {
|
||||
queryWrapper.orderBy(order != null, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
|
||||
}
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return CollUtil.isNotEmpty(entityList) ? BeanUtil.copyToList(entityList, targetClass) : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询
|
||||
*
|
||||
@@ -118,6 +169,57 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
*
|
||||
* @param baseObj
|
||||
* 待填充列表信息
|
||||
*/
|
||||
protected void fill(Object baseObj) {
|
||||
if (baseObj instanceof BaseVO) {
|
||||
BaseVO baseVO = (BaseVO)baseObj;
|
||||
Long createUser = baseVO.getCreateUser();
|
||||
if (createUser == null) {
|
||||
return;
|
||||
}
|
||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
||||
baseVO.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(createUser)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充详情数据
|
||||
*
|
||||
* @param detailObj
|
||||
* 待填充详情信息
|
||||
*/
|
||||
protected void fillDetail(Object detailObj) {
|
||||
if (detailObj instanceof BaseDetailVO) {
|
||||
BaseDetailVO detailVO = (BaseDetailVO)detailObj;
|
||||
this.fill(detailVO);
|
||||
|
||||
Long updateUser = detailVO.getUpdateUser();
|
||||
if (updateUser == null) {
|
||||
return;
|
||||
}
|
||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
||||
detailVO.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 ID 名称
|
||||
*
|
||||
* @return 实体类 ID 名称
|
||||
*/
|
||||
protected String currentEntityIdName() {
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
|
||||
String keyProperty = tableInfo.getKeyProperty();
|
||||
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
|
||||
return keyProperty;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user