Subversion Repositories SmartDukaan

Rev

Rev 28166 | Rev 28653 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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