use of com.amusing.start.order.pojo.OrderProductInfo in project amusing-project by m-lvqingyu.
the class CreateServiceImpl method doSaveOrder.
/**
* 保存订单
*
* @param createDto 订单信息
* @param userAccount 账户详情
* @param productsDetails 商品详情
* @return
* @throws OrderException
*/
public String doSaveOrder(CreateDto createDto, UserAccountOutput userAccount, Map<String, Integer> productMap, List<ProductOutput> productsDetails) throws OrderException {
Snowflake snowflake = IdUtil.getSnowflake(orderWorker, orderDataCenter);
String orderNo = snowflake.nextIdStr();
Iterator<Map.Entry<String, Integer>> iterator = productMap.entrySet().iterator();
BigDecimal totalAmount = BigDecimal.ZERO;
List<OrderProductInfo> infoList = new ArrayList<>();
while (iterator.hasNext()) {
Map.Entry<String, Integer> next = iterator.next();
String productId = next.getKey();
Integer productNum = next.getValue();
for (ProductOutput output : productsDetails) {
if (productId.equals(output.getProductId())) {
BigDecimal price = output.getPrice();
BigDecimal amount = price.multiply(new BigDecimal(productNum));
totalAmount = totalAmount.add(amount);
OrderProductInfo productInfo = OrderProductInfo.builder().orderNo(orderNo).shopId(output.getShopId()).shopName(output.getShopName()).productId(output.getProductId()).productName(output.getProductName()).priceId(output.getPriceId()).price(output.getPrice()).num(productNum).amount(amount).build();
infoList.add(productInfo);
}
}
}
Long currentTime = System.currentTimeMillis();
OrderInfo orderInfo = OrderInfo.builder().orderNo(orderNo).reserveId(createDto.getReserveId()).consigneeId(createDto.getConsigneeId()).freightAmount(BigDecimal.ZERO).totalAmount(totalAmount).couponAmount(BigDecimal.ZERO).activityAmount(BigDecimal.ZERO).status(OrderStatus.SCHEDULED.getKey()).isFreight(YesNo.YES.getKey()).isEvaluate(YesNo.NO.getKey()).createBy(createDto.getReserveId()).createTime(currentTime).updateBy(createDto.getReserveId()).updateTime(currentTime).build();
BigDecimal userAmount = userAccount.getMainAmount().add(userAccount.getGiveAmount()).subtract(userAccount.getFrozenAmount());
if (userAmount.compareTo(totalAmount) < OrderConstant.ZERO) {
throw new OrderException(OrderCode.INSUFFICIENT_BALANCE);
}
// 保存订单-口库存-扣余额
orderInfoMapper.insert(orderInfo);
for (OrderProductInfo orderProductInfo : infoList) {
orderProductInfoMapper.insert(orderProductInfo);
}
Boolean result = inwardManager.mainSettlement(orderInfo.getReserveId(), totalAmount);
if (!result) {
throw new OrderException(OrderCode.UNABLE_PROVIDE_SERVICE);
}
result = inwardManager.deductionStock(productMap);
if (!result) {
throw new OrderException(OrderCode.UNABLE_PROVIDE_SERVICE);
}
return orderNo;
}
Aggregations