Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
25979 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
29426 manish 3
import java.io.ByteArrayInputStream;
4
import java.io.InputStream;
29266 manish 5
import java.time.LocalDate;
25979 tejbeer 6
import java.time.LocalDateTime;
7
import java.util.ArrayList;
25988 tejbeer 8
import java.util.Arrays;
27604 tejbeer 9
import java.util.HashMap;
29266 manish 10
import java.util.HashSet;
25979 tejbeer 11
import java.util.List;
12
import java.util.Map;
13
import java.util.stream.Collectors;
14
 
15
import javax.servlet.http.HttpServletRequest;
29426 manish 16
import javax.servlet.http.HttpServletResponse;
25979 tejbeer 17
import javax.transaction.Transactional;
18
 
29598 tejbeer 19
import org.apache.commons.csv.CSVRecord;
25979 tejbeer 20
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
22
import org.springframework.beans.factory.annotation.Autowired;
29426 manish 23
import org.springframework.core.io.InputStreamResource;
24
import org.springframework.http.HttpHeaders;
25
import org.springframework.http.HttpStatus;
26
import org.springframework.http.ResponseEntity;
25979 tejbeer 27
import org.springframework.stereotype.Controller;
28
import org.springframework.ui.Model;
29
import org.springframework.web.bind.annotation.RequestBody;
30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
32
import org.springframework.web.bind.annotation.RequestParam;
29426 manish 33
import org.springframework.web.bind.annotation.RequestPart;
34
import org.springframework.web.multipart.MultipartFile;
25979 tejbeer 35
 
29266 manish 36
import com.google.gson.Gson;
25988 tejbeer 37
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29266 manish 38
import com.spice.profitmandi.common.model.Legend;
39
import com.spice.profitmandi.common.model.OptionModel;
40
import com.spice.profitmandi.common.model.PieLables;
29278 manish 41
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29426 manish 42
import com.spice.profitmandi.common.util.FileUtil;
25979 tejbeer 43
import com.spice.profitmandi.dao.entity.auth.AuthUser;
44
import com.spice.profitmandi.dao.entity.user.Lead;
45
import com.spice.profitmandi.dao.entity.user.LeadActivity;
29598 tejbeer 46
import com.spice.profitmandi.dao.enumuration.dtr.LeadSource;
25979 tejbeer 47
import com.spice.profitmandi.dao.enumuration.dtr.LeadStatus;
29266 manish 48
import com.spice.profitmandi.dao.model.ChartLeadModel;
27605 tejbeer 49
import com.spice.profitmandi.dao.model.CreateLeacdActivityRequest;
25979 tejbeer 50
import com.spice.profitmandi.dao.model.CreateRefferalRequest;
29266 manish 51
import com.spice.profitmandi.dao.model.DataLeadModel;
52
import com.spice.profitmandi.dao.model.LeadStatusData;
25979 tejbeer 53
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
29266 manish 54
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
25979 tejbeer 55
import com.spice.profitmandi.dao.repository.dtr.LeadActivityRepository;
56
import com.spice.profitmandi.dao.repository.dtr.LeadRepository;
57
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
29266 manish 58
import com.spice.profitmandi.service.AuthService;
25979 tejbeer 59
import com.spice.profitmandi.web.model.LoginDetails;
60
import com.spice.profitmandi.web.util.CookiesProcessor;
61
import com.spice.profitmandi.web.util.MVCResponseSender;
62
 
63
@Controller
64
@Transactional(rollbackOn = Throwable.class)
65
public class LeadController {
66
	private static final Logger LOGGER = LogManager.getLogger(LeadController.class);
67
 
68
	@Autowired
69
	private LeadRepository leadRepository;
70
 
71
	@Autowired
72
	private LeadActivityRepository leadActivityRepository;
73
 
74
	@Autowired
75
	private StateRepository stateRepository;
76
 
77
	@Autowired
29266 manish 78
	private AuthService authService;
79
 
80
	@Autowired
25979 tejbeer 81
	private AuthRepository authRepository;
82
 
83
	@Autowired
29266 manish 84
	private Gson gson;
85
 
86
	@Autowired
25979 tejbeer 87
	private CookiesProcessor cookiesProcessor;
29426 manish 88
 
29278 manish 89
	@Autowired
90
	PositionRepository positionRepository;
25979 tejbeer 91
 
92
	@Autowired
93
	private MVCResponseSender mvcResponseSender;
94
 
25988 tejbeer 95
	List<LeadStatus> status = Arrays.asList(LeadStatus.notInterested, LeadStatus.finalized);
96
 
25979 tejbeer 97
	@RequestMapping(value = "/getOpenLead", method = RequestMethod.GET)
27609 tejbeer 98
	public String getOpenLead(HttpServletRequest request,
29426 manish 99
			@RequestParam(name = "leadStatus", required = false, defaultValue = "All") List<LeadStatus> leadStatus,
29445 manish 100
			@RequestParam(name = "color", required = false, defaultValue = "") List<String> color,
101
			@RequestParam(name = "leadDate", required = false, defaultValue = "") LocalDate leadDate, Model model)
27642 tejbeer 102
			throws Exception {
27609 tejbeer 103
 
29445 manish 104
		LocalDateTime localDateTime = null;
105
 
106
		LOGGER.info("color" + color);
107
 
108
		LOGGER.info("leadDate" + leadDate);
109
 
110
		if (leadDate != null) {
111
			localDateTime = leadDate.atStartOfDay();
112
 
113
		}
114
 
115
		if (color == null) {
116
			color.add("");
117
		}
118
 
119
		LOGGER.info("localDateTime" + localDateTime);
120
 
29266 manish 121
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
122
		String emailId = loginDetails.getEmailId();
123
		AuthUser authUser = authRepository.selectByEmailOrMobile(emailId);
29426 manish 124
 
125
		boolean authPositon = positionRepository.hasCategory(authUser.getId(),
126
				ProfitMandiConstants.TICKET_CATEGORY_SALES);
127
 
27610 tejbeer 128
		if (leadStatus.contains(LeadStatus.All)) {
129
 
29445 manish 130
			leadStatus.add(LeadStatus.pending);
131
			leadStatus.add(LeadStatus.notInterested);
132
			leadStatus.add(LeadStatus.finalized);
27610 tejbeer 133
		}
27615 tejbeer 134
 
27642 tejbeer 135
		List<Lead> leads = null;
29445 manish 136
 
137
		model.addAttribute("colors", color);
138
 
27642 tejbeer 139
		Map<Integer, LeadActivity> leadActivityMap = new HashMap<>();
29287 amit.gupta 140
		List<Integer> authUserIds = null;
27723 tejbeer 141
		if (!color.isEmpty()) {
142
			if (color.contains("All")) {
143
				color.add("yellow");
144
				color.add("green");
145
			}
29445 manish 146
 
147
			LOGGER.info("color111" + color);
148
 
29426 manish 149
			if (authPositon) {
29287 amit.gupta 150
				authUserIds = authService.getAllReportees(authUser.getId());
29284 amit.gupta 151
				authUserIds.add(authUser.getId());
29278 manish 152
				LOGGER.info("authIds" + authUserIds);
29426 manish 153
 
29445 manish 154
				if (leadStatus.contains(LeadStatus.All)) {
155
					if (localDateTime == null) {
29426 manish 156
 
29445 manish 157
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestampAndAuthIds(leadStatus,
29479 manish 158
								authUserIds, color, LocalDateTime.now().minusMonths(1));
29445 manish 159
 
160
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
161
 
162
						leads.addAll(leadFollowUp);
163
 
164
					}
165
 
166
					else {
167
 
168
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestampAndAuthIds(leadStatus,
169
								authUserIds, color, localDateTime);
170
 
171
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
172
 
173
						leads.addAll(leadFollowUp);
174
 
175
					}
176
				} else
177
 
178
				{
179
					if (localDateTime == null) {
180
 
181
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestampAndAuthIds(leadStatus,
29479 manish 182
								authUserIds, color, LocalDateTime.now().minusMonths(1));
29445 manish 183
 
184
					} else {
185
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestampAndAuthIds(leadStatus,
186
								authUserIds, color, localDateTime);
187
 
188
					}
189
 
190
				}
191
 
29278 manish 192
			}
29426 manish 193
 
29445 manish 194
			else {
27642 tejbeer 195
 
29445 manish 196
				if (leadStatus.contains(LeadStatus.All)) {
197
 
198
					if (localDateTime == null) {
199
 
200
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestamp(leadStatus, color,
29479 manish 201
								LocalDateTime.now().minusMonths(1));
29445 manish 202
 
203
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
204
						leads.addAll(leadFollowUp);
205
						LOGGER.info("leadlocalDateTime1" + leads);
206
 
207
					} else {
208
 
209
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestamp(leadStatus, color,
210
								localDateTime);
211
 
212
						LOGGER.info("leadlocalDateTime2" + leads);
213
 
214
					}
215
				}
216
 
217
				else {
218
 
219
					if (localDateTime == null) {
220
 
221
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestamp(leadStatus, color,
29479 manish 222
								LocalDateTime.now().minusMonths(1));
29445 manish 223
 
224
						LOGGER.info("leadlocalDateTime3" + leads);
225
					} else {
226
 
227
						leads = leadRepository.selectAllByColorStatusAndUpdatedTimestamp(leadStatus, color,
228
								localDateTime);
229
						LOGGER.info("leadlocalDateTime4" + leads);
230
					}
231
				}
232
			}
233
		}
234
 
235
		else {
236
 
29426 manish 237
			if (authPositon) {
29287 amit.gupta 238
				authUserIds = authService.getAllReportees(authUser.getId());
29284 amit.gupta 239
				authUserIds.add(authUser.getId());
29278 manish 240
				LOGGER.info("authIds" + authUserIds);
29445 manish 241
				if (leadStatus.contains(LeadStatus.All)) {
242
					if (localDateTime == null) {
29426 manish 243
 
29445 manish 244
						leads = leadRepository.selectAllByStatusAndUpdatedTimestampAndAuthId(leadStatus,
29479 manish 245
								LocalDateTime.now().minusMonths(1), authUserIds);
29445 manish 246
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
247
						leads.addAll(leadFollowUp);
248
						LOGGER.info("leadlocalDateTime1" + leads);
249
					}
250
 
251
					else {
252
 
253
						leads = leadRepository.selectAllByStatusAndUpdatedTimestampAndAuthId(leadStatus, localDateTime,
254
								authUserIds);
255
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
256
						leads.addAll(leadFollowUp);
257
						LOGGER.info("leadlocalDateTime1" + leads);
258
 
259
					}
260
				}
261
 
262
				else {
263
 
264
					if (localDateTime == null) {
265
						leads = leadRepository.selectAllByStatusAndUpdatedTimestampAndAuthId(leadStatus,
29479 manish 266
								LocalDateTime.now().minusMonths(1), authUserIds);
29445 manish 267
					}
268
 
269
					else {
270
						leads = leadRepository.selectAllByStatusAndUpdatedTimestampAndAuthId(leadStatus, localDateTime,
271
								authUserIds);
272
					}
273
				}
29278 manish 274
			}
29445 manish 275
 
276
			else {
277
 
278
				if (leadStatus.contains(LeadStatus.All)) {
279
 
280
					if (localDateTime == null) {
281
 
282
						leads = leadRepository.selectAllByStatusAndUpdatedTimestamp(leadStatus,
29479 manish 283
								LocalDateTime.now().minusMonths(1));
29445 manish 284
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
285
						leads.addAll(leadFollowUp);
286
						LOGGER.info("leadlocalDateTime1" + leads);
287
 
288
					}
289
 
290
					else {
291
 
292
						leads = leadRepository.selectAllByStatusAndUpdatedTimestamp(leadStatus, localDateTime);
293
						List<Lead> leadFollowUp = leadRepository.selectAllByStatus(LeadStatus.followUp);
294
						leads.addAll(leadFollowUp);
295
						LOGGER.info("leadlocalDateTime1" + leads);
296
					}
297
				}
298
 
299
				else {
300
 
301
					if (localDateTime == null) {
302
 
303
						leads = leadRepository.selectAllByStatusAndUpdatedTimestamp(leadStatus,
29479 manish 304
								LocalDateTime.now().minusMonths(1));
29445 manish 305
 
306
					}
307
 
308
					else {
309
 
310
						leads = leadRepository.selectAllByStatusAndUpdatedTimestamp(leadStatus, localDateTime);
311
					}
312
 
313
				}
314
 
315
			}
27642 tejbeer 316
			// List<Lead> leads = leadRepository.selectAllBylistStatus(leadStatus);
27652 tejbeer 317
		}
29426 manish 318
		if (authPositon && authUserIds.size() > 0) {
29287 amit.gupta 319
			List<String> leadCreators = new ArrayList<>();
320
			leadCreators.add("daily-sync");
29426 manish 321
			leadCreators.addAll(authRepository.selectAllAuthUserByIds(authUserIds).stream().map(x -> x.getFullName())
322
					.collect(Collectors.toList()));
29289 amit.gupta 323
			model.addAttribute("leadCreators", leadCreators);
29287 amit.gupta 324
		}
29266 manish 325
		List<Lead> weekLast = new ArrayList<>();
326
		List<Lead> weekThird = new ArrayList<>();
327
		List<Lead> weekSecond = new ArrayList<>();
328
		List<Lead> weekFirst = new ArrayList<>();
329
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
330
		List<LeadWeakWiseModel> lwDays = new ArrayList<>();
27642 tejbeer 331
 
29266 manish 332
		for (int i = 7; i >= 1; i--) {
333
			LocalDateTime startOfDay = curDate.minusDays(i);
334
 
335
			LeadWeakWiseModel lm = new LeadWeakWiseModel();
336
			lm.setMonth(startOfDay.toLocalDate());
337
			lwDays.add(lm);
338
		}
29426 manish 339
		if (!leads.isEmpty()) {
340
			for (Lead lead : leads) {
29266 manish 341
 
29426 manish 342
				if (lead.getCreatedTimestamp().isAfter(curDate.minusDays(28))
343
						&& lead.getCreatedTimestamp().isBefore(curDate.minusDays(21))) {
344
					weekLast.add(lead);
29266 manish 345
 
29426 manish 346
				} else if (lead.getCreatedTimestamp().isAfter(curDate.minusDays(21))
347
						&& lead.getCreatedTimestamp().isBefore(curDate.minusDays(14))) {
348
					weekThird.add(lead);
349
				} else if (lead.getCreatedTimestamp().isAfter(curDate.minusDays(14))
350
						&& lead.getCreatedTimestamp().isBefore(curDate.minusDays(7))) {
29266 manish 351
 
29426 manish 352
					weekSecond.add(lead);
353
				} else if (lead.getCreatedTimestamp().isAfter(curDate.minusDays(7))) {
354
					weekFirst.add(lead);
355
				}
356
 
29266 manish 357
			}
358
		}
359
		LOGGER.info("weekLast" + weekLast);
360
		LOGGER.info("weekThird" + weekThird);
361
		LOGGER.info("weekSecond" + weekSecond);
29426 manish 362
		Map<String, Long> leadLasts = weekLast.stream()
363
				.collect(Collectors.groupingBy(x -> x.getCreatedBy(), Collectors.counting()));
364
		Map<String, Long> leadThirds = weekThird.stream()
365
				.collect(Collectors.groupingBy(x -> x.getCreatedBy(), Collectors.counting()));
29266 manish 366
		Map<String, Long> leadSeconds = weekSecond.stream().collect(Collectors.groupingBy(x -> x.getCreatedBy(),
367
				Collectors.mapping(Lead::getCreatedBy, Collectors.counting())));
368
 
369
		Map<String, Map<LocalDate, Long>> leadFirsts = weekFirst.stream()
370
				.collect(Collectors.groupingBy(x -> x.getCreatedBy(),
371
						Collectors.groupingBy(x -> x.getCreatedTimestamp().toLocalDate(), Collectors.counting())));
372
 
373
		LOGGER.info("leadFirsts" + leadFirsts);
374
 
27723 tejbeer 375
		List<Integer> authIds = new ArrayList<>();
376
		if (!leads.isEmpty()) {
25979 tejbeer 377
 
27723 tejbeer 378
			authIds.addAll(leads.stream().map(x -> x.getAssignTo()).collect(Collectors.toList()));
27610 tejbeer 379
 
27723 tejbeer 380
			for (Lead lead : leads) {
381
				List<LeadActivity> leadActivity = leadActivityRepository.selectBYLeadId(lead.getId());
382
				if (!leadActivity.isEmpty()) {
383
					leadActivityMap.put(lead.getId(), leadActivity.get(0));
27604 tejbeer 384
				}
27723 tejbeer 385
			}
386
			Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
387
					.collect(Collectors.toMap(x -> x.getId(), x -> x));
388
			model.addAttribute("leadActivityMap", leadActivityMap);
389
			model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
390
		}
391
 
29266 manish 392
		ChartLeadModel cm = this.getLeadChart(leads);
393
		model.addAttribute("chartLead", gson.toJson(cm));
394
 
395
		LOGGER.info("ChartLeadModel" + gson.toJson(cm));
396
		LOGGER.info("lwDays" + lwDays);
397
 
27610 tejbeer 398
		LOGGER.info("leads" + leads);
25979 tejbeer 399
		List<String> stateNames = stateRepository.selectAll().stream().map(x -> x.getName())
400
				.collect(Collectors.toList());
401
		List<AuthUser> authUsers = authRepository.selectAllActiveUser();
29266 manish 402
		LocalDate lastDate = curDate.minusDays(1).toLocalDate();
29426 manish 403
 
29266 manish 404
		List<LocalDate> weekDates = new ArrayList<>();
29426 manish 405
 
29266 manish 406
		weekDates.add(curDate.minusDays(28).toLocalDate());
407
		weekDates.add(curDate.minusDays(22).toLocalDate());
408
		weekDates.add(curDate.minusDays(21).toLocalDate());
409
		weekDates.add(curDate.minusDays(15).toLocalDate());
410
		weekDates.add(curDate.minusDays(14).toLocalDate());
411
		weekDates.add(curDate.minusDays(8).toLocalDate());
412
		weekDates.add(curDate.minusDays(7).toLocalDate());
413
		LOGGER.info("lastDate" + lastDate);
29426 manish 414
 
29266 manish 415
		model.addAttribute("weekDates", weekDates);
29426 manish 416
 
29445 manish 417
		model.addAttribute("leadDate", leadDate);
29266 manish 418
		model.addAttribute("lastDate", lastDate);
25979 tejbeer 419
		model.addAttribute("stateNames", stateNames);
420
		model.addAttribute("authUsers", authUsers);
27610 tejbeer 421
		model.addAttribute("lead", leads);
422
		model.addAttribute("leadStatus", LeadStatus.values());
29266 manish 423
		model.addAttribute("leadLasts", leadLasts);
424
		model.addAttribute("leadThirds", leadThirds);
425
		model.addAttribute("leadSeconds", leadSeconds);
426
		model.addAttribute("leadFirsts", leadFirsts);
427
		model.addAttribute("lwDays", lwDays);
25979 tejbeer 428
 
27610 tejbeer 429
		model.addAttribute("selectedLeadStatus", leadStatus.get(0));
25979 tejbeer 430
		return "lead";
27642 tejbeer 431
 
25979 tejbeer 432
	}
433
 
29266 manish 434
	public ChartLeadModel getLeadChart(List<Lead> leads) throws ProfitMandiBusinessException {
435
 
436
		Map<LeadStatus, Long> leadStatusMap = leads.stream().collect(
437
				Collectors.groupingBy(x -> x.getStatus(), Collectors.mapping(Lead::getStatus, Collectors.counting())));
438
 
29426 manish 439
		Map<String, Long> hotLeadsMap = leads.stream().collect(
440
				Collectors.groupingBy(x -> x.getColor(), Collectors.mapping(Lead::getColor, Collectors.counting())));
441
 
442
		LOGGER.info("hotLeadsMap" + hotLeadsMap);
29266 manish 443
		ChartLeadModel cm = new ChartLeadModel();
444
 
445
		HashSet<LeadStatus> labels = new HashSet<LeadStatus>();
446
		labels.addAll(leadStatusMap.keySet());
29445 manish 447
 
29426 manish 448
		HashSet<String> hotLeads = new HashSet<String>();
449
		hotLeads.addAll(leadStatusMap.keySet().stream().map(x -> x.toString()).collect(Collectors.toSet()));
450
		hotLeads.add("HotLead");
29445 manish 451
 
29426 manish 452
		HashSet<String> hotLeadKeys = new HashSet<String>();
29445 manish 453
 
29426 manish 454
		hotLeadKeys.add("HotLead");
455
		List<String> hotLeadGreen = new ArrayList<>(hotLeadKeys);
29598 tejbeer 456
 
29448 manish 457
		LOGGER.info("hotLeads" + hotLeads);
29445 manish 458
 
29266 manish 459
		List<LeadStatus> labelList = new ArrayList<>(labels);
460
		List<String> backgroundColor = new ArrayList<>();
29426 manish 461
		LOGGER.info("hotLeadKeys" + hotLeadKeys);
29266 manish 462
		List<Long> values = new ArrayList<>();
463
 
29448 manish 464
		for (String hotLead : hotLeads) {
29266 manish 465
 
29448 manish 466
			if (hotLead.equals("pending")) {
29266 manish 467
				backgroundColor.add("pink");
29448 manish 468
				values.add(leadStatusMap.get(LeadStatus.pending));
29266 manish 469
			}
29448 manish 470
			if (hotLead.equals("notInterested")) {
29266 manish 471
				backgroundColor.add("red");
29448 manish 472
				values.add(leadStatusMap.get(LeadStatus.notInterested));
29266 manish 473
			}
29448 manish 474
			if (hotLead.equals("followUp")) {
29266 manish 475
				backgroundColor.add("#9ACD32");
29448 manish 476
				values.add(leadStatusMap.get(LeadStatus.followUp));
29266 manish 477
			}
29448 manish 478
			if (hotLead.equals("finalized")) {
29266 manish 479
				backgroundColor.add("blue");
29448 manish 480
				values.add(leadStatusMap.get(LeadStatus.finalized));
29445 manish 481
			}
29598 tejbeer 482
 
29448 manish 483
			if (hotLead.equals("HotLead")) {
29426 manish 484
				backgroundColor.add("green");
485
				values.add(hotLeadsMap.get("Green"));
29445 manish 486
 
29266 manish 487
			}
29445 manish 488
 
29266 manish 489
		}
490
		LOGGER.info("labelList" + labelList);
491
 
492
		LOGGER.info("backgroundColor" + backgroundColor);
493
		LOGGER.info("labelsChartLead" + labels);
494
		LeadStatusData data = new LeadStatusData();
495
		data.setData(values);
496
		data.setBackgroundColor(backgroundColor);
497
		data.setLabel("DataSet 1");
498
 
499
		PieLables label = new PieLables();
500
		label.setFontColor("black");
501
		label.setFontSize(15);
502
 
503
		Legend legend = new Legend();
504
		legend.setLabels(label);
505
		legend.setPosition("left");
506
 
507
		List<LeadStatusData> dataList = new ArrayList<>();
508
		dataList.add(data);
509
 
510
		DataLeadModel datasets = new DataLeadModel();
511
		datasets.setDatasets(dataList);
29426 manish 512
		datasets.setLabels(hotLeads);
29266 manish 513
 
514
		OptionModel om = new OptionModel();
515
		om.setLegend(legend);
516
 
517
		cm.setType("pie");
518
		cm.setData(datasets);
519
		cm.setOptions(om);
520
 
521
		return cm;
522
	}
523
 
25988 tejbeer 524
	@RequestMapping(value = "/getClosedLead", method = RequestMethod.GET)
525
	public String getClosedLead(HttpServletRequest request,
526
			@RequestParam(name = "offset", defaultValue = "0") int offset,
527
			@RequestParam(name = "limit", defaultValue = "10") int limit,
528
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
529
			throws Exception {
530
		List<AuthUser> authUsers = authRepository.selectAllActiveUser();
531
		List<Lead> leads = null;
532
		long size = 0;
533
		leads = leadRepository.selectAllByStatus(status, offset, limit);
534
		size = leadRepository.selectCountByStatus(status);
535
 
536
		if (!leads.isEmpty()) {
537
			List<Integer> authIds = new ArrayList<>();
538
			for (Lead lead : leads) {
539
				authIds.add(lead.getAssignTo());
540
			}
541
			Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
542
					.collect(Collectors.toMap(x -> x.getId(), x -> x));
543
 
544
			model.addAttribute("leads", leads);
545
			model.addAttribute("start", offset + 1);
546
			model.addAttribute("size", size);
547
			model.addAttribute("searchTerm", searchTerm);
548
			model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
25990 tejbeer 549
			model.addAttribute("url", "/getPaginatedClosedLeads");
25988 tejbeer 550
 
551
			if (leads.size() < limit) {
552
				model.addAttribute("end", offset + leads.size());
553
			} else {
554
				model.addAttribute("end", offset + limit);
555
			}
556
 
557
		} else {
558
			model.addAttribute("lead", leads);
559
 
560
			model.addAttribute("size", size);
561
		}
562
 
563
		model.addAttribute("authUsers", authUsers);
564
		return "lead-close";
565
	}
566
 
567
	@RequestMapping(value = "/getPaginatedClosedLeads", method = RequestMethod.GET)
25990 tejbeer 568
	public String getPaginatedClosedLeads(HttpServletRequest request,
25988 tejbeer 569
			@RequestParam(name = "offset", defaultValue = "0") int offset,
570
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
571
			throws ProfitMandiBusinessException {
572
		List<Lead> leads = null;
573
		leads = leadRepository.selectAllByStatus(status, offset, limit);
574
 
575
		if (!leads.isEmpty()) {
576
			List<Integer> authIds = new ArrayList<>();
577
			for (Lead lead : leads) {
578
				authIds.add(lead.getAssignTo());
579
			}
580
			Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
581
					.collect(Collectors.toMap(x -> x.getId(), x -> x));
582
			model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
583
			model.addAttribute("leads", leads);
25990 tejbeer 584
			model.addAttribute("url", "/getPaginatedClosedLeads");
25988 tejbeer 585
 
586
		} else {
587
			model.addAttribute("leads", leads);
588
 
589
		}
590
		return "lead-close-paginated";
591
	}
592
 
593
	@RequestMapping(value = "/searchLeads")
594
	public String getClosedLeads(HttpServletRequest request,
595
			@RequestParam(name = "offset", defaultValue = "0") int offset,
596
			@RequestParam(name = "limit", defaultValue = "10") int limit,
597
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
598
			throws ProfitMandiBusinessException {
599
		List<AuthUser> authUsers = authRepository.selectAllActiveUser();
600
		List<Lead> leads = null;
601
		long size = 0;
602
		if (!(searchTerm.equals(""))) {
603
			leads = leadRepository.selectBySearchTerm(status, searchTerm, offset, limit);
604
			if (!(leads.size() == 0)) {
605
				size = leadRepository.selectCountByStatus(status);
606
				List<Integer> authIds = new ArrayList<>();
607
				for (Lead lead : leads) {
608
					authIds.add(lead.getAssignTo());
609
				}
610
				Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
611
						.collect(Collectors.toMap(x -> x.getId(), x -> x));
612
				model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
613
				model.addAttribute("leads", leads);
614
				model.addAttribute("start", offset + 1);
615
				model.addAttribute("size", size);
616
				model.addAttribute("searchTerm", searchTerm);
617
 
618
				if (leads.size() < limit) {
619
					model.addAttribute("end", offset + leads.size());
620
				} else {
621
					model.addAttribute("end", offset + limit);
622
				}
623
			} else {
624
				throw new ProfitMandiBusinessException("lead", searchTerm, "leads Not Found");
625
			}
626
		} else {
627
			leads = leadRepository.selectAllByStatus(status, offset, limit);
628
			size = leadRepository.selectCountByStatus(status);
629
 
630
			if (!leads.isEmpty()) {
631
				List<Integer> authIds = new ArrayList<>();
632
				for (Lead lead : leads) {
633
					authIds.add(lead.getAssignTo());
634
				}
635
				Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
636
						.collect(Collectors.toMap(x -> x.getId(), x -> x));
637
 
638
				model.addAttribute("leads", leads);
639
				model.addAttribute("start", offset + 1);
640
				model.addAttribute("size", size);
641
				model.addAttribute("searchTerm", searchTerm);
642
				model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
643
 
644
				if (leads.size() < limit) {
645
					model.addAttribute("end", offset + leads.size());
646
				} else {
647
					model.addAttribute("end", offset + limit);
648
				}
649
 
650
			}
651
		}
652
 
653
		model.addAttribute("authUsers", authUsers);
654
		return "lead-close";
655
	}
656
 
657
	@RequestMapping(value = "/searchLeadPaginated")
25990 tejbeer 658
	public String searchLeadPaginated(HttpServletRequest request,
25988 tejbeer 659
			@RequestParam(name = "offset", defaultValue = "0") int offset,
660
			@RequestParam(name = "limit", defaultValue = "10") int limit,
661
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
662
			throws ProfitMandiBusinessException {
663
		LOGGER.info("In search Item....");
664
		List<Lead> leads = null;
665
		if (!searchTerm.equals("")) {
666
			leads = leadRepository.selectBySearchTerm(status, searchTerm, offset, limit);
667
			if (!(leads.size() == 0)) {
668
 
669
				List<Integer> authIds = new ArrayList<>();
670
				for (Lead lead : leads) {
671
					authIds.add(lead.getAssignTo());
672
				}
673
				Map<Integer, AuthUser> authIdAndAuthUserMap = authRepository.selectAllAuthUserByIds(authIds).stream()
674
						.collect(Collectors.toMap(x -> x.getId(), x -> x));
675
				model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
676
				model.addAttribute("leads", leads);
677
			}
678
		}
679
 
680
		return "lead-close-paginated";
681
 
682
	}
683
 
25979 tejbeer 684
	@RequestMapping(value = "/getLeadActivity", method = RequestMethod.GET)
685
	public String getLeadActivity(HttpServletRequest request, @RequestParam int leadId, Model model) throws Exception {
686
		List<LeadActivity> leadActivity = leadActivityRepository.selectBYLeadId(leadId);
687
		model.addAttribute("leadActivity", leadActivity);
688
		return "lead_activity_modal";
689
	}
690
 
691
	@RequestMapping(value = "/createLead", method = RequestMethod.POST)
692
	public String CreateLead(HttpServletRequest request, @RequestBody CreateRefferalRequest createRefferalRequest,
693
			Model model) throws Exception {
694
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
695
		LOGGER.info("createRefferalRequest" + createRefferalRequest.getSchelduleTimestamp());
696
		Lead lead = new Lead();
697
		lead.setFirstName(createRefferalRequest.getFirstName());
698
		lead.setLastName(createRefferalRequest.getLastName());
699
		lead.setLeadMobile(createRefferalRequest.getMobile());
700
		lead.setState(createRefferalRequest.getState());
701
		lead.setCity(createRefferalRequest.getCity());
702
		lead.setAddress(createRefferalRequest.getAddress());
703
		lead.setCreatedTimestamp(LocalDateTime.now());
704
		lead.setUpdatedTimestamp(LocalDateTime.now());
705
		lead.setStatus(createRefferalRequest.getStatus());
706
		lead.setAssignTo(createRefferalRequest.getAssignTo());
707
		lead.setSource(createRefferalRequest.getSource());
26298 tejbeer 708
		lead.setColor("yellow");
25979 tejbeer 709
		// change
25989 tejbeer 710
		AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
25979 tejbeer 711
		String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
712
		lead.setCreatedBy(authUserName);
713
		lead.setAuthId(authUser.getId());
714
 
715
		leadRepository.persist(lead);
716
		LeadActivity leadActivity = new LeadActivity();
717
		leadActivity.setLeadId(lead.getId());
718
		leadActivity.setRemark(createRefferalRequest.getRemark());
719
 
720
		if (createRefferalRequest.getStatus() == LeadStatus.followUp) {
721
			leadActivity.setSchelduleTimestamp(createRefferalRequest.getSchelduleTimestamp());
722
		} else {
723
			leadActivity.setSchelduleTimestamp(null);
724
		}
725
		leadActivity.setCreatedTimestamp(LocalDateTime.now());
726
		leadActivityRepository.persist(leadActivity);
727
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
728
		return "response";
729
 
730
	}
731
 
732
	@RequestMapping(value = "/editLead", method = RequestMethod.POST)
27605 tejbeer 733
	public String EditLead(HttpServletRequest request,
734
			@RequestBody CreateLeacdActivityRequest createLeadActivityRequest, Model model) throws Exception {
25979 tejbeer 735
 
736
		LeadActivity leadActivity = new LeadActivity();
27605 tejbeer 737
		leadActivity.setLeadId(createLeadActivityRequest.getId());
738
		leadActivity.setRemark(createLeadActivityRequest.getRemark());
29598 tejbeer 739
 
29476 manish 740
		LOGGER.info("createRefferalRequest" + createLeadActivityRequest);
27605 tejbeer 741
 
742
		if (createLeadActivityRequest.getStatus() == LeadStatus.followUp) {
743
			leadActivity.setSchelduleTimestamp(createLeadActivityRequest.getScheldule());
744
		} else {
745
			leadActivity.setSchelduleTimestamp(null);
746
		}
25979 tejbeer 747
		leadActivity.setCreatedTimestamp(LocalDateTime.now());
748
		leadActivityRepository.persist(leadActivity);
29598 tejbeer 749
		Lead lead = leadRepository.selectById(createLeadActivityRequest.getId());
27605 tejbeer 750
		lead.setAssignTo(createLeadActivityRequest.getAssignTo());
751
		lead.setStatus(createLeadActivityRequest.getStatus());
28200 tejbeer 752
		lead.setNotinterestedReason(createLeadActivityRequest.getReason());
25979 tejbeer 753
		lead.setUpdatedTimestamp(LocalDateTime.now());
754
 
29473 manish 755
		lead = leadRepository.selectById(createLeadActivityRequest.getId());
29598 tejbeer 756
 
29476 manish 757
		int authId = 0;
758
		if (lead != null) {
25979 tejbeer 759
 
29476 manish 760
			authId = lead.getAssignTo();
761
			Map<Integer, LeadActivity> leadActivityMap = new HashMap<>();
29598 tejbeer 762
 
763
			List<LeadActivity> leadActivitys = leadActivityRepository.selectBYLeadId(lead.getId());
764
 
765
			if (!leadActivitys.isEmpty()) {
766
				leadActivityMap.put(lead.getId(), leadActivitys.get(0));
767
			}
768
 
769
			Map<Integer, AuthUser> authIdAndAuthUserMap = new HashMap<>();
770
			AuthUser AuthUser = authRepository.selectById(authId);
771
 
772
			authIdAndAuthUserMap.put(AuthUser.getId(), AuthUser);
773
 
29476 manish 774
			model.addAttribute("leadActivityMap", leadActivityMap);
775
			model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
776
		}
777
 
29473 manish 778
		model.addAttribute("request", lead);
779
 
780
		return "edit-lead";
781
 
25979 tejbeer 782
	}
29445 manish 783
 
29426 manish 784
	@RequestMapping(value = "/downloadIvoryLead", method = RequestMethod.GET)
785
	public ResponseEntity<?> downloadDelayDayTemplate(HttpServletRequest request) throws Exception {
29598 tejbeer 786
		List<String> stateNames = stateRepository.selectAll().stream().map(x -> x.getName())
787
				.collect(Collectors.toList());
788
 
29426 manish 789
		List<List<?>> rows = new ArrayList<>();
790
 
29598 tejbeer 791
		List<LeadSource> lss = LeadSource.enumValues;
29426 manish 792
 
29598 tejbeer 793
		for (LeadSource ls : lss) {
794
			rows.add(Arrays.asList("-", "-", "-", "-", "-", "-", "-", ls));
795
 
796
		}
797
		for (String stateName : stateNames) {
798
			rows.add(Arrays.asList("-", "-", "-", "-", stateName, "-", "-", "-"));
799
 
800
		}
801
 
802
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("First Name",
803
				"Last Name", "Address", "City", "State", "Mobile", "Assign To(email)", "Source"), rows);
804
 
29426 manish 805
		final HttpHeaders headers = new HttpHeaders();
806
		headers.set("Content-Type", "text/csv");
29598 tejbeer 807
		headers.set("Content-disposition", "inline; filename=leads.format.csv");
29426 manish 808
		headers.setContentLength(baos.toByteArray().length);
809
 
810
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
811
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
812
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
813
 
814
	}
29445 manish 815
 
29426 manish 816
	@RequestMapping(value = "/csvFileAndSetLead", method = RequestMethod.POST)
817
	public String readCsvFileAndSetLead(HttpServletRequest request, Model model, HttpServletResponse response,
818
			@RequestPart MultipartFile file) throws Throwable {
819
 
29598 tejbeer 820
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
821
		String email = loginDetails.getEmailId();
822
 
823
		AuthUser createdBy = authRepository.selectByEmailOrMobile(email);
824
 
29426 manish 825
		LOGGER.info("file" + file.toString());
826
 
827
		String fileName = file.getName();
828
 
829
		String fileNames = file.getOriginalFilename();
830
 
831
		LOGGER.info("fileName" + fileName);
832
		LOGGER.info("fileNames" + fileNames);
833
 
29598 tejbeer 834
		List<CSVRecord> records = FileUtil.readFile(file);
29426 manish 835
 
29598 tejbeer 836
		for (CSVRecord record : records) {
29426 manish 837
 
838
			Lead lead = new Lead();
29598 tejbeer 839
			lead.setFirstName(record.get(0));
840
			lead.setLastName(record.get(1));
841
			lead.setAddress(record.get(2));
842
			lead.setCity(record.get(3));
843
			lead.setState(record.get(4));
844
			lead.setLeadMobile(record.get(5));
845
			LOGGER.info("record" + record.get(6));
29426 manish 846
 
29598 tejbeer 847
			AuthUser assignTo = authRepository.selectByEmailOrMobile(record.get(6));
29426 manish 848
 
29598 tejbeer 849
			if (assignTo == null) {
850
				throw new ProfitMandiBusinessException("Assign To ", record.get(6), "email id not exist");
851
			}
29445 manish 852
 
29598 tejbeer 853
			lead.setAssignTo(assignTo.getId());
854
			lead.setSource(record.get(7));
855
 
856
			lead.setCreatedTimestamp(LocalDateTime.now());
857
 
29426 manish 858
			lead.setUpdatedTimestamp(LocalDateTime.now());
859
			lead.setStatus(LeadStatus.pending);
860
			lead.setColor("yellow");
861
			// change
29598 tejbeer 862
			lead.setCreatedBy(createdBy.getFullName());
863
			lead.setAuthId(createdBy.getId());
29426 manish 864
 
865
			leadRepository.persist(lead);
866
 
867
			LeadActivity leadActivity = new LeadActivity();
868
			leadActivity.setLeadId(lead.getId());
29598 tejbeer 869
			leadActivity.setRemark("New Lead");
29426 manish 870
			leadActivity.setSchelduleTimestamp(null);
871
 
872
			leadActivity.setCreatedTimestamp(LocalDateTime.now());
873
			leadActivityRepository.persist(leadActivity);
874
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
875
 
876
		}
877
 
878
		model.addAttribute("responseSTG", mvcResponseSender.createResponseString(true));
879
 
880
		return "response";
881
 
882
	}
883
 
25979 tejbeer 884
}