Subversion Repositories SmartDukaan

Rev

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