Rev 30957 | Rev 30969 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.google.gson.Gson;import com.jcraft.jsch.*;import com.spice.profitmandi.common.enumuration.MessageType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.CustomRetailer;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.SendNotificationModel;import com.spice.profitmandi.common.util.FileUtil;import com.spice.profitmandi.common.util.FormattingUtils;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.catalog.Offer;import com.spice.profitmandi.dao.entity.fofo.PartnerType;import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;import com.spice.profitmandi.dao.model.CreateOfferRequest;import com.spice.profitmandi.dao.model.OfferRowModel;import com.spice.profitmandi.dao.repository.catalog.ItemRepository;import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;import com.spice.profitmandi.dao.repository.catalog.OfferRepository;import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;import com.spice.profitmandi.dao.repository.dtr.Mongo;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;import com.spice.profitmandi.service.NotificationService;import com.spice.profitmandi.service.authentication.RoleManager;import com.spice.profitmandi.service.offers.OfferService;import com.spice.profitmandi.service.user.RetailerService;import com.spice.profitmandi.web.model.LoginDetails;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.MVCResponseSender;import org.apache.commons.io.FileUtils;import org.apache.commons.io.output.ByteArrayOutputStream;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.apache.velocity.app.VelocityEngine;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.cache.CacheManager;import org.springframework.core.io.InputStreamResource;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.velocity.VelocityEngineUtils;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import org.xhtmlrenderer.swing.Java2DRenderer;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletRequest;import javax.transaction.Transactional;import java.awt.*;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.InputStream;import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.YearMonth;import java.util.List;import java.util.*;import java.util.stream.Collectors;@Controller@Transactional(rollbackOn = Throwable.class)public class OfferController {private static final Logger LOGGER = LogManager.getLogger(OfferController.class);@Autowiredprivate OfferRepository offerRepository;@Autowiredprivate OfferMarginRepository offerMarginRepository;@Autowiredprivate FofoStoreRepository fofoStoreRepository;@Autowiredprivate ResponseSender responseSender;@Autowiredprivate OfferPartnerRepository offerPartnerRepository;private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";@Autowiredprivate ItemRepository itemRepository;@Autowiredprivate MVCResponseSender mvcResponseSender;@Autowiredprivate Gson gson;@Autowiredprivate RetailerService retailerService;@Autowiredprivate Mongo mongoClient;@Autowiredprivate NotificationService notificationService;@Autowiredprivate CookiesProcessor cookiesProcessor;@Autowiredprivate OfferService offerService;@Autowiredprivate CacheManager thirtyMinsTimeOutCacheManager;@Autowiredprivate CacheManager oneDayCacheManager;@Autowiredprivate PartnerTypeChangeService partnerTypeChangeService;@RequestMapping(value = "/getCreateOffer", method = RequestMethod.GET)public String getCreateOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);List<Integer> fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toList());Set<String> brands = mongoClient.getMongoBrands(loginDetails.getFofoId(), null, 3).stream().map(x -> (String) x.get("name")).collect(Collectors.toSet());brands.addAll(itemRepository.selectAllBrands(ProfitMandiConstants.LED_CATEGORY_ID));Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();Map<Integer, CustomRetailer> customRetailersMap = fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null).collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));model.addAttribute("customRetailersMap", customRetailersMap);model.addAttribute("itemCriteriaType", ItemCriteriaType.values());model.addAttribute("brands", brands);model.addAttribute("partnerCategories", PartnerType.values());model.addAttribute("warehouseRegion", ProfitMandiConstants.WAREHOUSE_MAP);return "scheme_offer";}@RequestMapping(value = "/createOffer", method = RequestMethod.POST)public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,Model model) throws Exception {LOGGER.info("createOfferRequest [{}]", createOfferRequest);offerService.addOfferService(createOfferRequest);thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}@RequestMapping(value = "/offers/published", method = RequestMethod.GET)public String getPublishedOffers(HttpServletRequest request, @RequestParam int fofoId, Model model)throws Exception {LOGGER.info("Published");offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));return "scheme_offer/published";}@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)public String activateOffer(HttpServletRequest request, @PathVariable(name = "offerId") String offerIdsString,Model model, @RequestParam(defaultValue = "true") boolean active)throws ProfitMandiBusinessException, Exception {List<Integer> offerIds = Arrays.stream(offerIdsString.split(",")).map(x -> Integer.parseInt(x)).collect(Collectors.toList());List<Offer> offers = offerRepository.selectAllByIds(offerIds);boolean shouldEvict = false;for (Offer offer : offers) {offer.setAlreadyActive(offer.isActive());if (offer.isActive() != active) {offer.setActive(active);shouldEvict = true;}}if (shouldEvict) {oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());}for (Offer offer : offers) {if (active && !offer.isAlreadyActive()) {this.sendNotification(offer);}}model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";// CreateOfferRequest createOfferRequest =// offerService.getCreateOfferRequest(offer);// model.addAttribute("offer", createOfferRequest);// return "offer_margin_detail_partner2";}@RequestMapping(value = "/offer/testimage/{offerId}", method = RequestMethod.GET)public String testOffer(HttpServletRequest request, @PathVariable int offerId, Model model,@RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {Offer offer = offerRepository.selectById(offerId);// model.addAttribute("response", mvcResponseSender.createResponseString(true));// return "response";CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);model.addAttribute("offer", createOfferRequest);return "offer_margin_detail_partner2";}@Autowiredprivate com.spice.profitmandi.dao.repository.dtr.UserRepository dtrUserRepository;@RequestMapping(value = "/offer/active/fofo/{fofoId}", method = RequestMethod.GET)public String sendOfferNotification(HttpServletRequest request, @PathVariable int fofoId, Model model,@RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {List<Offer> offers = offerRepository.selectAllPublishedMapByPartner(YearMonth.now()).get(fofoId);for (Offer offer : offers) {this.sendNotification(offer);}model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}@AutowiredUserAccountRepository userAccountRepository;private void sendNotification(Offer offer) throws Exception {String fileName = "offer-" + offer.getId() + ".png";String htmlFileName = fileName.replace("png", "html");CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);SendNotificationModel sendNotificationModel = new SendNotificationModel();sendNotificationModel.setCampaignName("SchemeOffer");sendNotificationModel.setTitle(offer.getName());sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "+ FormattingUtils.formatDateMonth(offer.getStartDate()) + " to "+ FormattingUtils.formatDateMonth(offer.getEndDate()));sendNotificationModel.setType("url");sendNotificationModel.setImageUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));sendNotificationModel.setMessageType(MessageType.scheme);Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());Map<String, InputStream> fileStreamsMap = new HashMap<>();Map<String, Object> model = new HashMap<>();model.put("offer", createOfferRequest);String htmlContent = this.getContentFromTemplate("offer_margin_detail_partner.vm", model);LOGGER.info("this.getContentFromTemplate {}", htmlContent);fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));// fileStreamsMap.put(htmlFileName, new// ByteArrayInputStream(htmlContent.getBytes()));this.uploadFile(fileStreamsMap);if (createOfferRequest.getPartnerCriteria().getRegionIds().size() > 0|| createOfferRequest.getPartnerCriteria().getFofoIds().size() > 0|| createOfferRequest.getPartnerCriteria().getRegionIds().size() > 0 ||createOfferRequest.getPartnerCriteria().getExcludeFofoIds().size() > 0) {List<Integer> fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer)).map(x -> x.getKey()).collect(Collectors.toList());List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(fofoIds);sendNotificationModel.setUserIds(userIds);notificationService.sendNotification(sendNotificationModel);} else {notificationService.sendNotificationToAll(sendNotificationModel);}}private InputStream asInputStream(BufferedImage bi) throws Exception {ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(bi, "png", baos);return new ByteArrayInputStream(baos.toByteArray());}private ChannelSftp setupJsch() throws JSchException {JSch jsch = new JSch();Session jschSession = jsch.getSession("root", "192.168.179.131");// Session jschSession = jsch.getSession("root", "173.255.254.24");LOGGER.info("getClass().getResource(\"id_rsa\") {}",getClass().getClassLoader().getResource("id_rsa").getPath());jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());// jschSession.setPassword("spic@2015static0");jschSession.setConfig("StrictHostKeyChecking", "no");jschSession.connect();return (ChannelSftp) jschSession.openChannel("sftp");}private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)throws SftpException, FileNotFoundException {channelSftp.cd(destinationPath);String folderName = "image" + LocalDate.now();channelSftp.cd(destinationPath);SftpATTRS attrs = null;// check if the directory is already existingtry {attrs = channelSftp.stat(folderName);} catch (Exception e) {System.out.println(destinationPath + "/" + folderName + " not found");}// else create a directoryif (attrs == null) {channelSftp.mkdir(folderName);channelSftp.chmod(0755, ".");}channelSftp.cd(folderName);for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);}}private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {ChannelSftp channelSftp = setupJsch();channelSftp.connect();this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");channelSftp.exit();}private InputStream getImageBuffer(String html) throws Exception {String fileName = "/tmp/" + Instant.now().toEpochMilli();FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");String address = "file:" + fileName;Java2DRenderer renderer = new Java2DRenderer(address, 400);RenderingHints hints = new RenderingHints(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_QUALITY);hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));hints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC));renderer.setRenderingHints(hints);BufferedImage img = renderer.getImage();ByteArrayOutputStream os = new ByteArrayOutputStream();ImageIO.write(img, "png", os);return new ByteArrayInputStream(os.toByteArray());}@Autowiredprivate VelocityEngine velocityEngine;@Autowired@Qualifier("velocityAttributesMap")private Map<String, Object> velocityAttributesMap;private String getContentFromTemplate(String template, Map<String, Object> model) {StringBuffer content = new StringBuffer();try {((HashMap<String, Object>) (velocityAttributesMap.entrySet().stream().findAny().get()).getValue()).entrySet().stream().forEach(x -> {model.put(x.getKey(), x.getValue());});content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model));} catch (Exception e) {e.printStackTrace();}return content.toString();}@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)throws ProfitMandiBusinessException {List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream().sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());model.addAttribute("offers", publishedOffers);model.addAttribute("yearMonth", yearMonth);model.addAttribute("currentMonth", yearMonth.equals(YearMonth.now()));return "offer_history";}@RequestMapping(value = "/offer-details", method = RequestMethod.GET)public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)throws ProfitMandiBusinessException {CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);model.addAttribute("offer", createOfferRequest);return "offer-details";}@RequestMapping(value = "/offer/process/{offerId}", method = RequestMethod.GET)public ResponseEntity<?> processOfferRequest(HttpServletRequest request, @PathVariable int offerId, Model model)throws Exception {CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {offerRepository.processSellin(createOfferRequest);} else if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {offerRepository.processActivationtOffer(createOfferRequest);}return responseSender.ok(true);}@RequestMapping(value = "/offerDownload", method = RequestMethod.GET)public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)throws Exception {List<List<?>> listOfRows = new ArrayList<>();final HttpHeaders headers = new HttpHeaders();headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);ByteArrayOutputStream baos = null;if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {for (OfferRowModel offerRowModel : offerRowModels) {CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),"--", createOfferRequest.getItemCriteriaString(),createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),customRetailer.getCode(), offerRowModel.getTotalBasePurchaseValue(), offerRowModel.getAchievedTarget(),offerRowModel.getPayoutPurchaseValue(), offerRowModel.getPayout(), offerRowModel.getPayoutType(), offerRowModel.getFinalPayout(), String.join(", ", offerRowModel.getEligibleImeis())));}baos = FileUtil.getCSVByteStream(Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria","Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code","Base Purchase", "Achieved Target", "Eligible Purchase", "Slab Amount", "Slab Amount Type", "Payout Value(Rs.)","Eligible IMEIs"), listOfRows);} else {for (OfferRowModel offerRowModel : offerRowModels) {CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),createOfferRequest.getPartnerCriteriaString(), createOfferRequest.getItemCriteriaString(),createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),customRetailer.getCode(),offerRowModel.getTotalSale(),offerRowModel.getEligibleSale(),offerRowModel.getTotalBasePurchaseValue(),offerRowModel.getCurrentPayoutTarget(),offerRowModel.getPayoutTargetAchieved(),offerRowModel.getPayoutValue(),offerRowModel.getPayoutType(),offerRowModel.getPayoutValueDp(), offerRowModel.getFinalPayout(),String.join(", ", offerRowModel.getPendingImeis())));}baos = FileUtil.getCSVByteStream(Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria","Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code","Total Value","Eligible Value",//"Achieved Target",//"Next Target",//"Eligible Sale DP","Total Purchase", "Current Payout Target", "Payout Target Achieved", "Payout Amount", "Payout Amount Type","Payout Value DP", "Amount to be credited", "IMEIs pending for Activation"// "Payout Sale Qty", "Activated Value", "Activated Qty",), listOfRows);}final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);}@RequestMapping(value = "/offerById", method = RequestMethod.GET)public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {Offer offer = offerRepository.selectById(offerId);model.addAttribute("offer", offer);return "offer-edit";}@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);int fofoId = loginDetails.getFofoId();List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,YearMonth.from(LocalDate.now()).minusMonths(yearMonth));model.addAttribute("publishedOffers", createOffers);return "published-offers";}@PostMapping(value = "/offers/upload")public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)throws Exception {offerService.createOffers(targetFile.getInputStream());model.addAttribute("response", true);return "response";}@AutowiredRoleManager roleManager;@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)public String getOfferMargins(HttpServletRequest request,@RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);model.addAttribute("offer", createOfferRequest);return "offer_margin_detail_partner";}}