@Test public void testUpdateTopSellers() throws Exception { Map<Long, BigDecimal> expectation = new HashMap<Long, BigDecimal>() {{ put(15120L, new BigDecimal("2")); put(15122L, new BigDecimal("4")); put(15123L, new BigDecimal("2")); put(15125L, new BigDecimal("400")); put(15126L, new BigDecimal("6")); put(15127L, new BigDecimal("2")); put(15128L, new BigDecimal("2")); put(15129L, new BigDecimal("2")); }}; Customer customer = createCustomer(); ShoppingCart shoppingCart = getShoppingCart2(customer.getEmail(), false); CustomerOrder customerOrder = orderAssembler.assembleCustomerOrder(shoppingCart, RandomStringUtils.random(10)); customerOrderService.create(customerOrder); Customer customer2 = createCustomer2(); ShoppingCart shoppingCart2 = getShoppingCart2(customer2.getEmail(), false); CustomerOrder customerOrder2 = orderAssembler.assembleCustomerOrder(shoppingCart2, RandomStringUtils.random(10)); customerOrderService.create(customerOrder2); shopTopSellerService.updateTopSellers(10); List<ShopTopSeller> allTopSellers = shopTopSellerService.findAll(); for (ShopTopSeller ts : allTopSellers) { Long key = ts.getProduct().getId(); BigDecimal expectedCounter = expectation.remove(key); if (expectedCounter != null) { assertThat("Unexpected counter for product with id = " + key, ts.getCounter(), greaterThanOrEqualTo(expectedCounter)); } } assertThat(expectation.keySet(), hasSize(0)); }
@Override public void updateTopSellers(final int calculationPeriodInDays) { final LocalDateTime calendar = now().plusDays(-calculationPeriodInDays); List<Shop> shops = shopDao.findAll(); for(Shop shop : shops) { getGenericDao().executeUpdate("TOP.SELLER.SHOP.CLEAN", shop); List<Object[]> list = getGenericDao().findQueryObjectsByNamedQuery("TOP.SELLER.SHOP.CALCULATE", shop, calendar); final Map<Long, BigDecimal> topSellersCount = new HashMap<>(); final Map<Long, Product> topSellersProducts = new HashMap<>(); for(Object [] tuple : list) { final String skuCode = (String) tuple[0]; final BigDecimal qty = (BigDecimal) tuple[1]; final Product product = productDao.findSingleByNamedQuery("PRODUCT.BY.SKU.CODE", skuCode); if (product != null) { topSellersProducts.put(product.getProductId(), product); topSellersCount.merge(product.getProductId(), qty, BigDecimal::add); } } for(Map.Entry<Long, Product> top : topSellersProducts.entrySet()) { ShopTopSeller shopTopSeller = getGenericDao().getEntityFactory().getByIface(ShopTopSeller.class); shopTopSeller.setCounter(topSellersCount.get(top.getKey())); shopTopSeller.setShop(shop); shopTopSeller.setProduct(top.getValue()); getGenericDao().create(shopTopSeller) ; } } }
ShopTopSellerServiceImpl extends BaseGenericServiceImpl<ShopTopSeller> implements ShopTopSellerService { @Override public void updateTopSellers(final int calculationPeriodInDays) { final LocalDateTime calendar = now().plusDays(-calculationPeriodInDays); List<Shop> shops = shopDao.findAll(); for(Shop shop : shops) { getGenericDao().executeUpdate("TOP.SELLER.SHOP.CLEAN", shop); List<Object[]> list = getGenericDao().findQueryObjectsByNamedQuery("TOP.SELLER.SHOP.CALCULATE", shop, calendar); final Map<Long, BigDecimal> topSellersCount = new HashMap<>(); final Map<Long, Product> topSellersProducts = new HashMap<>(); for(Object [] tuple : list) { final String skuCode = (String) tuple[0]; final BigDecimal qty = (BigDecimal) tuple[1]; final Product product = productDao.findSingleByNamedQuery("PRODUCT.BY.SKU.CODE", skuCode); if (product != null) { topSellersProducts.put(product.getProductId(), product); topSellersCount.merge(product.getProductId(), qty, BigDecimal::add); } } for(Map.Entry<Long, Product> top : topSellersProducts.entrySet()) { ShopTopSeller shopTopSeller = getGenericDao().getEntityFactory().getByIface(ShopTopSeller.class); shopTopSeller.setCounter(topSellersCount.get(top.getKey())); shopTopSeller.setShop(shop); shopTopSeller.setProduct(top.getValue()); getGenericDao().create(shopTopSeller) ; } } } }
ShopTopSellerServiceImpl extends BaseGenericServiceImpl<ShopTopSeller> implements ShopTopSellerService { @Override public void updateTopSellers(final int calculationPeriodInDays) { final LocalDateTime calendar = now().plusDays(-calculationPeriodInDays); List<Shop> shops = shopDao.findAll(); for(Shop shop : shops) { getGenericDao().executeUpdate("TOP.SELLER.SHOP.CLEAN", shop); List<Object[]> list = getGenericDao().findQueryObjectsByNamedQuery("TOP.SELLER.SHOP.CALCULATE", shop, calendar); final Map<Long, BigDecimal> topSellersCount = new HashMap<>(); final Map<Long, Product> topSellersProducts = new HashMap<>(); for(Object [] tuple : list) { final String skuCode = (String) tuple[0]; final BigDecimal qty = (BigDecimal) tuple[1]; final Product product = productDao.findSingleByNamedQuery("PRODUCT.BY.SKU.CODE", skuCode); if (product != null) { topSellersProducts.put(product.getProductId(), product); topSellersCount.merge(product.getProductId(), qty, BigDecimal::add); } } for(Map.Entry<Long, Product> top : topSellersProducts.entrySet()) { ShopTopSeller shopTopSeller = getGenericDao().getEntityFactory().getByIface(ShopTopSeller.class); shopTopSeller.setCounter(topSellersCount.get(top.getKey())); shopTopSeller.setShop(shop); shopTopSeller.setProduct(top.getValue()); getGenericDao().create(shopTopSeller) ; } } } ShopTopSellerServiceImpl(final GenericDAO<ShopTopSeller, Long> shopTopSellerDao,
                                    final GenericDAO<Shop, Long> shopDao,
                                    final GenericDAO<Product, Long> productDao); }
ShopTopSellerServiceImpl extends BaseGenericServiceImpl<ShopTopSeller> implements ShopTopSellerService { @Override public void updateTopSellers(final int calculationPeriodInDays) { final LocalDateTime calendar = now().plusDays(-calculationPeriodInDays); List<Shop> shops = shopDao.findAll(); for(Shop shop : shops) { getGenericDao().executeUpdate("TOP.SELLER.SHOP.CLEAN", shop); List<Object[]> list = getGenericDao().findQueryObjectsByNamedQuery("TOP.SELLER.SHOP.CALCULATE", shop, calendar); final Map<Long, BigDecimal> topSellersCount = new HashMap<>(); final Map<Long, Product> topSellersProducts = new HashMap<>(); for(Object [] tuple : list) { final String skuCode = (String) tuple[0]; final BigDecimal qty = (BigDecimal) tuple[1]; final Product product = productDao.findSingleByNamedQuery("PRODUCT.BY.SKU.CODE", skuCode); if (product != null) { topSellersProducts.put(product.getProductId(), product); topSellersCount.merge(product.getProductId(), qty, BigDecimal::add); } } for(Map.Entry<Long, Product> top : topSellersProducts.entrySet()) { ShopTopSeller shopTopSeller = getGenericDao().getEntityFactory().getByIface(ShopTopSeller.class); shopTopSeller.setCounter(topSellersCount.get(top.getKey())); shopTopSeller.setShop(shop); shopTopSeller.setProduct(top.getValue()); getGenericDao().create(shopTopSeller) ; } } } ShopTopSellerServiceImpl(final GenericDAO<ShopTopSeller, Long> shopTopSellerDao,
                                    final GenericDAO<Shop, Long> shopDao,
                                    final GenericDAO<Product, Long> productDao); @Override void updateTopSellers(final int calculationPeriodInDays); }
ShopTopSellerServiceImpl extends BaseGenericServiceImpl<ShopTopSeller> implements ShopTopSellerService { @Override public void updateTopSellers(final int calculationPeriodInDays) { final LocalDateTime calendar = now().plusDays(-calculationPeriodInDays); List<Shop> shops = shopDao.findAll(); for(Shop shop : shops) { getGenericDao().executeUpdate("TOP.SELLER.SHOP.CLEAN", shop); List<Object[]> list = getGenericDao().findQueryObjectsByNamedQuery("TOP.SELLER.SHOP.CALCULATE", shop, calendar); final Map<Long, BigDecimal> topSellersCount = new HashMap<>(); final Map<Long, Product> topSellersProducts = new HashMap<>(); for(Object [] tuple : list) { final String skuCode = (String) tuple[0]; final BigDecimal qty = (BigDecimal) tuple[1]; final Product product = productDao.findSingleByNamedQuery("PRODUCT.BY.SKU.CODE", skuCode); if (product != null) { topSellersProducts.put(product.getProductId(), product); topSellersCount.merge(product.getProductId(), qty, BigDecimal::add); } } for(Map.Entry<Long, Product> top : topSellersProducts.entrySet()) { ShopTopSeller shopTopSeller = getGenericDao().getEntityFactory().getByIface(ShopTopSeller.class); shopTopSeller.setCounter(topSellersCount.get(top.getKey())); shopTopSeller.setShop(shop); shopTopSeller.setProduct(top.getValue()); getGenericDao().create(shopTopSeller) ; } } } ShopTopSellerServiceImpl(final GenericDAO<ShopTopSeller, Long> shopTopSellerDao,
                                    final GenericDAO<Shop, Long> shopDao,
                                    final GenericDAO<Product, Long> productDao); @Override void updateTopSellers(final int calculationPeriodInDays);  }