refactor: 分页响应结构体统一使用PageResult

This commit is contained in:
郝先瑞 2022-02-19 12:15:17 +08:00
parent 2388d32662
commit fa6a600b02
22 changed files with 111 additions and 75 deletions

View File

@ -2,6 +2,7 @@ package com.youlai.mall.oms.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.oms.pojo.dto.OrderDTO;
import com.youlai.mall.oms.pojo.entity.OmsOrder;
@ -39,9 +40,9 @@ public class OmsOrderController {
@ApiOperation("订单列表")
@GetMapping
public Result listOrdersWithPage(OrderPageQuery queryParams) {
public PageResult listOrdersWithPage(OrderPageQuery queryParams) {
IPage<OmsOrder> result = orderService.listOrdersWithPage(queryParams);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "订单详情")

View File

@ -32,13 +32,10 @@ public class CartController {
@GetMapping
@ApiOperationSupport(order = 1)
public <T> Result<T> getCart() {
try {
Long memberId = MemberUtils.getMemberId();
List<CartItemDTO> result = cartService.listCartItemByMemberId(memberId);
return Result.success((T) result);
} catch (Exception e) {
return Result.success((T) Collections.EMPTY_LIST);
}
Long memberId = MemberUtils.getMemberId();
List<CartItemDTO> result = cartService.listCartItemByMemberId(memberId);
return Result.success((T) result);
}
@ApiOperation(value = "删除购物车")

View File

@ -3,6 +3,7 @@ package com.youlai.mall.oms.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.common.web.util.MemberUtils;
import com.youlai.mall.oms.enums.PayTypeEnum;
@ -37,14 +38,14 @@ public class OrderController {
@ApiOperation("分页列表")
@GetMapping
public Result listOrdersWithPage(OrderPageQuery queryParams) {
public PageResult listOrdersWithPage(OrderPageQuery queryParams) {
IPage<OmsOrder> result = orderService.page(
new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
new LambdaQueryWrapper<OmsOrder>()
.eq(queryParams.getStatus() != null, OmsOrder::getStatus, queryParams.getStatus())
.eq(OmsOrder::getMemberId, MemberUtils.getMemberId())
);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation("订单确认")

View File

@ -3,6 +3,7 @@ package com.youlai.mall.pms.controller.admin;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.pms.pojo.entity.PmsBrand;
import com.youlai.mall.pms.service.IPmsBrandService;
@ -26,7 +27,7 @@ public class BrandController {
@ApiOperation(value = "品牌列表分页")
@GetMapping("/page")
public Result getBrandPageList(
public PageResult getBrandPageList(
@ApiParam("页码") Long pageNum,
@ApiParam("每页数量") Long pageSize,
@ApiParam("品牌名称") String name
@ -36,7 +37,7 @@ public class BrandController {
.orderByDesc(PmsBrand::getGmtModified)
.orderByDesc(PmsBrand::getGmtCreate);
Page<PmsBrand> result = iPmsBrandService.page(new Page<>(pageNum, pageSize), queryWrapper);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "品牌列表")

View File

@ -3,6 +3,7 @@ package com.youlai.mall.pms.controller.admin;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.pms.pojo.dto.admin.GoodsFormDTO;
import com.youlai.mall.pms.pojo.entity.PmsSpu;
@ -31,18 +32,17 @@ public class GoodsController {
@ApiOperation(value = "分页列表")
@GetMapping("/page")
public Result list(
public PageResult list(
@ApiParam(value = "页码", example = "1") long pageNum,
@ApiParam(value = "每页数量", example = "10") long pageSize,
@ApiParam("商品分类ID") Long categoryId,
@ApiParam("商品名称") String name
) {
IPage<PmsSpu> result = iPmsSpuService.list(new Page<>(pageNum, pageSize), name, categoryId);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "商品详情")
@ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "path", dataType = "Long")
@GetMapping("/{id}")
public Result detail(
@ApiParam("商品ID") @PathVariable Long id

View File

@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
/**
*
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
* @date 2022/2/8
*/
@ -20,10 +21,9 @@ public class OmsSkuController {
private final IPmsSkuService skuService;
@ApiOperation(value = "商品库存详情")
@ApiImplicitParam(name = "id", value = "商品ID", required = true, paramType = "path", dataType = "Long")
@GetMapping("/{skuId}")
public Result detail(
@PathVariable Long skuId
public Result getSkuDetail(
@ApiParam("SKU ID") @PathVariable Long skuId
) {
PmsSku sku = skuService.getById(skuId);
return Result.success(sku);

View File

@ -16,7 +16,7 @@ import java.util.List;
/**
* 商品库存单元控制器 (Stock Keeping Unit)
*/
@Api(tags = "「移动端」商品库存")
@Api(tags = "「移动端」库存信息")
@RestController
@RequestMapping("/app-api/v1/sku")
@RequiredArgsConstructor

View File

@ -1,6 +1,7 @@
package com.youlai.mall.pms.controller.app;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.pms.pojo.query.SpuPageQuery;
import com.youlai.mall.pms.pojo.vo.GoodsPageVO;
@ -27,9 +28,9 @@ public class SpuController {
@ApiOperation(value = "商品分页列表")
@GetMapping("/page")
public Result<List<GoodsPageVO>> listGoodsWithPage(SpuPageQuery queryParams) {
public PageResult listGoodsWithPage(SpuPageQuery queryParams) {
IPage<GoodsPageVO> result = iPmsSpuService.listAppSpuWithPage(queryParams);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "获取商品详情")

View File

@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.sms.pojo.SmsAdvert;
import com.youlai.mall.sms.service.ISmsAdvertService;
@ -25,17 +26,18 @@ public class AdvertController {
@ApiOperation(value = "列表分页")
@GetMapping
public Result getAdvertPageList(
public PageResult getAdvertPageList(
@ApiParam("页码") Long pageNum,
@ApiParam("每页数量") Long pageSize,
@ApiParam("广告标题") String title) {
@ApiParam("广告标题") String title
) {
Page<SmsAdvert> result = iSmsAdvertService.page(new Page<>(pageNum, pageSize),
new LambdaQueryWrapper<SmsAdvert>()
.like(StrUtil.isNotBlank(title), SmsAdvert::getTitle, StrUtil.isNotBlank(title)?title:null)
.like(StrUtil.isNotBlank(title), SmsAdvert::getTitle, StrUtil.isNotBlank(title) ? title : null)
.orderByAsc(SmsAdvert::getSort)
.orderByDesc(SmsAdvert::getGmtModified)
);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "广告详情")

View File

@ -1,6 +1,7 @@
package com.youlai.mall.sms.controller.admin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.sms.pojo.domain.SmsCoupon;
import com.youlai.mall.sms.pojo.form.CouponTemplateForm;
@ -43,9 +44,9 @@ public class CouponTemplateController {
@ApiImplicitParam(name = "name", value = "广告名称", paramType = "query", dataType = "String")
})
@GetMapping
public Result page(Integer page, Integer limit, String name) {
IPage<SmsCouponTemplateVO> pageResult = couponTemplateService.pageQuery(page, limit, name);
return Result.success(pageResult.getRecords(), pageResult.getTotal());
public PageResult page(Integer page, Integer limit, String name) {
IPage<SmsCouponTemplateVO> result = couponTemplateService.pageQuery(page, limit, name);
return PageResult.success(result);
}
/**
@ -120,9 +121,9 @@ public class CouponTemplateController {
@ApiOperation(value = "优惠券领取使用条件分页查询")
@GetMapping("/template/coupon/page")
public Result couponPage(@ApiParam(value = "优惠券领取使用条件分页查询") CouponPageQuery query) {
IPage<SmsCoupon> iPage = couponService.pageQuery(query);
return Result.success(iPage.getRecords(), iPage.getTotal());
public PageResult couponPage(@ApiParam(value = "优惠券领取使用条件分页查询") CouponPageQuery query) {
IPage<SmsCoupon> result = couponService.pageQuery(query);
return PageResult.success(result);
}
@ApiOperation(value = "优惠券模板下优惠券领取详情")

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.ums.pojo.entity.UmsMember;
import com.youlai.mall.ums.service.IUmsMemberService;
@ -28,13 +29,13 @@ public class UmsMemberController {
@ApiOperation(value = "会员分页列表")
@GetMapping
public Result<List<UmsMember>> list(
public PageResult<UmsMember> listMembersWithPage(
@ApiParam("页码") Long pageNum,
@ApiParam("每页数量") Long pageSize,
@ApiParam("会员昵称") String nickName
) {
IPage<UmsMember> result = memberService.list(new Page<>(pageNum, pageSize), nickName);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "获取会员详情")

View File

@ -27,15 +27,15 @@ public class MemberController {
private final IUmsMemberService memberService;
@ApiOperation(value = "根据会员openid")
@ApiOperation(value = "根据会员ID获取openid")
@GetMapping("/{memberId}/openid")
public Result<String> getMemberById(
@ApiParam("会员ID") @PathVariable Long memberId
) {
UmsMember member = memberService.getOne(
new LambdaQueryWrapper<UmsMember>()
.eq(UmsMember::getId,memberId)
.select(UmsMember::getOpenid)
.eq(UmsMember::getId, memberId)
.select(UmsMember::getOpenid)
);
String openid = member.getOpenid();
return Result.success(openid);

View File

@ -31,14 +31,14 @@ public class DeptController {
@ApiImplicitParam(name = "status", value = "部门状态", paramType = "query", dataType = "Long"),
})
@GetMapping("/table")
public Result getTableList(Integer status, String name) {
public Result listDeptWithTable(Integer status, String name) {
List<DeptVO> deptTableList = deptService.listTable(status, name);
return Result.success(deptTableList);
}
@ApiOperation(value = "部门下拉TreeSelect层级列表")
@GetMapping("/select")
public Result getSelectList() {
public Result listDeptWithSelect() {
List<IdLabelVO> deptSelectList = deptService.listTreeSelect();
return Result.success(deptSelectList);
}

View File

@ -10,6 +10,7 @@ import com.youlai.admin.pojo.entity.SysDict;
import com.youlai.admin.pojo.entity.SysDictItem;
import com.youlai.admin.service.ISysDictItemService;
import com.youlai.admin.service.ISysDictService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -38,12 +39,12 @@ public class DictController {
@ApiImplicitParam(name = "name", value = "字典名称", paramType = "query", dataType = "String"),
})
@GetMapping("/page")
public Result list(Long pageNum, Long pageSize, String name) {
public PageResult<SysDict> list(Long pageNum, Long pageSize, String name) {
Page<SysDict> result = iSysDictService.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<SysDict>()
.like(StrUtil.isNotBlank(name), SysDict::getName, StrUtil.trimToNull(name))
.orderByDesc(SysDict::getGmtModified)
.orderByDesc(SysDict::getGmtCreate));
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "字典列表")

View File

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.admin.pojo.entity.SysDictItem;
import com.youlai.admin.service.ISysDictItemService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -26,7 +27,7 @@ public class DictItemController {
private final ISysDictItemService iSysDictItemService;
@ApiOperation(value = "列表分页")
@ApiOperation(value = "用户分页列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", defaultValue = "1", value = "页码", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "limit", defaultValue = "10", value = "每页数量", paramType = "query", dataType = "Integer"),
@ -34,15 +35,17 @@ public class DictItemController {
@ApiImplicitParam(name = "dictCode", value = "字典编码", paramType = "query", dataType = "String")
})
@GetMapping("/page")
public Result getPageList(
public PageResult listDictItemsWithPage(
long pageNum,
long pageSize,
String name,
String dictCode
) {
IPage<SysDictItem> result = iSysDictItemService.list(new Page<>(pageNum, pageSize),
new SysDictItem().setName(name).setDictCode(dictCode));
return Result.success(result.getRecords(), result.getTotal());
IPage<SysDictItem> result = iSysDictItemService.list(
new Page<>(pageNum, pageSize),
new SysDictItem().setName(name).setDictCode(dictCode)
);
return PageResult.success(result);
}
@ApiOperation(value = "字典项列表")

View File

@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.admin.dto.AuthClientDTO;
import com.youlai.admin.pojo.entity.SysOauthClient;
import com.youlai.admin.service.ISysOauthClientService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -36,12 +37,12 @@ public class OauthClientController {
@ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "query", dataType = "String")
})
@GetMapping
public Result list(long pageNum, long pageSize, String clientId) {
public PageResult<SysOauthClient> list(long pageNum, long pageSize, String clientId) {
IPage<SysOauthClient> result = iSysOauthClientService.page(
new Page<>(pageNum, pageSize),
new LambdaQueryWrapper<SysOauthClient>()
.like(StrUtil.isNotBlank(clientId), SysOauthClient::getClientId, clientId));
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "客户端详情")

View File

@ -6,6 +6,7 @@ import com.youlai.admin.pojo.entity.SysPermission;
import com.youlai.admin.pojo.query.PermissionPageQuery;
import com.youlai.admin.pojo.vo.permission.PermissionPageVO;
import com.youlai.admin.service.ISysPermissionService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
@ -24,11 +25,11 @@ public class PermissionController {
@ApiOperation(value = "列表分页")
@GetMapping("/page")
public Result listPermissionsWithPage(
public PageResult<PermissionPageVO> listPermissionsWithPage(
PermissionPageQuery permissionPageQuery
) {
IPage<PermissionPageVO> result = iSysPermissionService.listPermissionsWithPage(permissionPageQuery);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "权限列表")

View File

@ -12,10 +12,13 @@ import com.youlai.admin.service.ISysRoleMenuService;
import com.youlai.admin.service.ISysRolePermissionService;
import com.youlai.admin.service.ISysRoleService;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.common.web.util.JwtUtils;
import com.youlai.common.web.util.UserUtils;
import io.swagger.annotations.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
@ -37,7 +40,7 @@ public class RoleController {
@ApiOperation(value = "列表分页")
@GetMapping("/page")
public Result getRolePageList(
public PageResult<SysRole> getRolePageList(
@ApiParam("页码") long pageNum,
@ApiParam("每页数量") long pageSize,
@ApiParam("角色名称") String name
@ -51,7 +54,7 @@ public class RoleController {
.orderByDesc(SysRole::getGmtModified)
.orderByDesc(SysRole::getGmtCreate);
Page<SysRole> result = iSysRoleService.page(new Page<>(pageNum, pageSize), queryWrapper);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "角色列表")

View File

@ -11,6 +11,7 @@ import com.youlai.admin.pojo.vo.user.UserFormVO;
import com.youlai.admin.pojo.vo.user.UserPageVO;
import com.youlai.admin.service.ISysPermissionService;
import com.youlai.admin.service.ISysUserService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.common.web.util.UserUtils;
import io.swagger.annotations.Api;
@ -42,11 +43,11 @@ public class UserController {
@ApiOperation(value = "用户分页列表")
@GetMapping("/page")
public Result<List<UserPageVO>> listUsersWithPage(
public PageResult<UserPageVO> listUsersWithPage(
UserPageQuery queryParams
) {
IPage<UserPageVO> result = iSysUserService.listUsersWithPage(queryParams);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "获取用户表单详情")

View File

@ -8,6 +8,7 @@ import com.youlai.admin.pojo.entity.SysDict;
import com.youlai.admin.pojo.entity.SysDictItem;
import com.youlai.admin.service.ISysDictItemService;
import com.youlai.admin.service.ISysDictService;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
@ -26,7 +27,7 @@ public class DictV2Controller {
@ApiOperation(value = "字典分页列表")
@GetMapping("/page")
public Result listDictByPage(
public PageResult<SysDict> listDictByPage(
@ApiParam("页码") long pageNum,
@ApiParam("每页数量") long pageSize,
@ApiParam("字典名称") String name
@ -36,7 +37,7 @@ public class DictV2Controller {
.like(StrUtil.isNotBlank(name), SysDict::getName, StrUtil.trimToNull(name))
.orderByDesc(SysDict::getGmtModified)
.orderByDesc(SysDict::getGmtCreate));
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "字典详情")
@ -79,7 +80,7 @@ public class DictV2Controller {
@ApiImplicitParam(name = "dictCode", value = "字典编码", paramType = "query", dataType = "String")
})
@GetMapping("/items/page")
public Result getPageList(
public PageResult getPageList(
long pageNum,
long pageSize,
String name,
@ -89,7 +90,7 @@ public class DictV2Controller {
new Page<>(pageNum, pageSize),
new SysDictItem().setName(name).setDictCode(dictCode)
);
return Result.success(result.getRecords(), result.getTotal());
return PageResult.success(result);
}
@ApiOperation(value = "根据字典编码获取字典项列表")

View File

@ -0,0 +1,34 @@
package com.youlai.common.result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 分页响应结构体
*
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
* @date 2022/2/18 23:29
*/
@Data
public class PageResult<T> implements Serializable {
private String code;
private List<T> data;
private long total;
private String msg;
public static <T> PageResult<T> success(IPage<T> page) {
PageResult<T> result = new PageResult<>();
result.setCode(ResultCode.SUCCESS.getCode());
result.setData(page.getRecords());
result.setTotal(page.getTotal());
result.setMsg(ResultCode.SUCCESS.getMsg());
return result;
}
}

View File

@ -1,18 +1,16 @@
package com.youlai.common.result;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
/**
* 通用响应体封装
* 统一响应结构体
*
* @author haoxr
* @date 2022/1/30
**/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
private String code;
@ -21,26 +19,15 @@ public class Result<T> implements Serializable {
private String msg;
private long total;
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> success(T data) {
ResultCode rce = ResultCode.SUCCESS;
if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
rce = ResultCode.SYSTEM_EXECUTION_ERROR;
}
return result(rce, data);
}
public static <T> Result<T> success(T data, Long total) {
Result<T> result = new Result<>();
result.setCode(ResultCode.SUCCESS.getCode());
result.setMsg(ResultCode.SUCCESS.getMsg());
result.setData(data);
result.setTotal(total);
return result;
}
@ -80,7 +67,6 @@ public class Result<T> implements Serializable {
return result;
}
public static boolean isSuccess(Result<?> result) {
return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
}