Controller
package com.shiers.controller.center;import com.shiers.controller.BaseController;import com.shiers.enums.YesOrNo;import com.shiers.pojo.OrderItems;import com.shiers.pojo.Orders;import com.shiers.pojo.bo.center.OrderItemsCommentBO;import com.shiers.service.center.MyCommentsService;import com.shiers.utils.MyJSONResult;import com.shiers.utils.PagedGridResult;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.*;import java.util.List;/** * Demo class * * @author shierS * @date 2021/6/9 */@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})@RestController@RequestMapping("mycomments")public class MyCommentsController extends BaseController { @Autowired private MyCommentsService myCommentsService; @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST") @PostMapping("/pending") public MyJSONResult pending( @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId, @ApiParam(name = "orderId", value = "订单id", required = true) @RequestParam String orderId) { // 判断用户和订单是否关联 MyJSONResult checkResult = checkUserOrder(userId, orderId); if (checkResult.getStatus() != HttpStatus.OK.value()) { return checkResult; } // 判断该笔订单是否已经评价过,评价过了就不再继续 Orders myOrder = (Orders)checkResult.getData(); if (myOrder.getIsComment() == YesOrNo.YES.type) { return MyJSONResult.errorMsg("该笔订单已经评价"); } List<OrderItems> list = myCommentsService.queryPendingComment(orderId); return MyJSONResult.ok(list); } @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST") @PostMapping("/saveList") public MyJSONResult saveList( @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId, @ApiParam(name = "orderId", value = "订单id", required = true) @RequestParam String orderId, @RequestBody List<OrderItemsCommentBO> commentList) { System.out.println(commentList); // 判断用户和订单是否关联 MyJSONResult checkResult = checkUserOrder(userId, orderId); if (checkResult.getStatus() != HttpStatus.OK.value()) { return checkResult; } // 判断评论内容list不能为空 if (commentList == null || commentList.isEmpty() || commentList.size() == 0) { return MyJSONResult.errorMsg("评论内容不能为空!"); } myCommentsService.saveComments(orderId, userId, commentList); return MyJSONResult.ok(); } @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST") @PostMapping("/query") public MyJSONResult query( @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId, @ApiParam(name = "page", value = "查询下一页的第几页", required = false) @RequestParam Integer page, @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false) @RequestParam Integer pageSize) { if (StringUtils.isBlank(userId)) { return MyJSONResult.errorMsg(null); } if (page == null) { page = 1; } if (pageSize == null) { pageSize = COMMON_PAGE_SIZE; } PagedGridResult grid = myCommentsService.queryMyComments(userId, page, pageSize); return MyJSONResult.ok(grid); }}
Service
package com.shiers.service.center;import com.shiers.pojo.OrderItems;import com.shiers.pojo.bo.center.OrderItemsCommentBO;import com.shiers.utils.PagedGridResult;import java.util.List;/** * Demo class * * @author shierS * @date 2021/6/9 */public interface MyCommentsService { /** * 根据订单id查询关联的商品 * * @param orderId * @return */ public List<OrderItems> queryPendingComment(String orderId); /** * 保存用户的评论 * * @param orderId * @param userId * @param commentList */ public void saveComments(String orderId, String userId, List<OrderItemsCommentBO> commentList); /** * 我的评价查询 分页 * * @param userId * @param page * @param pageSize * @return */ public PagedGridResult queryMyComments(String userId, Integer page, Integer pageSize);}
ServiceImpl
package com.shiers.service.impl.center;import com.github.pagehelper.PageHelper;import com.shiers.enums.YesOrNo;import com.shiers.mapper.ItemsCommentsMapperCustom;import com.shiers.mapper.OrderItemsMapper;import com.shiers.mapper.OrderStatusMapper;import com.shiers.mapper.OrdersMapper;import com.shiers.pojo.OrderItems;import com.shiers.pojo.OrderStatus;import com.shiers.pojo.Orders;import com.shiers.pojo.bo.center.OrderItemsCommentBO;import com.shiers.pojo.vo.MyCommentVO;import com.shiers.service.center.MyCommentsService;import com.shiers.utils.PagedGridResult;import org.n3r.idworker.Sid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Demo class * * @author shierS * @date 2021/6/9 */@Servicepublic class MyCommentsServiceImpl extends BaseService implements MyCommentsService { @Autowired public OrderItemsMapper orderItemsMapper; @Autowired public OrdersMapper ordersMapper; @Autowired public OrderStatusMapper orderStatusMapper; @Autowired public ItemsCommentsMapperCustom itemsCommentsMapperCustom; @Autowired private Sid sid; @Transactional(propagation = Propagation.SUPPORTS) @Override public List<OrderItems> queryPendingComment(String orderId) { OrderItems query = new OrderItems(); query.setOrderId(orderId); return orderItemsMapper.select(query); } @Transactional(propagation = Propagation.REQUIRED) @Override public void saveComments(String orderId, String userId, List<OrderItemsCommentBO> commentList) { // 1. 保存评价 items_comments for (OrderItemsCommentBO oic : commentList) { oic.setCommentId(sid.nextShort()); } Map<String, Object> map = new HashMap<>(); map.put("userId", userId); map.put("commentList", commentList); itemsCommentsMapperCustom.saveComments(map); // 2. 修改订单表改已评价 orders Orders order = new Orders(); order.setId(orderId); order.setIsComment(YesOrNo.YES.type); ordersMapper.updateByPrimaryKeySelective(order); // 3. 修改订单状态表的留言时间 order_status OrderStatus orderStatus = new OrderStatus(); orderStatus.setOrderId(orderId); orderStatus.setCommentTime(new Date()); orderStatusMapper.updateByPrimaryKeySelective(orderStatus); } @Transactional(propagation = Propagation.SUPPORTS) @Override public PagedGridResult queryMyComments(String userId, Integer page, Integer pageSize) { Map<String, Object> map = new HashMap<>(); map.put("userId", userId); PageHelper.startPage(page, pageSize); List<MyCommentVO> list = itemsCommentsMapperCustom.queryMyComments(map); return setterPagedGrid(list, page); } // public PagedGridResult setterPagedGrid(List<?> list, Integer page) { // PageInfo<?> pageList = new PageInfo<>(list); // PagedGridResult grid = new PagedGridResult(); // grid.setPage(page); // grid.setRows(list); // grid.setTotal(pageList.getPages()); // grid.setRecords(pageList.getTotal()); // return grid; // }}