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