| 29707 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.common.model.CustomAddress;
|
|
|
5 |
import com.spice.profitmandi.common.model.CustomCustomer;
|
|
|
6 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
7 |
import com.spice.profitmandi.common.solr.SolrService;
|
|
|
8 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 31147 |
tejbeer |
9 |
import com.spice.profitmandi.dao.entity.catalog.CustomerOffer;
|
| 29707 |
tejbeer |
10 |
import com.spice.profitmandi.dao.entity.catalog.CustomerOfferItem;
|
|
|
11 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 31048 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.fofo.*;
|
| 29707 |
tejbeer |
13 |
import com.spice.profitmandi.dao.model.CustomerOrderDetail;
|
|
|
14 |
import com.spice.profitmandi.dao.model.UserCart;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferItemRepository;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferRepository;
|
|
|
17 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
18 |
import com.spice.profitmandi.dao.repository.catalog.SamsungUpgradeOfferRepository;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 31048 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.repository.fofo.*;
|
| 29707 |
tejbeer |
21 |
import com.spice.profitmandi.service.CustomerService;
|
|
|
22 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
23 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
24 |
import io.swagger.annotations.ApiImplicitParams;
|
| 31048 |
amit.gupta |
25 |
import org.apache.logging.log4j.LogManager;
|
|
|
26 |
import org.apache.logging.log4j.Logger;
|
|
|
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
28 |
import org.springframework.http.MediaType;
|
|
|
29 |
import org.springframework.http.ResponseEntity;
|
|
|
30 |
import org.springframework.stereotype.Controller;
|
|
|
31 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
32 |
import org.springframework.util.StringUtils;
|
|
|
33 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
34 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
35 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
36 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 29707 |
tejbeer |
37 |
|
| 31048 |
amit.gupta |
38 |
import javax.servlet.http.HttpServletRequest;
|
|
|
39 |
import java.time.LocalDate;
|
|
|
40 |
import java.time.LocalDateTime;
|
|
|
41 |
import java.util.ArrayList;
|
|
|
42 |
import java.util.List;
|
| 35415 |
amit |
43 |
import java.util.Map;
|
|
|
44 |
import java.util.Set;
|
|
|
45 |
import java.util.HashSet;
|
|
|
46 |
import java.util.function.Function;
|
| 31048 |
amit.gupta |
47 |
import java.util.stream.Collectors;
|
|
|
48 |
|
| 29707 |
tejbeer |
49 |
@Controller
|
|
|
50 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
51 |
public class CustomerController {
|
|
|
52 |
|
|
|
53 |
private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private ResponseSender<?> responseSender;
|
|
|
57 |
|
|
|
58 |
@Autowired
|
|
|
59 |
private UserAccountRepository userAccountRepository;
|
|
|
60 |
|
|
|
61 |
@Autowired
|
|
|
62 |
private CustomerRepository customerRepository;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
|
|
65 |
private InventoryService inventoryService;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
|
|
68 |
private CustomerService customerService;
|
|
|
69 |
|
|
|
70 |
@Autowired
|
|
|
71 |
private CustomerAddressRepository customerAddressRepository;
|
|
|
72 |
|
|
|
73 |
@Autowired
|
|
|
74 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
|
|
77 |
private SolrService commonSolrService;
|
|
|
78 |
|
|
|
79 |
@Autowired
|
|
|
80 |
private FofoLineItemRepository fofoLineItemRepository;
|
|
|
81 |
|
|
|
82 |
@Autowired
|
|
|
83 |
private ItemRepository itemRepository;
|
|
|
84 |
|
|
|
85 |
@Autowired
|
|
|
86 |
private FofoOrderRepository fofoOrderRepository;
|
|
|
87 |
|
|
|
88 |
@Autowired
|
|
|
89 |
private CustomerOfferRepository customerOfferRepository;
|
|
|
90 |
|
|
|
91 |
@Autowired
|
|
|
92 |
private CustomerOfferItemRepository customerOfferItemRepository;
|
|
|
93 |
|
|
|
94 |
@Autowired
|
|
|
95 |
private SamsungUpgradeOfferRepository samsungUpgradeOfferRepository;
|
|
|
96 |
|
|
|
97 |
@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
98 |
@ApiImplicitParams({
|
|
|
99 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
100 |
public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
|
|
|
101 |
@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
|
|
|
102 |
throws ProfitMandiBusinessException {
|
|
|
103 |
CustomCustomer customCustomer = null;
|
|
|
104 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
|
|
105 |
try {
|
|
|
106 |
Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
|
|
|
107 |
customCustomer = new CustomCustomer();
|
|
|
108 |
customCustomer.setCustomerId(customer.getId());
|
|
|
109 |
customCustomer.setEmailId(customer.getEmailId());
|
|
|
110 |
customCustomer.setFirstName(customer.getFirstName());
|
|
|
111 |
customCustomer.setLastName(customer.getLastName());
|
|
|
112 |
customCustomer.setMobileNumber(customer.getMobileNumber());
|
|
|
113 |
LOGGER.info(customer.getCustomerAddress());
|
| 35196 |
aman |
114 |
List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream().filter(c -> Boolean.TRUE.equals(c.getActive())).collect(Collectors.toList());
|
| 29707 |
tejbeer |
115 |
LOGGER.info(customerAddresses);
|
|
|
116 |
if (!customerAddresses.isEmpty()) {
|
|
|
117 |
List<CustomAddress> customAddresses = new ArrayList<>();
|
|
|
118 |
for (CustomerAddress customerAddress : customerAddresses) {
|
|
|
119 |
customAddresses.add(this.toCustomAddress(customerAddress));
|
|
|
120 |
}
|
|
|
121 |
customCustomer.setAddresses(customAddresses);
|
|
|
122 |
}
|
|
|
123 |
} catch (Exception e) {
|
|
|
124 |
e.printStackTrace();
|
|
|
125 |
}
|
|
|
126 |
return responseSender.ok(customCustomer);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
|
|
|
130 |
CustomAddress customAddress = new CustomAddress();
|
|
|
131 |
customAddress.setCity(customerAddress.getCity());
|
|
|
132 |
customAddress.setCountry(customerAddress.getCountry());
|
|
|
133 |
customAddress.setLandmark(customerAddress.getLandmark());
|
|
|
134 |
customAddress.setLine1(customerAddress.getLine1());
|
|
|
135 |
customAddress.setLine2(customerAddress.getLine2());
|
|
|
136 |
customAddress.setName(customerAddress.getName());
|
|
|
137 |
customAddress.setLastName(customerAddress.getLastName());
|
|
|
138 |
customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
|
|
|
139 |
customAddress.setPinCode(customerAddress.getPinCode());
|
|
|
140 |
customAddress.setState(customerAddress.getState());
|
|
|
141 |
customAddress.setId(customerAddress.getId());
|
| 35196 |
aman |
142 |
customAddress.setDefaultAddress(customerAddress.getDefault());
|
| 29707 |
tejbeer |
143 |
return customAddress;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
@RequestMapping(value = "/customer/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
147 |
@ApiImplicitParams({
|
|
|
148 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
149 |
public ResponseEntity<?> addCustomer(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
|
|
|
150 |
throws ProfitMandiBusinessException {
|
|
|
151 |
Customer customer = new Customer();
|
|
|
152 |
if (StringUtils.isEmpty(customCustomer.getFirstName())) {
|
|
|
153 |
throw new ProfitMandiBusinessException("First Name", "Empty", "First Name required");
|
|
|
154 |
}
|
|
|
155 |
customer.setEmailId(customCustomer.getEmailId());
|
|
|
156 |
customer.setFirstName(customCustomer.getFirstName());
|
|
|
157 |
customer.setLastName(customCustomer.getLastName());
|
|
|
158 |
customer.setMobileNumber(customCustomer.getMobileNumber());
|
|
|
159 |
customer = customerService.addCustomer(customer);
|
|
|
160 |
customCustomer.setCustomerId(customer.getId());
|
|
|
161 |
return responseSender.ok(customCustomer);
|
|
|
162 |
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
|
|
|
166 |
public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
|
|
|
167 |
@RequestBody CustomAddress customAddress) {
|
|
|
168 |
CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
|
| 35196 |
aman |
169 |
customerAddress.setActive(true);
|
| 29707 |
tejbeer |
170 |
customerAddressRepository.persist(customerAddress);
|
|
|
171 |
return responseSender.ok(this.toCustomAddress(customerAddress));
|
|
|
172 |
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
|
|
|
176 |
CustomerAddress customerAddress = new CustomerAddress();
|
|
|
177 |
customerAddress.setCustomerId(customerId);
|
|
|
178 |
customerAddress.setName(customAddress.getName());
|
|
|
179 |
customerAddress.setLastName(customAddress.getLastName());
|
|
|
180 |
customerAddress.setLine1(customAddress.getLine1());
|
|
|
181 |
customerAddress.setLine2(customAddress.getLine2());
|
|
|
182 |
customerAddress.setLandmark(customAddress.getLandmark());
|
|
|
183 |
customerAddress.setCity(customAddress.getCity());
|
|
|
184 |
customerAddress.setPinCode(customAddress.getPinCode());
|
|
|
185 |
customerAddress.setState(customAddress.getState());
|
|
|
186 |
customerAddress.setCountry(customAddress.getCountry());
|
|
|
187 |
customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
|
|
|
188 |
|
|
|
189 |
return customerAddress;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
@RequestMapping(value = "/customer/order", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
193 |
@ApiImplicitParams({
|
|
|
194 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
195 |
public ResponseEntity<?> getOrderInvoiceDetail(HttpServletRequest request,
|
|
|
196 |
@RequestParam(name = "offset") int offset, @RequestParam(name = "limit") int limit) throws Exception {
|
|
|
197 |
|
|
|
198 |
int userId = (int) request.getAttribute("userId");
|
|
|
199 |
UserCart uc = userAccountRepository.getUserCart(userId);
|
|
|
200 |
|
|
|
201 |
List<CustomerOrderDetail> customerOrderDetails = new ArrayList<>();
|
|
|
202 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectOrderByFofoId(uc.getUserId(), offset, limit);
|
|
|
203 |
|
| 35415 |
amit |
204 |
if (fofoOrders.isEmpty()) {
|
|
|
205 |
return responseSender.ok(customerOrderDetails);
|
|
|
206 |
}
|
| 29707 |
tejbeer |
207 |
|
| 35415 |
amit |
208 |
// Filter out cancelled orders
|
|
|
209 |
List<FofoOrder> activeOrders = fofoOrders.stream()
|
|
|
210 |
.filter(fo -> fo.getCancelledTimestamp() == null)
|
|
|
211 |
.collect(Collectors.toList());
|
| 29707 |
tejbeer |
212 |
|
| 35415 |
amit |
213 |
if (activeOrders.isEmpty()) {
|
|
|
214 |
return responseSender.ok(customerOrderDetails);
|
|
|
215 |
}
|
| 29707 |
tejbeer |
216 |
|
| 35415 |
amit |
217 |
// Batch fetch all order items for all orders at once
|
|
|
218 |
List<Integer> orderIds = activeOrders.stream().map(FofoOrder::getId).collect(Collectors.toList());
|
|
|
219 |
List<FofoOrderItem> allOrderItems = fofoOrderItemRepository.selectByOrderIds(orderIds);
|
|
|
220 |
Map<Integer, List<FofoOrderItem>> orderItemsByOrderId = allOrderItems.stream()
|
|
|
221 |
.collect(Collectors.groupingBy(FofoOrderItem::getOrderId));
|
| 29707 |
tejbeer |
222 |
|
| 35415 |
amit |
223 |
// Batch fetch all customers
|
|
|
224 |
List<Integer> customerIds = activeOrders.stream().map(FofoOrder::getCustomerId).distinct().collect(Collectors.toList());
|
|
|
225 |
Map<Integer, Customer> customerMap = customerRepository.selectAllByIds(customerIds).stream()
|
|
|
226 |
.collect(Collectors.toMap(Customer::getId, Function.identity()));
|
| 29707 |
tejbeer |
227 |
|
| 35415 |
amit |
228 |
// Batch fetch all customer addresses
|
|
|
229 |
List<Integer> addressIds = activeOrders.stream().map(FofoOrder::getCustomerAddressId).distinct().collect(Collectors.toList());
|
|
|
230 |
Map<Integer, CustomerAddress> addressMap = customerAddressRepository.selectAllByIds(addressIds).stream()
|
|
|
231 |
.collect(Collectors.toMap(CustomerAddress::getId, Function.identity()));
|
| 29707 |
tejbeer |
232 |
|
| 35415 |
amit |
233 |
// Batch fetch all items
|
|
|
234 |
Set<Integer> itemIds = allOrderItems.stream().map(FofoOrderItem::getItemId).collect(Collectors.toSet());
|
|
|
235 |
Map<Integer, Item> itemMap = itemRepository.selectByIds(new ArrayList<>(itemIds)).stream()
|
|
|
236 |
.collect(Collectors.toMap(Item::getId, Function.identity()));
|
| 29707 |
tejbeer |
237 |
|
| 35415 |
amit |
238 |
// Batch fetch all line items
|
|
|
239 |
Set<Integer> orderItemIds = allOrderItems.stream().map(FofoOrderItem::getId).collect(Collectors.toSet());
|
|
|
240 |
List<FofoLineItem> allLineItems = fofoLineItemRepository.selectByFofoOrderItemIds(orderItemIds);
|
|
|
241 |
Map<Integer, List<FofoLineItem>> lineItemsByOrderItemId = allLineItems.stream()
|
|
|
242 |
.collect(Collectors.groupingBy(FofoLineItem::getFofoOrderItemId));
|
| 29707 |
tejbeer |
243 |
|
| 35415 |
amit |
244 |
// Fetch customer offers once outside the loops
|
|
|
245 |
Map<Integer, List<CustomerOffer>> customerOffersMap = customerOfferRepository.getCustomerOffer(LocalDate.now().atStartOfDay());
|
|
|
246 |
List<CustomerOffer> customerOffers = customerOffersMap.get(uc.getUserId());
|
|
|
247 |
List<Integer> offerIds = (customerOffers != null && !customerOffers.isEmpty())
|
|
|
248 |
? customerOffers.stream().map(CustomerOffer::getId).collect(Collectors.toList())
|
|
|
249 |
: new ArrayList<>();
|
| 29707 |
tejbeer |
250 |
|
| 35415 |
amit |
251 |
// Process orders using pre-fetched data
|
|
|
252 |
for (FofoOrder fo : activeOrders) {
|
|
|
253 |
List<FofoOrderItem> fofoOrderItems = orderItemsByOrderId.getOrDefault(fo.getId(), new ArrayList<>());
|
|
|
254 |
Customer customer = customerMap.get(fo.getCustomerId());
|
|
|
255 |
CustomerAddress customerAddress = addressMap.get(fo.getCustomerAddressId());
|
| 29707 |
tejbeer |
256 |
|
| 35415 |
amit |
257 |
for (FofoOrderItem foi : fofoOrderItems) {
|
|
|
258 |
Item item = itemMap.get(foi.getItemId());
|
|
|
259 |
if (item != null) {
|
|
|
260 |
foi.setItemName(item.getItemDescription());
|
|
|
261 |
}
|
| 29707 |
tejbeer |
262 |
|
| 35415 |
amit |
263 |
List<FofoLineItem> fofoLineItems = lineItemsByOrderItemId.getOrDefault(foi.getId(), new ArrayList<>());
|
| 29707 |
tejbeer |
264 |
|
| 35415 |
amit |
265 |
CustomerOrderDetail customerOrderDetail = new CustomerOrderDetail();
|
| 29707 |
tejbeer |
266 |
|
| 35415 |
amit |
267 |
List<String> serialNumbers = fofoLineItems.stream()
|
|
|
268 |
.map(FofoLineItem::getSerialNumber)
|
|
|
269 |
.filter(x -> x != null)
|
|
|
270 |
.collect(Collectors.toList());
|
| 29707 |
tejbeer |
271 |
|
| 35415 |
amit |
272 |
customerOrderDetail.setSerialNumber(serialNumbers);
|
| 29707 |
tejbeer |
273 |
|
| 35415 |
amit |
274 |
if (customerAddress != null) {
|
|
|
275 |
customerOrderDetail.setCustomerName(customerAddress.getName());
|
|
|
276 |
customerOrderDetail.setCustomerMobile(customerAddress.getPhoneNumber());
|
|
|
277 |
customerOrderDetail.setCustomerEmail(customer != null ? customer.getEmailId() : null);
|
|
|
278 |
customerOrderDetail.setCustomerCity(customerAddress.getCity());
|
|
|
279 |
customerOrderDetail.setCustomerState(customerAddress.getState());
|
|
|
280 |
}
|
| 29707 |
tejbeer |
281 |
|
| 35415 |
amit |
282 |
if (item != null) {
|
|
|
283 |
customerOrderDetail.setBrand(item.getBrand());
|
|
|
284 |
customerOrderDetail.setColor(item.getColor());
|
|
|
285 |
customerOrderDetail.setModelName(item.getModelName());
|
|
|
286 |
customerOrderDetail.setModelNumber(item.getModelNumber());
|
|
|
287 |
}
|
|
|
288 |
customerOrderDetail.setFofoOrderItemId(foi.getId());
|
|
|
289 |
customerOrderDetail.setFofoOrderId(foi.getOrderId());
|
|
|
290 |
customerOrderDetail.setItemId(foi.getItemId());
|
|
|
291 |
customerOrderDetail.setQuantity(foi.getQuantity());
|
|
|
292 |
customerOrderDetail.setTotalPrice(foi.getSellingPrice());
|
|
|
293 |
customerOrderDetail.setCreatedTimeStamp(foi.getCreateTimestamp());
|
|
|
294 |
customerOrderDetail.setInvoiceNumber(fo.getInvoiceNumber());
|
|
|
295 |
customerOrderDetail.setCancelledTimestamp(fo.getCancelledTimestamp());
|
|
|
296 |
customerOrderDetail.setInsurance(false);
|
| 29707 |
tejbeer |
297 |
|
| 35415 |
amit |
298 |
if (!serialNumbers.isEmpty() && item != null) {
|
|
|
299 |
if ("85171300".equals(item.getHsnCode())) {
|
|
|
300 |
customerOrderDetail.setInsurance(true);
|
| 29707 |
tejbeer |
301 |
}
|
|
|
302 |
}
|
| 35415 |
amit |
303 |
|
|
|
304 |
long offerCount = 0;
|
|
|
305 |
if (!offerIds.isEmpty() && item != null) {
|
|
|
306 |
List<CustomerOfferItem> customerOfferItems = customerOfferItemRepository
|
|
|
307 |
.selectByOfferIds(offerIds, item.getCatalogItemId(), LocalDate.now());
|
|
|
308 |
offerCount = customerOfferItems.size();
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
customerOrderDetail.setOfferCount(offerCount);
|
|
|
312 |
customerOrderDetails.add(customerOrderDetail);
|
| 29707 |
tejbeer |
313 |
}
|
| 35415 |
amit |
314 |
}
|
| 29707 |
tejbeer |
315 |
|
|
|
316 |
return responseSender.ok(customerOrderDetails);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
}
|