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