feat: 订单状态0未完成+编辑+确认完成功能
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -42,6 +42,16 @@ public interface OrderService {
|
||||
*/
|
||||
void refundOrder(String orderId, String operatorId);
|
||||
|
||||
/**
|
||||
* 更新订单状态
|
||||
*/
|
||||
void updateOrderStatus(String orderId, Integer status, String operatorId);
|
||||
|
||||
/**
|
||||
* 更新订单(编辑)
|
||||
*/
|
||||
Order updateOrder(String orderId, CreateOrderRequest request, String operatorId);
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*/
|
||||
|
||||
@@ -62,7 +62,7 @@ public class OrderServiceImpl implements OrderService {
|
||||
order.setDiscountRate(request.getDiscountRate() != null ? request.getDiscountRate() : new BigDecimal("100"));
|
||||
order.setRemark(request.getRemark());
|
||||
order.setPaymentMethod(request.getPaymentMethod());
|
||||
order.setStatus(1); // 已完成
|
||||
order.setStatus(0); // 未完成
|
||||
|
||||
// 2. 查询客户信息(如果指定了客户)
|
||||
if (request.getCustomerId() != null) {
|
||||
@@ -109,8 +109,8 @@ public class OrderServiceImpl implements OrderService {
|
||||
item.setSubtotal(subtotal);
|
||||
orderItems.add(item);
|
||||
|
||||
// 4. 扣减库存
|
||||
decreaseStock(product.getProductId(), itemDTO.getQuantity(), order.getOrderId(), operatorId);
|
||||
// 4. 暂时不扣减库存,等确认完成时再扣减
|
||||
// decreaseStock(product.getProductId(), itemDTO.getQuantity(), order.getOrderId(), operatorId);
|
||||
}
|
||||
|
||||
// 设置订单原价
|
||||
@@ -204,17 +204,11 @@ public class OrderServiceImpl implements OrderService {
|
||||
if (order == null) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
if (order.getStatus() != 1) {
|
||||
if (order.getStatus() != 0) {
|
||||
throw new RuntimeException("订单状态不允许取消");
|
||||
}
|
||||
|
||||
// 恢复库存
|
||||
List<OrderItem> items = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
|
||||
.eq(OrderItem::getOrderId, orderId));
|
||||
for (OrderItem item : items) {
|
||||
increaseStock(item.getProductId(), item.getQuantity(), orderId, operatorId);
|
||||
}
|
||||
|
||||
// 未完成订单没有扣减库存,不需要恢复
|
||||
// 更新订单状态
|
||||
order.setStatus(2); // 已取消
|
||||
orderMapper.updateById(order);
|
||||
@@ -246,6 +240,121 @@ public class OrderServiceImpl implements OrderService {
|
||||
orderMapper.updateById(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新订单状态(确认完成/取消)
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void updateOrderStatus(String orderId, Integer status, String operatorId) {
|
||||
Order order = orderMapper.selectById(orderId);
|
||||
if (order == null) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
// 只有未完成状态可以操作
|
||||
if (order.getStatus() != 0) {
|
||||
throw new RuntimeException("订单状态不允许操作");
|
||||
}
|
||||
|
||||
if (status == 1) {
|
||||
// 确认完成:扣减库存
|
||||
List<OrderItem> items = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
|
||||
.eq(OrderItem::getOrderId, orderId));
|
||||
for (OrderItem item : items) {
|
||||
decreaseStock(item.getProductId(), item.getQuantity(), orderId, operatorId);
|
||||
}
|
||||
}
|
||||
// 取消不需要恢复库存(因为创建时没扣减)
|
||||
|
||||
order.setStatus(status);
|
||||
orderMapper.updateById(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新订单(编辑)
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Order updateOrder(String orderId, CreateOrderRequest request, String operatorId) {
|
||||
Order order = orderMapper.selectById(orderId);
|
||||
if (order == null) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
// 只有未完成状态可以编辑
|
||||
if (order.getStatus() != 0) {
|
||||
throw new RuntimeException("订单状态不允许编辑");
|
||||
}
|
||||
|
||||
// 更新客户信息
|
||||
if (request.getCustomerId() != null) {
|
||||
Customer customer = customerMapper.selectById(request.getCustomerId());
|
||||
if (customer != null) {
|
||||
order.setCustomerId(customer.getCustomerId());
|
||||
order.setCustomerName(customer.getName());
|
||||
order.setCustomerPhone(customer.getPhone());
|
||||
order.setCustomerWechat(customer.getWechatOpenid());
|
||||
}
|
||||
} else {
|
||||
order.setCustomerId(null);
|
||||
order.setCustomerName(null);
|
||||
order.setCustomerPhone(null);
|
||||
order.setCustomerWechat(null);
|
||||
}
|
||||
|
||||
// 更新折扣和备注
|
||||
order.setDiscountRate(request.getDiscountRate() != null ? request.getDiscountRate() : new BigDecimal("100"));
|
||||
order.setRemark(request.getRemark());
|
||||
order.setPaymentMethod(request.getPaymentMethod());
|
||||
|
||||
// 重新计算金额
|
||||
BigDecimal totalAmount = BigDecimal.ZERO;
|
||||
|
||||
// 删除旧的订单明细
|
||||
orderItemMapper.delete(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, orderId));
|
||||
|
||||
// 重新创建订单明细
|
||||
List<OrderItem> orderItems = new ArrayList<>();
|
||||
for (CreateOrderRequest.OrderItemDTO itemDTO : request.getItems()) {
|
||||
Product product = productMapper.selectById(itemDTO.getProductId());
|
||||
if (product == null) {
|
||||
throw new RuntimeException("商品不存在: " + itemDTO.getProductId());
|
||||
}
|
||||
|
||||
BigDecimal price = itemDTO.getPrice() != null ? itemDTO.getPrice() : product.getPrice();
|
||||
BigDecimal subtotal = price.multiply(new BigDecimal(itemDTO.getQuantity()))
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
totalAmount = totalAmount.add(subtotal);
|
||||
|
||||
OrderItem item = new OrderItem();
|
||||
item.setItemId(UUID.randomUUID().toString());
|
||||
item.setOrderId(orderId);
|
||||
item.setProductId(product.getProductId());
|
||||
item.setProductName(product.getName());
|
||||
item.setProductSpec(product.getSpec());
|
||||
item.setUnit(product.getUnit());
|
||||
item.setPrice(price);
|
||||
item.setQuantity(itemDTO.getQuantity());
|
||||
item.setSubtotal(subtotal);
|
||||
orderItems.add(item);
|
||||
orderItemMapper.insert(item);
|
||||
}
|
||||
|
||||
order.setTotalAmount(totalAmount);
|
||||
|
||||
BigDecimal discountRate = order.getDiscountRate();
|
||||
BigDecimal discountAmount = totalAmount.multiply(new BigDecimal("100").subtract(discountRate))
|
||||
.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
|
||||
BigDecimal actualAmount = totalAmount.subtract(discountAmount);
|
||||
|
||||
order.setDiscountAmount(discountAmount);
|
||||
order.setActualAmount(actualAmount);
|
||||
|
||||
orderMapper.updateById(order);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user