| 28371 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 37897 |
ranu |
3 |
import com.spice.profitmandi.common.solr.SolrService;
|
|
|
4 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.dtr.WebOffer;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.dtr.WebOfferProduct;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.dtr.WebOfferProductRepository;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.dtr.WebOfferRepository;
|
|
|
9 |
import com.spice.profitmandi.web.service.EmiOfferImportService;
|
| 28371 |
amit.gupta |
10 |
import org.apache.logging.log4j.LogManager;
|
|
|
11 |
import org.apache.logging.log4j.Logger;
|
|
|
12 |
import org.json.JSONObject;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 37897 |
ranu |
14 |
import org.springframework.http.ResponseEntity;
|
| 28371 |
amit.gupta |
15 |
import org.springframework.stereotype.Controller;
|
| 37897 |
ranu |
16 |
import org.springframework.transaction.annotation.Transactional;
|
| 28371 |
amit.gupta |
17 |
import org.springframework.ui.Model;
|
| 37897 |
ranu |
18 |
import org.springframework.web.bind.annotation.*;
|
|
|
19 |
import org.springframework.web.multipart.MultipartFile;
|
| 28371 |
amit.gupta |
20 |
|
| 37897 |
ranu |
21 |
import javax.servlet.http.HttpServletRequest;
|
|
|
22 |
import java.time.LocalDate;
|
|
|
23 |
import java.time.LocalDateTime;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
import java.util.Optional;
|
|
|
27 |
import java.util.stream.Collectors;
|
| 28371 |
amit.gupta |
28 |
|
|
|
29 |
@Controller
|
| 35458 |
amit |
30 |
@Transactional(rollbackFor = Throwable.class)
|
| 28371 |
amit.gupta |
31 |
public class WebOffersController {
|
|
|
32 |
|
|
|
33 |
private static final Logger LOGGER = LogManager.getLogger(WebOffersController.class);
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
ResponseSender<?> responseSender;
|
| 30596 |
tejbeer |
37 |
|
| 28371 |
amit.gupta |
38 |
@Autowired
|
|
|
39 |
WebOfferRepository webOfferRepository;
|
| 30596 |
tejbeer |
40 |
|
| 28371 |
amit.gupta |
41 |
@Autowired
|
|
|
42 |
WebOfferProductRepository webOfferProductRepository;
|
|
|
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
SolrService solrService;
|
|
|
46 |
|
| 37897 |
ranu |
47 |
@Autowired
|
|
|
48 |
EmiOfferImportService emiOfferImportService;
|
|
|
49 |
|
| 28371 |
amit.gupta |
50 |
@RequestMapping(value = "/web-offers", method = RequestMethod.GET)
|
|
|
51 |
public String getOfferListing(HttpServletRequest request, Model model) {
|
|
|
52 |
List<WebOffer> webListing = webOfferRepository.selectAllWebListing(Optional.of(true));
|
|
|
53 |
model.addAttribute("webListings", webListing);
|
|
|
54 |
return "web-offer";
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@RequestMapping(value = "/web-offer/add", method = RequestMethod.POST)
|
|
|
58 |
public String addWebListing(HttpServletRequest request, @RequestBody WebOffer webOffer, Model model) {
|
|
|
59 |
webOffer.setCreatedDate(LocalDateTime.now());
|
| 30610 |
tejbeer |
60 |
|
| 28371 |
amit.gupta |
61 |
webOfferRepository.persist(webOffer);
|
|
|
62 |
return getOfferListing(request, model);
|
|
|
63 |
}
|
| 30596 |
tejbeer |
64 |
|
|
|
65 |
@RequestMapping(value = "/getwebOffer/{webOfferId}", method = RequestMethod.GET)
|
|
|
66 |
public String getWebOfferbyId(HttpServletRequest request, Model model, @PathVariable int webOfferId)
|
|
|
67 |
throws Exception {
|
|
|
68 |
WebOffer webOffer = webOfferRepository.selectById(webOfferId);
|
|
|
69 |
|
|
|
70 |
LOGGER.info("webOffer {}", webOffer);
|
|
|
71 |
model.addAttribute("webOffer", webOffer);
|
|
|
72 |
return "web-offer-edit";
|
|
|
73 |
}
|
|
|
74 |
|
| 28371 |
amit.gupta |
75 |
@RequestMapping(value = "/web-offer/{webOfferId}", method = RequestMethod.GET)
|
|
|
76 |
public String getWebProductListing(HttpServletRequest request, Model model, @PathVariable int webOfferId)
|
|
|
77 |
throws Exception {
|
|
|
78 |
List<WebOfferProduct> webOfferProducts = webOfferProductRepository.selectAllByWebOfferId(webOfferId);
|
|
|
79 |
if (webOfferProducts.size() > 0) {
|
|
|
80 |
Map<Integer, JSONObject> entityMap = solrService.getContentByCatalogIds(
|
|
|
81 |
webOfferProducts.stream().map(x -> x.getEntityId()).collect(Collectors.toList()));
|
|
|
82 |
webOfferProducts.stream()
|
|
|
83 |
.forEach(x -> x.setProductName(entityMap.get(x.getEntityId()).getString("title_s")));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
WebOffer webOffer = webOfferRepository.selectById(webOfferId);
|
|
|
87 |
model.addAttribute("productListings", webOfferProducts);
|
| 30596 |
tejbeer |
88 |
model.addAttribute("webOffer", webOffer);
|
| 28371 |
amit.gupta |
89 |
return "web-offer-product";
|
|
|
90 |
}
|
| 30596 |
tejbeer |
91 |
|
| 28371 |
amit.gupta |
92 |
@RequestMapping(value = "/web-offer-product/add", method = RequestMethod.POST)
|
| 30596 |
tejbeer |
93 |
public String addWebOfferProduct(HttpServletRequest request, @RequestBody List<WebOfferProduct> webOfferProducts,
|
|
|
94 |
Model model) throws Exception {
|
| 28371 |
amit.gupta |
95 |
webOfferProducts.stream().forEach(webOfferProduct -> {
|
|
|
96 |
boolean exists = webOfferProductRepository.isExists(webOfferProduct.getWebOfferId(),
|
|
|
97 |
webOfferProduct.getEntityId());
|
|
|
98 |
if (!exists) {
|
|
|
99 |
webOfferProductRepository.persist(webOfferProduct);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
});
|
|
|
103 |
return getWebProductListing(request, model, webOfferProducts.get(0).getWebOfferId());
|
|
|
104 |
}
|
| 30596 |
tejbeer |
105 |
|
| 28371 |
amit.gupta |
106 |
@RequestMapping(value = "/web-offer-product/remove", method = RequestMethod.DELETE)
|
| 30596 |
tejbeer |
107 |
public String addWebOfferProduct(HttpServletRequest request, @RequestParam int webOfferProductId, Model model)
|
|
|
108 |
throws Exception {
|
| 28371 |
amit.gupta |
109 |
WebOfferProduct webOfferProduct = webOfferProductRepository.selectById(webOfferProductId);
|
|
|
110 |
webOfferProductRepository.delete(webOfferProduct);
|
|
|
111 |
return getWebProductListing(request, model, webOfferProduct.getWebOfferId());
|
|
|
112 |
}
|
|
|
113 |
|
| 30596 |
tejbeer |
114 |
@RequestMapping(value = "/web-offer-duration/update", method = RequestMethod.POST)
|
| 30610 |
tejbeer |
115 |
public String updateWebOfferDuration(HttpServletRequest request, @RequestBody WebOffer wo, Model model)
|
|
|
116 |
throws Exception {
|
|
|
117 |
WebOffer webOffer = webOfferRepository.selectById(wo.getId());
|
| 31323 |
tejbeer |
118 |
LOGGER.info("webOffer {}", webOffer);
|
| 30596 |
tejbeer |
119 |
|
| 30610 |
tejbeer |
120 |
if (webOffer != null) {
|
|
|
121 |
|
|
|
122 |
webOffer.setStartDate(wo.getStartDate());
|
|
|
123 |
webOffer.setEndDate(wo.getEndDate());
|
|
|
124 |
webOffer.setDetailedText(wo.getDetailedText());
|
| 31323 |
tejbeer |
125 |
webOffer.setLargeBannerUrl(wo.getLargeBannerUrl());
|
| 30610 |
tejbeer |
126 |
webOffer.setTitle(wo.getTitle());
|
| 30622 |
tejbeer |
127 |
webOfferRepository.persist(webOffer);
|
| 30610 |
tejbeer |
128 |
}
|
| 30596 |
tejbeer |
129 |
return getOfferListing(request, model);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
@RequestMapping(value = "/web-offer/order/{webListingId}", method = RequestMethod.POST)
|
| 28371 |
amit.gupta |
133 |
public String orderWebProductListing(HttpServletRequest request, Model model, @PathVariable int webListingId,
|
| 30596 |
tejbeer |
134 |
|
| 28371 |
amit.gupta |
135 |
@RequestBody List<Integer> webListingProductIds) throws Exception {
|
| 30596 |
tejbeer |
136 |
List<WebOfferProduct> productListings = webOfferProductRepository.selectAllByWebOfferId(webListingId);
|
| 28371 |
amit.gupta |
137 |
|
|
|
138 |
productListings.forEach(productListing -> {
|
|
|
139 |
productListing.setRank(webListingProductIds.indexOf(productListing.getId()) + 1);
|
|
|
140 |
});
|
|
|
141 |
return getWebProductListing(request, model, webListingId);
|
|
|
142 |
}
|
|
|
143 |
|
| 37897 |
ranu |
144 |
@RequestMapping(value = "/web-offer/import-emi-json", method = RequestMethod.POST)
|
|
|
145 |
@ResponseBody
|
|
|
146 |
public ResponseEntity<EmiOfferImportService.ImportResult> importEmiJson(
|
|
|
147 |
@RequestPart("file") MultipartFile file,
|
|
|
148 |
@RequestParam(value = "startDate", required = false) String startDate,
|
|
|
149 |
@RequestParam(value = "endDate", required = false) String endDate,
|
|
|
150 |
@RequestParam(value = "dryRun", defaultValue = "true") boolean dryRun) throws Exception {
|
|
|
151 |
|
|
|
152 |
LocalDate today = LocalDate.now();
|
|
|
153 |
LocalDateTime start = (startDate == null || startDate.isEmpty())
|
|
|
154 |
? today.atStartOfDay() : LocalDate.parse(startDate).atStartOfDay();
|
|
|
155 |
LocalDateTime end = (endDate == null || endDate.isEmpty())
|
|
|
156 |
? today.plusDays(30).atTime(23, 59, 59) : LocalDate.parse(endDate).atTime(23, 59, 59);
|
|
|
157 |
|
|
|
158 |
LOGGER.info("EmiImport: file={} size={} dryRun={} start={} end={}",
|
|
|
159 |
file.getOriginalFilename(), file.getSize(), dryRun, start, end);
|
|
|
160 |
|
|
|
161 |
EmiOfferImportService.ImportResult result = emiOfferImportService.importFromJson(
|
|
|
162 |
file.getInputStream(), start, end, dryRun);
|
|
|
163 |
return ResponseEntity.ok(result);
|
|
|
164 |
}
|
|
|
165 |
|
| 28371 |
amit.gupta |
166 |
}
|