开始
前端页面
增加service类
MyCommentsService
import com.imooc.pojo.OrderItems;
import com.imooc.pojo.Orders;
import com.imooc.utils.PagedGridResult;
import java.util.List;
public interface MyCommentsService {
/**
* 根据订单id查询关联的商品
* @param orderId
* @return
*/
public List<OrderItems> queryPendingComment(String orderId);
}
实现类 MyCommentServiceImpl
@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
@Api(value = "用户中心评价模块",tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController {
@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{
return IMOOCJSONResult.ok();
}
加上userId和orderId的关系,复制之前的controller写的代码
为了避免重复代码,我们把checkUserOrder单独放到baseController里面。
复制到baseController
这里也注释掉
@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,否则
这样在子的controller里面就可以用了
@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();
}
}
判断订单是否评价过
@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();
}
@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
页面没有任何的反应
空指针异常
这里空指针
myOrder是一个空的对象
获取到的data为空
查询userId和orderId的关系时候,把结果返回回来
@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);
}