Subversion Repositories SmartDukaan

Rev

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