1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| @Service @Slf4j public class OrderServiceImpl implements OrderService {
@Autowired private OrderDetailRepository orderDetailRepository; @Autowired private OrderMasterRepository orderMasterRepository; @Autowired private ProductInfoService productInfoService; /* 创建订单,逻辑: 1、遍历所有的OrderDetail(与从数据库中查出的OrderDetail不同,这是用户传过来 数据),从orderDetail中取出orderId,根据orderId查询出ProduceInfo信息 2、根据Product Info的信息(Price和Quantity)计算出总金额 3、将从数据库中查出的ProduceInfo信息拷贝到OrderDetail对象中,并生成openid与detailid设置到OrderDetail中,保存到数据库。 4、遍历完OrderDTO中所有的OrdeDetail。 5、新建OrderMaster对象,将OrderDTO属性设置到OrderMaste,补齐其他属性,保存到数据库 6、扣库存:遍历cartList中所有的Cart对象,一个一个的减去库存 @Override @Transactional public void decreStock(List<Cart> cartList) { for (Cart cart:cartList ) { ProductInfo productInfo = repository.findOne(cart.getProductId()); if (productInfo==null) { throw new SellException(ResultEums.PRODUCT_NOT_EXIST); } Integer stock = productInfo.getProductStock()-cart.getProductQuantity();; if (stock<0){ throw new SellException(ResultEums.PRODUCT_STOCK_ERROR); } productInfo.setProductStock(stock); repository.save(productInfo); } } */ @Override @Transactional public OrderDTO create(OrderDTO orderDTO) {
String orderId = KeyUtils.genUniqueKey(); BigDecimal ordrAmount = new BigDecimal(BigInteger.ZERO); //查询商品,从前台传来得OrderDTO中获取商品详细列表,遍历列表得中得所有OrderDetail, for (OrderDetail orderDetail:orderDTO.getOrderDetailList()) { //计算订单总价:(1)、从orderDetail中获取商品Id,查出商品信息,主要是为了获取ProducePrie和ProductQuantity ProductInfo productInfo = productInfoService.findOne(orderDetail.getProductId()); if (productInfo==null) { throw new SellException(ResultEums.PRODUCT_NOT_EXIST); } //(2)、根据获取得到得ProducePrie和ProductQuantity计算商品总价ordrAmount ordrAmount = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity())) .add(ordrAmount);
//订单详情入库:要将从数据库中查出得productInfo复制到orderDetail,productInfo必须从数据库获取, // 并且设置detailId与orderId
BeanUtils.copyProperties(productInfo,orderDetail); orderDetail.setDetailId(KeyUtils.genUniqueKey()); orderDetail.setOrderId(orderId);
orderDetailRepository.save(orderDetail); } //写入订单数据库:将订单信息,即OrderMaster信息入库,要将OrderDTO中的信息复制到OrderMaster对象中 // ,同时设置orderId,总金额ordrAmount,支付状态,订单状态,并保存入库 OrderMaster orderMaster = new OrderMaster(); orderDTO.setOrderId(orderId); BeanUtils.copyProperties(orderDTO,orderMaster); orderMaster.setOrderAmount(ordrAmount); orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode()); orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode()); orderMasterRepository.save(orderMaster);
//扣库存 List<Cart> cartList = orderDTO.getOrderDetailList().stream() .map(e ->new Cart(e.getProductId(),e.getProductQuantity())) .collect(Collectors.toList()); productInfoService.decreStock(cartList);
return orderDTO; }
@Override public OrderDTO findOrder(String orderId) { OrderDTO orderDTO = new OrderDTO();
OrderMaster orderMaster = orderMasterRepository.findOne(orderId); if (orderMaster==null) { throw new SellException(ResultEums.PRODUCT_NOT_EXIST); } List<OrderDetail> orderDetails = orderDetailRepository.findByOrderId(orderId); if (CollectionUtils.isEmpty(orderDetails)) { throw new SellException(ResultEums.ORDER_DETAIL_NOT_EXIT); } BeanUtils.copyProperties(orderMaster,orderDTO); orderDTO.setOrderDetailList(orderDetails); return orderDTO; }
@Override public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) { Page<OrderMaster> orderMasters = orderMasterRepository.findByBuyerOpenid(buyerOpenid,pageable);
Page<OrderDTO> orderDTOPage = new PageImpl<OrderDTO>(OrderMaster2OrderDetailUtils .convert(orderMasters.getContent()),pageable,orderMasters.getTotalElements());
return orderDTOPage; }
@Override @Transactional public OrderDTO cancle(OrderDTO orderDTO) { //判断订单状态 if (!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())){ log.error("【取消订单】订单状态不正确,orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus()); throw new SellException(ResultEums.ORDER_STATUS_ERROR); } //修改订单状态 OrderMaster orderMaster = new OrderMaster();
orderDTO.setOrderStatus(OrderStatusEnum.CANCEL.getCode()); BeanUtils.copyProperties(orderDTO,orderMaster); OrderMaster orderResult = orderMasterRepository.save(orderMaster); if (orderResult==null) { log.error("【取消订单】订单取更新败,orderMaster={}",orderMaster); throw new SellException(ResultEums.ORDER_UPDATE_FAIL);
} //归还库存
List<OrderDetail> orderDetails = orderDTO.getOrderDetailList(); if (orderDetails==null) { log.error("【取消订单】,订单中无商品详情,orderDTO={}",orderDTO);
} List<Cart> cartList = orderDetails.stream() .map(e ->new Cart(e.getProductId(),e.getProductQuantity())) .collect(Collectors.toList()); productInfoService.increStock(cartList); //如果已支付,退款 if (orderDTO.getPayStatus().equals(PayStatusEnum.PAY.getCode())) {
//TODO }
return orderDTO; }
@Override @Transactional public OrderDTO finish(OrderDTO orderDTO) {
//判断订单状态 if (!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) { log.error("【完结订单】,订单状态不正确 orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus()); throw new SellException(ResultEums.ORDER_STATUS_ERROR); }
//修改订单状态 OrderMaster orderMaster = new OrderMaster(); orderDTO.setOrderStatus(OrderStatusEnum.FINISH.getCode()); BeanUtils.copyProperties(orderDTO,orderMaster);
OrderMaster result = orderMasterRepository.save(orderMaster); if (result==null) { log.error("【完结订单】订单状态更新失败 orderId={},orderStatus={}",orderMaster.getOrderId(),orderMaster.getOrderStatus()); } return orderDTO; }
@Override @Transactional public OrderDTO pay(OrderDTO orderDTO) { //判断订单状态 if (!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) { log.error("【完结订单】,订单状态不正确 orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus()); throw new SellException(ResultEums.ORDER_STATUS_ERROR); } //判断支付状态 if (!orderDTO.getPayStatus().equals(PayStatusEnum.WAIT.getCode())) { log.error("【完结订单】,支付状态不正确 orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus()); throw new SellException(ResultEums.PAY_STATUS_ERROR); } // 去支付 TODO
// 修改订单支付状态
OrderMaster orderMaster = new OrderMaster();
orderDTO.setPayStatus(PayStatusEnum.PAY.getCode()); BeanUtils.copyProperties(orderDTO,orderMaster); OrderMaster result = orderMasterRepository.save(orderMaster); if (result==null) { log.error("【完结订单】订单支付支付状态更新失败,orderId={},orderPayStatus={}",orderMaster.getOrderId(),orderMaster.getPayStatus()); throw new SellException(ResultEums.PAY_STATUS_ERROR); }
return orderDTO; } }
|