买家订单
标签(空格分隔): springboot java wechat
Dao层设计与开发
- 数据库字段模型创建
- 一个是订单表(注意其中的创建时间和更新时间),另一个是订单详情表。
- 订单表:dao层用到通过openid查某个用户的所有订单,这里需要添加一个分页查询。
/** 根据买家openid分页查询订单. */
Page<OrderMaster> findByBuyerOpenid (String buyerOpenid, Pageable pageable);
- 订单详情表:dao层通过orderid查询订单信息,这里会返回一个list集合。
/** 根据orderId查询订单详情. */
List<OrderDetail> findByOrderId(String orderId);
注意:单元测试不能忘。
Service层设计与开发
- 通过忽略字段的方式来进行字段传输
@Transient 注解表示数据库对应字段忽略
- 通过忽略字段的方式不能保证数据的保密性,采用新建数据传输对象(dto),用于mvc之间的传送以及接收。
//传送
{
name:"张三",
phone:"13333333333",
address:"滁州学院",
openid:"110110",
items:[{
productid:"123132",
productQuantity:2
}]
}
//返回
{
code:0,
msg:"成功",
data:{
orderid:"123153131"
}
}
- 订单业务:创建订单、查询单个订单、查询多个订单、取消订单、完结订单、支付订单。
/** 创建订单. */
OrderMasterDTO create(OrderMasterDTO orderMasterDTO);
/** 查询单个订单. */
OrderMasterDTO findOne(String orderId);
/** 查询多个订单. */
Page<OrderMasterDTO> findList(String buyerOpenid, Pageable pageable);
/** 取消订单. */
OrderMasterDTO cancle(OrderMasterDTO orderMasterDTO);
/** 完结订单. */
OrderMasterDTO finish(OrderMasterDTO orderMasterDTO);
/** 支付订单. */
OrderMasterDTO pay(OrderMasterDTO orderMasterDTO);
- 创建订单:1.查询商品(数量和价格)。2.计算总价。3.写入数据库(写入详情表和订单表)。4.扣库存。
String orderId = KeyUtil.genUniqueKey();
BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
//1.查询商品(数量,价格)
for(OrderDetail orderDetail : orderMasterDTO.getOrderDetailList()){
ProductInfo productInfo = productInfoService.findOne(orderDetail.getProductId());
if(productInfo==null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
//2.计算总价
orderAmount = orderDetail.getProductPrice().
multiply(new BigDecimal(orderDetail.getProductQuantity())).add(orderAmount);
//写入详细表
orderDetail.setDetailId(KeyUtil.genUniqueKey());
orderDetail.setOrderId(orderId);
BeanUtils.copyProperties(productInfo,orderDetail);
orderDetailRepository.save(orderDetail);
}
//3.写入数据库(OrderMaster和OrderDetail)
OrderMaster orderMaster = new OrderMaster();
orderMaster.setOrderId(orderId);
orderMaster.setOrderAmount(orderAmount);
BeanUtils.copyProperties(orderMasterDTO,orderMaster);
orderMasterRepository.save(orderMaster);
//4.扣库存
List<CartDTO> cartDTOList = orderMasterDTO.getOrderDetailList().stream().map(e -> new CartDTO(e.getProductId(),e.getProductQuantity()) ).collect(Collectors.toList());
productInfoService.decreaseStock(cartDTOList);
return orderMasterDTO;
- 扣库存
for(CartDTO cartDTO :cartDTOS){
ProductInfo productInfo = repository.findOne(cartDTO.getProductid());
if(productInfo == null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
Integer stock = productInfo.getProductStock() - cartDTO.getProductQuantity();
if(stock < 0 ){
throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
}
productInfo.setProductStock(stock);
repository.save(productInfo);
}
- 查询订单,需要查询订单表和订单详情表 需要用到转换器
/**
* 属性赋值
* @param orderMaster
* @return
*/
public static OrderMasterDTO convert(OrderMaster orderMaster){
OrderMasterDTO orderMasterDTO = new OrderMasterDTO();
BeanUtils.copyProperties(orderMaster,orderMasterDTO);
return orderMasterDTO;
}
/**
* 集合转换
* @param orderMasterList
* @return
*/
public static List<OrderMasterDTO> covert(List<OrderMaster> orderMasterList){
return orderMasterList.stream().map(e -> convert(e)).collect(Collectors.toList());
}
- 取消订单
OrderMaster orderMaster = new OrderMaster();
//判断订单状态
if(!orderMasterDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())){
log.error("【订单状态不正确】: orderId={},orderStatus={}",orderMasterDTO.getOrderId(),orderMasterDTO.getOrderStatus());
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
//修改订单状态
orderMasterDTO.setOrderStatus(OrderStatusEnum.CANCEL.getCode());
BeanUtils.copyProperties(orderMasterDTO,orderMaster);
OrderMaster updateResult = orderMasterRepository.save(orderMaster);
if(updateResult==null){
log.error("【更新失败】,orderMaser={}",orderMaster);
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
//返还库存
if(CollectionUtils.isEmpty(orderMasterDTO.getOrderDetailList())){
log.error("【订单详情没有】,orderMasterDto={}",orderMasterDTO);
throw new SellException(ResultEnum.ORDER_ERROR_NULLLIST);
}
List<CartDTO> cartDTOList = orderMasterDTO.getOrderDetailList().stream().map(e->new CartDTO(e.getProductId(),e.getProductQuantity())).collect(Collectors.toList());
productInfoService.increaseStock(cartDTOList);
//退还金额
if(orderMasterDTO.getPayStaus().equals(PayStatusEnum.SUCCESS.getCode())){
//TODO
}
return orderMasterDTO;
- 完成订单
OrderMaster orderMaster = new OrderMaster();
//判断订单状态
if(!orderMasterDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())){
log.error("【完成订单】订单状态不正确: orderId={},orderStatus={}",orderMasterDTO.getOrderId(),orderMasterDTO.getOrderStatus());
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
//修改订单状态
orderMasterDTO.setOrderStatus(OrderStatusEnum.FINISHED.getCode());
BeanUtils.copyProperties(orderMasterDTO,orderMaster);
OrderMaster updateResult = orderMasterRepository.save(orderMaster);
if(updateResult==null){
log.error("【完成订单】订单状态不正确,orderMaser={}",orderMaster);
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
return orderMasterDTO;
- 支付变更
OrderMaster orderMaster = new OrderMaster();
//判断订单状态
if(!orderMasterDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())){
log.error("【支付状态】订单状态不正确: orderId={},orderStatus={}",orderMasterDTO.getOrderId(),orderMasterDTO.getOrderStatus());
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
//判断支付状态
if(!orderMasterDTO.getPayStaus().equals(PayStatusEnum.WAIT.getCode())){
log.error("【支付状态】支付状态错误,orderMasterDTO={}",orderMasterDTO);
throw new SellException(ResultEnum.ORDER_PAY_ERROE);
}
//修改支付状态
orderMasterDTO.setPayStaus(PayStatusEnum.SUCCESS.getCode());
BeanUtils.copyProperties(orderMasterDTO,orderMaster);
OrderMaster updateResult = orderMasterRepository.save(orderMaster);
if(updateResult==null){
log.error("【支付状态】更新失败,orderMaser={}",orderMaster);
throw new SellException(ResultEnum.ORDER_ERROR_STATUS);
}
return orderMasterDTO;
Controller层设计与开发
- controller
/**
* 创建订单
* @param orderForm
* @param bindingResult
* @return
*/
@PostMapping("/create")
public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
if(bindingResult.hasErrors()){
log.error("【创建订单】 表单验证错误 , orderForm={}",orderForm);
throw new SellException(ResultEnum.PARAM_ERRO.getCode(),bindingResult.getFieldError().getDefaultMessage());
}
OrderMasterDTO orderMasterDTO = OrderForm2OrderMasterDTOConvert.convert(orderForm);
if(CollectionUtils.isEmpty(orderMasterDTO.getOrderDetailList())){
log.error("【创建订单】 购物车为空 , orderForm={}",orderForm);
throw new SellException(ResultEnum.CART_ERROR);
}
OrderMasterDTO resultOrder = orderService.create(orderMasterDTO);
Map<String,String> map = new HashMap<>();
map.put("orderId",resultOrder.getOrderId());
return ResultVOUtil.success(map);
}
/**
* 订单列表
* @param openid
* @param page
* @param size
* @return
*/
@GetMapping("list")
public ResultVo<List<OrderMasterDTO>> list(@RequestParam(value="openid") String openid,
@RequestParam(value = "page",defaultValue ="0") Integer page,
@RequestParam(value = "size",defaultValue = "10") Integer size){
if(StringUtils.isEmpty(openid)){
log.error("【订单列表】 openid不能为空 ");
throw new SellException(ResultEnum.PARAM_ERRO);
}
PageRequest request = new PageRequest(page,size);
Page<OrderMasterDTO> orderMasterDTOPage = orderService.findList(openid,request);
return ResultVOUtil.success(orderMasterDTOPage.getContent());
}
/**
* 订单查询
* @param openid
* @param orderId
* @return
*/
@GetMapping("detail")
public ResultVo<OrderMasterDTO> detail(@RequestParam("openid") String openid,
@RequestParam("orderId") String orderId){
OrderMasterDTO orderMasterDTO = buyerService.findOrderOne(openid, orderId);
return ResultVOUtil.success(orderMasterDTO);
}
/**
* 取消订单
* @param openid
* @param orderId
* @return
*/
@GetMapping("cancle")
public ResultVo cancel(@RequestParam("openid") String openid,
@RequestParam("orderId") String orderId){
buyerService.cancleOrderOne(openid, orderId);
return ResultVOUtil.success();
}
- 回传值处理 serializer
public class Date2LongSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeNumber(date.getTime() / 1000);
}
}
- 原视频UP主慕课网(SpringBoot企业级微信点餐项目)
- 本篇博客撰写人: XiaoJinZi 转载请注明出处
- 学生能力有限 附上邮箱: 986209501@qq.com 不足以及误处请大佬指责
网友评论