Subversion Repositories SmartDukaan

Rev

Rev 32692 | Rev 35415 | 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());
35196 aman 110
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream().filter(c -> Boolean.TRUE.equals(c.getActive())).collect(Collectors.toList());
29707 tejbeer 111
			LOGGER.info(customerAddresses);
112
			if (!customerAddresses.isEmpty()) {
113
				List<CustomAddress> customAddresses = new ArrayList<>();
114
				for (CustomerAddress customerAddress : customerAddresses) {
115
					customAddresses.add(this.toCustomAddress(customerAddress));
116
				}
117
				customCustomer.setAddresses(customAddresses);
118
			}
119
		} catch (Exception e) {
120
			e.printStackTrace();
121
		}
122
		return responseSender.ok(customCustomer);
123
	}
124
 
125
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
126
		CustomAddress customAddress = new CustomAddress();
127
		customAddress.setCity(customerAddress.getCity());
128
		customAddress.setCountry(customerAddress.getCountry());
129
		customAddress.setLandmark(customerAddress.getLandmark());
130
		customAddress.setLine1(customerAddress.getLine1());
131
		customAddress.setLine2(customerAddress.getLine2());
132
		customAddress.setName(customerAddress.getName());
133
		customAddress.setLastName(customerAddress.getLastName());
134
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
135
		customAddress.setPinCode(customerAddress.getPinCode());
136
		customAddress.setState(customerAddress.getState());
137
		customAddress.setId(customerAddress.getId());
35196 aman 138
		customAddress.setDefaultAddress(customerAddress.getDefault());
29707 tejbeer 139
		return customAddress;
140
	}
141
 
142
	@RequestMapping(value = "/customer/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
143
	@ApiImplicitParams({
144
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
145
	public ResponseEntity<?> addCustomer(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
146
			throws ProfitMandiBusinessException {
147
		Customer customer = new Customer();
148
		if (StringUtils.isEmpty(customCustomer.getFirstName())) {
149
			throw new ProfitMandiBusinessException("First Name", "Empty", "First Name required");
150
		}
151
		customer.setEmailId(customCustomer.getEmailId());
152
		customer.setFirstName(customCustomer.getFirstName());
153
		customer.setLastName(customCustomer.getLastName());
154
		customer.setMobileNumber(customCustomer.getMobileNumber());
155
		customer = customerService.addCustomer(customer);
156
		customCustomer.setCustomerId(customer.getId());
157
		return responseSender.ok(customCustomer);
158
 
159
	}
160
 
161
	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
162
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
163
			@RequestBody CustomAddress customAddress) {
164
		CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
35196 aman 165
		customerAddress.setActive(true);
29707 tejbeer 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
 
31050 amit.gupta 220
						List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemId(foi.getId());
29707 tejbeer 221
 
222
						CustomerOrderDetail customerOrderDetail = new CustomerOrderDetail();
223
 
31050 amit.gupta 224
						List<String> serialNumbers = fofoLineItems.stream().map(x -> x.getSerialNumber())
29707 tejbeer 225
								.collect(Collectors.toList()).stream().filter(x -> x != null)
226
								.collect(Collectors.toList());
227
 
31050 amit.gupta 228
						customerOrderDetail.setSerialNumber(serialNumbers);
29707 tejbeer 229
 
32671 ranu 230
						if(customerAddress != null) {
231
							customerOrderDetail.setCustomerName(customerAddress.getName());
232
							customerOrderDetail.setCustomerMobile(customerAddress.getPhoneNumber());
233
							customerOrderDetail.setCustomerEmail(customer.getEmailId());
234
							customerOrderDetail.setCustomerCity(customerAddress.getCity());
235
							customerOrderDetail.setCustomerState(customerAddress.getState());
236
						}
29707 tejbeer 237
 
238
						Item item = itemRepository.selectById(foi.getItemId());
239
						// JSONObject jsonObj = contentMap.get(item.getCatalogItemId());
240
						// customerOrderDetail.setImageUrl(jsonObj.getString("imageUrl_s"));
241
						customerOrderDetail.setBrand(item.getBrand());
242
						customerOrderDetail.setColor(item.getColor());
243
						customerOrderDetail.setFofoOrderItemId(foi.getId());
244
						customerOrderDetail.setFofoOrderId(foi.getOrderId());
245
						customerOrderDetail.setItemId(foi.getItemId());
246
						customerOrderDetail.setModelName(item.getModelName());
247
						customerOrderDetail.setModelNumber(item.getModelNumber());
248
						customerOrderDetail.setQuantity(foi.getQuantity());
249
						customerOrderDetail.setTotalPrice(foi.getSellingPrice());
250
						customerOrderDetail.setCreatedTimeStamp(foi.getCreateTimestamp());
251
						customerOrderDetail.setInvoiceNumber(fo.getInvoiceNumber());
252
						customerOrderDetail.setCancelledTimestamp(fo.getCancelledTimestamp());
253
						customerOrderDetail.setInsurance(false);
254
 
31050 amit.gupta 255
						if (!serialNumbers.isEmpty()) {
29707 tejbeer 256
 
32692 ranu 257
							if (item.getHsnCode().equals("85171300")) {
29707 tejbeer 258
								customerOrderDetail.setInsurance(true);
259
							} else {
260
								customerOrderDetail.setInsurance(false);
261
							}
262
						}
263
 
31147 tejbeer 264
						List<CustomerOffer> customerOffers = customerOfferRepository
32692 ranu 265
								.getCustomerOffer(LocalDate.now().atStartOfDay()).get(uc.getUserId());
29707 tejbeer 266
 
267
						long offerCount = 0;
32692 ranu 268
						if (customerOffers != null && !customerOffers.isEmpty()){
31147 tejbeer 269
							List<Integer> offerIds = customerOffers.stream().map(x -> x.getId())
270
									.collect(Collectors.toList());
29707 tejbeer 271
 
272
							List<CustomerOfferItem> customerOfferItems = customerOfferItemRepository
31147 tejbeer 273
									.selectByOfferIds(offerIds, item.getCatalogItemId(), LocalDate.now());
29707 tejbeer 274
 
275
							offerCount = customerOfferItems.stream().collect(Collectors.counting());
276
						}
277
 
278
						customerOrderDetail.setOfferCount(offerCount);
279
 
280
						customerOrderDetails.add(customerOrderDetail);
281
					}
282
				}
283
			}
284
 
285
		}
286
		return responseSender.ok(customerOrderDetails);
287
	}
288
 
289
}