Subversion Repositories SmartDukaan

Rev

Rev 28166 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
26522 amit.gupta 1
package com.spice.profitmandi.dao.cart;
2
 
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
import java.util.stream.Collectors;
11
 
12
import org.springframework.beans.factory.annotation.Autowired;
13
 
14
import com.spice.profitmandi.common.util.FormattingUtils;
15
import com.spice.profitmandi.dao.entity.catalog.Item;
16
import com.spice.profitmandi.dao.entity.catalog.TagListing;
17
import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;
18
import com.spice.profitmandi.dao.entity.user.Cart;
19
import com.spice.profitmandi.dao.entity.user.CartItem;
20
import com.spice.profitmandi.dao.entity.user.User;
21
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
22
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
25
import com.spice.profitmandi.dao.repository.user.CartItemRepository;
26
import com.spice.profitmandi.dao.repository.user.CartRepository;
27
import com.spice.profitmandi.dao.repository.user.UserRepository;
28
 
29
public class CartServiceImpl implements CartService {
30
 
31
	private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
32
 
33
	@Autowired
34
	CartRepository cartRepository;
35
 
36
	@Autowired
37
	CartItemRepository cartItemRepository;
38
 
39
	@Autowired
40
	FofoStoreRepository fofoStoreRepository;
41
 
42
	@Autowired
43
	TagListingRepository tagListingRepository;
44
 
45
	@Autowired
46
	UserRepository saholicUserRepository;
47
 
48
	@Autowired
49
	ItemRepository itemRepository;
50
 
51
	@Autowired
52
	CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
53
 
54
	@Override
55
	public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
56
		CartItem cartItem = cartItemRepository.selectByCartItem(cartId, itemId);
57
		if (cartItem == null) {
58
			this.createCartItem(cartId, itemId, quantity, sellingPrice);
59
		} else {
60
			cartItem.setQuantity(quantity);
61
			cartItem.setActualPrice(sellingPrice);
62
		}
63
		return null;
64
	}
65
 
66
	private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
67
		CartItem cartItem = new CartItem();
68
		cartItem.setActualPrice(sellingPrice);
69
		cartItem.setCartId(cartId);
70
		cartItem.setQuantity(quantity);
71
		// TODO: Estmiate Logic
72
		cartItem.setEstimate(2);
73
		cartItemRepository.persist(cartItem);
74
 
75
	}
76
 
77
	@Override
78
	public CartModel removeFromCart(int cartId, int itemId) {
79
		CartItem cartItem = cartItemRepository.selectByCartItem(cartId, itemId);
80
		if (cartItem != null) {
81
			cartItemRepository.delete(cartItem);
82
		}
83
		return this.validateCart(cartId);
84
	}
85
 
86
	@Override
87
	public void clearCart(int cartId) {
88
		List<CartItem> cartItems = cartItemRepository.selectAllByCartId(cartId);
89
		cartItems.stream().forEach(cartItem -> cartItemRepository.delete(cartItem));
90
	}
91
 
92
	@Override
93
	public CartModel getCart(int cartId) {
94
		// TODO Auto-generated method stub
95
		return null;
96
	}
97
 
98
	private CartModel validateCart(int cartId) {
99
		CartModel cartModel = null;
100
		try {
101
			Cart cart = cartRepository.selectById(cartId);
102
			User user = saholicUserRepository.selectByCartId(cartId);
103
			List<CartItem> cartItems = cartItemRepository.selectAllByCartId(cartId);
104
			cartModel = new CartModel();
105
			List<CartItemModel> cartItemModels = new ArrayList<>();
106
			Set<Integer> itemIds = cartItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
107
			boolean isFofo = true;
108
			Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemIds).stream()
109
					.collect(Collectors.toMap(x -> x.getId(), x -> x));
110
 
111
			Map<Integer, Float> priceMap = this.getPriceMap(user, itemIds);
112
			Map<Integer, Integer> availabilityMap = this.getAvailabilityMap(user, itemIds);
113
			for (CartItem cartItem : cartItems) {
114
				try {
115
					CartItemModel cartItemModel = new CartItemModel();
116
					cartItemModel.setItemId(cartItem.getItemId());
117
					Item item = itemsMap.get(cartItem.getItemId());
118
					cartItemModel.setTitle(item.getItemDescription());
119
					float currentSellingPrice = priceMap.get(cartItem.getItemId());
120
					if (currentSellingPrice != cartItem.getActualPrice()) {
121
						CartItemMessage cartItemMessage = new CartItemMessage();
122
						int diffPrice = (int) (currentSellingPrice - cartItem.getActualPrice());
123
						String diff = currentSellingPrice - cartItem.getActualPrice() > 0 ? "increased" : "decreased";
124
						cartItem.setActualPrice(cartItem.getActualPrice());
125
						cartItemMessage.setMessageText("Pricing have been " + diff + "by Rs."
126
								+ FormattingUtils.formatDecimalTwoDigits(Math.abs(diffPrice)));
127
						cartItemMessage.setType("info");
128
					}
129
					cartItemModel.setSellingPrice(cartItem.getActualPrice());
130
 
131
					// Check stock availability and check if stock is active
132
				} catch (Exception e) {
133
 
134
				}
135
			}
136
		} catch (Exception e) {
137
			e.printStackTrace();
138
		}
139
		return cartModel;
140
	}
141
 
142
	// As of now it only works for Fofo Partners need to be refactored later
143
	private Map<Integer, Integer> getAvailabilityMap(User user, Set<Integer> itemIds) {
144
		Map<Integer, Integer> itemAvailabilityMap = new HashMap<>();
145
		List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
146
		tags.stream().forEach(tagListing -> {
147
			if (!tagListing.isActive() || tagListing.isHotDeals()) {
148
				// show actual values here
149
			} else {
150
				itemAvailabilityMap.put(tagListing.getItemId(), 100);
151
			}
152
		});
153
		return itemAvailabilityMap;
154
	}
155
 
156
	// As of now it only works for Fofo Partners need to be refactored later
157
	private Map<Integer, Integer> getMinBuyQtyMap(User user, Set<Integer> itemIds) {
158
		List<Item> items = itemRepository.selectByIds(itemIds);
159
		return items.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getCategoryId() == 10020 ? 1 : 10));
160
	}
161
 
162
	// As of now it only works for Fofo Partners need to be refactored later
163
	private Map<Integer, Float> getPriceMap(User user, Set<Integer> itemIds) {
164
		List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
165
		return tags.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getSellingPrice()));
166
 
167
	}
168
 
169
}