/**
* 查询订单列表
* 默认分页为第一页,共3条
* @param openid
* @param page
* @param size
* @return
*/
@GetMapping("/list")
public ResultVo> list(@RequestParam("openid") String openid,
@RequestParam(value = "page",defaultValue = "0") Integer page,
@RequestParam(value = "size",defaultValue = "3") Integer size){
if (StringUtils.isEmpty(openid)){
log.info("【查询订单列表】openid为空");
throw new VxsellException(ExceptionEnums.PARAM_ERROR);
}
PageRequest pageRequest = new PageRequest(page,size);
Page orderDTOPage = orderService.findOrderList(openid,pageRequest);
return ResultVoUtils.success(orderDTOPage.getContent());
}
接口文档给的时间是秒,但是我们数据库中的是毫秒,接下来:
package com.fjz.vxsell.utils.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;
/**
* @author 冯师兄
* @date 2020-05-13 18:36
*/
public class DateToLongSerializer extends JsonSerializer {
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNumber(date.getTime()/1000);
}
}
/*创建时间*/
@JsonSerialize(using = DateToLongSerializer.class)
private Date createTime;
/*修改时间*/
@JsonSerialize(using = DateToLongSerializer.class)
private Date updateTime;
====================================================
package com.fjz.vxsell.service;
import com.fjz.vxsell.dto.OrderDTO;
/**
* 买家
* @author 冯师兄
* @date 2020-05-14 17:23
*/
public interface BuyerService {
//查询一个订单
OrderDTO findOrderOne(String openid, String orderId);
//取消订单
OrderDTO cancelOrder(String openid, String orderId);
}
package com.fjz.vxsell.service.impl;
import com.fjz.vxsell.dto.OrderDTO;
import com.fjz.vxsell.enums.ExceptionEnums;
import com.fjz.vxsell.exception.VxsellException;
import com.fjz.vxsell.service.BuyerService;
import com.fjz.vxsell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 冯师兄
* @date 2020-05-14 17:25
*/
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {
@Autowired
private OrderService orderService;
/**
* 查询一个订单
* @param openid
* @param orderId
* @return
*/
@Override
public OrderDTO findOrderOne(String openid, String orderId) {
OrderDTO orderDTO = checkOrder(openid, orderId);
return orderService.findById(orderId);
}
/**
* 取消订单
* @param openid
* @param orderId
* @return
*/
@Override
public OrderDTO cancelOrder(String openid, String orderId) {
OrderDTO orderDTO = checkOrder(openid, orderId);
if (orderDTO == null){
log.info("【取消订单】该订单不存在");
throw new VxsellException(ExceptionEnums.ORDER_NOT_EXSIT);
}
return orderService.cancel(orderDTO);
}
/**
* 因为查询订单和取消订单,里面代码基本相同,所以将里面的方法抽出来封装
* @param openid
* @param orderId
* @return
*/
private OrderDTO checkOrder(String openid, String orderId){
OrderDTO orderDTO = orderService.findById(orderId);
if (orderDTO == null){
return null;
}
//判断订单的微信openID是否是拥有者的
if (! orderDTO.getBuyerOpenid().equalsIgnoreCase(openid)){
log.info("【对比微信openID】openID不一致:openid={}, buyerOpenid={} ", openid, orderDTO.getBuyerOpenid());
throw new VxsellException(ExceptionEnums.ORDER_OPENID_NOT_OWNER);
}
return orderDTO;
}
}
/**
* 查询订单详情
* @param openid
* @param orderId
* @return
*/
@PostMapping("/detail")
public ResultVo detail(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId){
OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
return ResultVoUtils.success(orderDTO);
}
/**
* 取消订单
* @param openid
* @param orderId
* @return
*/
@PostMapping("/cancel")
public ResultVo cancel(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId){
buyerService.cancelOrder(openid, orderId);
return ResultVoUtils.success();
}
数据库数据发生变化,说明已经取消成功