Subversion Repositories SmartDukaan

Rev

Rev 25726 | Rev 25747 | 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);
25653 amit.gupta 336
		LocalDateTime currentMonthEnd = currentMonthStart.plusMonths(1).withDayOfMonth(1);
337
		double currentMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), currentMonthStart,
25677 amit.gupta 338
				currentMonthEnd) / 2;
25653 amit.gupta 339
		double lastMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), currentMonthStart,
25677 amit.gupta 340
				currentMonthEnd) / 2;
25653 amit.gupta 341
		double ratingTillDate = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), currentMonthStart,
25677 amit.gupta 342
				currentMonthEnd) / 2;
343
		model.addAttribute("currentMonthRating", (float) Math.round(currentMonthRating * 10) / 10);
344
		model.addAttribute("lastMonthRating", (float) Math.round(lastMonthRating * 10) / 10);
345
		model.addAttribute("ratingTillDate", (float) Math.round(ratingTillDate * 10) / 10);
25649 tejbeer 346
 
25653 amit.gupta 347
		long hygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), true, currentMonthStart,
348
				currentMonthEnd);
25651 tejbeer 349
 
25653 amit.gupta 350
		long invalidHygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), false,
351
				currentMonthStart, currentMonthEnd);
352
		if (hygieneCount == 0 && invalidHygieneCount == 0) {
353
			invalidHygieneCount = 1;
354
		}
355
		model.addAttribute("hygienePercentage", (hygieneCount * 100) / (invalidHygieneCount + hygieneCount));
25651 tejbeer 356
 
25197 amit.gupta 357
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
358
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
25140 amit.gupta 359
		return "dashboard1";
21615 kshitij.so 360
	}
23923 amit.gupta 361
 
24288 amit.gupta 362
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 363
	private boolean hasGift(int fofoId) {
364
		try {
24288 amit.gupta 365
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
366
					.getAvailability() > 0;
24203 amit.gupta 367
		} catch (ProfitMandiBusinessException e) {
368
			return false;
369
		}
370
	}
24288 amit.gupta 371
 
22354 ashik.ali 372
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 373
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 374
		model.addAttribute("appContextPath", request.getContextPath());
375
		return "contact-us";
376
	}
23923 amit.gupta 377
 
25649 tejbeer 378
	@RequestMapping(value = "/notifications", method = RequestMethod.GET)
25683 tejbeer 379
	public String getNotificationsWithType(HttpServletRequest request,
380
			@RequestParam(required = false) MessageType messageType,
25649 tejbeer 381
			@RequestParam(name = "offset", defaultValue = "0") int offset,
382
			@RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
383
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
384
		int userId = userAccountRepository.selectUserIdByRetailerId(loginDetails.getFofoId());
385
		List<UserCampaign> userCampaigns = userCampaignRepository.selectByUserId(userId);
25683 tejbeer 386
 
387
		List<Notification> notifications = null;
25649 tejbeer 388
		if (!userCampaigns.isEmpty()) {
389
			List<Integer> campaignIds = new ArrayList<>();
390
			for (UserCampaign userCampaign : userCampaigns) {
391
				campaignIds.add(userCampaign.getCampaignId());
392
			}
393
			List<NotificationCampaign> notificationCampaigns = notificationCampaignRepository.selectbyCids(campaignIds);
25683 tejbeer 394
			LOGGER.info("messageType" + messageType);
395
			notifications = getNotifications(notificationCampaigns, messageType);
396
 
397
		}
398
 
399
		model.addAttribute("notifications", notifications);
400
 
401
		LOGGER.info("notifications" + notifications);
402
		return "notification-template";
403
	}
404
 
405
	public List<Notification> getNotifications(List<NotificationCampaign> nc, MessageType messageType)
406
			throws ProfitMandiBusinessException {
407
		List<Notification> notifications = new ArrayList<>();
408
		Document document = null;
409
		if (messageType != null) {
410
			for (NotificationCampaign notificationCampaign : nc) {
411
				if (notificationCampaign.getMessageType() == messageType) {
25649 tejbeer 412
					Notification ns = new Notification();
413
					SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
414
							SimpleCampaignParams.class);
415
					Campaign campaign = new SimpleCampaign(scp);
416
					LocalDateTime expire = campaign.getExpireTimestamp();
417
					ns.setCid(Integer.toString(notificationCampaign.getId()));
418
					ns.setType(campaign.getType());
419
					ns.setMessage(campaign.getMessage());
420
					ns.setTitle(campaign.getTitle());
25651 tejbeer 421
					if (notificationCampaign.getDocumentId() != null) {
422
						document = documentRepository.selectById(notificationCampaign.getDocumentId());
423
						ns.setDocumentName(document.getDisplayName());
424
					}
25683 tejbeer 425
					ns.setUrl(campaign.getUrl());
25649 tejbeer 426
					ns.setShowImage(campaign.getShowImage());
427
					ns.setImageUrl(campaign.getImageUrl());
25683 tejbeer 428
					ns.setDocumentId(notificationCampaign.getDocumentId());
429
					ns.setMessageType(notificationCampaign.getMessageType());
25649 tejbeer 430
					ns.setCreated(
431
							notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
432
									* 1000);
433
					if (LocalDateTime.now().isAfter(expire)) {
434
						ns.setExpired(true);
435
					} else {
436
						ns.setExpired(false);
437
					}
438
					notifications.add(ns);
439
				}
440
			}
25683 tejbeer 441
		} else {
442
			for (NotificationCampaign notificationCampaign : nc) {
443
 
444
				Notification ns = new Notification();
445
				SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
446
						SimpleCampaignParams.class);
447
				Campaign campaign = new SimpleCampaign(scp);
448
				LocalDateTime expire = campaign.getExpireTimestamp();
449
				ns.setCid(Integer.toString(notificationCampaign.getId()));
450
				ns.setType(campaign.getType());
451
				ns.setMessage(campaign.getMessage());
452
				ns.setTitle(campaign.getTitle());
453
				if (notificationCampaign.getDocumentId() != null) {
454
					document = documentRepository.selectById(notificationCampaign.getDocumentId());
455
					ns.setDocumentName(document.getDisplayName());
456
				}
457
				ns.setUrl(campaign.getUrl());
458
				ns.setShowImage(campaign.getShowImage());
459
				ns.setImageUrl(campaign.getImageUrl());
460
				ns.setDocumentId(notificationCampaign.getDocumentId());
461
				ns.setMessageType(notificationCampaign.getMessageType());
462
				ns.setCreated(notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
463
						* 1000);
464
				if (LocalDateTime.now().isAfter(expire)) {
465
					ns.setExpired(true);
466
				} else {
467
					ns.setExpired(false);
468
				}
469
				notifications.add(ns);
470
			}
471
 
25649 tejbeer 472
		}
25683 tejbeer 473
		return notifications;
25651 tejbeer 474
 
475
	}
25649 tejbeer 476
 
25651 tejbeer 477
	@RequestMapping(value = "/notifyDocument/documentId", method = RequestMethod.GET)
478
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
479
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId, @RequestParam int cid)
480
			throws ProfitMandiBusinessException {
481
		Document document = documentRepository.selectById(documentId);
482
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
483
		if (nc.getDocumentId() == null) {
484
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
485
		}
486
		if (nc.getDocumentId() != documentId) {
487
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
488
		}
489
		return responseSender.ok(document);
25649 tejbeer 490
	}
491
 
25651 tejbeer 492
	@RequestMapping(value = "/notifyDocument/download", method = RequestMethod.GET)
493
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, @RequestParam int cid, Model model)
494
			throws ProfitMandiBusinessException {
495
 
496
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
497
 
498
		if (nc.getDocumentId() == null) {
499
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
500
		}
501
 
502
		Document document = documentRepository.selectById(nc.getDocumentId());
503
 
504
		FileInputStream file = null;
505
		try {
506
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
507
		} catch (FileNotFoundException e) {
508
			LOGGER.error("Retailer Document file not found : ", e);
509
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
510
		}
511
		// ByteArrayOutputStream byteArrayOutputStream = new
512
		// ByteArrayOutputStream();
513
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
514
 
515
		final HttpHeaders headers = new HttpHeaders();
516
		String contentType = "";
517
		if (document.getContentType() == ContentType.JPEG) {
518
			contentType = "image/jpeg";
519
		} else if (document.getContentType() == ContentType.PNG) {
520
			contentType = "image/png";
521
		} else if (document.getContentType() == ContentType.PDF) {
522
			contentType = "application/pdf";
523
		}
524
		headers.set("Content-Type", contentType);
525
		headers.set("Content-disposition", "inline; filename=" + document.getName());
526
		headers.setContentLength(document.getSize());
527
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
528
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
529
	}
530
 
21615 kshitij.so 531
}