Subversion Repositories SmartDukaan

Rev

Rev 28166 | Rev 28653 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.cart;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.util.FormattingUtils;
import com.spice.profitmandi.dao.entity.catalog.Item;
import com.spice.profitmandi.dao.entity.catalog.TagListing;
import com.spice.profitmandi.dao.entity.user.Cart;
import com.spice.profitmandi.dao.entity.user.CartItem;
import com.spice.profitmandi.dao.entity.user.CartLine;
import com.spice.profitmandi.dao.entity.user.User;
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
import com.spice.profitmandi.dao.repository.user.CartItemRepository;
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
import com.spice.profitmandi.dao.repository.user.CartRepository;
import com.spice.profitmandi.dao.repository.user.UserRepository;

@Component
public class CartServiceImpl implements CartService {

        private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));

        @Autowired
        CartRepository cartRepository;

        @Autowired
        CartItemRepository cartItemRepository;

        @Autowired
        FofoStoreRepository fofoStoreRepository;

        @Autowired
        TagListingRepository tagListingRepository;

        @Autowired
        UserRepository saholicUserRepository;

        @Autowired
        ItemRepository itemRepository;

        @Autowired
        CurrentInventorySnapshotRepository currentInventorySnapshotRepository;

        @Autowired
        CartLineRepository cartLineRepository;

        @Override
        public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
                CartItem cartItem = cartItemRepository.selectByCartItem(cartId, itemId);
                if (cartItem == null) {
                        this.createCartItem(cartId, itemId, quantity, sellingPrice);
                } else {
                        cartItem.setQuantity(quantity);
                        cartItem.setActualPrice(sellingPrice);
                }
                return null;
        }

        private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
                CartItem cartItem = new CartItem();
                cartItem.setActualPrice(sellingPrice);
                cartItem.setCartId(cartId);
                cartItem.setQuantity(quantity);
                // TODO: Estmiate Logic
                cartItem.setEstimate(2);
                cartItemRepository.persist(cartItem);

        }

        @Override
        public CartModel removeFromCart(int cartId, int itemId) {
                CartItem cartItem = cartItemRepository.selectByCartItem(cartId, itemId);
                if (cartItem != null) {
                        cartItemRepository.delete(cartItem);
                }
                return this.validateCart(cartId);
        }

        @Override
        public void clearCart(int cartId) {
                List<CartItem> cartItems = cartItemRepository.selectAllByCartId(cartId);
                cartItems.stream().forEach(cartItem -> cartItemRepository.delete(cartItem));
        }

        @Override
        public CartModel getCart(int cartId) {
                // TODO Auto-generated method stub
                return null;
        }

        private CartModel validateCart(int cartId) {
                CartModel cartModel = null;
                try {
                        Cart cart = cartRepository.selectById(cartId);
                        User user = saholicUserRepository.selectByCartId(cartId);
                        List<CartItem> cartItems = cartItemRepository.selectAllByCartId(cartId);
                        cartModel = new CartModel();
                        List<CartItemModel> cartItemModels = new ArrayList<>();
                        Set<Integer> itemIds = cartItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
                        boolean isFofo = true;
                        Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemIds).stream()
                                        .collect(Collectors.toMap(x -> x.getId(), x -> x));

                        Map<Integer, Float> priceMap = this.getPriceMap(user, itemIds);
                        Map<Integer, Integer> availabilityMap = this.getAvailabilityMap(user, itemIds);
                        for (CartItem cartItem : cartItems) {
                                try {
                                        CartItemModel cartItemModel = new CartItemModel();
                                        cartItemModel.setItemId(cartItem.getItemId());
                                        Item item = itemsMap.get(cartItem.getItemId());
                                        cartItemModel.setTitle(item.getItemDescription());
                                        float currentSellingPrice = priceMap.get(cartItem.getItemId());
                                        if (currentSellingPrice != cartItem.getActualPrice()) {
                                                CartItemMessage cartItemMessage = new CartItemMessage();
                                                int diffPrice = (int) (currentSellingPrice - cartItem.getActualPrice());
                                                String diff = currentSellingPrice - cartItem.getActualPrice() > 0 ? "increased" : "decreased";
                                                cartItem.setActualPrice(cartItem.getActualPrice());
                                                cartItemMessage.setMessageText("Pricing have been " + diff + "by Rs."
                                                                + FormattingUtils.formatDecimalTwoDigits(Math.abs(diffPrice)));
                                                cartItemMessage.setType("info");
                                        }
                                        cartItemModel.setSellingPrice(cartItem.getActualPrice());

                                        // Check stock availability and check if stock is active
                                } catch (Exception e) {

                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return cartModel;
        }

        // As of now it only works for Fofo Partners need to be refactored later
        private Map<Integer, Integer> getAvailabilityMap(User user, Set<Integer> itemIds) {
                Map<Integer, Integer> itemAvailabilityMap = new HashMap<>();
                List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
                tags.stream().forEach(tagListing -> {
                        if (!tagListing.isActive() || tagListing.isHotDeals()) {
                                // show actual values here
                        } else {
                                itemAvailabilityMap.put(tagListing.getItemId(), 100);
                        }
                });
                return itemAvailabilityMap;
        }

        // As of now it only works for Fofo Partners need to be refactored later
        private Map<Integer, Integer> getMinBuyQtyMap(User user, Set<Integer> itemIds) {
                List<Item> items = itemRepository.selectByIds(itemIds);
                return items.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getCategoryId() == 10020 ? 1 : 10));
        }

        // As of now it only works for Fofo Partners need to be refactored later
        private Map<Integer, Float> getPriceMap(User user, Set<Integer> itemIds) {
                List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
                return tags.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getSellingPrice()));

        }

        @Override
        public void addShoppingBag(int cartId, long qty) {
                CartLine cl = new CartLine();
                cl.setItemId(32046);
                cl.setQuantity((int) qty);
                cl.setCartId(cartId);
                cl.setCreateTimestamp(LocalDateTime.now());
                cl.setDataProtectionAmount(0);
                cl.setEstimate(0);
                cl.setInsuranceAmount((float) 0);
                cl.setDataProtectionInsurer(0);
                cl.setActualPrice((float) 0.01 * qty);
                cartLineRepository.persist(cl);

        }

}