Subversion Repositories SmartDukaan

Rev

Rev 36347 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36342 amit 1
package com.spice.profitmandi.service.offers;
2
 
3
import com.spice.profitmandi.dao.entity.transaction.CronBatch;
4
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
5
import com.spice.profitmandi.dao.model.CreateOfferRequest;
6
import com.spice.profitmandi.service.cron.CronBatchService;
7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Service;
11
 
12
import java.util.LinkedHashMap;
13
import java.util.List;
14
 
15
/**
16
 * Orchestrates offer processing with per-partner transaction isolation and batch tracking.
17
 *
18
 * NO @Transactional on this class or any of its methods — the orchestrator must not
19
 * carry an outer transaction. Each partner's write work runs in REQUIRES_NEW via
20
 * OfferProcessingHelper; batch metadata writes run in REQUIRES_NEW via CronBatchService.
21
 * If the caller is already inside a transaction (e.g., controller class-level @Transactional),
22
 * REQUIRES_NEW will suspend it cleanly.
23
 */
24
@Service
25
public class OfferBatchService {
26
 
27
    private static final Logger LOGGER = LogManager.getLogger(OfferBatchService.class);
28
 
29
    @Autowired
30
    private OfferService offerService;
31
 
32
    @Autowired
33
    private OfferProcessingHelper offerProcessingHelper;
34
 
35
    @Autowired
36
    private CronBatchService cronBatchService;
37
 
38
    public void processOfferWithBatch(int offerId) throws Exception {
39
        CreateOfferRequest createOfferRequest = offerProcessingHelper.loadOfferRequest(offerId);
40
        if (createOfferRequest == null) {
41
            return;
42
        }
43
        LOGGER.info("Processing offer {} (type={}) with batch tracking", offerId, createOfferRequest.getSchemeType());
44
 
45
        if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
46
            processActivationOfferWithBatch(offerId, createOfferRequest);
47
        } else if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
48
            processSellinOfferWithBatch(offerId, createOfferRequest);
49
        } else {
50
            LOGGER.warn("Offer {} has unsupported scheme type: {}", offerId, createOfferRequest.getSchemeType());
51
        }
52
    }
53
 
54
    private void processActivationOfferWithBatch(int offerId, CreateOfferRequest createOfferRequest) throws Exception {
55
        List<OfferPartnerPayoutData> partnerPayouts;
56
        try {
57
            partnerPayouts = offerService.calculateOfferPayouts(createOfferRequest);
58
        } catch (Exception e) {
59
            LOGGER.error("Failed to calculate activation payouts for offer {}: {}", offerId, e.getMessage());
60
            return;
61
        }
62
 
63
        if (partnerPayouts.isEmpty()) {
64
            LOGGER.info("No eligible partners for activation offer {}", offerId);
65
            return;
66
        }
67
 
68
        LinkedHashMap<Integer, String> fofoIdPartnerNameMap = new LinkedHashMap<>();
69
        for (OfferPartnerPayoutData data : partnerPayouts) {
70
            fofoIdPartnerNameMap.put(data.getFofoId(), "fofo-" + data.getFofoId());
71
        }
72
 
73
        CronBatch batch = cronBatchService.createBatch("processActivationOffer-" + offerId, fofoIdPartnerNameMap);
74
 
75
        for (OfferPartnerPayoutData data : partnerPayouts) {
76
            try {
77
                offerProcessingHelper.processPartnerOfferPayout(
78
                        batch.getId(), data.getFofoId(), offerId, createOfferRequest.getDescription(),
79
                        data.getLineItemValueMap(), data.getItemCriteriaPayout(), data.getCriteriaId(),
80
                        data.getSerialNumberPaid(), data.getAgeingSummaryModelsMap(),
81
                        data.getEligiblePayoutValue(), data.isDiscount());
82
            } catch (Exception e) {
83
                LOGGER.error("Activation offer {} failed for fofoId={}: {}", offerId, data.getFofoId(), e.getMessage());
84
                cronBatchService.markItemFailed(batch.getId(), data.getFofoId(), e.getMessage());
85
            }
86
        }
87
 
88
        cronBatchService.finalizeBatch(batch.getId());
89
    }
90
 
91
    private void processSellinOfferWithBatch(int offerId, CreateOfferRequest createOfferRequest) throws Exception {
92
        List<SellinPartnerPayoutData> partnerPayouts;
93
        try {
94
            partnerPayouts = offerService.calculateSellinPayouts(createOfferRequest);
95
        } catch (Exception e) {
96
            LOGGER.error("Failed to calculate sellin payouts for offer {}: {}", offerId, e.getMessage());
97
            return;
98
        }
99
 
100
        if (partnerPayouts.isEmpty()) {
101
            LOGGER.info("No eligible partners for sellin offer {}", offerId);
102
            return;
103
        }
104
 
105
        LinkedHashMap<Integer, String> fofoIdPartnerNameMap = new LinkedHashMap<>();
106
        for (SellinPartnerPayoutData data : partnerPayouts) {
107
            fofoIdPartnerNameMap.put(data.getFofoId(), "fofo-" + data.getFofoId());
108
        }
109
 
110
        CronBatch batch = cronBatchService.createBatch("processSellinOffer-" + offerId, fofoIdPartnerNameMap);
111
 
112
        for (SellinPartnerPayoutData data : partnerPayouts) {
113
            try {
114
                offerProcessingHelper.processPartnerSellinPayout(
115
                        batch.getId(), data.getFofoId(), offerId, createOfferRequest.getDescription(),
116
                        data.getSerialNumberInventoryItemMap(), data.getItemCriteriaPayout(), data.getCriteriaId(),
117
                        data.getSerialNumberPaid(), data.getEligiblePayoutValue(), data.isDiscount());
118
            } catch (Exception e) {
119
                LOGGER.error("Sellin offer {} failed for fofoId={}: {}", offerId, data.getFofoId(), e.getMessage());
120
                cronBatchService.markItemFailed(batch.getId(), data.getFofoId(), e.getMessage());
121
            }
122
        }
123
 
124
        cronBatchService.finalizeBatch(batch.getId());
125
    }
126
}