|
|
@@ -1,115 +0,0 @@
|
|
|
-package cn.reghao.tnb.content.app.mall.service;
|
|
|
-
|
|
|
-import cn.reghao.file.api.iface.JobService;
|
|
|
-import cn.reghao.jutil.tool.id.SnowFlake;
|
|
|
-import cn.reghao.tnb.common.auth.UserContext;
|
|
|
-import cn.reghao.tnb.content.app.mall.db.mapper.OrderMapper;
|
|
|
-import cn.reghao.tnb.content.app.mall.db.repository.MallRepository;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.constant.OrderStatus;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.dto.OrderDto;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.dto.OrderItem;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.po.Order;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.po.Product;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.po.ProductSnapshot;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.vo.OrderDetail;
|
|
|
-import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author reghao
|
|
|
- * @date 2024-04-16 11:00:24
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class OrderService {
|
|
|
- @DubboReference(check = false)
|
|
|
- private JobService jobService;
|
|
|
- private final OrderMapper orderMapper;
|
|
|
- private final SnowFlake idGenerator;
|
|
|
- private final CartService cartService;
|
|
|
- private final MallRepository mallRepository;
|
|
|
-
|
|
|
- public OrderService(OrderMapper orderMapper, CartService cartService,
|
|
|
- MallRepository mallRepository) {
|
|
|
- this.orderMapper = orderMapper;
|
|
|
- this.idGenerator = new SnowFlake(1L, 1L);
|
|
|
- this.cartService = cartService;
|
|
|
- this.mallRepository = mallRepository;
|
|
|
- }
|
|
|
-
|
|
|
- public long submitCart(OrderDto orderDto) {
|
|
|
- long deliveryId = orderDto.getDeliveryId();
|
|
|
- // shop -> products
|
|
|
- Map<Long, List<OrderItem>> map = orderDto.getItems().stream().collect(Collectors.groupingBy(OrderItem::getShopId));
|
|
|
- List<Order> orders = new ArrayList<>();
|
|
|
- map.forEach((shopId, items) -> {
|
|
|
- items.forEach(item -> {
|
|
|
- long orderId = idGenerator.nextId();
|
|
|
- Product product = mallRepository.getProduct(item.getItemId());
|
|
|
- Order order = new Order(deliveryId, product.getPrice(), orderId, item);
|
|
|
- orders.add(order);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- if (!orders.isEmpty()) {
|
|
|
- orderMapper.saveAll(orders);
|
|
|
- }
|
|
|
-
|
|
|
- long loginUser = UserContext.getUser();
|
|
|
- map.forEach((shopId, items) -> {
|
|
|
- items.forEach(item -> {
|
|
|
- long itemId = item.getItemId();
|
|
|
- cartService.deleteCartItem(loginUser, itemId);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- public long submitOrder(OrderDto orderDto) {
|
|
|
- long deliveryId = orderDto.getDeliveryId();
|
|
|
- OrderItem orderItem = orderDto.getItems().get(0);
|
|
|
- long orderId = idGenerator.nextId();
|
|
|
- Product product = mallRepository.getProduct(orderItem.getItemId());
|
|
|
- Order order = new Order(orderId, product.getPrice(), deliveryId, orderItem);
|
|
|
- ProductSnapshot productSnapshot = new ProductSnapshot(orderId, product);
|
|
|
- mallRepository.saveOrder(order, productSnapshot);
|
|
|
-
|
|
|
- long timeout = 15*60;
|
|
|
- long jobId = jobService.addOrderTimeoutJob(orderId, timeout);
|
|
|
- return orderId;
|
|
|
- }
|
|
|
-
|
|
|
- public void deliveryOrder(long orderId) {
|
|
|
- int status = OrderStatus.toConfirm.getCode();
|
|
|
- orderMapper.updateOrderStatus(orderId, status);
|
|
|
- }
|
|
|
-
|
|
|
- public void receiveOrder(long orderId) {
|
|
|
- int status = OrderStatus.completed.getCode();
|
|
|
- orderMapper.updateOrderStatus(orderId, status);
|
|
|
- }
|
|
|
-
|
|
|
- public List<OrderDetail> getOrders(int pageNumber) {
|
|
|
- long loginUser = UserContext.getUser();
|
|
|
- int pageSize = 100;
|
|
|
- List<Order> list = orderMapper.findByOwner(pageSize, loginUser);
|
|
|
- return list.stream().map(order -> {
|
|
|
- long orderId = order.getOrderId();
|
|
|
- return getOrderDetail(orderId);
|
|
|
- }).collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- public Order getOrder(long orderId) {
|
|
|
- return orderMapper.findByOrderId(orderId);
|
|
|
- }
|
|
|
-
|
|
|
- public OrderDetail getOrderDetail(long orderId) {
|
|
|
- OrderDetail orderDetail = mallRepository.getOrderDetail(orderId);
|
|
|
- return orderDetail;
|
|
|
- }
|
|
|
-}
|