Subversion Repositories SmartDukaan

Rev

Rev 35893 | Rev 35923 | 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
 
29943 amit.gupta 3
import com.jcraft.jsch.*;
29900 amit.gupta 4
import com.spice.profitmandi.common.enumuration.MessageType;
27391 tejbeer 5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.model.CustomRetailer;
27876 amit.gupta 7
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29900 amit.gupta 8
import com.spice.profitmandi.common.model.SendNotificationModel;
29904 amit.gupta 9
import com.spice.profitmandi.common.util.FormattingUtils;
27876 amit.gupta 10
import com.spice.profitmandi.common.web.util.ResponseSender;
35501 ranu 11
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
27391 tejbeer 12
import com.spice.profitmandi.dao.entity.catalog.Offer;
13
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
14
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
30651 amit.gupta 15
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
27391 tejbeer 16
import com.spice.profitmandi.dao.model.CreateOfferRequest;
34176 tejus.loha 17
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
35501 ranu 18
import com.spice.profitmandi.dao.model.TodayOfferModel;
34552 amit.gupta 19
import com.spice.profitmandi.dao.repository.catalog.*;
27391 tejbeer 20
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
29926 amit.gupta 21
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
29900 amit.gupta 22
import com.spice.profitmandi.service.NotificationService;
29785 amit.gupta 23
import com.spice.profitmandi.service.authentication.RoleManager;
33043 amit.gupta 24
import com.spice.profitmandi.service.catalog.BrandsService;
34176 tejus.loha 25
import com.spice.profitmandi.service.offers.ItemCriteria;
27876 amit.gupta 26
import com.spice.profitmandi.service.offers.OfferService;
35501 ranu 27
import com.spice.profitmandi.service.offers.TodayOfferService;
27391 tejbeer 28
import com.spice.profitmandi.service.user.RetailerService;
29
import com.spice.profitmandi.web.model.LoginDetails;
30
import com.spice.profitmandi.web.util.CookiesProcessor;
31
import com.spice.profitmandi.web.util.MVCResponseSender;
29943 amit.gupta 32
import org.apache.commons.io.FileUtils;
33
import org.apache.commons.io.output.ByteArrayOutputStream;
34
import org.apache.logging.log4j.LogManager;
35
import org.apache.logging.log4j.Logger;
35892 amit 36
import com.google.gson.Gson;
37
import com.spice.profitmandi.service.offers.PartnerCriteria;
29943 amit.gupta 38
import org.springframework.beans.factory.annotation.Autowired;
35892 amit 39
import org.springframework.cache.interceptor.SimpleKey;
32868 amit.gupta 40
import org.springframework.beans.factory.annotation.Value;
29943 amit.gupta 41
import org.springframework.cache.CacheManager;
42
import org.springframework.core.io.InputStreamResource;
43
import org.springframework.http.HttpHeaders;
44
import org.springframework.http.HttpStatus;
45
import org.springframework.http.ResponseEntity;
32204 amit.gupta 46
import org.springframework.mock.web.MockHttpServletRequest;
47
import org.springframework.mock.web.MockHttpServletResponse;
29943 amit.gupta 48
import org.springframework.stereotype.Controller;
35501 ranu 49
import org.springframework.transaction.annotation.Transactional;
29943 amit.gupta 50
import org.springframework.ui.Model;
51
import org.springframework.web.bind.annotation.*;
52
import org.springframework.web.multipart.MultipartFile;
32204 amit.gupta 53
import org.springframework.web.servlet.View;
54
import org.springframework.web.servlet.ViewResolver;
29943 amit.gupta 55
import org.xhtmlrenderer.swing.Java2DRenderer;
27391 tejbeer 56
 
29943 amit.gupta 57
import javax.imageio.ImageIO;
58
import javax.servlet.http.HttpServletRequest;
59
import java.awt.*;
60
import java.awt.image.BufferedImage;
61
import java.io.ByteArrayInputStream;
62
import java.io.File;
63
import java.io.FileNotFoundException;
64
import java.io.InputStream;
33713 tejus.loha 65
import java.time.Instant;
66
import java.time.LocalDate;
67
import java.time.LocalDateTime;
68
import java.time.YearMonth;
35501 ranu 69
import java.util.List;
35205 amit 70
import java.util.*;
29943 amit.gupta 71
import java.util.stream.Collectors;
72
 
27391 tejbeer 73
@Controller
35458 amit 74
@Transactional(rollbackFor = Throwable.class)
27391 tejbeer 75
public class OfferController {
32505 amit.gupta 76
    private static final Logger LOGGER = LogManager.getLogger(OfferController.class);
77
    private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
78
    private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
79
    @Autowired
80
    UserAccountRepository userAccountRepository;
81
    @Autowired
82
    RoleManager roleManager;
83
    @Autowired
84
    private OfferRepository offerRepository;
85
    @Autowired
86
    private OfferMarginRepository offerMarginRepository;
87
    @Autowired
88
    private FofoStoreRepository fofoStoreRepository;
89
    @Autowired
90
    private ResponseSender responseSender;
91
    @Autowired
92
    private ViewResolver viewResolver;
93
    @Autowired
94
    private ItemRepository itemRepository;
95
    @Autowired
96
    private MVCResponseSender mvcResponseSender;
97
    @Autowired
98
    private RetailerService retailerService;
99
    @Autowired
100
    private NotificationService notificationService;
101
    @Autowired
102
    private CookiesProcessor cookiesProcessor;
103
    @Autowired
104
    private OfferService offerService;
105
    @Autowired
106
    private CacheManager oneDayCacheManager;
27391 tejbeer 107
 
33043 amit.gupta 108
    @Autowired
35857 amit 109
    private CacheManager redisCacheManager;
110
 
111
    @Autowired
112
    private CacheManager redisShortCacheManager;
113
 
114
    @Autowired
35892 amit 115
    private CacheManager thirtyMinsTimeOutCacheManager;
116
 
117
    @Autowired
35893 amit 118
    private Gson gson;
119
 
120
    @Autowired
33043 amit.gupta 121
    BrandsService brandsService;
34553 amit.gupta 122
 
34552 amit.gupta 123
    @Autowired
124
    private CatalogRepository catalogRepository;
33043 amit.gupta 125
 
35501 ranu 126
    @Autowired
127
    TodayOfferService todayOfferService;
128
 
32505 amit.gupta 129
    @RequestMapping(value = "/getCreateOffer", method = RequestMethod.GET)
130
    public String getCreateOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
131
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
132
        List<Integer> fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
133
                .collect(Collectors.toList());
27391 tejbeer 134
 
33713 tejus.loha 135
        Set<String> brands = brandsService.getBrandsToDisplay(3).stream().map(x -> x.getName()).collect(Collectors.toSet());
32505 amit.gupta 136
        brands.addAll(itemRepository.selectAllBrands(ProfitMandiConstants.LED_CATEGORY_ID));
33615 amit.gupta 137
        brands.addAll(itemRepository.selectAllBrands(ProfitMandiConstants.SMART_WATCH_CATEGORY_ID));
32505 amit.gupta 138
        //Lets allow demo
139
        brands.add("Live Demo");
27391 tejbeer 140
 
32505 amit.gupta 141
        Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
27876 amit.gupta 142
 
32505 amit.gupta 143
        Map<Integer, CustomRetailer> customRetailersMap = fofoIds.stream().map(x -> customRetailerMap.get(x))
144
                .filter(x -> x != null).collect(Collectors.toList()).stream()
145
                .collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
32204 amit.gupta 146
 
32505 amit.gupta 147
        model.addAttribute("customRetailersMap", customRetailersMap);
148
        model.addAttribute("itemCriteriaType", ItemCriteriaType.values());
149
        model.addAttribute("brands", brands);
150
        model.addAttribute("partnerCategories", PartnerType.values());
151
        model.addAttribute("warehouseRegion", ProfitMandiConstants.WAREHOUSE_MAP);
152
        return "scheme_offer";
27391 tejbeer 153
 
32505 amit.gupta 154
    }
29926 amit.gupta 155
 
32505 amit.gupta 156
    @RequestMapping(value = "/createOffer", method = RequestMethod.POST)
157
    public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
158
                              Model model) throws Exception {
159
        LOGGER.info("createOfferRequest [{}]", createOfferRequest);
160
        offerService.addOfferService(createOfferRequest);
32868 amit.gupta 161
        oneDayCacheManager.getCache("allOffers").evict(YearMonth.from(createOfferRequest.getStartDate()));
32505 amit.gupta 162
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
163
        return "response";
27391 tejbeer 164
 
32505 amit.gupta 165
    }
27391 tejbeer 166
 
32505 amit.gupta 167
    @RequestMapping(value = "/offers/published", method = RequestMethod.GET)
168
    public String getPublishedOffers(HttpServletRequest request, @RequestParam int fofoId, Model model)
169
            throws Exception {
170
        LOGGER.info("Published");
171
        offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
172
        return "scheme_offer/published";
27391 tejbeer 173
 
32505 amit.gupta 174
    }
27391 tejbeer 175
 
32868 amit.gupta 176
    @Value("${prod}")
177
    private boolean isProd;
178
 
32505 amit.gupta 179
    @RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
180
    public String activateOffer(HttpServletRequest request, @PathVariable(name = "offerId") String offerIdsString,
181
                                Model model, @RequestParam(defaultValue = "true") boolean active)
182
            throws ProfitMandiBusinessException, Exception {
183
        List<Integer> offerIds = Arrays.stream(offerIdsString.split(",")).map(x -> Integer.parseInt(x))
184
                .collect(Collectors.toList());
185
        List<Offer> offers = offerRepository.selectAllByIds(offerIds);
32868 amit.gupta 186
 
187
        //Consider only offers that have opposite status
188
        offers = offers.stream().filter(x -> x.isActive() != active).collect(Collectors.toList());
189
 
190
        Set<YearMonth> yearMonthsToEvict = new HashSet<>();
32505 amit.gupta 191
        for (Offer offer : offers) {
32868 amit.gupta 192
            offer.setActive(active);
193
            yearMonthsToEvict.add(YearMonth.from(offer.getStartDate()));
32505 amit.gupta 194
        }
35894 amit 195
        // Evict partner-to-offer mapping (which partners see which offers)
32868 amit.gupta 196
        for (YearMonth ymToEvict : yearMonthsToEvict) {
35857 amit 197
            redisCacheManager.getCache("catalog.published_yearmonth").evict(ymToEvict);
32868 amit.gupta 198
            oneDayCacheManager.getCache("allOffers").evict(ymToEvict);
32505 amit.gupta 199
        }
35894 amit 200
        // Evict partner's published offer list with achievement data
35857 amit 201
        redisShortCacheManager.getCache("publishedOffersWithAchievement").clear();
35894 amit 202
        // Evict offer detail (price circular) for each involved partner; key=(fofoId, offerId), 0=admin
35893 amit 203
        for (Offer offer : offers) {
204
            PartnerCriteria partnerCriteria = gson.fromJson(offer.getPartnerCriteria(), PartnerCriteria.class);
205
            if (partnerCriteria != null && partnerCriteria.getFofoIds() != null) {
206
                for (Integer fofoId : partnerCriteria.getFofoIds()) {
207
                    thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(fofoId, offer.getId()));
208
                }
209
            }
210
            thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(0, offer.getId()));
211
        }
32868 amit.gupta 212
        if (active) {
213
            for (Offer offer : offers) {
32505 amit.gupta 214
                this.sendNotification(offer);
215
            }
216
        }
32868 amit.gupta 217
 
218
 
32505 amit.gupta 219
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
220
        return "response";
221
    }
27391 tejbeer 222
 
35857 amit 223
    @RequestMapping(value = "/offers/publishAll", method = RequestMethod.POST)
224
    public ResponseEntity<?> publishAllUnpublished(@RequestParam YearMonth yearMonth)
225
            throws ProfitMandiBusinessException, Exception {
226
        List<Offer> published = offerService.publishAllUnpublished(yearMonth);
227
        if (!published.isEmpty()) {
35894 amit 228
            // Evict partner-to-offer mapping and offer listing
35857 amit 229
            redisCacheManager.getCache("catalog.published_yearmonth").evict(yearMonth);
230
            oneDayCacheManager.getCache("allOffers").evict(yearMonth);
231
            redisShortCacheManager.getCache("publishedOffersWithAchievement").clear();
35894 amit 232
            // Evict offer detail (price circular) for each involved partner; key=(fofoId, offerId), 0=admin
35857 amit 233
            for (Offer offer : published) {
35893 amit 234
                PartnerCriteria pc = gson.fromJson(offer.getPartnerCriteria(), PartnerCriteria.class);
235
                if (pc != null && pc.getFofoIds() != null) {
236
                    for (Integer fofoId : pc.getFofoIds()) {
237
                        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(fofoId, offer.getId()));
238
                    }
239
                }
240
                thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(0, offer.getId()));
241
            }
242
            for (Offer offer : published) {
35857 amit 243
                this.sendNotification(offer);
244
            }
245
        }
246
        return responseSender.ok(published.size() + " offers published");
247
    }
248
 
249
    @RequestMapping(value = "/offer/delete/{offerId}", method = RequestMethod.DELETE)
250
    public ResponseEntity<?> deleteOffer(@PathVariable int offerId) throws ProfitMandiBusinessException {
251
        offerService.deleteOffer(offerId);
252
        return responseSender.ok(true);
253
    }
254
 
32505 amit.gupta 255
    @RequestMapping(value = "/offer/testimage/{offerId}", method = RequestMethod.GET)
256
    public String testOffer(HttpServletRequest request, @PathVariable int offerId, Model model,
257
                            @RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
258
        Offer offer = offerRepository.selectById(offerId);
259
        // model.addAttribute("response1", mvcResponseSender.createResponseString(true));
260
        // return "response";
261
        CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
262
        Map<String, Object> model1 = new HashMap<>();
263
        model1.put("offer", createOfferRequest);
264
        model1.put("lessThan", "<");
265
        String htmlContent = this.getContentFromTemplate("offer_margin_detail_notify", model1);
266
        model.addAttribute("response1", htmlContent);
267
        return "response";
268
    }
29900 amit.gupta 269
 
32505 amit.gupta 270
    private void sendNotification(Offer offer) throws Exception {
271
        if (!YearMonth.from(offer.getStartDate()).equals(YearMonth.now())) {
272
            return;
273
        }
274
        String fileName = "offer-" + offer.getId() + ".png";
32868 amit.gupta 275
        //String htmlFileName = fileName.replace("png", "html");
32505 amit.gupta 276
        CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
34620 amit.gupta 277
        boolean isLiveDemo = createOfferRequest.getTargetSlabs().stream()
278
                .map(x -> x.getItemCriteriaPayouts())
279
                .flatMap(List::stream)
280
                .map(ItemCriteriaPayout::getItemCriteria)
281
                .map(ItemCriteria::getCatalogIds)
282
                .flatMap(List::stream)
283
                .anyMatch(catalogId -> catalogRepository.selectCatalogById(catalogId).getBrand().equals("Live Demo"));
284
        if (!isLiveDemo) {
285
            SendNotificationModel sendNotificationModel = new SendNotificationModel();
286
            sendNotificationModel.setCampaignName("SchemeOffer");
287
            sendNotificationModel.setTitle(offer.getName());
288
            sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "
289
                    + FormattingUtils.formatDateMonth(offer.getStartDate()) + " to "
290
                    + FormattingUtils.formatDateMonth(offer.getEndDate()));
291
            sendNotificationModel.setType("url");
292
            String imageUrl = IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName;
293
            sendNotificationModel.setImageUrl(imageUrl);
294
            sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
295
            sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
296
            sendNotificationModel.setMessageType(MessageType.scheme);
297
            //Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());
29900 amit.gupta 298
 
34620 amit.gupta 299
            Map<String, InputStream> fileStreamsMap = new HashMap<>();
300
            Map<String, Object> model = new HashMap<>();
301
            model.put("offer", createOfferRequest);
302
            String htmlContent = this.getContentFromTemplate("offer_margin_detail_notify", model);
303
            LOGGER.info("this.getContentFromTemplate {}", htmlContent);
304
            fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
305
            // fileStreamsMap.put(htmlFileName, new
306
            // ByteArrayInputStream(htmlContent.getBytes()));
307
            List<Integer> fofoIds = null;
308
            if (isProd) {
309
                this.uploadFile(fileStreamsMap);
310
            }
311
 
312
            List<Integer> fofoIdSet = new ArrayList<>(offerRepository.getEligibleFofoIds(offer));
313
            //LOGGER.info(fofoIdSet);
314
            List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(new ArrayList<>(fofoIdSet));
315
            sendNotificationModel.setUserIds(userIds);
316
            notificationService.sendNotification(sendNotificationModel);
317
            sendWhatsapp(offer, fofoIds, imageUrl);
32868 amit.gupta 318
        }
32505 amit.gupta 319
    }
27876 amit.gupta 320
 
32505 amit.gupta 321
    private void sendWhatsapp(Offer offer, List<Integer> fofoIds, String imageUrl) throws Exception {
35205 amit 322
        offerService.sendWhatsapp(offer, fofoIds, imageUrl);
32505 amit.gupta 323
    }
27391 tejbeer 324
 
32505 amit.gupta 325
    private InputStream asInputStream(BufferedImage bi) throws Exception {
326
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
327
        ImageIO.write(bi, "png", baos);
328
        return new ByteArrayInputStream(baos.toByteArray());
27391 tejbeer 329
 
32505 amit.gupta 330
    }
27391 tejbeer 331
 
32505 amit.gupta 332
    private ChannelSftp setupJsch() throws JSchException {
333
        JSch jsch = new JSch();
33471 amit.gupta 334
        Session jschSession = jsch.getSession("root", "172.105.58.16");
32505 amit.gupta 335
        // Session jschSession = jsch.getSession("root", "173.255.254.24");
336
        LOGGER.info("getClass().getResource(\"id_rsa\") {}",
337
                getClass().getClassLoader().getResource("id_rsa").getPath());
338
        jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
339
        // jschSession.setPassword("spic@2015static0");
340
        jschSession.setConfig("StrictHostKeyChecking", "no");
341
        jschSession.connect();
342
        return (ChannelSftp) jschSession.openChannel("sftp");
343
    }
30426 tejbeer 344
 
32505 amit.gupta 345
    private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
346
            throws SftpException, FileNotFoundException {
27391 tejbeer 347
 
32505 amit.gupta 348
        channelSftp.cd(destinationPath);
349
        String folderName = "image" + LocalDate.now();
27391 tejbeer 350
 
32505 amit.gupta 351
        channelSftp.cd(destinationPath);
352
        SftpATTRS attrs = null;
27391 tejbeer 353
 
32505 amit.gupta 354
        // check if the directory is already existing
355
        try {
356
            attrs = channelSftp.stat(folderName);
357
        } catch (Exception e) {
358
            System.out.println(destinationPath + "/" + folderName + " not found");
359
        }
27391 tejbeer 360
 
32505 amit.gupta 361
        // else create a directory
362
        if (attrs == null) {
363
            channelSftp.mkdir(folderName);
364
            channelSftp.chmod(0755, ".");
365
        }
366
        channelSftp.cd(folderName);
27391 tejbeer 367
 
32505 amit.gupta 368
        for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
369
            channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
370
        }
27391 tejbeer 371
 
32505 amit.gupta 372
    }
29926 amit.gupta 373
 
32505 amit.gupta 374
    private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
375
        ChannelSftp channelSftp = setupJsch();
376
        channelSftp.connect();
377
        this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
378
        channelSftp.exit();
379
    }
27391 tejbeer 380
 
32505 amit.gupta 381
    private InputStream getImageBuffer(String html) throws Exception {
29900 amit.gupta 382
 
32505 amit.gupta 383
        String fileName = "/tmp/" + Instant.now().toEpochMilli();
384
        FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");
385
        String address = "file:" + fileName;
386
        Java2DRenderer renderer = new Java2DRenderer(address, 400);
387
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_COLOR_RENDERING,
388
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
389
        hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
390
        hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
391
        hints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC));
392
        renderer.setRenderingHints(hints);
393
        BufferedImage img = renderer.getImage();
394
        ByteArrayOutputStream os = new ByteArrayOutputStream();
395
        ImageIO.write(img, "png", os);
396
        return new ByteArrayInputStream(os.toByteArray());
397
    }
29926 amit.gupta 398
 
32505 amit.gupta 399
    private String getContentFromTemplate(String template, Map<String, Object> model) throws Exception {
400
        View resolvedView = viewResolver.resolveViewName(template, Locale.US);
401
        MockHttpServletResponse mockResp = new MockHttpServletResponse();
402
        MockHttpServletRequest req = new MockHttpServletRequest();
403
        LOGGER.info("Resolved view ->  {}, {}, {}, {}", resolvedView, model, req, mockResp);
404
        resolvedView.render(model, req, mockResp);
405
        return mockResp.getContentAsString();
406
    }
29926 amit.gupta 407
 
32505 amit.gupta 408
    @RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
409
    public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
410
            throws ProfitMandiBusinessException {
30017 amit.gupta 411
 
32505 amit.gupta 412
        List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream()
413
                .sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
414
        model.addAttribute("offers", publishedOffers);
415
        model.addAttribute("yearMonth", yearMonth);
416
        model.addAttribute("currentMonth", yearMonth.equals(YearMonth.now()));
29926 amit.gupta 417
 
32505 amit.gupta 418
        return "offer_history";
419
    }
30723 amit.gupta 420
 
32505 amit.gupta 421
    @RequestMapping(value = "/offer-details", method = RequestMethod.GET)
422
    public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)
423
            throws ProfitMandiBusinessException {
424
        CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
29900 amit.gupta 425
 
32505 amit.gupta 426
        model.addAttribute("offer", createOfferRequest);
427
        return "offer-details";
428
    }
29926 amit.gupta 429
 
32505 amit.gupta 430
    @RequestMapping(value = "/offer/process/{offerId}", method = RequestMethod.GET)
431
    public ResponseEntity<?> processOfferRequest(HttpServletRequest request, @PathVariable int offerId, Model model)
432
            throws Exception {
433
        CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
434
        if (!createOfferRequest.isActive()) {
435
            throw new ProfitMandiBusinessException("Offer not active", "Offer not active", "Offer not active");
436
        }
437
        if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
438
            offerService.processSellin(createOfferRequest);
439
        } else if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
440
            offerService.processActivationtOffer(createOfferRequest);
441
        }
442
        return responseSender.ok(true);
443
    }
29926 amit.gupta 444
 
32505 amit.gupta 445
    @RequestMapping(value = "/offerDownload", method = RequestMethod.GET)
446
    public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)
447
            throws Exception {
448
        final HttpHeaders headers = new HttpHeaders();
449
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
450
        headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");
451
        CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
33999 tejus.loha 452
        ByteArrayOutputStream baos = offerService.createCSVOfferReport(createOfferRequest);
32505 amit.gupta 453
        final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
454
        final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
455
        return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
456
    }
29900 amit.gupta 457
 
32505 amit.gupta 458
    @RequestMapping(value = "/offerById", method = RequestMethod.GET)
459
    public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
460
        Offer offer = offerRepository.selectById(offerId);
461
        model.addAttribute("offer", offer);
462
        return "offer-edit";
29900 amit.gupta 463
 
32505 amit.gupta 464
    }
29900 amit.gupta 465
 
34176 tejus.loha 466
    @RequestMapping(value = "/published-offers", method = RequestMethod.GET)
467
    public String publishedOffersOnMonthBefore(HttpServletRequest request, @RequestParam int yearMonth, @RequestParam(required = false, defaultValue = "") String brandFilter, Model model)
32505 amit.gupta 468
            throws ProfitMandiBusinessException {
34176 tejus.loha 469
        LOGGER.info("publishedOffersCalled");
32505 amit.gupta 470
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
471
        int fofoId = loginDetails.getFofoId();
472
        List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,
473
                YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
29900 amit.gupta 474
 
34617 amit.gupta 475
        List<CreateOfferRequest> publishedOffers = null;
476
        if (!brandFilter.isEmpty()) {
477
            publishedOffers = createOffers.stream()
34176 tejus.loha 478
                    .filter(createOffer -> createOffer.getTargetSlabs().stream()
479
                            .map(x -> x.getItemCriteriaPayouts())
480
                            .flatMap(List::stream)
481
                            .map(ItemCriteriaPayout::getItemCriteria)
482
                            .map(ItemCriteria::getBrands)
483
                            .flatMap(List::stream)
484
                            .anyMatch(brand -> brand.equals(brandFilter)))
485
                    .collect(Collectors.toList());
34617 amit.gupta 486
        } else {
487
            publishedOffers = createOffers.stream().filter(createOffer -> createOffer.getTargetSlabs().stream()
34557 amit.gupta 488
                    .map(x -> x.getItemCriteriaPayouts())
489
                    .flatMap(List::stream)
490
                    .map(ItemCriteriaPayout::getItemCriteria)
34559 amit.gupta 491
                    .map(ItemCriteria::getCatalogIds)
34557 amit.gupta 492
                    .flatMap(List::stream)
34559 amit.gupta 493
                    .noneMatch(catalogId -> catalogRepository.selectCatalogById(catalogId).getBrand().equals("Live Demo"))).collect(Collectors.toList());
34176 tejus.loha 494
        }
29926 amit.gupta 495
 
34176 tejus.loha 496
        model.addAttribute("publishedOffers", publishedOffers);
497
 
32505 amit.gupta 498
        return "published-offers";
499
    }
29926 amit.gupta 500
 
32505 amit.gupta 501
    @PostMapping(value = "/offers/upload")
502
    public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
503
            throws Exception {
504
        offerService.createOffers(targetFile.getInputStream());
505
        model.addAttribute("response1", true);
506
        return "response";
507
    }
29926 amit.gupta 508
 
32505 amit.gupta 509
    @RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
510
    public String getOfferMargins(HttpServletRequest request,
511
                                  @RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
512
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
513
        boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
514
        CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);
29900 amit.gupta 515
 
32505 amit.gupta 516
        model.addAttribute("offer", createOfferRequest);
35415 amit 517
        model.addAttribute("isAdmin", isAdmin);
29900 amit.gupta 518
 
32505 amit.gupta 519
        return "offer_margin_detail_partner";
30470 amit.gupta 520
 
32505 amit.gupta 521
    }
29900 amit.gupta 522
 
35501 ranu 523
    @RequestMapping(value = "/todayOffer")
524
    public String todayOffer(HttpServletRequest request, Model model, @RequestParam(name = "fofoId", defaultValue = "0") int fofoId) throws ProfitMandiBusinessException {
525
 
526
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
527
        if (fofoId == 0) {
528
            fofoId = loginDetails.getFofoId();
529
        }
530
        List<BrandCatalog> allBrands = brandsService.getBrandsToDisplay(3);
531
 
532
        // 1. IDs to exclude entirely
533
        List<Integer> excludedIds = Arrays.asList(132, 133, 28, 17, 125);
534
 
535
        // 2. Brands that must come first (in this specific order)
536
        List<String> priorityOrder = Arrays.asList("Samsung", "Oppo", "Vivo", "Xiaomi", "Realme");
537
 
538
        List<BrandCatalog> sortedBrands = allBrands.stream()
539
                .filter(brand -> !excludedIds.contains(brand.getId())) // Remove excluded
540
                .sorted((b1, b2) -> {
541
                    // Get the index of the brand name in our priority list
542
                    int index1 = priorityOrder.indexOf(b1.getName());
543
                    int index2 = priorityOrder.indexOf(b2.getName());
544
 
545
                    // If brand is NOT in priority list, give it a high index (move to bottom)
546
                    int p1 = (index1 != -1) ? index1 : Integer.MAX_VALUE;
547
                    int p2 = (index2 != -1) ? index2 : Integer.MAX_VALUE;
548
 
549
                    if (p1 != p2) {
550
                        return Integer.compare(p1, p2); // Sort by priority first
551
                    }
552
 
553
                    // If both are "Others", sort them alphabetically
554
                    return b1.getName().compareToIgnoreCase(b2.getName());
555
                })
556
                .collect(Collectors.toList());
557
 
558
        model.addAttribute("brands", sortedBrands);
559
        model.addAttribute("fofoId", fofoId);
560
        model.addAttribute("date", FormattingUtils.format(LocalDateTime.now()));
561
 
562
        return "today-offer";
563
    }
564
 
565
    @RequestMapping(value = "/todayOfferList")
566
    public String todayOfferList(HttpServletRequest request, Model model, @RequestParam String brand, @RequestParam(defaultValue = "0", required = false) int fofoId) throws ProfitMandiBusinessException {
567
 
568
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
569
        if (fofoId == 0) {
570
            fofoId = loginDetails.getFofoId();
571
        }
572
 
573
        List<String> brands = brandsService.getBrandsToDisplay(3).stream().map(x -> x.getName()).collect(Collectors.toList());
574
 
575
        List<TodayOfferModel> todayOfferModels = todayOfferService.findAllTodayOffer(brand, fofoId);
576
 
577
        List<TodayOfferModel> groupedOffers = todayOfferService.groupSameOffers(todayOfferModels);
578
        model.addAttribute("brands", brands);
35505 ranu 579
        model.addAttribute("fofoId", fofoId);
35501 ranu 580
        model.addAttribute("todayOfferModels", todayOfferModels);
581
        model.addAttribute("groupedOffers", groupedOffers);
582
 
583
 
584
        return "today-offer-list";
585
    }
586
 
587
    @RequestMapping(value = "/todayFofoOffer")
588
    public String todayFofoOffer(HttpServletRequest request, Model model, @RequestParam(name = "fofoId", defaultValue = "0") int fofoId) throws ProfitMandiBusinessException {
589
 
590
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
591
        if (fofoId == 0) {
592
            fofoId = loginDetails.getFofoId();
593
        }
594
        List<BrandCatalog> allBrands = brandsService.getBrandsToDisplay(3);
595
 
596
        // 1. IDs to exclude entirely
597
        List<Integer> excludedIds = Arrays.asList(132, 133, 28, 17, 125);
598
 
599
        // 2. Brands that must come first (in this specific order)
600
        List<String> priorityOrder = Arrays.asList("Samsung", "Oppo", "Vivo", "Xiaomi", "Realme");
601
 
602
        List<BrandCatalog> sortedBrands = allBrands.stream()
603
                .filter(brand -> !excludedIds.contains(brand.getId())) // Remove excluded
604
                .sorted((b1, b2) -> {
605
                    // Get the index of the brand name in our priority list
606
                    int index1 = priorityOrder.indexOf(b1.getName());
607
                    int index2 = priorityOrder.indexOf(b2.getName());
608
 
609
                    // If brand is NOT in priority list, give it a high index (move to bottom)
610
                    int p1 = (index1 != -1) ? index1 : Integer.MAX_VALUE;
611
                    int p2 = (index2 != -1) ? index2 : Integer.MAX_VALUE;
612
 
613
                    if (p1 != p2) {
614
                        return Integer.compare(p1, p2); // Sort by priority first
615
                    }
616
 
617
                    // If both are "Others", sort them alphabetically
618
                    return b1.getName().compareToIgnoreCase(b2.getName());
619
                })
620
                .collect(Collectors.toList());
621
 
622
        model.addAttribute("brands", sortedBrands);
623
        model.addAttribute("fofoId", fofoId);
624
        model.addAttribute("date", FormattingUtils.format(LocalDateTime.now()));
625
 
626
        return "today-fofo-offer";
627
    }
628
 
35886 amit 629
    // ===== Offer Partner & Target Management (Admin Only) =====
630
 
631
    @RequestMapping(value = "/offer/partners", method = RequestMethod.GET)
632
    public String getOfferPartners(HttpServletRequest request, @RequestParam int offerId, Model model) throws Exception {
633
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
634
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
635
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
636
        }
637
        CreateOfferRequest offer = offerService.getOffer(0, offerId);
638
        Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
639
 
35888 amit 640
        // Partners are stored in partner_criteria JSON, not in offer_partners table
641
        List<Integer> partnerFofoIds = offer.getPartnerCriteria() != null
642
                ? offer.getPartnerCriteria().getFofoIds() : new ArrayList<>();
643
        if (partnerFofoIds == null) partnerFofoIds = new ArrayList<>();
644
 
35886 amit 645
        List<Integer> allFofoIds = fofoStoreRepository.selectActiveStores().stream()
646
                .map(x -> x.getId()).collect(Collectors.toList());
647
        Map<Integer, CustomRetailer> allRetailersMap = allFofoIds.stream()
648
                .map(id -> customRetailerMap.get(id))
649
                .filter(x -> x != null)
650
                .collect(Collectors.toMap(CustomRetailer::getPartnerId, x -> x));
651
 
652
        model.addAttribute("offer", offer);
653
        model.addAttribute("offerId", offerId);
35888 amit 654
        model.addAttribute("partnerFofoIds", partnerFofoIds);
35886 amit 655
        model.addAttribute("customRetailerMap", customRetailerMap);
656
        model.addAttribute("allRetailersMap", allRetailersMap);
657
        return "offer_partners";
658
    }
659
 
660
    @RequestMapping(value = "/offer/removePartners", method = RequestMethod.POST)
661
    public ResponseEntity<?> removePartnersFromOffer(HttpServletRequest request,
662
            @RequestParam int offerId, @RequestParam List<Integer> fofoIds,
663
            @RequestParam(required = false, defaultValue = "false") boolean createNewOffer,
664
            @RequestParam(required = false) List<Integer> targets) throws Exception {
665
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
666
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
667
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
668
        }
669
        Offer offer = offerRepository.selectById(offerId);
670
        YearMonth ym = YearMonth.from(offer.getStartDate());
671
 
672
        offerService.removePartnersFromOffer(offerId, fofoIds);
673
 
674
        Integer newOfferId = null;
675
        String message;
676
        if (createNewOffer && targets != null && !targets.isEmpty()) {
677
            newOfferId = offerService.cloneOfferForPartners(offerId, fofoIds, targets);
678
            message = "Partner(s) removed from Offer #" + offerId + ". New Offer #" + newOfferId + " created (Unpublished).";
679
        } else {
680
            message = "Partner(s) removed from Offer #" + offerId + ".";
681
        }
682
 
35894 amit 683
        // Evict partner-to-offer mapping (removed partners no longer see this offer)
35892 amit 684
        oneDayCacheManager.getCache("allOffers").evict(ym);
685
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
686
        redisShortCacheManager.getCache("publishedOffersWithAchievement").clear();
35894 amit 687
        // Evict offer detail (price circular) for removed partners; key=(fofoId, offerId), 0=admin
35892 amit 688
        for (Integer fofoId : fofoIds) {
689
            thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(fofoId, offerId));
690
        }
691
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(0, offerId));
692
 
35886 amit 693
        Map<String, Object> response = new HashMap<>();
694
        response.put("message", message);
695
        response.put("newOfferId", newOfferId);
696
        response.put("yearMonth", ym.toString());
697
        return responseSender.ok(response);
698
    }
699
 
700
    @RequestMapping(value = "/offer/addPartners", method = RequestMethod.POST)
701
    public ResponseEntity<?> addPartnersToOffer(HttpServletRequest request,
702
            @RequestParam int offerId, @RequestParam List<Integer> fofoIds) throws Exception {
703
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
704
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
705
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
706
        }
707
        offerService.addPartnersToOffer(offerId, fofoIds);
708
 
709
        Offer offer = offerRepository.selectById(offerId);
710
        YearMonth ym = YearMonth.from(offer.getStartDate());
35894 amit 711
        // Evict partner-to-offer mapping (added partners now see this offer)
35892 amit 712
        oneDayCacheManager.getCache("allOffers").evict(ym);
713
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
714
        redisShortCacheManager.getCache("publishedOffersWithAchievement").clear();
35894 amit 715
        // Evict offer detail (price circular) for added partners; key=(fofoId, offerId), 0=admin
35892 amit 716
        for (Integer fofoId : fofoIds) {
717
            thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(fofoId, offerId));
718
        }
719
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(0, offerId));
35886 amit 720
 
721
        Map<String, Object> response = new HashMap<>();
722
        response.put("message", "Partner(s) added to Offer #" + offerId + ".");
723
        response.put("yearMonth", ym.toString());
724
        return responseSender.ok(response);
725
    }
726
 
727
    @RequestMapping(value = "/offer/updateTargets", method = RequestMethod.POST)
728
    public ResponseEntity<?> updateOfferTargets(HttpServletRequest request,
729
            @RequestParam int offerId, @RequestParam List<Integer> targets) throws Exception {
730
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
731
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
732
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
733
        }
734
        offerService.updateOfferTargets(offerId, targets);
735
 
736
        Offer offer = offerRepository.selectById(offerId);
737
        YearMonth ym = YearMonth.from(offer.getStartDate());
35894 amit 738
        // Evict offer listing and partner-to-offer mapping (targets changed for published offer)
35886 amit 739
        oneDayCacheManager.getCache("allOffers").evict(ym);
740
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
741
        redisShortCacheManager.getCache("publishedOffersWithAchievement").clear();
35894 amit 742
        // Evict offer detail (price circular) for all partners of this offer; key=(fofoId, offerId), 0=admin
35893 amit 743
        PartnerCriteria partnerCriteria = gson.fromJson(offer.getPartnerCriteria(), PartnerCriteria.class);
35892 amit 744
        if (partnerCriteria != null && partnerCriteria.getFofoIds() != null) {
745
            for (Integer fofoId : partnerCriteria.getFofoIds()) {
746
                thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(fofoId, offerId));
747
            }
748
        }
749
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").evict(new SimpleKey(0, offerId));
35886 amit 750
 
751
        return responseSender.ok("Targets updated for Offer #" + offerId);
752
    }
753
 
27895 amit.gupta 754
}