From ca0b887c60b103b30b9aa91dd79ccf759e3465a6 Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Sun, 24 May 2020 16:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95=E8=AF=84?= =?UTF-8?q?=E4=BB=B7=E5=88=86=E9=A1=B5--Mongodb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/bo/comment/OrderCommentPageBO.java | 156 ++++++++++++------ .../convert/comment/OrderCommentConvert.java | 10 ++ .../biz/dto/comment/OrderCommentPageDTO.java | 20 +-- .../service/comment/OrderCommentService.java | 11 +- .../comment/OrderCommentServiceImpl.java | 40 +++++ .../comment/UsersOrderCommentController.java | 15 ++ .../comment/UsersOrderCommentConvert.java | 21 +++ .../comment/UsersOrderCommentPageRequest.java | 28 ++++ .../UsersOrderCommentPageResponse.java | 90 ++++++++++ 9 files changed, 323 insertions(+), 68 deletions(-) create mode 100644 order/order-rest/src/main/java/cn/iocoder/mall/order/rest/request/comment/UsersOrderCommentPageRequest.java create mode 100644 order/order-rest/src/main/java/cn/iocoder/mall/order/rest/response/comment/UsersOrderCommentPageResponse.java diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/bo/comment/OrderCommentPageBO.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/bo/comment/OrderCommentPageBO.java index cc198a069..286db0f92 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/bo/comment/OrderCommentPageBO.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/bo/comment/OrderCommentPageBO.java @@ -41,66 +41,118 @@ public class OrderCommentPageBO implements Serializable { // */ // private Integer negativeTotal; +// /** +// * 评论列表 +// */ +// private List orderCommentItems; + /** - * 评论列表 + * 评论 id */ - private List orderCommentItems; + private Integer id; + + /** + * 用户头像 + */ + private String userAvatar; + + /** + * 用户的真实姓名 + */ + private String userNickName; + + /** + * 评价星 + */ + private Integer star; + + /** + * 评论的内容 + */ + private String commentContent; + + /** + * 评论的图片地址 + */ + private String commentPics; + + /** + * 回复条数 + */ + private Integer replayCount; + + /** + * 点赞数 + */ + private Integer likeCount; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 商家回复列表 + * 只展示最近的一条 + */ + private String replyContent; + @Data @Accessors(chain = true) @AllArgsConstructor public static class OrderCommentItem{ - /** - * 评论 id - */ - private Integer id; - - /** - * 用户头像 - */ - private String userAvatar; - - /** - * 用户的真实姓名 - */ - private String userNickName; - - /** - * 评价星 - */ - private Integer star; - - /** - * 评论的内容 - */ - private String commentContent; - - /** - * 评论的图片地址 - */ - private String commentPics; - - /** - * 回复条数 - */ - private Integer replayCount; - - /** - * 点赞数 - */ - private Integer likeCount; - - /** - * 创建时间 - */ - private Date createTime; - - /** - * 商家回复列表 - * 只展示最近的一条 - */ - private String replyContent; +// /** +// * 评论 id +// */ +// private Integer id; +// +// /** +// * 用户头像 +// */ +// private String userAvatar; +// +// /** +// * 用户的真实姓名 +// */ +// private String userNickName; +// +// /** +// * 评价星 +// */ +// private Integer star; +// +// /** +// * 评论的内容 +// */ +// private String commentContent; +// +// /** +// * 评论的图片地址 +// */ +// private String commentPics; +// +// /** +// * 回复条数 +// */ +// private Integer replayCount; +// +// /** +// * 点赞数 +// */ +// private Integer likeCount; +// +// /** +// * 创建时间 +// */ +// private Date createTime; +// +// /** +// * 商家回复列表 +// * 只展示最近的一条 +// */ +// private String replyContent; } diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/convert/comment/OrderCommentConvert.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/convert/comment/OrderCommentConvert.java index 57a059a10..221e9319e 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/convert/comment/OrderCommentConvert.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/convert/comment/OrderCommentConvert.java @@ -1,7 +1,9 @@ package cn.iocoder.mall.order.biz.convert.comment; +import cn.iocoder.mall.order.biz.bo.comment.OrderCommentPageBO; import cn.iocoder.mall.order.biz.dataobject.comment.OrderCommentDO; import cn.iocoder.mall.order.biz.dto.comment.OrderCommentAddDTO; +import java.util.List; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -25,4 +27,12 @@ public interface OrderCommentConvert { */ OrderCommentDO convert(OrderCommentAddDTO orderCommentAddDTO); + /** + * 参数转成BO + * + * @param orderCommentList + * @return + */ + List convert(List orderCommentList); + } diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/dto/comment/OrderCommentPageDTO.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/dto/comment/OrderCommentPageDTO.java index 502ae9e0a..b839b06b5 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/dto/comment/OrderCommentPageDTO.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/dto/comment/OrderCommentPageDTO.java @@ -1,32 +1,22 @@ package cn.iocoder.mall.order.biz.dto.comment; +import cn.iocoder.common.framework.vo.PageParam; +import java.io.Serializable; import lombok.Data; import lombok.experimental.Accessors; -import java.io.Serializable; - /** + * 订单评论 page * - * 订单评论 query - * + * @author xiaofeng */ @Data @Accessors(chain = true) -public class OrderCommentPageDTO implements Serializable { +public class OrderCommentPageDTO extends PageParam implements Serializable { /** * 商品 sku id */ private Integer productSkuId; - /** - * 页码 - */ - private Integer pageNo; - - /** - * 每页条数 - */ - private Integer pageSize; - } diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentService.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentService.java index 6ea648af3..54497090f 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentService.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentService.java @@ -1,7 +1,9 @@ package cn.iocoder.mall.order.biz.service.comment; -import cn.iocoder.mall.order.biz.bo.comment.OrderCommentBO; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.order.biz.bo.comment.OrderCommentPageBO; import cn.iocoder.mall.order.biz.dto.comment.OrderCommentAddDTO; +import cn.iocoder.mall.order.biz.dto.comment.OrderCommentPageDTO; import javax.validation.Valid; import org.springframework.validation.annotation.Validated; @@ -23,4 +25,11 @@ public interface OrderCommentService { */ Boolean addOrderComment(@Valid OrderCommentAddDTO orderCommentAddDTO); + /** + * 分页 + * + * @param orderCommentPageDTO + * @return + */ + PageResult page(OrderCommentPageDTO orderCommentPageDTO); } diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentServiceImpl.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentServiceImpl.java index d37963593..c5e320be3 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentServiceImpl.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/comment/OrderCommentServiceImpl.java @@ -1,10 +1,21 @@ package cn.iocoder.mall.order.biz.service.comment; +import cn.iocoder.common.framework.util.CollectionUtil; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.order.biz.bo.comment.OrderCommentPageBO; import cn.iocoder.mall.order.biz.convert.comment.OrderCommentConvert; import cn.iocoder.mall.order.biz.dataobject.comment.OrderCommentDO; import cn.iocoder.mall.order.biz.dto.comment.OrderCommentAddDTO; +import cn.iocoder.mall.order.biz.dto.comment.OrderCommentPageDTO; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import javax.validation.Valid; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; /** @@ -31,4 +42,33 @@ public class OrderCommentServiceImpl implements OrderCommentService { .save(OrderCommentConvert.INSTANCE.convert(orderCommentAddDTO)); return null != orderCommentDO ? Boolean.TRUE : Boolean.FALSE; } + + @Override + public PageResult page( + OrderCommentPageDTO orderCommentPageDTO) { + Query query = new Query(); + query.with(Sort.by(Direction.ASC, "_id")); + List orderCommentList = Collections.EMPTY_LIST; + if (orderCommentPageDTO.getPageNo() == 1) { + query.limit(orderCommentPageDTO.getPageSize()); + } else { + final int offset = orderCommentPageDTO.getOffset(); + query.limit(offset); + List list = mongoTemplate.find(query, OrderCommentDO.class); + if (!CollectionUtil.isEmpty(list)) { + // 获取最后一条记录 + OrderCommentDO orderCommentDO = list.get(orderCommentList.size() - 1); + final Integer orderCommentId = orderCommentDO.getId(); + // 从上一页最后一条开始查 + query.addCriteria(Criteria.where("_id").gt(orderCommentId)); + // 重新赋值 + query.limit(orderCommentPageDTO.getPageSize()); + } + } + orderCommentList = mongoTemplate.find(query, OrderCommentDO.class); + + PageResult pageResult = new PageResult<>(); + pageResult.setList(OrderCommentConvert.INSTANCE.convert(orderCommentList)); + return pageResult; + } } diff --git a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/controller/comment/UsersOrderCommentController.java b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/controller/comment/UsersOrderCommentController.java index 2d61e1d90..16a45a21e 100644 --- a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/controller/comment/UsersOrderCommentController.java +++ b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/controller/comment/UsersOrderCommentController.java @@ -2,13 +2,18 @@ package cn.iocoder.mall.order.rest.controller.comment; import cn.iocoder.common.framework.constant.MallConstants; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.order.biz.dto.comment.OrderCommentPageDTO; import cn.iocoder.mall.order.biz.service.comment.OrderCommentService; import cn.iocoder.mall.order.rest.convert.comment.UsersOrderCommentConvert; import cn.iocoder.mall.order.rest.request.comment.UsersOrderCommentAddRequest; +import cn.iocoder.mall.order.rest.request.comment.UsersOrderCommentPageRequest; +import cn.iocoder.mall.order.rest.response.comment.UsersOrderCommentPageResponse; import cn.iocoder.mall.security.core.context.UserSecurityContextHolder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -44,5 +49,15 @@ public class UsersOrderCommentController { UsersOrderCommentConvert.INSTANCE.convert(request))); } + @GetMapping("/page") + @ApiOperation(value = "获取订单评论") + public CommonResult> page( + UsersOrderCommentPageRequest request) { + OrderCommentPageDTO orderCommentPageDTO = UsersOrderCommentConvert.INSTANCE + .convert(request); + return CommonResult.success(UsersOrderCommentConvert.INSTANCE + .convert(orderCommentService.page(orderCommentPageDTO))); + } + } diff --git a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/convert/comment/UsersOrderCommentConvert.java b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/convert/comment/UsersOrderCommentConvert.java index 00d86ec7b..a453e503a 100644 --- a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/convert/comment/UsersOrderCommentConvert.java +++ b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/convert/comment/UsersOrderCommentConvert.java @@ -1,7 +1,12 @@ package cn.iocoder.mall.order.rest.convert.comment; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.order.biz.bo.comment.OrderCommentPageBO; import cn.iocoder.mall.order.biz.dto.comment.OrderCommentAddDTO; +import cn.iocoder.mall.order.biz.dto.comment.OrderCommentPageDTO; import cn.iocoder.mall.order.rest.request.comment.UsersOrderCommentAddRequest; +import cn.iocoder.mall.order.rest.request.comment.UsersOrderCommentPageRequest; +import cn.iocoder.mall.order.rest.response.comment.UsersOrderCommentPageResponse; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -26,4 +31,20 @@ public interface UsersOrderCommentConvert { */ OrderCommentAddDTO convert(UsersOrderCommentAddRequest request); + /** + * 分页参数转换 + * + * @param request + * @return + */ + OrderCommentPageDTO convert(UsersOrderCommentPageRequest request); + + /** + * 分页转换 + * + * @param pageResult + * @return + */ + PageResult convert(PageResult pageResult); + } diff --git a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/request/comment/UsersOrderCommentPageRequest.java b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/request/comment/UsersOrderCommentPageRequest.java new file mode 100644 index 000000000..f5c09e3a2 --- /dev/null +++ b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/request/comment/UsersOrderCommentPageRequest.java @@ -0,0 +1,28 @@ +package cn.iocoder.mall.order.rest.request.comment; + +import cn.iocoder.common.framework.vo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.NotNull; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * UsersOrderCommentPageRequest + * + * @author xiaofeng + * @version 1.0 + * @date 2020/05/20 23:21 + */ +@ApiModel("用户 - Order 模块 - 订单评论分页") +@Data +@Accessors(chain = true) +public class UsersOrderCommentPageRequest extends PageParam { + + /** + * 商品 sku id + */ + @ApiModelProperty(value = "商品 SKU id", required = true) + @NotNull(message = "商品 SKU id 不能为空") + private Integer productSkuId; +} diff --git a/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/response/comment/UsersOrderCommentPageResponse.java b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/response/comment/UsersOrderCommentPageResponse.java new file mode 100644 index 000000000..3545dcd58 --- /dev/null +++ b/order/order-rest/src/main/java/cn/iocoder/mall/order/rest/response/comment/UsersOrderCommentPageResponse.java @@ -0,0 +1,90 @@ +package cn.iocoder.mall.order.rest.response.comment; + +import io.swagger.annotations.ApiModel; +import java.util.Date; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * UsersOrderCommentPageResponse + * + * @author xiaofeng + * @version 1.0 + * @date 2020/05/20 23:32 + */ +@ApiModel("用户 - Order 模块 - 订单评论分页列表") +@Data +@Accessors(chain = true) +public class UsersOrderCommentPageResponse { + + /** + * 总条数 + */ + private Integer total; + + /** + * 评论列表 + */ + private List orderCommentItems; + + + @Data + @Accessors(chain = true) + @AllArgsConstructor + public static class OrderCommentItem { + + /** + * 评论 id + */ + private Integer id; + + /** + * 用户头像 + */ + private String userAvatar; + + /** + * 用户的真实姓名 + */ + private String userNickName; + + /** + * 评价星 + */ + private Integer star; + + /** + * 评论的内容 + */ + private String commentContent; + + /** + * 评论的图片地址 + */ + private String commentPics; + + /** + * 回复条数 + */ + private Integer replayCount; + + /** + * 点赞数 + */ + private Integer likeCount; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 商家回复列表 只展示最近的一条 + */ + private String replyContent; + + } + +}