- 新增 UserAddress/UserFavorite/ProductReview/OrderItem 实体类 - 新增对应的 DTO、Repository、Service 和 Controller - 新增 OrderMigrationService 订单数据迁移服务
64 lines
2.2 KiB
Java
64 lines
2.2 KiB
Java
package com.org.flashsalesystem.service;
|
|
|
|
import com.org.flashsalesystem.dto.ProductDTO;
|
|
import com.org.flashsalesystem.entity.Order;
|
|
import com.org.flashsalesystem.entity.OrderItem;
|
|
import com.org.flashsalesystem.repository.OrderItemRepository;
|
|
import com.org.flashsalesystem.repository.OrderRepository;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
@Slf4j
|
|
public class OrderMigrationService {
|
|
|
|
@Autowired
|
|
private OrderRepository orderRepository;
|
|
|
|
@Autowired
|
|
private OrderItemRepository orderItemRepository;
|
|
|
|
@Autowired
|
|
private ProductService productService;
|
|
|
|
@Transactional
|
|
public Map<String, Object> migrateLegacyOrderItems() {
|
|
List<Order> orders = orderRepository.findAll();
|
|
int migrated = 0;
|
|
int skipped = 0;
|
|
|
|
for (Order order : orders) {
|
|
if (orderItemRepository.existsByOrderId(order.getId())) {
|
|
skipped++;
|
|
continue;
|
|
}
|
|
|
|
ProductDTO product = productService.getProductById(order.getProductId());
|
|
OrderItem orderItem = new OrderItem();
|
|
orderItem.setOrderId(order.getId());
|
|
orderItem.setProductId(order.getProductId());
|
|
orderItem.setProductName(product != null ? product.getName() : "未知商品");
|
|
orderItem.setProductImageUrl(product != null ? product.getImageUrl() : null);
|
|
orderItem.setPrice(order.getQuantity() != null && order.getQuantity() > 0
|
|
? order.getTotalPrice().divide(java.math.BigDecimal.valueOf(order.getQuantity()), 2, java.math.RoundingMode.HALF_UP)
|
|
: order.getTotalPrice());
|
|
orderItem.setQuantity(order.getQuantity());
|
|
orderItem.setSubtotal(order.getTotalPrice());
|
|
orderItemRepository.save(orderItem);
|
|
migrated++;
|
|
}
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
result.put("totalOrders", orders.size());
|
|
result.put("migrated", migrated);
|
|
result.put("skipped", skipped);
|
|
return result;
|
|
}
|
|
}
|