Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21615 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
25651 tejbeer 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
24288 amit.gupta 6
import java.time.LocalDate;
24276 amit.gupta 7
import java.time.LocalDateTime;
24880 govind 8
import java.time.LocalTime;
25649 tejbeer 9
import java.time.ZoneOffset;
24339 amit.gupta 10
import java.util.ArrayList;
25136 amit.gupta 11
import java.util.LinkedHashMap;
23884 amit.gupta 12
import java.util.List;
24880 govind 13
import java.util.Map;
14
import java.util.stream.Collectors;
23568 govind 15
 
22086 amit.gupta 16
import javax.servlet.http.HttpServletRequest;
25221 amit.gupta 17
import javax.transaction.Transactional;
22086 amit.gupta 18
 
23786 amit.gupta 19
import org.apache.logging.log4j.LogManager;
23568 govind 20
import org.apache.logging.log4j.Logger;
22481 ashik.ali 21
import org.springframework.beans.factory.annotation.Autowired;
23379 ashik.ali 22
import org.springframework.beans.factory.annotation.Value;
25651 tejbeer 23
import org.springframework.core.io.InputStreamResource;
24
import org.springframework.http.HttpHeaders;
25
import org.springframework.http.HttpStatus;
25649 tejbeer 26
import org.springframework.http.ResponseEntity;
21615 kshitij.so 27
import org.springframework.stereotype.Controller;
22073 ashik.ali 28
import org.springframework.ui.Model;
21615 kshitij.so 29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
25649 tejbeer 31
import org.springframework.web.bind.annotation.RequestParam;
21615 kshitij.so 32
 
25649 tejbeer 33
import com.google.gson.Gson;
25677 amit.gupta 34
import com.mongodb.DBObject;
25651 tejbeer 35
import com.spice.profitmandi.common.enumuration.ContentType;
25683 tejbeer 36
import com.spice.profitmandi.common.enumuration.MessageType;
22481 ashik.ali 37
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25677 amit.gupta 38
import com.spice.profitmandi.common.model.BrandStockPrice;
25649 tejbeer 39
import com.spice.profitmandi.common.model.Notification;
24203 amit.gupta 40
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25651 tejbeer 41
import com.spice.profitmandi.common.web.util.ResponseSender;
25649 tejbeer 42
import com.spice.profitmandi.dao.Interface.Campaign;
25651 tejbeer 43
import com.spice.profitmandi.dao.entity.dtr.Document;
25649 tejbeer 44
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
45
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
22654 ashik.ali 46
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24288 amit.gupta 47
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
25214 amit.gupta 48
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
25566 tejbeer 49
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
25683 tejbeer 50
 
25649 tejbeer 51
import com.spice.profitmandi.dao.model.SimpleCampaign;
52
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
25651 tejbeer 53
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
22481 ashik.ali 54
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24996 amit.gupta 55
import com.spice.profitmandi.dao.repository.dtr.Mongo;
25649 tejbeer 56
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
57
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
58
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
24203 amit.gupta 59
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24880 govind 60
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
25649 tejbeer 61
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
24288 amit.gupta 62
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
25214 amit.gupta 63
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
25566 tejbeer 64
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
24336 amit.gupta 65
import com.spice.profitmandi.service.PartnerInvestmentService;
23844 amit.gupta 66
import com.spice.profitmandi.service.authentication.RoleManager;
25677 amit.gupta 67
import com.spice.profitmandi.service.inventory.InventoryService;
22481 ashik.ali 68
import com.spice.profitmandi.web.model.LoginDetails;
69
import com.spice.profitmandi.web.util.CookiesProcessor;
70
 
21615 kshitij.so 71
@Controller
25222 amit.gupta 72
@Transactional(rollbackOn = Throwable.class)
21615 kshitij.so 73
public class DashboardController {
23923 amit.gupta 74
 
23379 ashik.ali 75
	@Value("${web.api.host}")
76
	private String webApiHost;
23923 amit.gupta 77
 
24072 amit.gupta 78
	@Value("${web.api.scheme}")
79
	private String webApiScheme;
24288 amit.gupta 80
 
24078 amit.gupta 81
	@Value("${web.api.root}")
82
	private String webApiRoot;
83
 
23379 ashik.ali 84
	@Value("${web.api.port}")
85
	private int webApiPort;
21615 kshitij.so 86
 
22481 ashik.ali 87
	@Autowired
22927 ashik.ali 88
	private CookiesProcessor cookiesProcessor;
23923 amit.gupta 89
 
23568 govind 90
	@Autowired
25214 amit.gupta 91
	private PartnerTargetRepository partnerTargetRepository;
92
 
93
	@Autowired
25653 amit.gupta 94
	private ResponseSender<?> responseSender;
25214 amit.gupta 95
 
96
	@Autowired
23786 amit.gupta 97
	private RoleManager roleManager;
23923 amit.gupta 98
 
23838 ashik.ali 99
	@Autowired
100
	private FofoStoreRepository fofoStoreRepository;
23884 amit.gupta 101
 
102
	@Autowired
24880 govind 103
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
23923 amit.gupta 104
 
23884 amit.gupta 105
	@Autowired
24880 govind 106
	private PartnerInvestmentService partnerInvestmentService;
24288 amit.gupta 107
 
23884 amit.gupta 108
	@Autowired
25653 amit.gupta 109
	DocumentRepository documentRepository;
25683 tejbeer 110
 
25677 amit.gupta 111
	@Autowired
112
	InventoryService inventoryService;
23923 amit.gupta 113
 
23884 amit.gupta 114
	@Autowired
24203 amit.gupta 115
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
116
 
24880 govind 117
	@Autowired
118
	private FofoOrderItemRepository fofoOrderItemRepository;
119
 
120
	@Autowired
25566 tejbeer 121
	private PartnerTypeChangeService partnerTypeChangeService;
25136 amit.gupta 122
 
24996 amit.gupta 123
	@Autowired
25649 tejbeer 124
	private HygieneDataRepository hygieneDataRepository;
125
 
126
	@Autowired
127
	private UserCampaignRepository userCampaignRepository;
128
 
129
	@Autowired
130
	private UserAccountRepository userAccountRepository;
131
 
132
	@Autowired
133
	private NotificationCampaignRepository notificationCampaignRepository;
134
 
135
	@Autowired
24996 amit.gupta 136
	private Mongo mongoClient;
24880 govind 137
 
25649 tejbeer 138
	@Autowired
139
	private Gson gson;
140
 
23568 govind 141
	private static final Logger LOGGER = LogManager.getLogger(DashboardController.class);
23923 amit.gupta 142
 
25136 amit.gupta 143
	@RequestMapping(value = "/12dashboard34", method = RequestMethod.GET)
25726 amit.gupta 144
	public String dashboard1(HttpServletRequest request, Model model, @RequestParam int fofoId) throws Exception {
25740 amit.gupta 145
		boolean isAdmin = false;
146
		model.addAttribute("isAdmin", isAdmin);
147
 
148
		model.addAttribute("webApiHost", webApiHost);
149
		model.addAttribute("webApiPort", webApiPort);
150
		model.addAttribute("webApiScheme", webApiScheme);
151
		model.addAttribute("webApiRoot", webApiRoot);
152
		if (isAdmin) {
153
			return "dashboard1";
154
		} else {
155
			FofoStore fofoStore = null;
156
			try {
157
				fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
158
				if (!fofoStore.isActive()) {
159
					return "redirect:/login";
160
				}
161
 
162
				PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoStore.getId(), LocalDate.now());
163
				model.addAttribute("partnerType", partnerType);
164
				model.addAttribute("partnerTypeImage", PartnerType.imageMap.get(partnerType));
165
				model.addAttribute("fofoStore", fofoStore);
166
				model.addAttribute("partnerType", partnerType);
167
				model.addAttribute("hasGift", hasGift(fofoId));
168
				model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
169
 
170
				model.addAttribute("brandStockPrices", this.getBrandStockPrices(fofoId));
171
				model.addAttribute("salesMap", this.getSales(fofoId));
172
				// this.setInvestments
173
				//
174
				model.addAttribute("investments", this.getInvestments(fofoId));
175
				model.addAttribute("isInvestmentOk", partnerInvestmentService.isInvestmentOk(fofoId,
176
						10, ProfitMandiConstants.CUTOFF_INVESTMENT));
177
			} catch (ProfitMandiBusinessException e) {
178
				LOGGER.error("FofoStore Code not found of fofoId {}", fofoId);
179
 
180
			}
181
		}
182
 
183
		LocalDateTime currentMonthStart = LocalDateTime.now().withDayOfMonth(1);
184
		LocalDateTime currentMonthEnd = currentMonthStart.plusMonths(1).withDayOfMonth(1);
185
		double currentMonthRating = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
186
				currentMonthEnd) / 2;
187
		double lastMonthRating = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
188
				currentMonthEnd) / 2;
189
		double ratingTillDate = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
190
				currentMonthEnd) / 2;
191
		model.addAttribute("currentMonthRating", (float) Math.round(currentMonthRating * 10) / 10);
192
		model.addAttribute("lastMonthRating", (float) Math.round(lastMonthRating * 10) / 10);
193
		model.addAttribute("ratingTillDate", (float) Math.round(ratingTillDate * 10) / 10);
194
 
195
		long hygieneCount = hygieneDataRepository.selectHygieneCount(fofoId, true, currentMonthStart,
196
				currentMonthEnd);
197
 
198
		long invalidHygieneCount = hygieneDataRepository.selectHygieneCount(fofoId, false,
199
				currentMonthStart, currentMonthEnd);
200
		if (hygieneCount == 0 && invalidHygieneCount == 0) {
201
			invalidHygieneCount = 1;
202
		}
203
		model.addAttribute("hygienePercentage", (hygieneCount * 100) / (invalidHygieneCount + hygieneCount));
204
 
205
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
206
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
207
		return "12dashboard34";
25136 amit.gupta 208
	}
209
 
210
	private Map<String, Object> getInvestments(int fofoId) throws Exception {
211
		Map<String, Object> investments = new LinkedHashMap<>();
212
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 1);
25649 tejbeer 213
		LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1);
25136 amit.gupta 214
		LocalDate yesterDate = LocalDate.now().minusDays(1);
215
		PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
216
		if (yesterdayInvestment == null) {
217
			yesterdayInvestment = new PartnerDailyInvestment();
218
		}
219
 
220
		List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId,
221
				currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
222
 
25140 amit.gupta 223
		long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= 10)
25136 amit.gupta 224
				.collect(Collectors.counting());
25140 amit.gupta 225
		investments.put("today", investment.getTotalInvestment());
226
		investments.put("investment", investment);
227
		investments.put("inStock", investment.getInStockAmount());
25182 amit.gupta 228
		investments.put("minimum", investment.getMinInvestmentString());
25140 amit.gupta 229
		investments.put("short", investment.getShortPercentage());
25649 tejbeer 230
		investments.put("okDays", okInvestmentDays);
25136 amit.gupta 231
		return investments;
232
	}
233
 
25221 amit.gupta 234
	private Map<String, Object> getSales(int fofoId) {
25653 amit.gupta 235
 
25221 amit.gupta 236
		Map<String, Object> salesMap = new LinkedHashMap<>();
25140 amit.gupta 237
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
25214 amit.gupta 238
		int monthLength = LocalDate.now().lengthOfMonth();
25653 amit.gupta 239
		float todaySale = fofoOrderItemRepository.selectSumBetweenCreatedTime(fofoId, curDate,
25140 amit.gupta 240
				curDate.with(LocalTime.MAX));
25653 amit.gupta 241
		float mtdSale = fofoOrderItemRepository.selectSumBetweenCreatedTime(fofoId, curDate.withDayOfMonth(1),
242
				curDate.with(LocalTime.MAX));
243
		float lmtdSale = fofoOrderItemRepository.selectSumBetweenCreatedTime(fofoId,
244
				curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1));
25214 amit.gupta 245
 
246
		List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
247
				.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
25544 amit.gupta 248
		if (partnerTargetDetails.isEmpty()) {
25372 tejbeer 249
			partnerTargetDetails = partnerTargetRepository
25653 amit.gupta 250
					.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now().minusMonths(3));
25372 tejbeer 251
		}
25214 amit.gupta 252
 
25653 amit.gupta 253
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
25214 amit.gupta 254
 
25664 amit.gupta 255
		int currentRate = (int) (mtdSale / curDate.getDayOfMonth());
25653 amit.gupta 256
 
257
		salesMap.put("requiredType", partnerType.next());
258
		float reqdAmount = partnerTypeChangeService.getMinimumAmount(partnerType.next());
259
		int requiredRate = (int) ((reqdAmount - mtdSale) / (monthLength - curDate.getDayOfMonth()));
260
		salesMap.put("requiredRate", requiredRate);
261
		salesMap.put("requiredTypeImage", PartnerType.imageMap.get(partnerType.next()));
262
 
25140 amit.gupta 263
		salesMap.put("todaySale", todaySale);
264
		salesMap.put("mtdSale", mtdSale);
25653 amit.gupta 265
		salesMap.put("lmtdSale", lmtdSale);
266
 
267
		PartnerType currentType = partnerTypeChangeService.getPartnerTypeByAmount(currentRate * monthLength);
268
		salesMap.put("currentRate", currentRate);
269
		salesMap.put("currentType", currentType);
270
		salesMap.put("currentTypeImage", PartnerType.imageMap.get(currentType));
25136 amit.gupta 271
		return salesMap;
272
	}
273
 
25677 amit.gupta 274
	private List<BrandStockPrice> getBrandStockPrices(int fofoId) throws Exception {
275
		Map<String, BrandStockPrice> brandStockPricesMap = inventoryService.getBrandWiseStockValue(fofoId);
25175 amit.gupta 276
 
25677 amit.gupta 277
		List<DBObject> mobileBrands = mongoClient.getMongoBrands(fofoId, "", 3);
278
		List<BrandStockPrice> brandStockPrices = new ArrayList<>();
279
 
280
		mobileBrands.stream().forEach(x -> {
281
			String brand = (String) x.get("name");
282
			if (brandStockPricesMap.containsKey(brand)) {
283
				BrandStockPrice brandStockPrice = brandStockPricesMap.get(brand);
284
				brandStockPrice.setBrandUrl((String) x.get("url"));
285
				brandStockPrice.setRank(((Double) x.get("rank")).intValue());
286
				brandStockPrices.add(brandStockPrice);
287
			}
288
		});
289
 
290
		return brandStockPrices.stream().filter(x -> x.getTotalQty() > 0).sorted((x, y) -> x.getRank() - y.getRank())
291
				.collect(Collectors.toList());
292
	}
293
 
21615 kshitij.so 294
	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
25653 amit.gupta 295
	public String dashboard(HttpServletRequest request, Model model) throws Exception {
22927 ashik.ali 296
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
25180 amit.gupta 297
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
298
		model.addAttribute("isAdmin", isAdmin);
23923 amit.gupta 299
 
25274 amit.gupta 300
		model.addAttribute("webApiHost", webApiHost);
301
		model.addAttribute("webApiPort", webApiPort);
302
		model.addAttribute("webApiScheme", webApiScheme);
25544 amit.gupta 303
		model.addAttribute("webApiRoot", webApiRoot);
25183 amit.gupta 304
		if (isAdmin) {
25180 amit.gupta 305
			return "dashboard1";
306
		} else {
25740 amit.gupta 307
			FofoStore fofoStore = null;
25180 amit.gupta 308
			try {
309
				fofoStore = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
25544 amit.gupta 310
				if (!fofoStore.isActive()) {
311
					return "redirect:/login";
312
				}
25649 tejbeer 313
 
25740 amit.gupta 314
				PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoStore.getId(), LocalDate.now());
25566 tejbeer 315
				model.addAttribute("partnerType", partnerType);
25653 amit.gupta 316
				model.addAttribute("partnerTypeImage", PartnerType.imageMap.get(partnerType));
25180 amit.gupta 317
				model.addAttribute("fofoStore", fofoStore);
25566 tejbeer 318
				model.addAttribute("partnerType", partnerType);
25180 amit.gupta 319
				model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
320
				model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
321
 
25677 amit.gupta 322
				model.addAttribute("brandStockPrices", this.getBrandStockPrices(loginDetails.getFofoId()));
25180 amit.gupta 323
				model.addAttribute("salesMap", this.getSales(loginDetails.getFofoId()));
25544 amit.gupta 324
				// this.setInvestments
325
				//
25180 amit.gupta 326
				model.addAttribute("investments", this.getInvestments(loginDetails.getFofoId()));
25544 amit.gupta 327
				model.addAttribute("isInvestmentOk", partnerInvestmentService.isInvestmentOk(loginDetails.getFofoId(),
328
						10, ProfitMandiConstants.CUTOFF_INVESTMENT));
25180 amit.gupta 329
			} catch (ProfitMandiBusinessException e) {
330
				LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
331
 
332
			}
22481 ashik.ali 333
		}
25649 tejbeer 334
 
335
		LocalDateTime currentMonthStart = LocalDateTime.now().withDayOfMonth(1);
25747 amit.gupta 336
		LocalDateTime lastMonthStart = currentMonthStart.minusMonths(1);
25653 amit.gupta 337
		LocalDateTime currentMonthEnd = currentMonthStart.plusMonths(1).withDayOfMonth(1);
338
		double currentMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), currentMonthStart,
25677 amit.gupta 339
				currentMonthEnd) / 2;
25747 amit.gupta 340
		double lastMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), lastMonthStart,
341
				currentMonthStart) / 2;
342
		double ratingTillDate = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), LocalDateTime.MIN,
25677 amit.gupta 343
				currentMonthEnd) / 2;
344
		model.addAttribute("currentMonthRating", (float) Math.round(currentMonthRating * 10) / 10);
345
		model.addAttribute("lastMonthRating", (float) Math.round(lastMonthRating * 10) / 10);
346
		model.addAttribute("ratingTillDate", (float) Math.round(ratingTillDate * 10) / 10);
25649 tejbeer 347
 
25653 amit.gupta 348
		long hygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), true, currentMonthStart,
349
				currentMonthEnd);
25651 tejbeer 350
 
25653 amit.gupta 351
		long invalidHygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), false,
352
				currentMonthStart, currentMonthEnd);
353
		if (hygieneCount == 0 && invalidHygieneCount == 0) {
354
			invalidHygieneCount = 1;
355
		}
356
		model.addAttribute("hygienePercentage", (hygieneCount * 100) / (invalidHygieneCount + hygieneCount));
25651 tejbeer 357
 
25197 amit.gupta 358
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
359
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
25140 amit.gupta 360
		return "dashboard1";
21615 kshitij.so 361
	}
23923 amit.gupta 362
 
24288 amit.gupta 363
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 364
	private boolean hasGift(int fofoId) {
365
		try {
24288 amit.gupta 366
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
367
					.getAvailability() > 0;
24203 amit.gupta 368
		} catch (ProfitMandiBusinessException e) {
369
			return false;
370
		}
371
	}
24288 amit.gupta 372
 
22354 ashik.ali 373
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 374
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 375
		model.addAttribute("appContextPath", request.getContextPath());
376
		return "contact-us";
377
	}
23923 amit.gupta 378
 
25649 tejbeer 379
	@RequestMapping(value = "/notifications", method = RequestMethod.GET)
25683 tejbeer 380
	public String getNotificationsWithType(HttpServletRequest request,
381
			@RequestParam(required = false) MessageType messageType,
25649 tejbeer 382
			@RequestParam(name = "offset", defaultValue = "0") int offset,
383
			@RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
384
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
385
		int userId = userAccountRepository.selectUserIdByRetailerId(loginDetails.getFofoId());
386
		List<UserCampaign> userCampaigns = userCampaignRepository.selectByUserId(userId);
25683 tejbeer 387
 
388
		List<Notification> notifications = null;
25649 tejbeer 389
		if (!userCampaigns.isEmpty()) {
390
			List<Integer> campaignIds = new ArrayList<>();
391
			for (UserCampaign userCampaign : userCampaigns) {
392
				campaignIds.add(userCampaign.getCampaignId());
393
			}
394
			List<NotificationCampaign> notificationCampaigns = notificationCampaignRepository.selectbyCids(campaignIds);
25683 tejbeer 395
			LOGGER.info("messageType" + messageType);
396
			notifications = getNotifications(notificationCampaigns, messageType);
397
 
398
		}
399
 
400
		model.addAttribute("notifications", notifications);
401
 
402
		LOGGER.info("notifications" + notifications);
403
		return "notification-template";
404
	}
405
 
406
	public List<Notification> getNotifications(List<NotificationCampaign> nc, MessageType messageType)
407
			throws ProfitMandiBusinessException {
408
		List<Notification> notifications = new ArrayList<>();
409
		Document document = null;
410
		if (messageType != null) {
411
			for (NotificationCampaign notificationCampaign : nc) {
412
				if (notificationCampaign.getMessageType() == messageType) {
25649 tejbeer 413
					Notification ns = new Notification();
414
					SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
415
							SimpleCampaignParams.class);
416
					Campaign campaign = new SimpleCampaign(scp);
417
					LocalDateTime expire = campaign.getExpireTimestamp();
418
					ns.setCid(Integer.toString(notificationCampaign.getId()));
419
					ns.setType(campaign.getType());
420
					ns.setMessage(campaign.getMessage());
421
					ns.setTitle(campaign.getTitle());
25651 tejbeer 422
					if (notificationCampaign.getDocumentId() != null) {
423
						document = documentRepository.selectById(notificationCampaign.getDocumentId());
424
						ns.setDocumentName(document.getDisplayName());
425
					}
25683 tejbeer 426
					ns.setUrl(campaign.getUrl());
25649 tejbeer 427
					ns.setShowImage(campaign.getShowImage());
428
					ns.setImageUrl(campaign.getImageUrl());
25683 tejbeer 429
					ns.setDocumentId(notificationCampaign.getDocumentId());
430
					ns.setMessageType(notificationCampaign.getMessageType());
25649 tejbeer 431
					ns.setCreated(
432
							notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
433
									* 1000);
434
					if (LocalDateTime.now().isAfter(expire)) {
435
						ns.setExpired(true);
436
					} else {
437
						ns.setExpired(false);
438
					}
439
					notifications.add(ns);
440
				}
441
			}
25683 tejbeer 442
		} else {
443
			for (NotificationCampaign notificationCampaign : nc) {
444
 
445
				Notification ns = new Notification();
446
				SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
447
						SimpleCampaignParams.class);
448
				Campaign campaign = new SimpleCampaign(scp);
449
				LocalDateTime expire = campaign.getExpireTimestamp();
450
				ns.setCid(Integer.toString(notificationCampaign.getId()));
451
				ns.setType(campaign.getType());
452
				ns.setMessage(campaign.getMessage());
453
				ns.setTitle(campaign.getTitle());
454
				if (notificationCampaign.getDocumentId() != null) {
455
					document = documentRepository.selectById(notificationCampaign.getDocumentId());
456
					ns.setDocumentName(document.getDisplayName());
457
				}
458
				ns.setUrl(campaign.getUrl());
459
				ns.setShowImage(campaign.getShowImage());
460
				ns.setImageUrl(campaign.getImageUrl());
461
				ns.setDocumentId(notificationCampaign.getDocumentId());
462
				ns.setMessageType(notificationCampaign.getMessageType());
463
				ns.setCreated(notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
464
						* 1000);
465
				if (LocalDateTime.now().isAfter(expire)) {
466
					ns.setExpired(true);
467
				} else {
468
					ns.setExpired(false);
469
				}
470
				notifications.add(ns);
471
			}
472
 
25649 tejbeer 473
		}
25683 tejbeer 474
		return notifications;
25651 tejbeer 475
 
476
	}
25649 tejbeer 477
 
25651 tejbeer 478
	@RequestMapping(value = "/notifyDocument/documentId", method = RequestMethod.GET)
479
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
480
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId, @RequestParam int cid)
481
			throws ProfitMandiBusinessException {
482
		Document document = documentRepository.selectById(documentId);
483
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
484
		if (nc.getDocumentId() == null) {
485
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
486
		}
487
		if (nc.getDocumentId() != documentId) {
488
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
489
		}
490
		return responseSender.ok(document);
25649 tejbeer 491
	}
492
 
25651 tejbeer 493
	@RequestMapping(value = "/notifyDocument/download", method = RequestMethod.GET)
494
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, @RequestParam int cid, Model model)
495
			throws ProfitMandiBusinessException {
496
 
497
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
498
 
499
		if (nc.getDocumentId() == null) {
500
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
501
		}
502
 
503
		Document document = documentRepository.selectById(nc.getDocumentId());
504
 
505
		FileInputStream file = null;
506
		try {
507
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
508
		} catch (FileNotFoundException e) {
509
			LOGGER.error("Retailer Document file not found : ", e);
510
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
511
		}
512
		// ByteArrayOutputStream byteArrayOutputStream = new
513
		// ByteArrayOutputStream();
514
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
515
 
516
		final HttpHeaders headers = new HttpHeaders();
517
		String contentType = "";
518
		if (document.getContentType() == ContentType.JPEG) {
519
			contentType = "image/jpeg";
520
		} else if (document.getContentType() == ContentType.PNG) {
521
			contentType = "image/png";
522
		} else if (document.getContentType() == ContentType.PDF) {
523
			contentType = "application/pdf";
524
		}
525
		headers.set("Content-Type", contentType);
526
		headers.set("Content-disposition", "inline; filename=" + document.getName());
527
		headers.setContentLength(document.getSize());
528
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
529
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
530
	}
531
 
21615 kshitij.so 532
}