开始

image.png
对应的这张表,我们要查询order_items
image.png

前端页面

image.png

image.png

image.png

image.png

image.png

增加service类

MyCommentsService
image.png

image.png

image.png

  1. import com.imooc.pojo.OrderItems;
  2. import com.imooc.pojo.Orders;
  3. import com.imooc.utils.PagedGridResult;
  4. import java.util.List;
  5. public interface MyCommentsService {
  6. /**
  7. * 根据订单id查询关联的商品
  8. * @param orderId
  9. * @return
  10. */
  11. public List<OrderItems> queryPendingComment(String orderId);
  12. }

实现类 MyCommentServiceImpl
image.png

image.png

@Service
public class MyCommentsServiceImpl implements MyCommentsService {

    @Autowired
    private OrderItemsMapper orderItemsMapper;
    @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);
    }
}

controller

创建controller
image.png

image.png


@Api(value = "用户中心评价模块",tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController {
    @Autowired
    private MyCommentsService myCommentsService;
}

image.png

 @ApiOperation(value = "查询订单列表",notes = "查询订单列表",httpMethod ="POST" )
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId",value = "订单id",required = true)
            @RequestParam("userId") String userId,
            @ApiParam(name = "orderId",value = "订单id",required = true)
            @RequestParam String orderId
    ) throws Exception{


        return IMOOCJSONResult.ok();
    }

加上userId和orderId的关系,复制之前的controller写的代码
image.png
为了避免重复代码,我们把checkUserOrder单独放到baseController里面。
image.png
复制到baseController
image.png
这里也注释掉
image.png
image.png

 @Autowired
    private MyOrderService myOrderService;
    public IMOOCJSONResult checkUserOrder(String orderId, String userId) throws Exception{
        Orders order =myOrderService.queryMyOrder(userId, orderId);

        if(order==null){
            return IMOOCJSONResult.errorMsg("订单不存在!");
        }

        return IMOOCJSONResult.ok();
    }

这里要改成public,否则
image.png
这样在子的controller里面就可以用了
image.png


@Api(value = "用户中心评价模块",tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {
    @Autowired
    private MyCommentsService myCommentsService;

    @ApiOperation(value = "查询订单列表",notes = "查询订单列表",httpMethod ="POST" )
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId",value = "订单id",required = true)
            @RequestParam("userId") String userId,
            @ApiParam(name = "orderId",value = "订单id",required = true)
            @RequestParam String orderId
    ) throws Exception{
        IMOOCJSONResult checkResult = checkUserOrder(orderId, userId);
        if(checkResult.getStatus()!= HttpStatus.OK.value()) {
            return checkResult;
        }
        boolean res = myOrderService.updateReceiveOrderStatus(orderId);
        if(!res) {
            return IMOOCJSONResult.errorMsg("订单确认收货失败!");
        }

        return IMOOCJSONResult.ok();
    }

}

判断订单是否评价过
image.png

image.png

 @ApiOperation(value = "查询订单列表",notes = "查询订单列表",httpMethod ="POST" )
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId",value = "订单id",required = true)
            @RequestParam("userId") String userId,
            @ApiParam(name = "orderId",value = "订单id",required = true)
            @RequestParam String orderId
    ) throws Exception{
        // 判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(orderId, userId);
        if(checkResult.getStatus()!= HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断该笔订单是否已经评价过,评价过了就不再继续
        Orders myOrder = (Orders)checkResult.getData();
        if(myOrder.getIsComment()== YesOrNo.YES.type) {
            return IMOOCJSONResult.errorMsg("该笔订单货已经评价");
        }



        return IMOOCJSONResult.ok();
    }

image.png

 @ApiOperation(value = "查询订单列表",notes = "查询订单列表",httpMethod ="POST" )
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId",value = "订单id",required = true)
            @RequestParam("userId") String userId,
            @ApiParam(name = "orderId",value = "订单id",required = true)
            @RequestParam String orderId
    ) throws Exception{
        // 判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(orderId, userId);
        if(checkResult.getStatus()!= HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断该笔订单是否已经评价过,评价过了就不再继续
        Orders myOrder = (Orders)checkResult.getData();
        if(myOrder.getIsComment()== YesOrNo.YES.type) {
            return IMOOCJSONResult.errorMsg("该笔订单货已经评价");
        }
        // 查询这个订单内的 订单详情
        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
        return IMOOCJSONResult.ok(list);
    }

测试

maven install
页面没有任何的反应
image.png
空指针异常
image.png
这里空指针
image.png
myOrder是一个空的对象
image.png

获取到的data为空
image.png

查询userId和orderId的关系时候,把结果返回回来
image.png

  @Autowired
    public MyOrderService myOrderService;
    public IMOOCJSONResult checkUserOrder(String orderId, String userId) throws Exception{
        Orders order =myOrderService.queryMyOrder(userId, orderId);

        if(order==null){
            return IMOOCJSONResult.errorMsg("订单不存在!");
        }

        return IMOOCJSONResult.ok(order);
    }

这个时候有值了
image.png

结束