Search in sources :

Example 1 with OrderProductPosition

use of com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method buildOrder.

private Order buildOrder(User user, Geometry coords, CreateOrderDTO dto, Map<Long, Double> productPosPriceMap, Map<Long, Integer> productPosAmountMap) {
    Order order = new Order();
    order.setDiscount(dto.getDiscount());
    order.setHighDemandCoeff(dto.getHighDemandCoeff());
    order.setOverallCost(dto.getOverallCost());
    order.setClient(user.getClient());
    order.setWarehouse(warehouseService.findById(dto.getWarehouseId()));
    order.setAddress(dto.getAddress());
    order.setCoordinates(coords);
    order.setStatus(OrderStatus.CREATED);
    order.setDateStart(LocalDateTime.now());
    order = orderRepo.save(order);
    // adding records in DB table 'orders_product_positions'
    for (Long productPositionId : productPosAmountMap.keySet()) {
        orderProductPositionRepo.save(new OrderProductPosition(order, productPositionRepo.findById(productPositionId).get(), productPosAmountMap.get(productPositionId), productPosPriceMap.get(productPositionId)));
    }
    return orderRepo.save(order);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)

Example 2 with OrderProductPosition

use of com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method convertToOrderInfoDTO.

public OrderInfoDTO convertToOrderInfoDTO(Order order) {
    List<OrderProductPosition> orderProductPositions = orderProductPositionRepo.findAllByOrder(order);
    List<OrderInfoDTO.ProductAmountPair> products = new ArrayList<>();
    for (OrderProductPosition orderProductPosition : orderProductPositions) {
        ProductDTO p = productMapper.mapToDTO(orderProductPosition.getProductPosition().getProduct());
        Integer amount = orderProductPosition.getAmount();
        products.add(new OrderInfoDTO.ProductAmountPair(p, amount));
    }
    return new OrderInfoDTO(order.getId(), order.getClient(), order.getAddress(), order.getCoordinates(), order.getWarehouse(), order.getCourier(), order.getStatus(), order.getDateStart(), order.getDateEnd(), order.getOverallCost(), order.getHighDemandCoeff(), order.getDiscount(), order.getPromoCodeId(), order.getClientRating(), order.getDeliveryRating(), products);
}
Also used : OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)

Example 3 with OrderProductPosition

use of com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method buildOrder.

private Order buildOrder(User user, Geometry coords, CreateOrderDTO dto, List<ProductPosition> productPositions) {
    Order order = new Order();
    final int weightLimit = 15000;
    int currentWeight = 0;
    Map<Long, Integer> currOrderPositions = new HashMap<>();
    // adding records in DB table 'orders_product_positions'
    for (int j = 0; j < productPositions.size() && weightLimit != currentWeight; j++) {
        ProductPosition currPos = productPositions.get(j);
        int currPosWeight = currPos.getProduct().getWeight();
        if ((weightLimit - currentWeight) >= currPosWeight) {
            currentWeight += currPosWeight;
            currOrderPositions.merge(currPos.getId(), 1, Integer::sum);
            productPositions.remove(j);
            j--;
        }
    }
    Double orderCost = 0d;
    Double orderDiscount = 0d;
    for (Long psId : currOrderPositions.keySet()) {
        int amount = currOrderPositions.get(psId);
        ProductPosition ps = productPositionRepo.findById(psId).get();
        Double price = (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount;
        Double discount = amount * ps.getProduct().getDiscount();
        orderCost += price;
        orderDiscount += discount;
    }
    order.setDiscount(orderDiscount);
    order.setHighDemandCoeff(dto.getHighDemandCoeff());
    order.setOverallCost(orderCost);
    order.setClient(user.getClient());
    order.setWarehouse(warehouseService.findById(dto.getWarehouseId()));
    order.setAddress(dto.getAddress());
    order.setCoordinates(coords);
    order.setStatus(OrderStatus.CREATED);
    order.setDateStart(LocalDateTime.now());
    order = orderRepo.save(order);
    for (Long psId : currOrderPositions.keySet()) {
        int amount = currOrderPositions.get(psId);
        ProductPosition ps = productPositionRepo.findById(psId).get();
        orderProductPositionRepo.save(new OrderProductPosition(order, ps, amount, (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount));
    }
    return order;
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)

Example 4 with OrderProductPosition

use of com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition in project 2021-msk-food-delivery by netcracker-edu.

the class ProductPositionServiceImpl1 method getPositionsFromOrder.

@Override
public ProductPositionsFromOrderDTO getPositionsFromOrder(Order order) {
    List<OrderProductPosition> ordersProductPositions = orderProductPositionRepo.findAllByOrder(order);
    ProductPositionsFromOrderDTO dto = new ProductPositionsFromOrderDTO();
    List<ProductPositionsFromOrderDTO.ProductPositionAmountPair> pairs = new ArrayList<>();
    for (OrderProductPosition orderProductPosition : ordersProductPositions) {
        pairs.add(new ProductPositionsFromOrderDTO.ProductPositionAmountPair(orderProductPosition.getProductPosition(), orderProductPosition.getAmount()));
    }
    dto.setProductPositions(pairs);
    return dto;
}
Also used : OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPositionsFromOrderDTO(com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsFromOrderDTO)

Aggregations

OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)4 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 ProductPositionsFromOrderDTO (com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsFromOrderDTO)1 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)1 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)1