Subversion Repositories SmartDukaan

Rev

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