| 37212 |
amit |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
4 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.catalog.Catalog;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
|
|
|
8 |
import com.spice.profitmandi.service.catalog.BrandsService;
|
| 37246 |
amit |
9 |
import com.spice.profitmandi.service.catalog.HotDealAttributes;
|
| 37212 |
amit |
10 |
import com.spice.profitmandi.service.catalog.ModelHotDealService;
|
| 37270 |
amit |
11 |
import com.spice.profitmandi.dao.service.solr.FofoSolr;
|
| 37212 |
amit |
12 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
13 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
14 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
15 |
import org.slf4j.Logger;
|
|
|
16 |
import org.slf4j.LoggerFactory;
|
|
|
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.stereotype.Controller;
|
| 37229 |
amit |
19 |
import org.springframework.transaction.annotation.Transactional;
|
| 37212 |
amit |
20 |
import org.springframework.ui.Model;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
24 |
|
|
|
25 |
import javax.servlet.http.HttpServletRequest;
|
|
|
26 |
import java.time.LocalDate;
|
|
|
27 |
import java.util.HashMap;
|
|
|
28 |
import java.util.List;
|
|
|
29 |
import java.util.Map;
|
|
|
30 |
import java.util.Set;
|
|
|
31 |
import java.util.stream.Collectors;
|
|
|
32 |
|
|
|
33 |
@Controller
|
| 37229 |
amit |
34 |
@Transactional(rollbackFor = Throwable.class)
|
| 37212 |
amit |
35 |
public class HotDealController {
|
|
|
36 |
|
|
|
37 |
private static final Logger LOGGER = LoggerFactory.getLogger(HotDealController.class);
|
|
|
38 |
private static final int MOBILE_BRAND_CATEGORY_ID = 3;
|
|
|
39 |
|
|
|
40 |
@Autowired
|
|
|
41 |
private ModelHotDealService modelHotDealService;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
|
|
44 |
private BrandsService brandsService;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
|
|
47 |
private CatalogRepository catalogRepository;
|
|
|
48 |
|
|
|
49 |
@Autowired
|
|
|
50 |
private CookiesProcessor cookiesProcessor;
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private MVCResponseSender mvcResponseSender;
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private ObjectMapper objectMapper;
|
|
|
57 |
|
|
|
58 |
@RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)
|
|
|
59 |
public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {
|
|
|
60 |
model.addAttribute("brands", brandsService.getBrandsToDisplay(MOBILE_BRAND_CATEGORY_ID));
|
|
|
61 |
model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
|
|
|
62 |
return "hot-deals";
|
|
|
63 |
}
|
|
|
64 |
|
| 37256 |
amit |
65 |
/** All deals across brands: server-side searched + paginated table fragment. */
|
|
|
66 |
@RequestMapping(value = "/hotDeals/all", method = RequestMethod.GET)
|
|
|
67 |
public String getAllHotDeals(@RequestParam(defaultValue = "1") int page,
|
|
|
68 |
@RequestParam(required = false, defaultValue = "") String q,
|
|
|
69 |
Model model) throws Exception {
|
|
|
70 |
int pageSize = 20;
|
|
|
71 |
long total = modelHotDealService.countDeals(q);
|
|
|
72 |
int pages = (int) Math.max(1, (total + pageSize - 1) / pageSize);
|
|
|
73 |
page = Math.min(Math.max(1, page), pages);
|
|
|
74 |
model.addAttribute("deals", modelHotDealService.searchDeals(q, page, pageSize));
|
|
|
75 |
model.addAttribute("total", total);
|
|
|
76 |
model.addAttribute("page", page);
|
|
|
77 |
model.addAttribute("pages", pages);
|
| 37212 |
amit |
78 |
return "hot-deals-table";
|
|
|
79 |
}
|
|
|
80 |
|
| 37256 |
amit |
81 |
/** Active-slot usage for a brand, e.g. "3 / 15". */
|
|
|
82 |
@RequestMapping(value = "/hotDeals/slots", method = RequestMethod.GET)
|
|
|
83 |
public String getSlots(@RequestParam int brandId, Model model) throws Exception {
|
|
|
84 |
model.addAttribute("response1", modelHotDealService.countActive(brandId)
|
|
|
85 |
+ " / " + ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
|
|
|
86 |
return "response";
|
|
|
87 |
}
|
|
|
88 |
|
| 37212 |
amit |
89 |
@RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
|
|
|
90 |
public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
|
| 37261 |
ranu |
91 |
List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId);
|
| 37212 |
amit |
92 |
Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
|
|
|
93 |
.filter(deal -> !"EXPIRED".equals(deal.getStatus()))
|
|
|
94 |
.map(ModelHotDeal::getCatalogItemId)
|
|
|
95 |
.collect(Collectors.toSet());
|
|
|
96 |
List<Map<String, Object>> models = catalogs.stream()
|
|
|
97 |
.filter(catalog -> !activeIds.contains(catalog.getId()))
|
|
|
98 |
.map(catalog -> {
|
|
|
99 |
Map<String, Object> entry = new HashMap<>();
|
|
|
100 |
entry.put("catalogId", catalog.getId());
|
|
|
101 |
entry.put("description", catalog.getDescription());
|
|
|
102 |
return entry;
|
|
|
103 |
})
|
|
|
104 |
.collect(Collectors.toList());
|
|
|
105 |
model.addAttribute("response1", objectMapper.writeValueAsString(models));
|
|
|
106 |
return "response";
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
@RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
|
|
|
110 |
public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
|
|
|
111 |
@RequestParam int catalogItemId, @RequestParam String startDate,
|
| 37246 |
amit |
112 |
@RequestParam String endDate,
|
|
|
113 |
@RequestParam(required = false) Integer warrantyMonths,
|
|
|
114 |
@RequestParam(required = false) String condition,
|
| 37270 |
amit |
115 |
@RequestParam(required = false) Boolean fresh,
|
| 37246 |
amit |
116 |
@RequestParam(required = false) Boolean financeMapping,
|
|
|
117 |
@RequestParam(required = false) Boolean affordability,
|
|
|
118 |
Model model) throws Exception {
|
|
|
119 |
// required=false so a missing field reaches the service's mandatory-field
|
|
|
120 |
// validation and comes back as a readable message instead of a Spring 400
|
| 37212 |
amit |
121 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
122 |
try {
|
| 37246 |
amit |
123 |
HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
|
| 37270 |
amit |
124 |
fresh, financeMapping, affordability);
|
| 37212 |
amit |
125 |
modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
|
| 37246 |
amit |
126 |
LocalDate.parse(endDate), attributes, loginDetails.getEmailId());
|
| 37212 |
amit |
127 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
128 |
} catch (ProfitMandiBusinessException e) {
|
|
|
129 |
LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
|
|
|
130 |
model.addAttribute("response1", e.getMessage());
|
|
|
131 |
}
|
|
|
132 |
return "response";
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
@RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
|
|
|
136 |
public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
|
|
|
137 |
@RequestParam String startDate, @RequestParam String endDate,
|
| 37246 |
amit |
138 |
@RequestParam(required = false) Integer warrantyMonths,
|
|
|
139 |
@RequestParam(required = false) String condition,
|
| 37270 |
amit |
140 |
@RequestParam(required = false) Boolean fresh,
|
| 37246 |
amit |
141 |
@RequestParam(required = false) Boolean financeMapping,
|
|
|
142 |
@RequestParam(required = false) Boolean affordability,
|
| 37212 |
amit |
143 |
Model model) throws Exception {
|
|
|
144 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
145 |
try {
|
| 37246 |
amit |
146 |
HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
|
| 37270 |
amit |
147 |
fresh, financeMapping, affordability);
|
| 37246 |
amit |
148 |
modelHotDealService.updateDeal(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
|
|
|
149 |
attributes, loginDetails.getEmailId());
|
| 37212 |
amit |
150 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
151 |
} catch (ProfitMandiBusinessException e) {
|
|
|
152 |
LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
|
|
|
153 |
model.addAttribute("response1", e.getMessage());
|
|
|
154 |
}
|
|
|
155 |
return "response";
|
|
|
156 |
}
|
|
|
157 |
|
| 37270 |
amit |
158 |
@Autowired
|
|
|
159 |
private FofoSolr fofoSolr;
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Manual, explicit Solr push for the selected deal models (Category team button).
|
|
|
163 |
* Calls the indexer directly - deliberately NOT the prod-gated change event - so
|
|
|
164 |
* curated models are searchable immediately without waiting for the daily sync.
|
|
|
165 |
*/
|
|
|
166 |
@RequestMapping(value = "/hotDeals/pushSolr", method = RequestMethod.POST)
|
|
|
167 |
public String pushDealsToSolr(@RequestParam String catalogIds, Model model) throws Exception {
|
|
|
168 |
List<String> ok = new java.util.ArrayList<>();
|
|
|
169 |
List<String> failed = new java.util.ArrayList<>();
|
|
|
170 |
for (String raw : catalogIds.split(",")) {
|
|
|
171 |
String id = raw.trim();
|
|
|
172 |
if (id.isEmpty()) {
|
|
|
173 |
continue;
|
|
|
174 |
}
|
|
|
175 |
try {
|
|
|
176 |
fofoSolr.updateSingleCatalog(Integer.parseInt(id));
|
|
|
177 |
ok.add(id);
|
|
|
178 |
} catch (Exception e) {
|
|
|
179 |
LOGGER.error("Manual Solr push failed for catalogId={}", id, e);
|
|
|
180 |
failed.add(id + " (" + e.getMessage() + ")");
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
String summary = "Pushed " + ok.size() + " model(s) to Solr"
|
|
|
184 |
+ (failed.isEmpty() ? "" : "; FAILED: " + String.join(", ", failed));
|
|
|
185 |
model.addAttribute("response1", summary);
|
|
|
186 |
return "response";
|
|
|
187 |
}
|
|
|
188 |
|
| 37212 |
amit |
189 |
@RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
|
|
|
190 |
public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
|
|
|
191 |
try {
|
|
|
192 |
modelHotDealService.removeDeal(id);
|
|
|
193 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
194 |
} catch (ProfitMandiBusinessException e) {
|
|
|
195 |
LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
|
|
|
196 |
model.addAttribute("response1", e.getMessage());
|
|
|
197 |
}
|
|
|
198 |
return "response";
|
|
|
199 |
}
|
|
|
200 |
}
|