use of com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method createOrder.
@Override
@Transactional
public areCreatedDTO createOrder(CreateOrderDTO dto, User user) {
checkOrderDataActuality(dto);
Long warehouseId = dto.getWarehouseId();
Geometry coords = geometryFactory.createPoint(new Coordinate(dto.getGeo().getLon().doubleValue(), dto.getGeo().getLat().doubleValue()));
HashMap<Long, Integer> pairs = dto.getProductAmountPairs();
Object[] productPosMaps = getProductPositionsData(pairs, warehouseId);
// we will use later Maps below to reserve product positions from warehouse and put them in order(s)
HashMap<Long, Double> productPosPriceMap = (HashMap<Long, Double>) productPosMaps[0];
HashMap<Long, Integer> productPosAmountMap = (HashMap<Long, Integer>) productPosMaps[1];
Double orderWeight = countOrderWeight(pairs);
if (orderWeight > 15000d) {
int neededCouriersAmount = (int) Math.ceil(orderWeight / 15000d);
List<ProductPosition> productPositions = getSortedProductPositions(productPosAmountMap);
// we've got sorted by weight sequence of product positions, so we can put them into different orders
List<Order> orders = new ArrayList<>();
for (int i = 0; i < neededCouriersAmount; i++) {
Order order = buildOrder(user, coords, dto, productPositions);
// attaching courier and then order status = 'courier_appointed'
Courier courier;
try {
courier = courierService.findFreeCourier(warehouseId);
} catch (CourierAvailabilityEx ex) {
for (Order o : orders) {
// DB trigger will return all reserved product
o.setStatus(OrderStatus.CANCELLED);
// positions back to warehouse
orderRepo.save(o);
}
throw ex;
}
order.setCourier(courier);
order.setStatus(OrderStatus.COURIER_APPOINTED);
orders.add(orderRepo.save(order));
}
return new areCreatedDTO(orders.stream().map(order -> order.getId()).collect(Collectors.toList()));
} else {
Order order = buildOrder(user, coords, dto, productPosPriceMap, productPosAmountMap);
// attaching courier and then order status = 'courier_appointed'
Courier courier;
try {
courier = courierService.findFreeCourier(warehouseId);
} catch (CourierAvailabilityEx ex) {
// DB trigger will return all reserved product positions back
order.setStatus(OrderStatus.CANCELLED);
// to warehouse
orderRepo.save(order);
throw ex;
}
order.setCourier(courier);
order.setStatus(OrderStatus.COURIER_APPOINTED);
orderRepo.save(order);
return new areCreatedDTO(List.of(order.getId()));
}
}
use of com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionServiceImpl1 method getProductPositionInfoDTOById.
@Override
public ProductPositionInfoDTO getProductPositionInfoDTOById(Long id) {
Optional<ProductPosition> optionalProductPosition = this.productPositionRepo.findById(id);
if (optionalProductPosition.isEmpty())
return null;
ProductPosition productPosition = optionalProductPosition.get();
return convertToInfoDTO(productPosition);
}
use of com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionServiceImpl1 method updatePaymentStatus.
@Override
public boolean updatePaymentStatus(List<Long> ids) {
List<ProductPosition> productPositionList = new ArrayList<>();
for (Long id : ids) {
if (id == null)
return false;
ProductPosition productPosition = getProductPosition(id);
productPositionList.add(productPosition);
}
for (ProductPosition productPosition : productPositionList) {
productPosition.setIsInvoicePaid(true);
productPositionRepo.save(productPosition);
}
return true;
}
use of com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionServiceImpl1 method convertToProductPosition.
public ProductPosition convertToProductPosition(AcceptSupplyDTO acceptSupplyDTO) {
Optional<Product> productOptional = productRepo.findById(acceptSupplyDTO.getProductId());
Optional<Warehouse> warehouseOptional = warehouseRepo.findById(acceptSupplyDTO.getWarehouseId());
if (warehouseOptional.isEmpty())
throw new NotFoundEx(String.valueOf(acceptSupplyDTO.getWarehouseId()));
if (productOptional.isEmpty())
throw new NotFoundEx(String.valueOf(acceptSupplyDTO.getProductId()));
Product product = productOptional.get();
Warehouse warehouse = warehouseOptional.get();
ProductPosition productPosition = new ProductPosition(null, product, warehouse, acceptSupplyDTO.getWarehouseSection(), acceptSupplyDTO.getSupplyAmount(), acceptSupplyDTO.getSupplyAmount(), acceptSupplyDTO.getManufactureDate(), acceptSupplyDTO.getSupplierInvoice(), acceptSupplyDTO.getSupplierName(), acceptSupplyDTO.getIsInvoicePaid(), acceptSupplyDTO.getManufactureDate());
if (acceptSupplyDTO.getIsInvoicePaid() == null) {
productPosition.setIsInvoicePaid(false);
}
return productPosition;
}
use of com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionController method acceptSupply.
@PostMapping(path = "/api/v1/productPosition")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public isCreatedDTO acceptSupply(@AuthenticationPrincipal User user, @Valid @RequestBody AcceptSupplyDTO acceptSupplyDTO) {
if (Role.isMODERATOR(user.getRole().toString())) {
if (!user.getModerator().getWarehouseId().equals(acceptSupplyDTO.getWarehouseId())) {
throw new CustomAccessDeniedException();
}
}
Long id = productPositionService.acceptSupply(acceptSupplyDTO);
isCreatedDTO isCreated = new isCreatedDTO();
isCreated.setId(id);
return isCreated;
}
Aggregations