Subversion Repositories SmartDukaan

Rev

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