| 28371 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 30017 |
amit.gupta |
3 |
import com.spice.profitmandi.common.solr.SolrService;
|
|
|
4 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 30596 |
tejbeer |
5 |
import com.spice.profitmandi.dao.entity.dtr.WebListing;
|
| 30017 |
amit.gupta |
6 |
import com.spice.profitmandi.dao.entity.dtr.WebOffer;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.dtr.WebOfferProduct;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.dtr.WebOfferProductRepository;
|
|
|
9 |
import com.spice.profitmandi.dao.repository.dtr.WebOfferRepository;
|
| 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;
|
|
|
14 |
import org.springframework.stereotype.Controller;
|
|
|
15 |
import org.springframework.ui.Model;
|
| 30017 |
amit.gupta |
16 |
import org.springframework.web.bind.annotation.*;
|
| 28371 |
amit.gupta |
17 |
|
| 30017 |
amit.gupta |
18 |
import javax.servlet.http.HttpServletRequest;
|
|
|
19 |
import javax.transaction.Transactional;
|
|
|
20 |
import java.time.LocalDateTime;
|
|
|
21 |
import java.util.List;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
import java.util.Optional;
|
|
|
24 |
import java.util.stream.Collectors;
|
| 28371 |
amit.gupta |
25 |
|
|
|
26 |
@Controller
|
|
|
27 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
28 |
public class WebOffersController {
|
|
|
29 |
|
|
|
30 |
private static final Logger LOGGER = LogManager.getLogger(WebOffersController.class);
|
|
|
31 |
|
|
|
32 |
@Autowired
|
|
|
33 |
ResponseSender<?> responseSender;
|
| 30596 |
tejbeer |
34 |
|
| 28371 |
amit.gupta |
35 |
@Autowired
|
|
|
36 |
WebOfferRepository webOfferRepository;
|
| 30596 |
tejbeer |
37 |
|
| 28371 |
amit.gupta |
38 |
@Autowired
|
|
|
39 |
WebOfferProductRepository webOfferProductRepository;
|
|
|
40 |
|
|
|
41 |
@Autowired
|
|
|
42 |
SolrService solrService;
|
|
|
43 |
|
|
|
44 |
@RequestMapping(value = "/web-offers", method = RequestMethod.GET)
|
|
|
45 |
public String getOfferListing(HttpServletRequest request, Model model) {
|
|
|
46 |
List<WebOffer> webListing = webOfferRepository.selectAllWebListing(Optional.of(true));
|
|
|
47 |
model.addAttribute("webListings", webListing);
|
|
|
48 |
return "web-offer";
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
@RequestMapping(value = "/web-offer/add", method = RequestMethod.POST)
|
|
|
52 |
public String addWebListing(HttpServletRequest request, @RequestBody WebOffer webOffer, Model model) {
|
|
|
53 |
webOffer.setCreatedDate(LocalDateTime.now());
|
| 30610 |
tejbeer |
54 |
|
| 28371 |
amit.gupta |
55 |
webOfferRepository.persist(webOffer);
|
|
|
56 |
return getOfferListing(request, model);
|
|
|
57 |
}
|
| 30596 |
tejbeer |
58 |
|
|
|
59 |
@RequestMapping(value = "/getwebOffer/{webOfferId}", method = RequestMethod.GET)
|
|
|
60 |
public String getWebOfferbyId(HttpServletRequest request, Model model, @PathVariable int webOfferId)
|
|
|
61 |
throws Exception {
|
|
|
62 |
WebOffer webOffer = webOfferRepository.selectById(webOfferId);
|
|
|
63 |
|
|
|
64 |
LOGGER.info("webOffer {}", webOffer);
|
|
|
65 |
model.addAttribute("webOffer", webOffer);
|
|
|
66 |
return "web-offer-edit";
|
|
|
67 |
}
|
|
|
68 |
|
| 28371 |
amit.gupta |
69 |
@RequestMapping(value = "/web-offer/{webOfferId}", method = RequestMethod.GET)
|
|
|
70 |
public String getWebProductListing(HttpServletRequest request, Model model, @PathVariable int webOfferId)
|
|
|
71 |
throws Exception {
|
|
|
72 |
List<WebOfferProduct> webOfferProducts = webOfferProductRepository.selectAllByWebOfferId(webOfferId);
|
|
|
73 |
if (webOfferProducts.size() > 0) {
|
|
|
74 |
Map<Integer, JSONObject> entityMap = solrService.getContentByCatalogIds(
|
|
|
75 |
webOfferProducts.stream().map(x -> x.getEntityId()).collect(Collectors.toList()));
|
|
|
76 |
webOfferProducts.stream()
|
|
|
77 |
.forEach(x -> x.setProductName(entityMap.get(x.getEntityId()).getString("title_s")));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
WebOffer webOffer = webOfferRepository.selectById(webOfferId);
|
|
|
81 |
model.addAttribute("productListings", webOfferProducts);
|
| 30596 |
tejbeer |
82 |
model.addAttribute("webOffer", webOffer);
|
| 28371 |
amit.gupta |
83 |
return "web-offer-product";
|
|
|
84 |
}
|
| 30596 |
tejbeer |
85 |
|
| 28371 |
amit.gupta |
86 |
@RequestMapping(value = "/web-offer-product/add", method = RequestMethod.POST)
|
| 30596 |
tejbeer |
87 |
public String addWebOfferProduct(HttpServletRequest request, @RequestBody List<WebOfferProduct> webOfferProducts,
|
|
|
88 |
Model model) throws Exception {
|
| 28371 |
amit.gupta |
89 |
webOfferProducts.stream().forEach(webOfferProduct -> {
|
|
|
90 |
boolean exists = webOfferProductRepository.isExists(webOfferProduct.getWebOfferId(),
|
|
|
91 |
webOfferProduct.getEntityId());
|
|
|
92 |
if (!exists) {
|
|
|
93 |
webOfferProductRepository.persist(webOfferProduct);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
});
|
|
|
97 |
return getWebProductListing(request, model, webOfferProducts.get(0).getWebOfferId());
|
|
|
98 |
}
|
| 30596 |
tejbeer |
99 |
|
| 28371 |
amit.gupta |
100 |
@RequestMapping(value = "/web-offer-product/remove", method = RequestMethod.DELETE)
|
| 30596 |
tejbeer |
101 |
public String addWebOfferProduct(HttpServletRequest request, @RequestParam int webOfferProductId, Model model)
|
|
|
102 |
throws Exception {
|
| 28371 |
amit.gupta |
103 |
WebOfferProduct webOfferProduct = webOfferProductRepository.selectById(webOfferProductId);
|
|
|
104 |
webOfferProductRepository.delete(webOfferProduct);
|
|
|
105 |
return getWebProductListing(request, model, webOfferProduct.getWebOfferId());
|
|
|
106 |
}
|
|
|
107 |
|
| 30596 |
tejbeer |
108 |
@RequestMapping(value = "/web-offer-duration/update", method = RequestMethod.POST)
|
| 30610 |
tejbeer |
109 |
public String updateWebOfferDuration(HttpServletRequest request, @RequestBody WebOffer wo, Model model)
|
|
|
110 |
throws Exception {
|
|
|
111 |
WebOffer webOffer = webOfferRepository.selectById(wo.getId());
|
| 30596 |
tejbeer |
112 |
|
| 30610 |
tejbeer |
113 |
if (webOffer != null) {
|
|
|
114 |
|
|
|
115 |
webOffer.setStartDate(wo.getStartDate());
|
|
|
116 |
webOffer.setEndDate(wo.getEndDate());
|
|
|
117 |
webOffer.setDetailedText(wo.getDetailedText());
|
|
|
118 |
webOffer.setTitle(wo.getTitle());
|
|
|
119 |
}
|
| 30596 |
tejbeer |
120 |
return getOfferListing(request, model);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
@RequestMapping(value = "/web-offer/order/{webListingId}", method = RequestMethod.POST)
|
| 28371 |
amit.gupta |
124 |
public String orderWebProductListing(HttpServletRequest request, Model model, @PathVariable int webListingId,
|
| 30596 |
tejbeer |
125 |
|
| 28371 |
amit.gupta |
126 |
@RequestBody List<Integer> webListingProductIds) throws Exception {
|
| 30596 |
tejbeer |
127 |
List<WebOfferProduct> productListings = webOfferProductRepository.selectAllByWebOfferId(webListingId);
|
| 28371 |
amit.gupta |
128 |
|
|
|
129 |
productListings.forEach(productListing -> {
|
|
|
130 |
productListing.setRank(webListingProductIds.indexOf(productListing.getId()) + 1);
|
|
|
131 |
});
|
|
|
132 |
return getWebProductListing(request, model, webListingId);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
}
|