use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.
the class InwardManager method accountDetail.
/**
* 获取用户账户信息
*
* @param reserveId 预定人ID
* @return
* @throws OrderException
*/
public UserAccountOutput accountDetail(String reserveId) {
UserAccountOutput userAccountOutput = null;
try {
userAccountOutput = userClient.account(reserveId);
} catch (Exception e) {
log.error("[order]-create getUserAccountDetails err! reserveUserId:{}, msg:{}", reserveId, Throwables.getStackTraceAsString(e));
}
log.info("[order]-create reserveUserId:{}, userAccount:{}", reserveId, userAccountOutput);
return userAccountOutput;
}
use of com.amusing.start.order.exception.OrderException 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;
}
use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.
the class CreateServiceImpl method create.
/**
* 创建订单
*
* @param createDto 订单信息
* @return
* @throws OrderException
*/
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
@Override
public String create(CreateDto createDto) throws OrderException {
// 1.获取购物车信息
Map<String, Integer> productMap = shopCarService.get(createDto.getReserveId());
if (productMap == null || productMap.isEmpty()) {
throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
}
// 2.判断库存是否足够
Map<String, Long> productStock = inwardManager.productStock(productMap.keySet());
if (productStock == null || productMap.size() != productStock.size()) {
throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
}
Iterator<Map.Entry<String, Long>> iterator = productStock.entrySet().iterator();
if (iterator.hasNext()) {
Map.Entry<String, Long> next = iterator.next();
Long value = next.getValue();
if (value == null || value <= OrderConstant.ZERO) {
throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
}
}
// 3.获取预定人账户信息
UserAccountOutput userAccount = inwardManager.accountDetail(createDto.getReserveId());
Optional.ofNullable(userAccount).orElseThrow(() -> new OrderException(OrderCode.USER_NOT_FOUND));
// 4.获取商品信息及单价
Set<String> productIdSet = productMap.keySet();
List<ProductOutput> productsDetails = inwardManager.productDetails(productIdSet);
// 5.保存订单相关信息
return doSaveOrder(createDto, userAccount, productMap, productsDetails);
}
use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.
the class OrderInfoController method create.
/**
* 订单创建
*
* @param from 订单信息
* @return
*/
@PostMapping("create/v1")
public ApiResult<String> create(@Valid @RequestBody CreateFrom from) throws OrderException, UnauthorizedException {
CreateDto createDto = fromToDto(from);
String orderId = "";
try {
orderId = orderCreateService.create(createDto);
} catch (Exception e) {
log.error("[Order]-[create]-msg:{}", Throwables.getStackTraceAsString(e));
}
return StringUtils.isEmpty(orderId) ? ApiResult.result(OrderCode.ORDER_SAVE_FAIL) : ApiResult.ok(orderId);
}
Aggregations