Subversion Repositories SmartDukaan

Rev

Rev 32855 | Rev 33043 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 32855 Rev 32868
Line 36... Line 36...
36
import org.apache.commons.io.output.ByteArrayOutputStream;
36
import org.apache.commons.io.output.ByteArrayOutputStream;
37
import org.apache.logging.log4j.LogManager;
37
import org.apache.logging.log4j.LogManager;
38
import org.apache.logging.log4j.Logger;
38
import org.apache.logging.log4j.Logger;
39
import org.apache.velocity.app.VelocityEngine;
39
import org.apache.velocity.app.VelocityEngine;
40
import org.springframework.beans.factory.annotation.Autowired;
40
import org.springframework.beans.factory.annotation.Autowired;
-
 
41
import org.springframework.beans.factory.annotation.Value;
41
import org.springframework.cache.CacheManager;
42
import org.springframework.cache.CacheManager;
42
import org.springframework.core.io.InputStreamResource;
43
import org.springframework.core.io.InputStreamResource;
43
import org.springframework.http.HttpHeaders;
44
import org.springframework.http.HttpHeaders;
44
import org.springframework.http.HttpStatus;
45
import org.springframework.http.HttpStatus;
45
import org.springframework.http.ResponseEntity;
46
import org.springframework.http.ResponseEntity;
Line 60... Line 61...
60
import java.awt.image.BufferedImage;
61
import java.awt.image.BufferedImage;
61
import java.io.ByteArrayInputStream;
62
import java.io.ByteArrayInputStream;
62
import java.io.File;
63
import java.io.File;
63
import java.io.FileNotFoundException;
64
import java.io.FileNotFoundException;
64
import java.io.InputStream;
65
import java.io.InputStream;
65
import java.time.Instant;
66
import java.time.*;
66
import java.time.LocalDate;
-
 
67
import java.time.LocalDateTime;
-
 
68
import java.time.YearMonth;
-
 
69
import java.util.List;
67
import java.util.List;
70
import java.util.*;
68
import java.util.*;
71
import java.util.stream.Collectors;
69
import java.util.stream.Collectors;
72
 
70
 
73
@Controller
71
@Controller
Line 149... Line 147...
149
    @RequestMapping(value = "/createOffer", method = RequestMethod.POST)
147
    @RequestMapping(value = "/createOffer", method = RequestMethod.POST)
150
    public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
148
    public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
151
                              Model model) throws Exception {
149
                              Model model) throws Exception {
152
        LOGGER.info("createOfferRequest [{}]", createOfferRequest);
150
        LOGGER.info("createOfferRequest [{}]", createOfferRequest);
153
        offerService.addOfferService(createOfferRequest);
151
        offerService.addOfferService(createOfferRequest);
154
        thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
152
        oneDayCacheManager.getCache("allOffers").evict(YearMonth.from(createOfferRequest.getStartDate()));
155
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
153
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
156
        return "response";
154
        return "response";
157
 
155
 
158
    }
156
    }
159
 
157
 
Line 164... Line 162...
164
        offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
162
        offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
165
        return "scheme_offer/published";
163
        return "scheme_offer/published";
166
 
164
 
167
    }
165
    }
168
 
166
 
-
 
167
    @Value("${prod}")
-
 
168
    private boolean isProd;
-
 
169
 
169
    @RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
170
    @RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
170
    public String activateOffer(HttpServletRequest request, @PathVariable(name = "offerId") String offerIdsString,
171
    public String activateOffer(HttpServletRequest request, @PathVariable(name = "offerId") String offerIdsString,
171
                                Model model, @RequestParam(defaultValue = "true") boolean active)
172
                                Model model, @RequestParam(defaultValue = "true") boolean active)
172
            throws ProfitMandiBusinessException, Exception {
173
            throws ProfitMandiBusinessException, Exception {
173
        List<Integer> offerIds = Arrays.stream(offerIdsString.split(",")).map(x -> Integer.parseInt(x))
174
        List<Integer> offerIds = Arrays.stream(offerIdsString.split(",")).map(x -> Integer.parseInt(x))
174
                .collect(Collectors.toList());
175
                .collect(Collectors.toList());
175
        List<Offer> offers = offerRepository.selectAllByIds(offerIds);
176
        List<Offer> offers = offerRepository.selectAllByIds(offerIds);
-
 
177
 
-
 
178
        //Consider only offers that have opposite status
-
 
179
        offers = offers.stream().filter(x -> x.isActive() != active).collect(Collectors.toList());
-
 
180
 
176
        boolean shouldEvict = false;
181
        Set<YearMonth> yearMonthsToEvict = new HashSet<>();
177
        for (Offer offer : offers) {
182
        for (Offer offer : offers) {
178
            offer.setAlreadyActive(offer.isActive());
-
 
179
            if (offer.isActive() != active) {
-
 
180
                offer.setActive(active);
183
            offer.setActive(active);
181
                shouldEvict = true;
184
            yearMonthsToEvict.add(YearMonth.from(offer.getStartDate()));
182
            }
-
 
183
        }
185
        }
184
        if (shouldEvict) {
186
        //Evict caches
-
 
187
        for (YearMonth ymToEvict : yearMonthsToEvict) {
185
            oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
188
            oneDayCacheManager.getCache("catalog.published_yearmonth").evict(ymToEvict);
-
 
189
            oneDayCacheManager.getCache("allOffers").evict(ymToEvict);
186
        }
190
        }
187
        for (Offer offer : offers) {
191
        if (active) {
188
            if (active && !offer.isAlreadyActive()) {
192
            for (Offer offer : offers) {
189
                this.sendNotification(offer);
193
                this.sendNotification(offer);
190
            }
194
            }
191
        }
195
        }
-
 
196
 
-
 
197
 
192
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
198
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
193
        return "response";
199
        return "response";
194
        // CreateOfferRequest createOfferRequest =
-
 
195
        // offerService.getCreateOfferRequest(offer);
-
 
196
        // model.addAttribute("offer", createOfferRequest);
-
 
197
        // return "offer_margin_detail_partner2";
-
 
198
    }
200
    }
199
 
201
 
200
    @RequestMapping(value = "/offer/testimage/{offerId}", method = RequestMethod.GET)
202
    @RequestMapping(value = "/offer/testimage/{offerId}", method = RequestMethod.GET)
201
    public String testOffer(HttpServletRequest request, @PathVariable int offerId, Model model,
203
    public String testOffer(HttpServletRequest request, @PathVariable int offerId, Model model,
202
                            @RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
204
                            @RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
Line 210... Line 212...
210
        String htmlContent = this.getContentFromTemplate("offer_margin_detail_notify", model1);
212
        String htmlContent = this.getContentFromTemplate("offer_margin_detail_notify", model1);
211
        model.addAttribute("response1", htmlContent);
213
        model.addAttribute("response1", htmlContent);
212
        return "response";
214
        return "response";
213
    }
215
    }
214
 
216
 
215
    @RequestMapping(value = "/offer/active/fofo/{fofoId}", method = RequestMethod.GET)
-
 
216
    public String sendOfferNotification(HttpServletRequest request, @PathVariable int fofoId, Model model,
-
 
217
                                        @RequestParam(defaultValue = "true") boolean active) 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("response1", mvcResponseSender.createResponseString(true));
-
 
223
        return "response";
-
 
224
    }
-
 
225
 
-
 
226
    private void sendNotification(Offer offer) throws Exception {
217
    private void sendNotification(Offer offer) throws Exception {
227
        if (!YearMonth.from(offer.getStartDate()).equals(YearMonth.now())) {
218
        if (!YearMonth.from(offer.getStartDate()).equals(YearMonth.now())) {
228
            return;
219
            return;
229
        }
220
        }
230
        String fileName = "offer-" + offer.getId() + ".png";
221
        String fileName = "offer-" + offer.getId() + ".png";
231
        String htmlFileName = fileName.replace("png", "html");
222
        //String htmlFileName = fileName.replace("png", "html");
232
        CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
223
        CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
233
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
224
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
234
        sendNotificationModel.setCampaignName("SchemeOffer");
225
        sendNotificationModel.setCampaignName("SchemeOffer");
235
        sendNotificationModel.setTitle(offer.getName());
226
        sendNotificationModel.setTitle(offer.getName());
236
        sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "
227
        sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "
Line 251... Line 242...
251
        LOGGER.info("this.getContentFromTemplate {}", htmlContent);
242
        LOGGER.info("this.getContentFromTemplate {}", htmlContent);
252
        fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
243
        fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
253
        // fileStreamsMap.put(htmlFileName, new
244
        // fileStreamsMap.put(htmlFileName, new
254
        // ByteArrayInputStream(htmlContent.getBytes()));
245
        // ByteArrayInputStream(htmlContent.getBytes()));
255
        List<Integer> fofoIds = null;
246
        List<Integer> fofoIds = null;
-
 
247
        if (isProd) {
256
        this.uploadFile(fileStreamsMap);
248
            this.uploadFile(fileStreamsMap);
-
 
249
        }
257
        if (createOfferRequest.getPartnerCriteria().getRegionIds().size() > 0
250
        if (createOfferRequest.getPartnerCriteria().getRegionIds().size() > 0
258
                || createOfferRequest.getPartnerCriteria().getFofoIds().size() > 0
251
                || createOfferRequest.getPartnerCriteria().getFofoIds().size() > 0
259
                || createOfferRequest.getPartnerCriteria().getRegionIds().size() > 0 ||
252
                || createOfferRequest.getPartnerCriteria().getPartnerTypes().size() > 0
260
                createOfferRequest.getPartnerCriteria().getExcludeFofoIds().size() > 0) {
253
                || createOfferRequest.getPartnerCriteria().getExcludeFofoIds().size() > 0) {
261
            fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
254
            fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
262
                    .map(x -> x.getKey()).collect(Collectors.toList());
255
                    .map(x -> x.getKey()).collect(Collectors.toList());
263
            List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(fofoIds);
256
            List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(fofoIds);
264
            sendNotificationModel.setUserIds(userIds);
257
            sendNotificationModel.setUserIds(userIds);
265
            notificationService.sendNotification(sendNotificationModel);
258
            notificationService.sendNotification(sendNotificationModel);