| 26522 |
amit.gupta |
1 |
package com.spice.profitmandi.dao.cart;
|
|
|
2 |
|
| 28166 |
tejbeer |
3 |
import java.time.LocalDateTime;
|
| 26522 |
amit.gupta |
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.Arrays;
|
|
|
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.HashSet;
|
|
|
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
|
|
10 |
import java.util.Set;
|
|
|
11 |
import java.util.stream.Collectors;
|
|
|
12 |
|
| 28653 |
amit.gupta |
13 |
import org.apache.logging.log4j.LogManager;
|
|
|
14 |
import org.apache.logging.log4j.Logger;
|
| 26522 |
amit.gupta |
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 28170 |
tejbeer |
16 |
import org.springframework.stereotype.Component;
|
| 26522 |
amit.gupta |
17 |
|
| 28653 |
amit.gupta |
18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29261 |
amit.gupta |
19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 26522 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
|
|
22 |
import com.spice.profitmandi.dao.entity.user.Cart;
|
| 28166 |
tejbeer |
23 |
import com.spice.profitmandi.dao.entity.user.CartLine;
|
| 26522 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.entity.user.User;
|
| 28653 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.model.CartItem;
|
|
|
26 |
import com.spice.profitmandi.dao.model.CartItemResponseModel;
|
|
|
27 |
import com.spice.profitmandi.dao.model.CartMessage;
|
|
|
28 |
import com.spice.profitmandi.dao.model.CartResponse;
|
| 26522 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
|
|
31 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
| 28166 |
tejbeer |
33 |
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
|
| 26522 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.user.CartRepository;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 28653 |
amit.gupta |
36 |
import com.spice.profitmandi.service.inventory.SaholicInventoryService;
|
| 26522 |
amit.gupta |
37 |
|
| 28170 |
tejbeer |
38 |
@Component
|
| 26522 |
amit.gupta |
39 |
public class CartServiceImpl implements CartService {
|
|
|
40 |
|
|
|
41 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
|
|
42 |
|
| 28653 |
amit.gupta |
43 |
private static final Logger logger = LogManager.getLogger(CartServiceImpl.class);
|
|
|
44 |
|
| 26522 |
amit.gupta |
45 |
@Autowired
|
|
|
46 |
CartRepository cartRepository;
|
|
|
47 |
|
|
|
48 |
@Autowired
|
| 28653 |
amit.gupta |
49 |
CartLineRepository cartLineRepository;
|
| 26522 |
amit.gupta |
50 |
|
|
|
51 |
@Autowired
|
|
|
52 |
FofoStoreRepository fofoStoreRepository;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
|
|
55 |
TagListingRepository tagListingRepository;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
UserRepository saholicUserRepository;
|
|
|
59 |
|
|
|
60 |
@Autowired
|
|
|
61 |
ItemRepository itemRepository;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
| 28653 |
amit.gupta |
64 |
SaholicInventoryService saholicInventoryService;
|
| 28170 |
tejbeer |
65 |
|
| 28166 |
tejbeer |
66 |
@Autowired
|
| 28653 |
amit.gupta |
67 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
| 26522 |
amit.gupta |
68 |
|
|
|
69 |
@Override
|
| 29261 |
amit.gupta |
70 |
//Unused method
|
| 26522 |
amit.gupta |
71 |
public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 28653 |
amit.gupta |
72 |
CartLine cartItem = cartLineRepository.selectByCartItem(cartId, itemId);
|
| 26522 |
amit.gupta |
73 |
if (cartItem == null) {
|
|
|
74 |
this.createCartItem(cartId, itemId, quantity, sellingPrice);
|
|
|
75 |
} else {
|
|
|
76 |
cartItem.setQuantity(quantity);
|
|
|
77 |
cartItem.setActualPrice(sellingPrice);
|
|
|
78 |
}
|
|
|
79 |
return null;
|
|
|
80 |
}
|
| 29261 |
amit.gupta |
81 |
|
| 26522 |
amit.gupta |
82 |
private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 28653 |
amit.gupta |
83 |
CartLine cartLine = new CartLine();
|
|
|
84 |
cartLine.setActualPrice(sellingPrice);
|
|
|
85 |
cartLine.setCartId(cartId);
|
|
|
86 |
cartLine.setQuantity(quantity);
|
| 26522 |
amit.gupta |
87 |
// TODO: Estmiate Logic
|
| 28653 |
amit.gupta |
88 |
cartLine.setEstimate(2);
|
|
|
89 |
cartLineRepository.persist(cartLine);
|
| 26522 |
amit.gupta |
90 |
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
@Override
|
| 28653 |
amit.gupta |
94 |
public boolean clearCart(int cartId) throws ProfitMandiBusinessException {
|
|
|
95 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
96 |
cartLines.stream().forEach(cartLine -> cartLineRepository.delete(cartLine));
|
|
|
97 |
Cart cart = cartRepository.selectById(cartId);
|
| 26522 |
amit.gupta |
98 |
|
| 28653 |
amit.gupta |
99 |
cart.setUpdateTimestamp(LocalDateTime.now());
|
|
|
100 |
cart.setCheckoutTimestamp(null);
|
|
|
101 |
cart.setTotalPrice(0);
|
|
|
102 |
cart.setWalletAmount(0);
|
|
|
103 |
return true;
|
| 26522 |
amit.gupta |
104 |
}
|
|
|
105 |
|
|
|
106 |
@Override
|
|
|
107 |
public CartModel getCart(int cartId) {
|
|
|
108 |
// TODO Auto-generated method stub
|
|
|
109 |
return null;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
113 |
private Map<Integer, Integer> getAvailabilityMap(User user, Set<Integer> itemIds) {
|
|
|
114 |
Map<Integer, Integer> itemAvailabilityMap = new HashMap<>();
|
|
|
115 |
List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
116 |
tags.stream().forEach(tagListing -> {
|
|
|
117 |
if (!tagListing.isActive() || tagListing.isHotDeals()) {
|
|
|
118 |
// show actual values here
|
|
|
119 |
} else {
|
|
|
120 |
itemAvailabilityMap.put(tagListing.getItemId(), 100);
|
|
|
121 |
}
|
|
|
122 |
});
|
|
|
123 |
return itemAvailabilityMap;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
127 |
private Map<Integer, Integer> getMinBuyQtyMap(User user, Set<Integer> itemIds) {
|
|
|
128 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
129 |
return items.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getCategoryId() == 10020 ? 1 : 10));
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
133 |
private Map<Integer, Float> getPriceMap(User user, Set<Integer> itemIds) {
|
|
|
134 |
List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
135 |
return tags.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getSellingPrice()));
|
|
|
136 |
|
|
|
137 |
}
|
|
|
138 |
|
| 28166 |
tejbeer |
139 |
@Override
|
| 28653 |
amit.gupta |
140 |
public CartResponse getCartValidation(int cartId) throws ProfitMandiBusinessException {
|
|
|
141 |
/*
|
|
|
142 |
* # No need to validate duplicate items since there are only two ways # to add
|
|
|
143 |
* items to a cart and both of them check whether the item being # added is a
|
|
|
144 |
* duplicate of an already existing item.
|
|
|
145 |
*/
|
|
|
146 |
|
|
|
147 |
Cart cart = cartRepository.selectById(cartId);
|
|
|
148 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
149 |
|
|
|
150 |
User saholicUser = saholicUserRepository.selectByCartId(cartId);
|
|
|
151 |
|
|
|
152 |
int totalQty = 0;
|
|
|
153 |
double totalAmount = 0;
|
|
|
154 |
CartResponse cartResponse = new CartResponse();
|
|
|
155 |
List<CartMessage> cartMessages = new ArrayList<>();
|
|
|
156 |
List<CartItemResponseModel> cartItemModels = new ArrayList<>();
|
|
|
157 |
cartResponse.setCartMessages(cartMessages);
|
|
|
158 |
cartResponse.setCartItems(cartItemModels);
|
|
|
159 |
|
|
|
160 |
int cartMessageChanged = 0;
|
|
|
161 |
int cartMessageOOS = 0;
|
|
|
162 |
int cartMessageUndeliverable = 0;
|
|
|
163 |
|
|
|
164 |
Set<Integer> itemIds = cartLines.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
165 |
logger.info("iteme ids -- {}", itemIds);
|
|
|
166 |
List<TagListing> tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
167 |
Map<Integer, TagListing> tagListingsMap = tagListings.stream()
|
|
|
168 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
|
|
169 |
|
|
|
170 |
cart.setTotalPrice(0);
|
|
|
171 |
int nonAccessoryQuantity = 0;
|
|
|
172 |
for (CartLine cartLine : cartLines) {
|
|
|
173 |
boolean itemQuantityChanged = false;
|
|
|
174 |
CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
|
|
|
175 |
int oldEstimate = cartLine.getEstimate();
|
|
|
176 |
int itemId = cartLine.getItemId();
|
|
|
177 |
Item item = null;
|
|
|
178 |
try {
|
|
|
179 |
item = itemRepository.selectById(itemId);
|
|
|
180 |
cartItemResponseModel.setTitle(item.getItemDescriptionNoColor());
|
|
|
181 |
} catch (ProfitMandiBusinessException e) {
|
|
|
182 |
e.printStackTrace();
|
|
|
183 |
logger.info("Error finding item with id {}", itemId);
|
|
|
184 |
cartLineRepository.delete(cartLine);
|
|
|
185 |
continue;
|
|
|
186 |
}
|
|
|
187 |
cartItemResponseModel.setItemId(itemId);
|
|
|
188 |
cartItemResponseModel.setQuantity(0);
|
|
|
189 |
cartItemResponseModel.setColor(item.getColor());
|
|
|
190 |
List<CartItemMessage> cartItemMessages = new ArrayList<>();
|
|
|
191 |
cartItemResponseModel.setCartItemMessages(cartItemMessages);
|
|
|
192 |
cartItemResponseModel.setCatalogItemId(item.getCatalogItemId());
|
|
|
193 |
cartItemResponseModel.setMinBuyQuantity(1);
|
|
|
194 |
cartItemResponseModel.setQuantityStep(1);
|
|
|
195 |
cartItemResponseModel.setMaxQuantity(0);
|
|
|
196 |
|
|
|
197 |
/*
|
|
|
198 |
* int netAvailability = itemAvailabilityMap.get(itemId).stream()
|
|
|
199 |
* .collect(Collectors.summingInt(x -> x.getNetavailability()));
|
|
|
200 |
*/
|
|
|
201 |
int availability = 30;
|
|
|
202 |
if (availability < cartLine.getQuantity()) {
|
|
|
203 |
cartLine.setQuantity(availability);
|
|
|
204 |
itemQuantityChanged = true;
|
|
|
205 |
}
|
|
|
206 |
cartItemResponseModel.setMaxQuantity(Math.max(availability, 100));
|
|
|
207 |
if (availability < cartItemResponseModel.getMinBuyQuantity()) {
|
|
|
208 |
cartItemResponseModel.setMinBuyQuantity(availability);
|
|
|
209 |
}
|
|
|
210 |
if (cartLine.getQuantity() < cartItemResponseModel.getMinBuyQuantity()) {
|
|
|
211 |
itemQuantityChanged = true;
|
|
|
212 |
cartLine.setQuantity(cartItemResponseModel.getMinBuyQuantity());
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
if (cartLine.getQuantity() > cartItemResponseModel.getMaxQuantity()) {
|
|
|
216 |
itemQuantityChanged = true;
|
|
|
217 |
cartLine.setQuantity(cartItemResponseModel.getMaxQuantity());
|
|
|
218 |
}
|
|
|
219 |
cartLine.setActualPrice(tagListingsMap.get(itemId).getSellingPrice());
|
|
|
220 |
if (item.getCategoryId() == 10006 || item.getCategoryId() == 10010) {
|
|
|
221 |
nonAccessoryQuantity += cartItemResponseModel.getQuantity();
|
|
|
222 |
cartItemResponseModel.setCategoryName("mobile");
|
|
|
223 |
}
|
|
|
224 |
cartItemResponseModel.setSellingPrice(cartLine.getActualPrice());
|
|
|
225 |
|
|
|
226 |
if (availability > 0) {
|
|
|
227 |
cart.setTotalPrice(cart.getTotalPrice() + (cartLine.getActualPrice() * cartLine.getQuantity()));
|
|
|
228 |
cartItemResponseModel.setQuantity(cartLine.getQuantity());
|
|
|
229 |
cartItemResponseModel.setEstimate(2);
|
|
|
230 |
if (itemQuantityChanged) {
|
|
|
231 |
cartMessageChanged += 1;
|
|
|
232 |
if (oldEstimate != cartItemResponseModel.getEstimate()) {
|
|
|
233 |
cartLine.setEstimate(cartItemResponseModel.getEstimate());
|
|
|
234 |
cart.setUpdateTimestamp(LocalDateTime.now());
|
|
|
235 |
totalAmount += cartLine.getActualPrice() * cartLine.getQuantity();
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
} else {
|
|
|
239 |
cartItemResponseModel.setQuantity(0);
|
|
|
240 |
cartMessageOOS += 1;
|
|
|
241 |
CartItemMessage cartItemMessage = new CartItemMessage();
|
|
|
242 |
cartItemMessage.setType("danger");
|
|
|
243 |
cartItemMessage.setMessageText("Out of Stock");
|
|
|
244 |
cartItemMessages.add(cartItemMessage);
|
|
|
245 |
cartLineRepository.delete(cartLine);
|
|
|
246 |
}
|
|
|
247 |
totalQty += cartItemResponseModel.getQuantity();
|
|
|
248 |
|
|
|
249 |
if (cartItemMessages.size() > 0) {
|
|
|
250 |
cartResponse.getCartItems().add(0, cartItemResponseModel);
|
|
|
251 |
} else {
|
|
|
252 |
cartResponse.getCartItems().add(cartItemResponseModel);
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
if (cart.getCheckoutTimestamp() != null) {
|
|
|
256 |
if (cart.getUpdateTimestamp().isBefore(cart.getCheckoutTimestamp())) {
|
|
|
257 |
cart.setCheckoutTimestamp(null);
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
cartResponse.setTotalQty(totalQty);
|
|
|
261 |
cartResponse.setTotalAmount(totalAmount);
|
|
|
262 |
cartResponse.setNonAccessoryQuantity(nonAccessoryQuantity);
|
|
|
263 |
cartResponse.setShippingCharge(0);
|
|
|
264 |
cartResponse.setCartMessageChanged(cartMessageChanged);
|
|
|
265 |
cartResponse.setCartMessageOOS(cartMessageOOS);
|
|
|
266 |
cartResponse.setCartMessageUndeliverable(cartMessageUndeliverable);
|
|
|
267 |
cartResponse.setCod(false);
|
|
|
268 |
return cartResponse;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
@Override
|
|
|
272 |
public void addItemsToCart(int cartId, List<CartItem> cartItems) throws ProfitMandiBusinessException {
|
|
|
273 |
logger.info("cart items {}", cartItems);
|
|
|
274 |
cartItems = cartItems.stream().filter(x -> x.getQuantity() > 0).collect(Collectors.toList());
|
|
|
275 |
Map<Integer, Integer> itemQuantityMap = cartItems.stream()
|
|
|
276 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
|
|
|
277 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
278 |
// Delete cartLines with 0 values
|
|
|
279 |
for (CartLine cartLine : cartLines) {
|
|
|
280 |
Integer quantity = itemQuantityMap.get(cartLine.getItemId());
|
|
|
281 |
if (quantity == null || quantity.intValue() == 0) {
|
|
|
282 |
cartLineRepository.delete(cartLine);
|
|
|
283 |
} else {
|
|
|
284 |
cartLine.setQuantity(itemQuantityMap.get(cartLine.getItemId()));
|
|
|
285 |
cartLine.setUpdateTimestapm(LocalDateTime.now());
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
for (CartItem cartItem : cartItems) {
|
|
|
289 |
if (cartLineRepository.selectByCartItem(cartId, cartItem.getItemId()) == null) {
|
|
|
290 |
CartLine cartLineNew = new CartLine();
|
|
|
291 |
cartLineNew.setItemId(cartItem.getItemId());
|
|
|
292 |
cartLineNew.setQuantity(cartItem.getQuantity());
|
|
|
293 |
cartLineNew.setCartId(cartId);
|
|
|
294 |
cartLineNew.setCreateTimestamp(LocalDateTime.now());
|
|
|
295 |
cartLineRepository.persist(cartLineNew);
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
@Override
|
|
|
302 |
public void addAddressToCart(int cartId, long addressId) throws ProfitMandiBusinessException {
|
|
|
303 |
Cart cart = cartRepository.selectById(cartId);
|
|
|
304 |
cart.setAddressId((int) addressId);
|
|
|
305 |
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
@Override
|
| 28166 |
tejbeer |
309 |
public void addShoppingBag(int cartId, long qty) {
|
|
|
310 |
CartLine cl = new CartLine();
|
| 29261 |
amit.gupta |
311 |
cl.setItemId(ProfitMandiConstants.ITEM_CARRY_BAG);
|
| 28166 |
tejbeer |
312 |
cl.setQuantity((int) qty);
|
|
|
313 |
cl.setCartId(cartId);
|
|
|
314 |
cl.setCreateTimestamp(LocalDateTime.now());
|
|
|
315 |
cl.setDataProtectionAmount(0);
|
|
|
316 |
cl.setEstimate(0);
|
| 29261 |
amit.gupta |
317 |
cl.setInsuranceAmount(0f);
|
| 28166 |
tejbeer |
318 |
cl.setDataProtectionInsurer(0);
|
| 29261 |
amit.gupta |
319 |
cl.setActualPrice(0.01f);
|
| 28166 |
tejbeer |
320 |
cartLineRepository.persist(cl);
|
|
|
321 |
|
|
|
322 |
}
|
|
|
323 |
|
| 28653 |
amit.gupta |
324 |
@Override
|
|
|
325 |
public CartModel removeFromCart(int cartId, int itemId) {
|
|
|
326 |
// TODO Auto-generated method stub
|
|
|
327 |
return null;
|
|
|
328 |
}
|
|
|
329 |
|
| 26522 |
amit.gupta |
330 |
}
|