Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
27391 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
29900 amit.gupta 3
import java.awt.image.BufferedImage;
27876 amit.gupta 4
import java.io.ByteArrayInputStream;
29900 amit.gupta 5
import java.io.File;
6
import java.io.FileNotFoundException;
27876 amit.gupta 7
import java.io.InputStream;
29900 amit.gupta 8
import java.time.Instant;
27876 amit.gupta 9
import java.time.LocalDate;
27391 tejbeer 10
import java.time.LocalDateTime;
27876 amit.gupta 11
import java.time.YearMonth;
27391 tejbeer 12
import java.util.ArrayList;
27876 amit.gupta 13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.Comparator;
29900 amit.gupta 16
import java.util.HashMap;
27391 tejbeer 17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.stream.Collectors;
21
 
29900 amit.gupta 22
import javax.imageio.ImageIO;
27391 tejbeer 23
import javax.servlet.http.HttpServletRequest;
24
import javax.transaction.Transactional;
25
 
29900 amit.gupta 26
import org.apache.commons.io.FileUtils;
27876 amit.gupta 27
import org.apache.commons.io.output.ByteArrayOutputStream;
27391 tejbeer 28
import org.apache.logging.log4j.LogManager;
29
import org.apache.logging.log4j.Logger;
29900 amit.gupta 30
import org.apache.velocity.app.VelocityEngine;
27391 tejbeer 31
import org.springframework.beans.factory.annotation.Autowired;
29809 amit.gupta 32
import org.springframework.cache.CacheManager;
27876 amit.gupta 33
import org.springframework.core.io.InputStreamResource;
34
import org.springframework.http.HttpHeaders;
35
import org.springframework.http.HttpStatus;
36
import org.springframework.http.ResponseEntity;
27391 tejbeer 37
import org.springframework.stereotype.Controller;
38
import org.springframework.ui.Model;
29900 amit.gupta 39
import org.springframework.ui.velocity.VelocityEngineUtils;
27876 amit.gupta 40
import org.springframework.web.bind.annotation.PathVariable;
29682 amit.gupta 41
import org.springframework.web.bind.annotation.PostMapping;
27391 tejbeer 42
import org.springframework.web.bind.annotation.RequestBody;
43
import org.springframework.web.bind.annotation.RequestMapping;
44
import org.springframework.web.bind.annotation.RequestMethod;
45
import org.springframework.web.bind.annotation.RequestParam;
29682 amit.gupta 46
import org.springframework.web.bind.annotation.RequestPart;
47
import org.springframework.web.multipart.MultipartFile;
29900 amit.gupta 48
import org.xhtmlrenderer.simple.Graphics2DRenderer;
27391 tejbeer 49
 
50
import com.google.gson.Gson;
29900 amit.gupta 51
import com.jcraft.jsch.ChannelSftp;
52
import com.jcraft.jsch.JSch;
53
import com.jcraft.jsch.JSchException;
54
import com.jcraft.jsch.Session;
55
import com.jcraft.jsch.SftpATTRS;
56
import com.jcraft.jsch.SftpException;
57
import com.spice.profitmandi.common.enumuration.MessageType;
27391 tejbeer 58
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
59
import com.spice.profitmandi.common.model.CustomRetailer;
27876 amit.gupta 60
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29900 amit.gupta 61
import com.spice.profitmandi.common.model.SendNotificationModel;
27876 amit.gupta 62
import com.spice.profitmandi.common.util.FileUtil;
29904 amit.gupta 63
import com.spice.profitmandi.common.util.FormattingUtils;
27876 amit.gupta 64
import com.spice.profitmandi.common.web.util.ResponseSender;
27391 tejbeer 65
import com.spice.profitmandi.dao.entity.catalog.Offer;
66
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
67
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
68
import com.spice.profitmandi.dao.model.CreateOfferRequest;
27876 amit.gupta 69
import com.spice.profitmandi.dao.model.OfferRowModel;
27391 tejbeer 70
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
71
import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;
72
import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;
73
import com.spice.profitmandi.dao.repository.catalog.OfferRepository;
74
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
75
import com.spice.profitmandi.dao.repository.dtr.Mongo;
76
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
29900 amit.gupta 77
import com.spice.profitmandi.service.NotificationService;
29785 amit.gupta 78
import com.spice.profitmandi.service.authentication.RoleManager;
27876 amit.gupta 79
import com.spice.profitmandi.service.offers.OfferService;
27391 tejbeer 80
import com.spice.profitmandi.service.user.RetailerService;
81
import com.spice.profitmandi.web.model.LoginDetails;
82
import com.spice.profitmandi.web.util.CookiesProcessor;
83
import com.spice.profitmandi.web.util.MVCResponseSender;
84
 
85
@Controller
86
@Transactional(rollbackOn = Throwable.class)
87
public class OfferController {
88
	private static final Logger LOGGER = LogManager.getLogger(OfferController.class);
89
	@Autowired
90
	private OfferRepository offerRepository;
91
 
92
	@Autowired
93
	private OfferMarginRepository offerMarginRepository;
94
 
95
	@Autowired
96
	private FofoStoreRepository fofoStoreRepository;
97
 
98
	@Autowired
27876 amit.gupta 99
	private ResponseSender responseSender;
100
 
101
	@Autowired
27391 tejbeer 102
	private OfferPartnerRepository offerPartnerRepository;
103
 
29900 amit.gupta 104
	private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
105
	private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
106
 
27391 tejbeer 107
	@Autowired
108
	private ItemRepository itemRepository;
109
 
110
	@Autowired
111
	private MVCResponseSender mvcResponseSender;
112
 
113
	@Autowired
114
	private Gson gson;
115
 
116
	@Autowired
117
	private RetailerService retailerService;
118
 
119
	@Autowired
120
	private Mongo mongoClient;
121
 
122
	@Autowired
29900 amit.gupta 123
	private NotificationService notificationService;
124
 
125
	@Autowired
27391 tejbeer 126
	private CookiesProcessor cookiesProcessor;
127
 
128
	@Autowired
27876 amit.gupta 129
	private OfferService offerService;
29900 amit.gupta 130
 
29809 amit.gupta 131
	@Autowired
132
	private CacheManager thirtyMinsTimeOutCacheManager;
29900 amit.gupta 133
 
29855 amit.gupta 134
	@Autowired
135
	private CacheManager oneDayCacheManager;
27876 amit.gupta 136
 
137
	@Autowired
27391 tejbeer 138
	private PartnerTypeChangeService partnerTypeChangeService;
139
 
140
	@RequestMapping(value = "/getCreateOffer", method = RequestMethod.GET)
141
	public String getCreateOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
142
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
143
		List<Integer> fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
144
				.collect(Collectors.toList());
145
 
27876 amit.gupta 146
		Set<String> brands = mongoClient.getMongoBrands(loginDetails.getFofoId(), null, 3).stream()
147
				.map(x -> (String) x.get("name")).collect(Collectors.toSet());
27391 tejbeer 148
 
149
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
150
 
151
		model.addAttribute("customRetailersMap", customRetailersMap);
152
		model.addAttribute("itemCriteriaType", ItemCriteriaType.values());
153
		model.addAttribute("brands", brands);
154
		model.addAttribute("partnerCategories", PartnerType.values());
27876 amit.gupta 155
		model.addAttribute("warehouseRegion", ProfitMandiConstants.WAREHOUSE_MAP);
27391 tejbeer 156
		return "scheme_offer";
157
 
158
	}
159
 
160
	@RequestMapping(value = "/createOffer", method = RequestMethod.POST)
161
	public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
162
			Model model) throws Exception {
27876 amit.gupta 163
		LOGGER.info("createOfferRequest [{}]", createOfferRequest);
164
		offerService.addOfferService(createOfferRequest);
29809 amit.gupta 165
		thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
27391 tejbeer 166
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
167
		return "response";
168
 
169
	}
170
 
27876 amit.gupta 171
	@RequestMapping(value = "/offers/published", method = RequestMethod.GET)
172
	public String getPublishedOffers(HttpServletRequest request, @RequestParam int fofoId, Model model)
27391 tejbeer 173
			throws Exception {
27876 amit.gupta 174
		LOGGER.info("Published");
175
		offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
176
		return "scheme_offer/published";
27391 tejbeer 177
 
27876 amit.gupta 178
	}
27391 tejbeer 179
 
27876 amit.gupta 180
	@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
181
	public String activateOffer(HttpServletRequest request, @PathVariable int offerId, Model model)
182
			throws ProfitMandiBusinessException, Exception {
183
		Offer offer = offerRepository.selectById(offerId);
29900 amit.gupta 184
		boolean alreadyActive = false;
27876 amit.gupta 185
		offer.setActive(true);
29900 amit.gupta 186
		if(alreadyActive) {
187
			oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
188
		}
189
		this.sendNotification(offer);
27876 amit.gupta 190
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
191
		return "response";
29900 amit.gupta 192
		//CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
193
		//model.addAttribute("offer", createOfferRequest);
194
		//return "offer_margin_detail_partner2";
27876 amit.gupta 195
	}
27391 tejbeer 196
 
29900 amit.gupta 197
	@Autowired
198
	private com.spice.profitmandi.dao.repository.dtr.UserRepository dtrUserRepository;
199
 
200
	private void sendNotification(Offer offer) throws Exception {
201
		String fileName = "offer-" + offer.getId() + ".png";
202
		CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
203
		SendNotificationModel sendNotificationModel = new SendNotificationModel();
204
		sendNotificationModel.setCampaignName("SchemeOffer");
205
		sendNotificationModel.setTitle(offer.getName());
29905 amit.gupta 206
		sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, " 
207
		+ FormattingUtils.formatDateMonth(offer.getStartDate()) + " to " + FormattingUtils.formatDateMonth(offer.getEndDate()));
29900 amit.gupta 208
		sendNotificationModel.setType("url");
209
		sendNotificationModel.setImageUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
210
		sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
211
		sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
212
		sendNotificationModel.setMessageType(MessageType.scheme);
213
		Map<Integer, Map<Integer, Long>> partnerWiseMap = null;
214
		Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());
215
		List<Integer> fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
216
				.map(x -> x.getKey()).collect(Collectors.toList());
217
		sendNotificationModel.setFofoIds(fofoIds);
218
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
219
		List<Integer> dtrIds = dtrUserRepository
220
				.selectAllByEmailIds(Arrays.asList("tarun.verma@smartdukaan.com", "amit.gupta@shop2020.in"))
221
				.stream().map(x->x.getId()).collect(Collectors.toList());
222
		sendNotificationModel.setUserIds(dtrIds);
223
		notificationService.sendNotification(sendNotificationModel);
224
		try {
225
			Map<String, Object> model = new HashMap<>();
226
			model.put("offer", createOfferRequest);
227
			String htmlContent=this.getContentFromTemplate("offer_margin_detail_partner.vm", model);
228
			LOGGER.info("this.getContentFromTemplate {}", htmlContent);
229
			fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
230
			this.uploadFile(fileStreamsMap);
231
		} catch (Exception e) {
232
			e.printStackTrace();
233
		}
234
 
235
	}
236
 
237
	private InputStream	asInputStream(BufferedImage bi) throws Exception {
238
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
239
		ImageIO.write(bi, "png", baos);
240
		return new ByteArrayInputStream(baos.toByteArray());
241
 
242
	}
243
 
244
	private ChannelSftp setupJsch() throws JSchException {
245
		JSch jsch = new JSch();
246
		Session jschSession = jsch.getSession("root", "192.168.179.131");
247
		//Session jschSession = jsch.getSession("root", "173.255.254.24");
248
		LOGGER.info("getClass().getResource(\"id_rsa\") {}", getClass().getClassLoader().getResource("id_rsa").getPath());
249
		jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
250
		//jschSession.setPassword("spic@2015static0");
251
		jschSession.setConfig("StrictHostKeyChecking", "no");
252
		jschSession.connect();
253
		return (ChannelSftp) jschSession.openChannel("sftp");
254
	}
255
 
256
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
257
			throws SftpException, FileNotFoundException {
258
 
259
		channelSftp.cd(destinationPath);
260
		String folderName = "image" + LocalDate.now();
261
 
262
		channelSftp.cd(destinationPath);
263
		SftpATTRS attrs = null;
264
 
265
		// check if the directory is already existing
266
		try {
267
			attrs = channelSftp.stat(folderName);
268
		} catch (Exception e) {
269
			System.out.println(destinationPath + "/" + folderName + " not found");
270
		}
271
 
272
		// else create a directory
273
		if (attrs == null) {
274
			channelSftp.mkdir(folderName);
275
			channelSftp.chmod(0755, ".");
276
		}
277
		channelSftp.cd(folderName);
278
 
279
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
280
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
281
		}
282
 
283
	}
284
 
285
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
286
		ChannelSftp channelSftp = setupJsch();
287
		channelSftp.connect();
288
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
289
		channelSftp.exit();
290
	}
291
 
292
	private InputStream getImageBuffer (String html) throws Exception {
293
		String fileName = "/tmp/" + Instant.now().toEpochMilli();
294
		FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");
295
		return this.asInputStream(Graphics2DRenderer.renderToImageAutoSize("file:" + fileName, 500));
296
	}
297
 
298
	@Autowired
299
	VelocityEngine velocityEngine;
300
 
301
	private String getContentFromTemplate(String template, Map<String, Object> model) {
302
		StringBuffer content = new StringBuffer();
303
		try {
304
			content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model));
305
		} catch (Exception e) {
306
			e.printStackTrace();
307
		}
308
		return content.toString();
309
	}
310
 
27876 amit.gupta 311
	@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
312
	public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
313
			throws ProfitMandiBusinessException {
27391 tejbeer 314
 
29900 amit.gupta 315
		List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream()
316
				.sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
27876 amit.gupta 317
		model.addAttribute("offers", publishedOffers);
318
		model.addAttribute("yearMonth", yearMonth);
27391 tejbeer 319
 
320
		return "offer_history";
321
	}
322
 
27876 amit.gupta 323
	@RequestMapping(value = "/offer-details", method = RequestMethod.GET)
324
	public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)
27391 tejbeer 325
			throws ProfitMandiBusinessException {
29839 amit.gupta 326
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27391 tejbeer 327
 
27876 amit.gupta 328
		model.addAttribute("offer", createOfferRequest);
329
		return "offer-details";
27391 tejbeer 330
	}
331
 
27876 amit.gupta 332
	@RequestMapping(value = "/offerDownload", method = RequestMethod.GET)
333
	public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)
334
			throws Exception {
335
		List<List<?>> listOfRows = new ArrayList<>();
336
		final HttpHeaders headers = new HttpHeaders();
337
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
338
		headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");
29785 amit.gupta 339
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27876 amit.gupta 340
		Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);
27391 tejbeer 341
 
27876 amit.gupta 342
		for (OfferRowModel offerRowModel : offerRowModels) {
343
			CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());
344
			listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),
345
					createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),
346
					createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),
347
					createOfferRequest.getPartnerCriteriaString(), createOfferRequest.getItemCriteriaString(),
348
					createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),
349
					createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),
350
					customRetailer.getCode(), offerRowModel.getTotalSale(), offerRowModel.getEligibleSale(),
351
					offerRowModel.getAchievedTarget(), offerRowModel.getNextTarget(), offerRowModel.getEligibleSaleDp(),
352
					offerRowModel.getTotalPurchaseValue(), offerRowModel.getCurrentPayoutTarget(),
353
					offerRowModel.getPayoutTargetAchieved(), offerRowModel.getAmountType(),
354
					offerRowModel.getPayoutValue(), offerRowModel.getPayoutValueDp(), offerRowModel.getFinalPayout(),
355
					String.join(", ", offerRowModel.getPendingImeis())));
27391 tejbeer 356
		}
27876 amit.gupta 357
		ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
358
				Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria",
359
						"Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code",
360
						"Total Sale", "Eligible Sale", "Achieved Target", "Next Target", "Eligible Sale DP",
361
						"Total Purchase DP", "Current Payout Target", "Payout Target Achieved", "Payout Amount Type",
362
						"Payout Value", "Payout Value DP", "Amount to be credited", "IMEIs pending for Activation"
363
				// "Payout Sale Qty", "Activated Value", "Activated Qty",
364
				), listOfRows);
365
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
366
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
367
		return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
27391 tejbeer 368
	}
29900 amit.gupta 369
 
27876 amit.gupta 370
	@RequestMapping(value = "/offerById", method = RequestMethod.GET)
371
	public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
27391 tejbeer 372
		Offer offer = offerRepository.selectById(offerId);
373
 
374
		model.addAttribute("offer", offer);
375
 
27876 amit.gupta 376
		return "offer-edit";
27391 tejbeer 377
 
378
	}
29900 amit.gupta 379
 
29675 amit.gupta 380
	@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
29900 amit.gupta 381
	public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model)
382
			throws ProfitMandiBusinessException {
29675 amit.gupta 383
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
384
		int fofoId = loginDetails.getFofoId();
29900 amit.gupta 385
		List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,
386
				YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
387
 
29675 amit.gupta 388
		model.addAttribute("publishedOffers", createOffers);
29900 amit.gupta 389
 
29675 amit.gupta 390
		return "published-offers";
391
	}
29900 amit.gupta 392
 
29682 amit.gupta 393
	@PostMapping(value = "/offers/upload")
394
	public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
395
			throws Exception {
396
		offerService.createOffers(targetFile.getInputStream());
397
		model.addAttribute("response", true);
398
		return "response";
399
	}
29900 amit.gupta 400
 
29785 amit.gupta 401
	@Autowired
402
	RoleManager roleManager;
27391 tejbeer 403
 
27876 amit.gupta 404
	@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
405
	public String getOfferMargins(HttpServletRequest request,
406
			@RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
407
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29785 amit.gupta 408
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
29900 amit.gupta 409
		CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);
410
 
27876 amit.gupta 411
		model.addAttribute("offer", createOfferRequest);
27391 tejbeer 412
 
27876 amit.gupta 413
		return "offer_margin_detail_partner";
27391 tejbeer 414
 
415
	}
416
 
27895 amit.gupta 417
}