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);
}
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);
}
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;
}
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;
}
Aggregations