Skip to content

Commit

Permalink
refactor: 💥 调整后端请求、响应参数模型命名风格
Browse files Browse the repository at this point in the history
XxxRequest => XxxReq
XxxVO => XxxResp
  • Loading branch information
Charles7c committed Nov 4, 2023
1 parent 598dd39 commit 87f9056
Show file tree
Hide file tree
Showing 136 changed files with 828 additions and 832 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
import top.charles7c.cnadmin.common.constant.StringConsts;
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;
import top.charles7c.cnadmin.common.model.resp.PageDataResp;
import top.charles7c.cnadmin.common.model.resp.R;

/**
* 控制器基类
*
* @param <S>
* 业务接口
* @param <V>
* @param <L>
* 列表信息
* @param <D>
* 详情信息
Expand All @@ -61,7 +61,7 @@
* @since 2023/1/26 10:45
*/
@NoArgsConstructor
public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, C extends BaseRequest> {
public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q, C extends BaseReq> {

@Autowired
protected S baseService;
Expand All @@ -78,7 +78,7 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q,
@Operation(summary = "分页查询列表", description = "分页查询列表")
@ResponseBody
@GetMapping
public PageDataVO<V> page(Q query, @Validated PageQuery pageQuery) {
public PageDataResp<L> page(Q query, @Validated PageQuery pageQuery) {
this.checkPermission(Api.LIST);
return baseService.page(query, pageQuery);
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public List<Tree<Long>> tree(Q query, SortQuery sortQuery) {
@Operation(summary = "查询列表", description = "查询列表")
@ResponseBody
@GetMapping("/list")
public List<V> list(Q query, SortQuery sortQuery) {
public List<L> list(Q query, SortQuery sortQuery) {
this.checkPermission(Api.LIST);
return baseService.list(query, sortQuery);
}
Expand All @@ -136,23 +136,23 @@ public D get(@PathVariable Long id) {
/**
* 新增
*
* @param request
* @param req
* 创建信息
* @return 自增 ID
*/
@Operation(summary = "新增数据", description = "新增数据")
@ResponseBody
@PostMapping
public R<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody C request) {
public R<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody C req) {
this.checkPermission(Api.ADD);
Long id = baseService.add(request);
Long id = baseService.add(req);
return R.ok("新增成功", id);
}

/**
* 修改
*
* @param request
* @param req
* 修改信息
* @param id
* ID
Expand All @@ -162,9 +162,9 @@ public R<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody C reque
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
@ResponseBody
@PutMapping("/{id}")
public R update(@Validated(ValidateGroup.Crud.Update.class) @RequestBody C request, @PathVariable Long id) {
public R update(@Validated(ValidateGroup.Crud.Update.class) @RequestBody C req, @PathVariable Long id) {
this.checkPermission(Api.UPDATE);
baseService.update(request, id);
baseService.update(req, id);
return R.ok("修改成功");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* 详情 VO 基类
* 详情响应基类
*
* @author Charles7c
* @since 2023/1/26 10:40
*/
@Data
public class BaseDetailVO extends BaseVO {
public class BaseDetailResp extends BaseResp {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import lombok.Data;

/**
* Request 基类
* 请求基类
*
* @author Charles7c
* @since 2023/1/30 21:51
*/
@Data
public class BaseRequest implements Serializable {
public class BaseReq implements Serializable {

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.fasterxml.jackson.annotation.JsonInclude;

/**
* VO 基类
* 响应基类
*
* @author Charles7c
* @since 2023/1/26 10:40
*/
@Data
public class BaseVO implements Serializable {
public class BaseResp implements Serializable {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

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.resp.PageDataResp;

/**
* 业务接口基类
*
* @param <V>
* @param <L>
* 列表信息
* @param <D>
* 详情信息
Expand All @@ -40,7 +40,7 @@
* @author Charles7c
* @since 2023/1/26 16:54
*/
public interface BaseService<V, D, Q, C extends BaseRequest> {
public interface BaseService<L, D, Q, C extends BaseReq> {

/**
* 分页查询列表
Expand All @@ -51,7 +51,7 @@ public interface BaseService<V, D, Q, C extends BaseRequest> {
* 分页查询条件
* @return 分页列表信息
*/
PageDataVO<V> page(Q query, PageQuery pageQuery);
PageDataResp<L> page(Q query, PageQuery pageQuery);

/**
* 查询树列表
Expand All @@ -75,7 +75,7 @@ public interface BaseService<V, D, Q, C extends BaseRequest> {
* 排序查询条件
* @return 列表信息
*/
List<V> list(Q query, SortQuery sortQuery);
List<L> list(Q query, SortQuery sortQuery);

/**
* 查看详情
Expand All @@ -89,21 +89,21 @@ public interface BaseService<V, D, Q, C extends BaseRequest> {
/**
* 新增
*
* @param request
* @param req
* 创建信息
* @return 自增 ID
*/
Long add(C request);
Long add(C req);

/**
* 修改
*
* @param request
* @param req
* 修改信息
* @param id
* ID
*/
void update(C request, Long id);
void update(C req, Long id);

/**
* 删除
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import top.charles7c.cnadmin.common.annotation.TreeField;
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.resp.PageDataResp;
import top.charles7c.cnadmin.common.service.CommonUserService;
import top.charles7c.cnadmin.common.util.ExcelUtils;
import top.charles7c.cnadmin.common.util.ExceptionUtils;
Expand All @@ -60,7 +60,7 @@
* Mapper 接口
* @param <T>
* 实体类
* @param <V>
* @param <L>
* 列表信息
* @param <D>
* 详情信息
Expand All @@ -71,41 +71,41 @@
* @author Charles7c
* @since 2023/1/26 21:52
*/
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, V, D, Q, C extends BaseRequest>
implements BaseService<V, D, Q, C> {
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, L, D, Q, C extends BaseReq>
implements BaseService<L, D, Q, C> {

@Autowired
protected M baseMapper;

private final Class<T> entityClass;
private final Class<V> voClass;
private final Class<D> detailVoClass;
private final Class<L> listClass;
private final Class<D> detailClass;

protected BaseServiceImpl() {
this.entityClass = (Class<T>)ClassUtil.getTypeArgument(this.getClass(), 1);
this.voClass = (Class<V>)ClassUtil.getTypeArgument(this.getClass(), 2);
this.detailVoClass = (Class<D>)ClassUtil.getTypeArgument(this.getClass(), 3);
this.listClass = (Class<L>)ClassUtil.getTypeArgument(this.getClass(), 2);
this.detailClass = (Class<D>)ClassUtil.getTypeArgument(this.getClass(), 3);
}

@Override
public PageDataVO<V> page(Q query, PageQuery pageQuery) {
public PageDataResp<L> page(Q query, PageQuery pageQuery) {
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageDataVO<V> pageDataVO = PageDataVO.build(page, voClass);
pageDataVO.getList().forEach(this::fill);
return pageDataVO;
PageDataResp<L> pageDataResp = PageDataResp.build(page, listClass);
pageDataResp.getList().forEach(this::fill);
return pageDataResp;
}

@Override
public List<Tree<Long>> tree(Q query, SortQuery sortQuery, boolean isSimple) {
List<V> list = this.list(query, sortQuery);
List<L> list = this.list(query, sortQuery);
if (CollUtil.isEmpty(list)) {
return new ArrayList<>(0);
}

// 如果构建简单树结构,则不包含基本树结构之外的扩展字段
TreeNodeConfig treeNodeConfig = TreeUtils.DEFAULT_CONFIG;
TreeField treeField = voClass.getDeclaredAnnotation(TreeField.class);
TreeField treeField = listClass.getDeclaredAnnotation(TreeField.class);
if (!isSimple) {
// 根据 @TreeField 配置生成树结构配置
treeNodeConfig = TreeUtils.genTreeNodeConfig(treeField);
Expand All @@ -119,7 +119,7 @@ public List<Tree<Long>> tree(Q query, SortQuery sortQuery, boolean isSimple) {
tree.setName(ReflectUtil.invoke(node, StrUtil.genGetter(treeField.nameKey())));
tree.setWeight(ReflectUtil.invoke(node, StrUtil.genGetter(treeField.weightKey())));
if (!isSimple) {
List<Field> fieldList = ReflectUtils.getNonStaticFields(voClass);
List<Field> fieldList = ReflectUtils.getNonStaticFields(listClass);
fieldList.removeIf(f -> StrUtil.containsAnyIgnoreCase(f.getName(), treeField.value(),
treeField.parentIdKey(), treeField.nameKey(), treeField.weightKey(), treeField.childrenKey()));
fieldList
Expand All @@ -129,8 +129,8 @@ public List<Tree<Long>> tree(Q query, SortQuery sortQuery, boolean isSimple) {
}

@Override
public List<V> list(Q query, SortQuery sortQuery) {
List<V> list = this.list(query, sortQuery, voClass);
public List<L> list(Q query, SortQuery sortQuery) {
List<L> list = this.list(query, sortQuery, listClass);
list.forEach(this::fill);
return list;
}
Expand Down Expand Up @@ -174,27 +174,27 @@ protected void sort(QueryWrapper<T> queryWrapper, SortQuery sortQuery) {
@Override
public D get(Long id) {
T entity = this.getById(id);
D detailVO = BeanUtil.copyProperties(entity, detailVoClass);
this.fillDetail(detailVO);
return detailVO;
D detail = BeanUtil.copyProperties(entity, detailClass);
this.fillDetail(detail);
return detail;
}

@Override
@Transactional(rollbackFor = Exception.class)
public Long add(C request) {
if (null == request) {
public Long add(C req) {
if (null == req) {
return 0L;
}
T entity = BeanUtil.copyProperties(request, entityClass);
T entity = BeanUtil.copyProperties(req, entityClass);
baseMapper.insert(entity);
return entity.getId();
}

@Override
@Transactional(rollbackFor = Exception.class)
public void update(C request, Long id) {
public void update(C req, Long id) {
T entity = this.getById(id);
BeanUtil.copyProperties(request, entity, CopyOptions.create().ignoreNullValue());
BeanUtil.copyProperties(req, entity, CopyOptions.create().ignoreNullValue());
baseMapper.updateById(entity);
}

Expand All @@ -206,9 +206,9 @@ public void delete(List<Long> ids) {

@Override
public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
List<D> list = this.list(query, sortQuery, detailVoClass);
List<D> list = this.list(query, sortQuery, detailClass);
list.forEach(this::fillDetail);
ExcelUtils.export(list, "导出数据", detailVoClass, response);
ExcelUtils.export(list, "导出数据", detailClass, response);
}

/**
Expand All @@ -231,14 +231,14 @@ protected T getById(Object id) {
* 待填充列表信息
*/
protected void fill(Object baseObj) {
if (baseObj instanceof BaseVO) {
BaseVO baseVO = (BaseVO)baseObj;
Long createUser = baseVO.getCreateUser();
if (baseObj instanceof BaseResp) {
BaseResp baseResp = (BaseResp)baseObj;
Long createUser = baseResp.getCreateUser();
if (null == createUser) {
return;
}
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
baseVO.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(createUser)));
baseResp.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(createUser)));
}
}

Expand All @@ -249,16 +249,16 @@ protected void fill(Object baseObj) {
* 待填充详情信息
*/
public void fillDetail(Object detailObj) {
if (detailObj instanceof BaseDetailVO) {
BaseDetailVO detailVO = (BaseDetailVO)detailObj;
this.fill(detailVO);
if (detailObj instanceof BaseDetailResp) {
BaseDetailResp detail = (BaseDetailResp)detailObj;
this.fill(detail);

Long updateUser = detailVO.getUpdateUser();
Long updateUser = detail.getUpdateUser();
if (null == updateUser) {
return;
}
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
detailVO.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
detail.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
/**
* 枚举接口
*
* @param <V>
* @param <T>
* value 类型
* @author Charles7c
* @since 2023/2/5 20:44
*/
public interface IBaseEnum<V extends Serializable> extends IEnum<V> {
public interface IBaseEnum<T extends Serializable> extends IEnum<T> {

/**
* 枚举描述
Expand Down
Loading

0 comments on commit 87f9056

Please sign in to comment.