Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
31732 amit.gupta 1
package com.smartdukaan.cron.scheduled.b2b;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.dao.entity.catalog.Item;
5
import com.spice.profitmandi.dao.entity.dtr.WebListing;
6
import com.spice.profitmandi.dao.entity.dtr.WebProductListing;
37677 ranu 7
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
31732 amit.gupta 8
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
9
import com.spice.profitmandi.dao.repository.catalog.*;
10
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
11
import com.spice.profitmandi.dao.repository.dtr.WebProductListingRepository;
12
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
13
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
14
import com.spice.profitmandi.dao.service.solr.FofoSolr;
37567 amit 15
import com.spice.profitmandi.service.catalog.CatalogDelistService;
31732 amit.gupta 16
import in.shop2020.model.v1.order.OrderStatus;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
37567 amit 20
import org.springframework.beans.factory.annotation.Value;
31732 amit.gupta 21
import org.springframework.scheduling.annotation.Scheduled;
22
import org.springframework.stereotype.Component;
31754 tejbeer 23
import org.springframework.transaction.annotation.Transactional;
31732 amit.gupta 24
 
25
import java.time.LocalDate;
26
import java.time.LocalDateTime;
27
import java.util.*;
28
import java.util.stream.Collectors;
29
 
30
@Component
31754 tejbeer 31
@Transactional(rollbackFor = Throwable.class)
31732 amit.gupta 32
public class Listing {
33
    @Autowired
34
    private WebListingRepository webListingRepository;
35
 
36
    @Autowired
37
    private FofoSolr fofoSolr;
38
 
39
    @Autowired
40
    OrderRepository orderRepository;
41
 
42
    @Autowired
43
    ItemRepository itemRepository;
44
 
45
    @Autowired
46
    PriceDropRepository priceDropRepository;
47
 
48
    @Autowired
49
    CustomerOfferRepository customerOfferRepository;
50
 
51
    @Autowired
52
    CustomerOfferItemRepository customerOfferItemRepository;
53
 
54
    @Autowired
55
    SchemeRepository schemeRepository;
56
 
57
    @Autowired
58
    TagListingRepository tagListingRepository;
59
 
37567 amit 60
    @Autowired
61
    CatalogDelistService catalogDelistService;
31732 amit.gupta 62
 
37567 amit 63
    // Inline default: env profiles have NO fallback between each other, so a key
64
    // missing from one profile's properties would otherwise stop the context booting.
65
    @Value("${catalog.autoDelist.enabled:true}")
66
    private boolean autoDelistEnabled;
67
 
68
 
31732 amit.gupta 69
    private static final Logger LOGGER = LogManager.getLogger(Listing.class);
70
 
71
    @Autowired
72
    private WebProductListingRepository webProductListingRepository;
73
 
74
 
75
    public void pushDataToSolr() throws Exception {
76
        fofoSolr.pushData();
77
    }
78
 
79
    public void getBestSeller() throws ProfitMandiBusinessException {
80
        WebListing webListing = webListingRepository.selectByUrl("partner-best-sellers");
81
 
31754 tejbeer 82
        List<WebProductListing> webProductListings = webProductListingRepository.selectAllByWebListingId(webListing.getId());
31732 amit.gupta 83
 
31754 tejbeer 84
        List<Integer> catalogItemIds = orderRepository.selectAllOrderWeight(LocalDateTime.now().minusDays(30), LocalDateTime.now(), OrderStatus.DELIVERY_SUCCESS);
31732 amit.gupta 85
 
86
        LOGGER.info("catalogItemIds {}", catalogItemIds);
37677 ranu 87
        // Wipe first so a window that yields zero qualifying catalogs collapses
88
        // the list to empty instead of leaving yesterday's stale entries in place.
89
        webProductListingRepository.deleteByEqual(webListing.getId());
90
        int count = 0;
91
        for (Integer catalogItemId : catalogItemIds) {
31732 amit.gupta 92
 
37677 ranu 93
            WebProductListing webProductListing = new WebProductListing();
94
            webProductListing.setEntityId(catalogItemId);
95
            webProductListing.setWebListingId(webListing.getId());
96
            count = count + 1;
31732 amit.gupta 97
 
37677 ranu 98
            webProductListing.setRank(count + 1);
99
            webProductListingRepository.persist(webProductListing);
31732 amit.gupta 100
 
101
        }
102
    }
103
 
104
    public void getPriceDrop() throws ProfitMandiBusinessException {
105
        WebListing webListing = webListingRepository.selectByUrl("partner-price-drop");
106
 
37677 ranu 107
        // "Latest event wins" per catalog: group all price_drop rows in the
108
        // 30-day window by catalogItemId, take the row with the latest
109
        // affectedOn (tiebreak on id for determinism), and keep the catalog
110
        // only if that row is a drop (amount > 0). A hike after an earlier
111
        // drop removes the label immediately — previously the filter
112
        // considered any drop row in the window, so a later hike was ignored
113
        // and the catalog stayed labeled even when currently priced higher.
114
        Set<Integer> catalogItemIds = priceDropRepository.selectAllByDatesBetweenSortByDate(LocalDateTime.now().minusDays(30), LocalDateTime.now())
115
                .stream()
116
                .collect(Collectors.groupingBy(
117
                        PriceDrop::getCatalogItemId,
118
                        Collectors.maxBy(Comparator
119
                                .comparing(PriceDrop::getAffectedOn)
120
                                .thenComparingInt(PriceDrop::getId))))
121
                .entrySet().stream()
122
                .filter(e -> e.getValue().isPresent() && e.getValue().get().getAmount() > 0)
123
                .map(Map.Entry::getKey)
124
                .collect(Collectors.toSet());
31732 amit.gupta 125
 
126
        LOGGER.info("catalogItemIds {}", catalogItemIds);
37677 ranu 127
        // Wipe unconditionally: filter is on affectedOn (rolling 30 days), so
128
        // catalogs whose most recent drop slides out of the window must be
129
        // removed even when no new drops happened that day. Prior guard left
130
        // stale entries in place (see catalog 1026477 case).
131
        webProductListingRepository.deleteByEqual(webListing.getId());
132
        int count = 0;
133
        for (Integer catalogItemId : catalogItemIds) {
31732 amit.gupta 134
 
37677 ranu 135
            WebProductListing webProductListing = new WebProductListing();
136
            webProductListing.setEntityId(catalogItemId);
137
            webProductListing.setWebListingId(webListing.getId());
138
            count = count + 1;
139
            webProductListing.setRank(count);
140
            webProductListingRepository.persist(webProductListing);
31732 amit.gupta 141
 
142
        }
143
    }
144
 
145
    public void getNewLaunches() throws ProfitMandiBusinessException {
146
        WebListing webListing = webListingRepository.selectByUrl("new-launches");
147
 
31754 tejbeer 148
        List<Integer> catalogItemIds = tagListingRepository.getRecentLanuch(LocalDate.now().minusDays(30), LocalDate.now().plusDays(1));
31732 amit.gupta 149
 
37677 ranu 150
        // Wipe first so items that fall out of the 30-day window get removed
151
        // even when no fresh launches were tagged that day.
152
        webProductListingRepository.deleteByEqual(webListing.getId());
153
        int count = 0;
154
        for (Integer catalogItemId : catalogItemIds) {
31732 amit.gupta 155
 
37677 ranu 156
            WebProductListing webProductListing = new WebProductListing();
157
            webProductListing.setEntityId(catalogItemId);
158
            webProductListing.setWebListingId(webListing.getId());
159
            count = count + 1;
160
            webProductListing.setRank(count);
161
            webProductListingRepository.persist(webProductListing);
31732 amit.gupta 162
 
163
        }
164
    }
165
 
166
    public void getSpecialSupport() throws ProfitMandiBusinessException {
167
        WebListing webListing = webListingRepository.selectByUrl("special-support");
168
 
31754 tejbeer 169
        List<Integer> catalogItemIds = schemeRepository.getActiveScheme(SchemeType.SPECIAL_SUPPORT, LocalDate.now().plusDays(1));
31732 amit.gupta 170
 
171
        LOGGER.info("catalogItemIds {}", catalogItemIds);
37677 ranu 172
        // Wipe first so items whose scheme ended get cleared even when no
173
        // Special Support scheme is active on this day.
174
        webProductListingRepository.deleteByEqual(webListing.getId());
175
        int count = 0;
176
        for (Integer catalogItemId : catalogItemIds) {
31732 amit.gupta 177
 
37677 ranu 178
            WebProductListing webProductListing = new WebProductListing();
179
            webProductListing.setEntityId(catalogItemId);
180
            webProductListing.setWebListingId(webListing.getId());
181
            count = count + 1;
182
            webProductListing.setRank(count);
183
            webProductListingRepository.persist(webProductListing);
31732 amit.gupta 184
 
185
        }
186
    }
187
 
188
    public void getUpgradeOffer() throws ProfitMandiBusinessException {
189
        WebListing webListing = webListingRepository.selectByUrl("upgrade-offer");
190
        webProductListingRepository.deleteByEqual(webListing.getId());
191
 
31754 tejbeer 192
        List<Integer> customerOfferIds = customerOfferRepository.selectOffers(LocalDate.now()).stream().map(x -> x.getId()).collect(Collectors.toList());
31732 amit.gupta 193
 
31754 tejbeer 194
        List<Integer> catalogItemIds = customerOfferItemRepository.selectByOfferIdsRange(customerOfferIds, LocalDate.now()).stream().map(x -> x.getCatalogId()).distinct().collect(Collectors.toList());
31732 amit.gupta 195
 
196
        LOGGER.info("catalogItemIds {}", catalogItemIds);
197
        if (!catalogItemIds.isEmpty()) {
198
            int count = 0;
199
            for (Integer catalogItemId : catalogItemIds) {
200
 
201
                WebProductListing webProductListing = new WebProductListing();
202
                webProductListing.setEntityId(catalogItemId);
203
                webProductListing.setWebListingId(webListing.getId());
204
                count = count + 1;
205
                webProductListing.setRank(count);
206
                webProductListingRepository.persist(webProductListing);
207
 
208
            }
209
 
210
        }
211
    }
212
 
36058 ranu 213
    @Scheduled(cron = "0 0 6,18 * * *")
34555 tejus.loha 214
    public void scheduledPushDataToSolr() throws Throwable {
215
        this.pushDataToSolr();
216
    }
217
 
37567 amit 218
    /**
219
     * Retire dead catalogue, 30 minutes ahead of the 06:00 reindex above so the
220
     * same morning's Solr pass publishes it - a bulk UPDATE fires no
221
     * TagListingChangeListener event, so without that ordering the portal would
222
     * lag until 18:00.
223
     *
224
     * <p>Steady state is small: the 2026-09-10 backfill cleared a 2.5-year
225
     * backlog, so a run now catches only listings newly crossing the 24-month
226
     * line plus items whose stock just went to zero.
227
     */
228
    @Scheduled(cron = "0 30 5 * * *")
229
    public void scheduledDelistDeadListings() {
230
        if (!autoDelistEnabled) {
231
            LOGGER.info("catalog delist skipped: catalog.autoDelist.enabled=false");
232
            return;
233
        }
234
        LOGGER.info("catalog delist starting");
235
        LOGGER.info("catalog delist finished: {}", catalogDelistService.delistDeadListings());
236
    }
237
 
34555 tejus.loha 238
    @Scheduled(cron = "0 0 8 * * *")
239
    public void scheduledGetBestSeller() throws Throwable {
240
        this.getBestSeller();
241
    }
242
 
243
    @Scheduled(cron = "0 0 8 * * *")
244
    public void scheduledGetPriceDrop() throws Throwable {
245
        this.getPriceDrop();
246
    }
247
 
248
    @Scheduled(cron = "0 0 8 * * *")
249
    public void scheduledGetNewLaunches() throws Throwable {
250
        this.getNewLaunches();
251
    }
252
 
253
    @Scheduled(cron = "0 0 8 * * *")
254
    public void scheduledGetSpecialSupport() throws Throwable {
255
        this.getSpecialSupport();
256
    }
257
 
258
    @Scheduled(cron = "0 0 8 * * *")
259
    public void scheduledGetUpgradeOffer() throws Throwable {
260
        this.getUpgradeOffer();
261
    }
31732 amit.gupta 262
}
263