Controller

  1. package com.shiers.controller.center;
  2. import com.shiers.controller.BaseController;
  3. import com.shiers.enums.YesOrNo;
  4. import com.shiers.pojo.OrderItems;
  5. import com.shiers.pojo.Orders;
  6. import com.shiers.pojo.bo.center.OrderItemsCommentBO;
  7. import com.shiers.service.center.MyCommentsService;
  8. import com.shiers.utils.MyJSONResult;
  9. import com.shiers.utils.PagedGridResult;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import io.swagger.annotations.ApiParam;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.web.bind.annotation.*;
  17. import java.util.List;
  18. /**
  19. * Demo class
  20. *
  21. * @author shierS
  22. * @date 2021/6/9
  23. */
  24. @Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})
  25. @RestController
  26. @RequestMapping("mycomments")
  27. public class MyCommentsController extends BaseController {
  28. @Autowired
  29. private MyCommentsService myCommentsService;
  30. @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
  31. @PostMapping("/pending")
  32. public MyJSONResult pending(
  33. @ApiParam(name = "userId", value = "用户id", required = true)
  34. @RequestParam String userId,
  35. @ApiParam(name = "orderId", value = "订单id", required = true)
  36. @RequestParam String orderId) {
  37. // 判断用户和订单是否关联
  38. MyJSONResult checkResult = checkUserOrder(userId, orderId);
  39. if (checkResult.getStatus() != HttpStatus.OK.value()) {
  40. return checkResult;
  41. }
  42. // 判断该笔订单是否已经评价过,评价过了就不再继续
  43. Orders myOrder = (Orders)checkResult.getData();
  44. if (myOrder.getIsComment() == YesOrNo.YES.type) {
  45. return MyJSONResult.errorMsg("该笔订单已经评价");
  46. }
  47. List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
  48. return MyJSONResult.ok(list);
  49. }
  50. @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
  51. @PostMapping("/saveList")
  52. public MyJSONResult saveList(
  53. @ApiParam(name = "userId", value = "用户id", required = true)
  54. @RequestParam String userId,
  55. @ApiParam(name = "orderId", value = "订单id", required = true)
  56. @RequestParam String orderId,
  57. @RequestBody List<OrderItemsCommentBO> commentList) {
  58. System.out.println(commentList);
  59. // 判断用户和订单是否关联
  60. MyJSONResult checkResult = checkUserOrder(userId, orderId);
  61. if (checkResult.getStatus() != HttpStatus.OK.value()) {
  62. return checkResult;
  63. }
  64. // 判断评论内容list不能为空
  65. if (commentList == null || commentList.isEmpty() || commentList.size() == 0) {
  66. return MyJSONResult.errorMsg("评论内容不能为空!");
  67. }
  68. myCommentsService.saveComments(orderId, userId, commentList);
  69. return MyJSONResult.ok();
  70. }
  71. @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST")
  72. @PostMapping("/query")
  73. public MyJSONResult query(
  74. @ApiParam(name = "userId", value = "用户id", required = true)
  75. @RequestParam String userId,
  76. @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
  77. @RequestParam Integer page,
  78. @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
  79. @RequestParam Integer pageSize) {
  80. if (StringUtils.isBlank(userId)) {
  81. return MyJSONResult.errorMsg(null);
  82. }
  83. if (page == null) {
  84. page = 1;
  85. }
  86. if (pageSize == null) {
  87. pageSize = COMMON_PAGE_SIZE;
  88. }
  89. PagedGridResult grid = myCommentsService.queryMyComments(userId,
  90. page,
  91. pageSize);
  92. return MyJSONResult.ok(grid);
  93. }
  94. }

Service

  1. package com.shiers.service.center;
  2. import com.shiers.pojo.OrderItems;
  3. import com.shiers.pojo.bo.center.OrderItemsCommentBO;
  4. import com.shiers.utils.PagedGridResult;
  5. import java.util.List;
  6. /**
  7. * Demo class
  8. *
  9. * @author shierS
  10. * @date 2021/6/9
  11. */
  12. public interface MyCommentsService {
  13. /**
  14. * 根据订单id查询关联的商品
  15. *
  16. * @param orderId
  17. * @return
  18. */
  19. public List<OrderItems> queryPendingComment(String orderId);
  20. /**
  21. * 保存用户的评论
  22. *
  23. * @param orderId
  24. * @param userId
  25. * @param commentList
  26. */
  27. public void saveComments(String orderId, String userId, List<OrderItemsCommentBO> commentList);
  28. /**
  29. * 我的评价查询 分页
  30. *
  31. * @param userId
  32. * @param page
  33. * @param pageSize
  34. * @return
  35. */
  36. public PagedGridResult queryMyComments(String userId, Integer page, Integer pageSize);
  37. }

ServiceImpl

  1. package com.shiers.service.impl.center;
  2. import com.github.pagehelper.PageHelper;
  3. import com.shiers.enums.YesOrNo;
  4. import com.shiers.mapper.ItemsCommentsMapperCustom;
  5. import com.shiers.mapper.OrderItemsMapper;
  6. import com.shiers.mapper.OrderStatusMapper;
  7. import com.shiers.mapper.OrdersMapper;
  8. import com.shiers.pojo.OrderItems;
  9. import com.shiers.pojo.OrderStatus;
  10. import com.shiers.pojo.Orders;
  11. import com.shiers.pojo.bo.center.OrderItemsCommentBO;
  12. import com.shiers.pojo.vo.MyCommentVO;
  13. import com.shiers.service.center.MyCommentsService;
  14. import com.shiers.utils.PagedGridResult;
  15. import org.n3r.idworker.Sid;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Propagation;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * Demo class
  26. *
  27. * @author shierS
  28. * @date 2021/6/9
  29. */
  30. @Service
  31. public class MyCommentsServiceImpl extends BaseService implements MyCommentsService {
  32. @Autowired
  33. public OrderItemsMapper orderItemsMapper;
  34. @Autowired
  35. public OrdersMapper ordersMapper;
  36. @Autowired
  37. public OrderStatusMapper orderStatusMapper;
  38. @Autowired
  39. public ItemsCommentsMapperCustom itemsCommentsMapperCustom;
  40. @Autowired
  41. private Sid sid;
  42. @Transactional(propagation = Propagation.SUPPORTS)
  43. @Override
  44. public List<OrderItems> queryPendingComment(String orderId) {
  45. OrderItems query = new OrderItems();
  46. query.setOrderId(orderId);
  47. return orderItemsMapper.select(query);
  48. }
  49. @Transactional(propagation = Propagation.REQUIRED)
  50. @Override
  51. public void saveComments(String orderId, String userId,
  52. List<OrderItemsCommentBO> commentList) {
  53. // 1. 保存评价 items_comments
  54. for (OrderItemsCommentBO oic : commentList) {
  55. oic.setCommentId(sid.nextShort());
  56. }
  57. Map<String, Object> map = new HashMap<>();
  58. map.put("userId", userId);
  59. map.put("commentList", commentList);
  60. itemsCommentsMapperCustom.saveComments(map);
  61. // 2. 修改订单表改已评价 orders
  62. Orders order = new Orders();
  63. order.setId(orderId);
  64. order.setIsComment(YesOrNo.YES.type);
  65. ordersMapper.updateByPrimaryKeySelective(order);
  66. // 3. 修改订单状态表的留言时间 order_status
  67. OrderStatus orderStatus = new OrderStatus();
  68. orderStatus.setOrderId(orderId);
  69. orderStatus.setCommentTime(new Date());
  70. orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
  71. }
  72. @Transactional(propagation = Propagation.SUPPORTS)
  73. @Override
  74. public PagedGridResult queryMyComments(String userId,
  75. Integer page,
  76. Integer pageSize) {
  77. Map<String, Object> map = new HashMap<>();
  78. map.put("userId", userId);
  79. PageHelper.startPage(page, pageSize);
  80. List<MyCommentVO> list = itemsCommentsMapperCustom.queryMyComments(map);
  81. return setterPagedGrid(list, page);
  82. }
  83. // public PagedGridResult setterPagedGrid(List<?> list, Integer page) {
  84. // PageInfo<?> pageList = new PageInfo<>(list);
  85. // PagedGridResult grid = new PagedGridResult();
  86. // grid.setPage(page);
  87. // grid.setRows(list);
  88. // grid.setTotal(pageList.getPages());
  89. // grid.setRecords(pageList.getTotal());
  90. // return grid;
  91. // }
  92. }