Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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;
43
import java.util.stream.Collectors;
44
 
29707 tejbeer 45
@Controller
46
@Transactional(rollbackFor = Throwable.class)
47
public class CustomerController {
48
 
49
	private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);
50
 
51
	@Autowired
52
	private ResponseSender<?> responseSender;
53
 
54
	@Autowired
55
	private UserAccountRepository userAccountRepository;
56
 
57
	@Autowired
58
	private CustomerRepository customerRepository;
59
 
60
	@Autowired
61
	private InventoryService inventoryService;
62
 
63
	@Autowired
64
	private CustomerService customerService;
65
 
66
	@Autowired
67
	private CustomerAddressRepository customerAddressRepository;
68
 
69
	@Autowired
70
	private FofoOrderItemRepository fofoOrderItemRepository;
71
 
72
	@Autowired
73
	private SolrService commonSolrService;
74
 
75
	@Autowired
76
	private FofoLineItemRepository fofoLineItemRepository;
77
 
78
	@Autowired
79
	private ItemRepository itemRepository;
80
 
81
	@Autowired
82
	private FofoOrderRepository fofoOrderRepository;
83
 
84
	@Autowired
85
	private CustomerOfferRepository customerOfferRepository;
86
 
87
	@Autowired
88
	private CustomerOfferItemRepository customerOfferItemRepository;
89
 
90
	@Autowired
91
	private SamsungUpgradeOfferRepository samsungUpgradeOfferRepository;
92
 
93
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
94
	@ApiImplicitParams({
95
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
96
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
97
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
98
			throws ProfitMandiBusinessException {
99
		CustomCustomer customCustomer = null;
100
		LOGGER.info("Request Received at url {}", request.getRequestURI());
101
		try {
102
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
103
			customCustomer = new CustomCustomer();
104
			customCustomer.setCustomerId(customer.getId());
105
			customCustomer.setEmailId(customer.getEmailId());
106
			customCustomer.setFirstName(customer.getFirstName());
107
			customCustomer.setLastName(customer.getLastName());
108
			customCustomer.setMobileNumber(customer.getMobileNumber());
109
			LOGGER.info(customer.getCustomerAddress());
110
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
111
					.sorted((CustomerAddress c1, CustomerAddress c2) -> {
112
						return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
113
					}).limit(5).collect(Collectors.toList());
114
			LOGGER.info(customerAddresses);
115
			if (!customerAddresses.isEmpty()) {
116
				List<CustomAddress> customAddresses = new ArrayList<>();
117
				for (CustomerAddress customerAddress : customerAddresses) {
118
					customAddresses.add(this.toCustomAddress(customerAddress));
119
				}
120
				customCustomer.setAddresses(customAddresses);
121
			}
122
		} catch (Exception e) {
123
			e.printStackTrace();
124
		}
125
		return responseSender.ok(customCustomer);
126
	}
127
 
128
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
129
		CustomAddress customAddress = new CustomAddress();
130
		customAddress.setCity(customerAddress.getCity());
131
		customAddress.setCountry(customerAddress.getCountry());
132
		customAddress.setLandmark(customerAddress.getLandmark());
133
		customAddress.setLine1(customerAddress.getLine1());
134
		customAddress.setLine2(customerAddress.getLine2());
135
		customAddress.setName(customerAddress.getName());
136
		customAddress.setLastName(customerAddress.getLastName());
137
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
138
		customAddress.setPinCode(customerAddress.getPinCode());
139
		customAddress.setState(customerAddress.getState());
140
		customAddress.setId(customerAddress.getId());
141
		return customAddress;
142
	}
143
 
144
	@RequestMapping(value = "/customer/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
145
	@ApiImplicitParams({
146
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
147
	public ResponseEntity<?> addCustomer(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
148
			throws ProfitMandiBusinessException {
149
		Customer customer = new Customer();
150
		if (StringUtils.isEmpty(customCustomer.getFirstName())) {
151
			throw new ProfitMandiBusinessException("First Name", "Empty", "First Name required");
152
		}
153
		customer.setEmailId(customCustomer.getEmailId());
154
		customer.setFirstName(customCustomer.getFirstName());
155
		customer.setLastName(customCustomer.getLastName());
156
		customer.setMobileNumber(customCustomer.getMobileNumber());
157
		customer = customerService.addCustomer(customer);
158
		customCustomer.setCustomerId(customer.getId());
159
		return responseSender.ok(customCustomer);
160
 
161
	}
162
 
163
	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
164
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
165
			@RequestBody CustomAddress customAddress) {
166
		CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
167
		customerAddressRepository.persist(customerAddress);
168
		return responseSender.ok(this.toCustomAddress(customerAddress));
169
 
170
	}
171
 
172
	private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
173
		CustomerAddress customerAddress = new CustomerAddress();
174
		customerAddress.setCustomerId(customerId);
175
		customerAddress.setName(customAddress.getName());
176
		customerAddress.setLastName(customAddress.getLastName());
177
		customerAddress.setLine1(customAddress.getLine1());
178
		customerAddress.setLine2(customAddress.getLine2());
179
		customerAddress.setLandmark(customAddress.getLandmark());
180
		customerAddress.setCity(customAddress.getCity());
181
		customerAddress.setPinCode(customAddress.getPinCode());
182
		customerAddress.setState(customAddress.getState());
183
		customerAddress.setCountry(customAddress.getCountry());
184
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
185
 
186
		return customerAddress;
187
	}
188
 
189
	@RequestMapping(value = "/customer/order", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
190
	@ApiImplicitParams({
191
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
192
	public ResponseEntity<?> getOrderInvoiceDetail(HttpServletRequest request,
193
			@RequestParam(name = "offset") int offset, @RequestParam(name = "limit") int limit) throws Exception {
194
 
195
		int userId = (int) request.getAttribute("userId");
196
		UserCart uc = userAccountRepository.getUserCart(userId);
197
 
198
		List<CustomerOrderDetail> customerOrderDetails = new ArrayList<>();
199
		List<Integer> catalogIds = new ArrayList<>();
200
		List<FofoOrder> fofoOrders = fofoOrderRepository.selectOrderByFofoId(uc.getUserId(), offset, limit);
201
 
202
		if (!fofoOrders.isEmpty()) {
203
			for (FofoOrder fo : fofoOrders) {
204
				if (fo.getCancelledTimestamp() == null) {
205
					List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectByOrderId(fo.getId());
206
 
207
					Customer customer = customerRepository.selectById(fo.getCustomerId());
208
 
209
					CustomerAddress customerAddress = customerAddressRepository.selectById(fo.getCustomerAddressId());
210
 
211
					for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
212
						Item item = itemRepository.selectById(fofoOrderItem.getItemId());
213
						fofoOrderItem.setItemName(item.getItemDescription());
214
						catalogIds.add(item.getCatalogItemId());
215
					}
216
 
217
					// Map<Integer, JSONObject> contentMap =
218
					// commonSolrService.getContentByCatalogIds(catalogIds);
219
					for (FofoOrderItem foi : fofoOrderItems) {
220
 
31050 amit.gupta 221
						List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemId(foi.getId());
29707 tejbeer 222
 
223
						CustomerOrderDetail customerOrderDetail = new CustomerOrderDetail();
224
 
31050 amit.gupta 225
						List<String> serialNumbers = fofoLineItems.stream().map(x -> x.getSerialNumber())
29707 tejbeer 226
								.collect(Collectors.toList()).stream().filter(x -> x != null)
227
								.collect(Collectors.toList());
228
 
31050 amit.gupta 229
						customerOrderDetail.setSerialNumber(serialNumbers);
29707 tejbeer 230
 
32671 ranu 231
						if(customerAddress != null) {
232
							customerOrderDetail.setCustomerName(customerAddress.getName());
233
							customerOrderDetail.setCustomerMobile(customerAddress.getPhoneNumber());
234
							customerOrderDetail.setCustomerEmail(customer.getEmailId());
235
							customerOrderDetail.setCustomerCity(customerAddress.getCity());
236
							customerOrderDetail.setCustomerState(customerAddress.getState());
237
						}
29707 tejbeer 238
 
239
						Item item = itemRepository.selectById(foi.getItemId());
240
						// JSONObject jsonObj = contentMap.get(item.getCatalogItemId());
241
						// customerOrderDetail.setImageUrl(jsonObj.getString("imageUrl_s"));
242
						customerOrderDetail.setBrand(item.getBrand());
243
						customerOrderDetail.setColor(item.getColor());
244
						customerOrderDetail.setFofoOrderItemId(foi.getId());
245
						customerOrderDetail.setFofoOrderId(foi.getOrderId());
246
						customerOrderDetail.setItemId(foi.getItemId());
247
						customerOrderDetail.setModelName(item.getModelName());
248
						customerOrderDetail.setModelNumber(item.getModelNumber());
249
						customerOrderDetail.setQuantity(foi.getQuantity());
250
						customerOrderDetail.setTotalPrice(foi.getSellingPrice());
251
						customerOrderDetail.setCreatedTimeStamp(foi.getCreateTimestamp());
252
						customerOrderDetail.setInvoiceNumber(fo.getInvoiceNumber());
253
						customerOrderDetail.setCancelledTimestamp(fo.getCancelledTimestamp());
254
						customerOrderDetail.setInsurance(false);
255
 
31050 amit.gupta 256
						if (!serialNumbers.isEmpty()) {
29707 tejbeer 257
 
32692 ranu 258
							if (item.getHsnCode().equals("85171300")) {
29707 tejbeer 259
								customerOrderDetail.setInsurance(true);
260
							} else {
261
								customerOrderDetail.setInsurance(false);
262
							}
263
						}
264
 
31147 tejbeer 265
						List<CustomerOffer> customerOffers = customerOfferRepository
32692 ranu 266
								.getCustomerOffer(LocalDate.now().atStartOfDay()).get(uc.getUserId());
29707 tejbeer 267
 
268
						long offerCount = 0;
32692 ranu 269
						if (customerOffers != null && !customerOffers.isEmpty()){
31147 tejbeer 270
							List<Integer> offerIds = customerOffers.stream().map(x -> x.getId())
271
									.collect(Collectors.toList());
29707 tejbeer 272
 
273
							List<CustomerOfferItem> customerOfferItems = customerOfferItemRepository
31147 tejbeer 274
									.selectByOfferIds(offerIds, item.getCatalogItemId(), LocalDate.now());
29707 tejbeer 275
 
276
							offerCount = customerOfferItems.stream().collect(Collectors.counting());
277
						}
278
 
279
						customerOrderDetail.setOfferCount(offerCount);
280
 
281
						customerOrderDetails.add(customerOrderDetail);
282
					}
283
				}
284
			}
285
 
286
		}
287
		return responseSender.ok(customerOrderDetails);
288
	}
289
 
290
}