Subversion Repositories SmartDukaan

Rev

Rev 37288 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37288 Rev 37643
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import com.fasterxml.jackson.databind.ObjectMapper;
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.dao.entity.catalog.Catalog;
5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;
6
import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;
-
 
7
import com.spice.profitmandi.dao.entity.catalog.Catalog;
7
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
8
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
8
import com.spice.profitmandi.dao.service.solr.FofoSolr;
9
import com.spice.profitmandi.dao.service.solr.FofoSolr;
9
import com.spice.profitmandi.service.catalog.BrandsService;
10
import com.spice.profitmandi.service.catalog.BrandsService;
10
import com.spice.profitmandi.service.catalog.HotDealAttributes;
11
import com.spice.profitmandi.service.catalog.HotDealAttributes;
11
import com.spice.profitmandi.service.catalog.ModelHotDealService;
12
import com.spice.profitmandi.service.catalog.ModelHotDealService;
Line 21... Line 22...
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RequestParam;
24
 
25
 
25
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletRequest;
26
import java.time.LocalDate;
-
 
27
import java.util.HashMap;
27
import java.util.HashMap;
28
import java.util.List;
28
import java.util.List;
29
import java.util.Map;
29
import java.util.Map;
30
import java.util.Set;
30
import java.util.Set;
31
import java.util.stream.Collectors;
31
import java.util.stream.Collectors;
Line 55... Line 55...
55
    @Autowired
55
    @Autowired
56
    private ObjectMapper objectMapper;
56
    private ObjectMapper objectMapper;
57
 
57
 
58
    @RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)
58
    @RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)
59
    public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {
59
    public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {
60
        model.addAttribute("brands", brandsService.getAllActiveBrands());
-
 
61
        model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
-
 
62
        return "hot-deals";
60
        return "hot-deals";
63
    }
61
    }
64
 
62
 
65
    /** All deals across brands: server-side searched + paginated table fragment. */
63
    /** All hot deal models: server-side searched + paginated table fragment. */
66
    @RequestMapping(value = "/hotDeals/all", method = RequestMethod.GET)
64
    @RequestMapping(value = "/hotDeals/all", method = RequestMethod.GET)
67
    public String getAllHotDeals(@RequestParam(defaultValue = "1") int page,
65
    public String getAllHotDeals(@RequestParam(defaultValue = "1") int page,
68
                                 @RequestParam(required = false, defaultValue = "") String q,
66
                                 @RequestParam(required = false, defaultValue = "") String q,
69
                                 Model model) throws Exception {
67
                                 Model model) throws Exception {
70
        int pageSize = 20;
68
        int pageSize = 20;
71
        long total = modelHotDealService.countDeals(q);
69
        long total = modelHotDealService.countDeals(q);
72
        int pages = (int) Math.max(1, (total + pageSize - 1) / pageSize);
70
        int pages = (int) Math.max(1, (total + pageSize - 1) / pageSize);
73
        page = Math.min(Math.max(1, page), pages);
71
        page = Math.min(Math.max(1, page), pages);
74
        model.addAttribute("deals", modelHotDealService.searchDeals(q, page, pageSize));
72
        model.addAttribute("deals", modelHotDealService.listAll(q, page, pageSize));
75
        model.addAttribute("total", total);
73
        model.addAttribute("total", total);
76
        model.addAttribute("page", page);
74
        model.addAttribute("page", page);
77
        model.addAttribute("pages", pages);
75
        model.addAttribute("pages", pages);
78
        return "hot-deals-table";
76
        return "hot-deals-table";
79
    }
77
    }
80
 
78
 
81
    /** Active-slot usage for a brand, e.g. "3 / 15". */
79
    /**
82
    @RequestMapping(value = "/hotDeals/slots", method = RequestMethod.GET)
80
     * Every Hot Deal-brand model. One brand now, and no expiry, so there is nothing to
83
    public String getSlots(@RequestParam int brandId, Model model) throws Exception {
81
     * filter out. No category filter either: the brand spans mobile, refurbished, earbuds
84
        model.addAttribute("response1", modelHotDealService.countActive(brandId)
-
 
85
                + " / " + ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
82
     * and LED TV, so the old mobile-only restriction would have hidden most of it.
86
        return "response";
-
 
87
    }
83
     */
88
 
-
 
89
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
84
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
90
    public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
85
    public String getHotDealModels(Model model) throws Exception {
91
        List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId);
86
        List<Catalog> catalogs = catalogRepository.selectAllByCatalogIds(new java.util.ArrayList<>(
92
        Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
-
 
93
                .filter(deal -> !"EXPIRED".equals(deal.getStatus()))
87
                catalogRepository.selectCatalogIdsByBrand(ProfitMandiConstants.HOT_DEAL_BRAND)));
94
                .map(ModelHotDeal::getCatalogItemId)
-
 
95
                .collect(Collectors.toSet());
-
 
96
        List<Map<String, Object>> models = catalogs.stream()
88
        List<Map<String, Object>> models = catalogs.stream()
97
                .filter(catalog -> !activeIds.contains(catalog.getId()))
-
 
98
                .map(catalog -> {
89
                .map(catalog -> {
99
                    Map<String, Object> entry = new HashMap<>();
90
                    Map<String, Object> entry = new HashMap<>();
100
                    entry.put("catalogId", catalog.getId());
91
                    entry.put("catalogId", catalog.getId());
101
                    entry.put("description", catalog.getDescription());
92
                    entry.put("description", catalog.getDescription());
-
 
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());
102
                    return entry;
96
                    return entry;
103
                })
97
                })
104
                .collect(Collectors.toList());
98
                .collect(Collectors.toList());
105
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
99
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
106
        return "response";
100
        return "response";
107
    }
101
    }
108
 
102
 
-
 
103
    /**
-
 
104
     * Create or update the five pill attributes for one Hot Deal model. Replaces the old
109
    @RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
105
     * add/update pair: there is no date window to set and no cap to respect, so the only
110
    public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
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
     */
111
                             @RequestParam int catalogItemId, @RequestParam String startDate,
109
    @RequestMapping(value = "/hotDeals/saveAttributes", method = RequestMethod.POST)
112
                             @RequestParam String endDate,
110
    public String saveAttributes(HttpServletRequest request, @RequestParam int catalogItemId,
113
                             @RequestParam(required = false) Integer warrantyMonths,
111
                                 @RequestParam(required = false) Integer warrantyMonths,
114
                             @RequestParam(required = false) String condition,
112
                                 @RequestParam(required = false) String condition,
115
                             @RequestParam(required = false) Boolean fresh,
113
                                 @RequestParam(required = false) Boolean fresh,
116
                             @RequestParam(required = false) Boolean financeMapping,
114
                                 @RequestParam(required = false) Boolean financeMapping,
117
                             @RequestParam(required = false) Boolean affordability,
115
                                 @RequestParam(required = false) Boolean affordability,
-
 
116
                                 @RequestParam(required = false) Integer oemCatalogId,
118
                             Model model) throws Exception {
117
                                 Model model) throws Exception {
119
        // required=false so a missing field reaches the service's mandatory-field
118
        // 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
119
        // validation and comes back as a readable message instead of a Spring 400
121
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
120
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
122
        try {
121
        try {
123
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
122
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
124
                    fresh, financeMapping, affordability);
123
                    fresh, financeMapping, affordability);
125
            modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
-
 
126
                    LocalDate.parse(endDate), attributes, loginDetails.getEmailId());
124
            modelHotDealService.saveAttributes(catalogItemId, attributes, oemCatalogId, loginDetails.getEmailId());
127
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
125
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
128
        } catch (ProfitMandiBusinessException e) {
126
        } catch (ProfitMandiBusinessException e) {
129
            LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
127
            LOGGER.warn("Hot deal attributes rejected: {}", e.getMessage());
130
            model.addAttribute("response1", e.getMessage());
128
            model.addAttribute("response1", e.getMessage());
131
        }
129
        }
132
        return "response";
130
        return "response";
133
    }
131
    }
134
 
132
 
135
    @RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
-
 
136
    public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
-
 
-
 
133
    /**
137
                                @RequestParam String startDate, @RequestParam String endDate,
134
     * Catalogs under one OEM brand, for the optional channel-counterpart autosuggest.
138
                                @RequestParam(required = false) Integer warrantyMonths,
135
     * Scoped to the brand the hot-deal model came from, so ops cannot map a Samsung deal
139
                                @RequestParam(required = false) String condition,
136
     * onto a Vivo model; the service re-checks this on save.
-
 
137
     */
140
                                @RequestParam(required = false) Boolean fresh,
138
    @RequestMapping(value = "/hotDeals/oemModels", method = RequestMethod.GET)
141
                                @RequestParam(required = false) Boolean financeMapping,
139
    public String getOemModels(@RequestParam String brand, Model model) throws Exception {
142
                                @RequestParam(required = false) Boolean affordability,
140
        List<Map<String, Object>> models = new java.util.ArrayList<>();
143
                                Model model) throws Exception {
141
        if (brand != null && !brand.trim().isEmpty()
144
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
142
                && !ProfitMandiConstants.HOT_DEAL_BRAND.equalsIgnoreCase(brand.trim())) {
145
        try {
-
 
146
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
143
            for (Catalog catalog : catalogRepository.selectAllByCatalogIds(new java.util.ArrayList<>(
147
                    fresh, financeMapping, affordability);
144
                    catalogRepository.selectCatalogIdsByBrand(brand.trim())))) {
148
            modelHotDealService.updateDeal(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
145
                Map<String, Object> entry = new HashMap<>();
149
                    attributes, loginDetails.getEmailId());
146
                entry.put("catalogId", catalog.getId());
150
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
147
                entry.put("description", catalog.getDescription());
151
        } catch (ProfitMandiBusinessException e) {
148
                models.add(entry);
152
            LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
-
 
153
            model.addAttribute("response1", e.getMessage());
149
            }
154
        }
150
        }
-
 
151
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
155
        return "response";
152
        return "response";
156
    }
153
    }
157
 
154
 
158
    @Autowired
155
    @Autowired
159
    private FofoSolr fofoSolr;
156
    private FofoSolr fofoSolr;
Line 187... Line 184...
187
    }
184
    }
188
 
185
 
189
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
186
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
190
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
187
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
191
        try {
188
        try {
192
            modelHotDealService.removeDeal(id);
189
            modelHotDealService.removeAttributes(id);
193
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
190
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
194
        } catch (ProfitMandiBusinessException e) {
191
        } catch (ProfitMandiBusinessException e) {
195
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
192
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
196
            model.addAttribute("response1", e.getMessage());
193
            model.addAttribute("response1", e.getMessage());
197
        }
194
        }