Subversion Repositories SmartDukaan

Rev

Rev 37288 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
37643 amit 5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;
37212 amit 7
import com.spice.profitmandi.dao.entity.catalog.Catalog;
8
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
37288 ranu 9
import com.spice.profitmandi.dao.service.solr.FofoSolr;
37212 amit 10
import com.spice.profitmandi.service.catalog.BrandsService;
37246 amit 11
import com.spice.profitmandi.service.catalog.HotDealAttributes;
37212 amit 12
import com.spice.profitmandi.service.catalog.ModelHotDealService;
13
import com.spice.profitmandi.web.model.LoginDetails;
14
import com.spice.profitmandi.web.util.CookiesProcessor;
15
import com.spice.profitmandi.web.util.MVCResponseSender;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Controller;
37229 amit 20
import org.springframework.transaction.annotation.Transactional;
37212 amit 21
import org.springframework.ui.Model;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestParam;
25
 
26
import javax.servlet.http.HttpServletRequest;
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
        return "hot-deals";
61
    }
62
 
37643 amit 63
    /** All hot deal models: server-side searched + paginated table fragment. */
37256 amit 64
    @RequestMapping(value = "/hotDeals/all", method = RequestMethod.GET)
65
    public String getAllHotDeals(@RequestParam(defaultValue = "1") int page,
66
                                 @RequestParam(required = false, defaultValue = "") String q,
67
                                 Model model) throws Exception {
68
        int pageSize = 20;
69
        long total = modelHotDealService.countDeals(q);
70
        int pages = (int) Math.max(1, (total + pageSize - 1) / pageSize);
71
        page = Math.min(Math.max(1, page), pages);
37643 amit 72
        model.addAttribute("deals", modelHotDealService.listAll(q, page, pageSize));
37256 amit 73
        model.addAttribute("total", total);
74
        model.addAttribute("page", page);
75
        model.addAttribute("pages", pages);
37212 amit 76
        return "hot-deals-table";
77
    }
78
 
37643 amit 79
    /**
80
     * Every Hot Deal-brand model. One brand now, and no expiry, so there is nothing to
81
     * filter out. No category filter either: the brand spans mobile, refurbished, earbuds
82
     * and LED TV, so the old mobile-only restriction would have hidden most of it.
83
     */
37212 amit 84
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
37643 amit 85
    public String getHotDealModels(Model model) throws Exception {
86
        List<Catalog> catalogs = catalogRepository.selectAllByCatalogIds(new java.util.ArrayList<>(
87
                catalogRepository.selectCatalogIdsByBrand(ProfitMandiConstants.HOT_DEAL_BRAND)));
37212 amit 88
        List<Map<String, Object>> models = catalogs.stream()
89
                .map(catalog -> {
90
                    Map<String, Object> entry = new HashMap<>();
91
                    entry.put("catalogId", catalog.getId());
92
                    entry.put("description", catalog.getDescription());
37643 amit 93
                    // Drives the OEM mapping autosuggest, which is scoped to this brand.
94
                    ModelHotDeal attributes = modelHotDealService.getAttributes(catalog.getId());
95
                    entry.put("oemBrand", attributes == null ? "" : attributes.getOemBrand());
37212 amit 96
                    return entry;
97
                })
98
                .collect(Collectors.toList());
99
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
100
        return "response";
101
    }
102
 
37643 amit 103
    /**
104
     * Create or update the five pill attributes for one Hot Deal model. Replaces the old
105
     * add/update pair: there is no date window to set and no cap to respect, so the only
106
     * thing left to edit is the attributes, and whether a row already exists is an
107
     * implementation detail the caller should not have to know.
108
     */
109
    @RequestMapping(value = "/hotDeals/saveAttributes", method = RequestMethod.POST)
110
    public String saveAttributes(HttpServletRequest request, @RequestParam int catalogItemId,
111
                                 @RequestParam(required = false) Integer warrantyMonths,
112
                                 @RequestParam(required = false) String condition,
113
                                 @RequestParam(required = false) Boolean fresh,
114
                                 @RequestParam(required = false) Boolean financeMapping,
115
                                 @RequestParam(required = false) Boolean affordability,
116
                                 @RequestParam(required = false) Integer oemCatalogId,
117
                                 Model model) throws Exception {
37246 amit 118
        // required=false so a missing field reaches the service's mandatory-field
119
        // validation and comes back as a readable message instead of a Spring 400
37212 amit 120
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
121
        try {
37246 amit 122
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
37270 amit 123
                    fresh, financeMapping, affordability);
37643 amit 124
            modelHotDealService.saveAttributes(catalogItemId, attributes, oemCatalogId, loginDetails.getEmailId());
37212 amit 125
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
126
        } catch (ProfitMandiBusinessException e) {
37643 amit 127
            LOGGER.warn("Hot deal attributes rejected: {}", e.getMessage());
37212 amit 128
            model.addAttribute("response1", e.getMessage());
129
        }
130
        return "response";
131
    }
132
 
37643 amit 133
    /**
134
     * Catalogs under one OEM brand, for the optional channel-counterpart autosuggest.
135
     * Scoped to the brand the hot-deal model came from, so ops cannot map a Samsung deal
136
     * onto a Vivo model; the service re-checks this on save.
137
     */
138
    @RequestMapping(value = "/hotDeals/oemModels", method = RequestMethod.GET)
139
    public String getOemModels(@RequestParam String brand, Model model) throws Exception {
140
        List<Map<String, Object>> models = new java.util.ArrayList<>();
141
        if (brand != null && !brand.trim().isEmpty()
142
                && !ProfitMandiConstants.HOT_DEAL_BRAND.equalsIgnoreCase(brand.trim())) {
143
            for (Catalog catalog : catalogRepository.selectAllByCatalogIds(new java.util.ArrayList<>(
144
                    catalogRepository.selectCatalogIdsByBrand(brand.trim())))) {
145
                Map<String, Object> entry = new HashMap<>();
146
                entry.put("catalogId", catalog.getId());
147
                entry.put("description", catalog.getDescription());
148
                models.add(entry);
149
            }
37212 amit 150
        }
37643 amit 151
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
37212 amit 152
        return "response";
153
    }
154
 
37270 amit 155
    @Autowired
156
    private FofoSolr fofoSolr;
157
 
158
    /**
159
     * Manual, explicit Solr push for the selected deal models (Category team button).
160
     * Calls the indexer directly - deliberately NOT the prod-gated change event - so
161
     * curated models are searchable immediately without waiting for the daily sync.
162
     */
163
    @RequestMapping(value = "/hotDeals/pushSolr", method = RequestMethod.POST)
164
    public String pushDealsToSolr(@RequestParam String catalogIds, Model model) throws Exception {
165
        List<String> ok = new java.util.ArrayList<>();
166
        List<String> failed = new java.util.ArrayList<>();
167
        for (String raw : catalogIds.split(",")) {
168
            String id = raw.trim();
169
            if (id.isEmpty()) {
170
                continue;
171
            }
172
            try {
173
                fofoSolr.updateSingleCatalog(Integer.parseInt(id));
174
                ok.add(id);
175
            } catch (Exception e) {
176
                LOGGER.error("Manual Solr push failed for catalogId={}", id, e);
177
                failed.add(id + " (" + e.getMessage() + ")");
178
            }
179
        }
180
        String summary = "Pushed " + ok.size() + " model(s) to Solr"
181
                + (failed.isEmpty() ? "" : "; FAILED: " + String.join(", ", failed));
182
        model.addAttribute("response1", summary);
183
        return "response";
184
    }
185
 
37212 amit 186
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
187
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
188
        try {
37643 amit 189
            modelHotDealService.removeAttributes(id);
37212 amit 190
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
191
        } catch (ProfitMandiBusinessException e) {
192
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
193
            model.addAttribute("response1", e.getMessage());
194
        }
195
        return "response";
196
    }
197
}