| Line 86... |
Line 86... |
| 86 |
Map<Integer, BulkCreditSummary> bulkSummaryMap = sdCreditService.getCreditSummaryBulk();
|
86 |
Map<Integer, BulkCreditSummary> bulkSummaryMap = sdCreditService.getCreditSummaryBulk();
|
| 87 |
|
87 |
|
| 88 |
List<Integer> sortedFofoIds = customRetailerMap.keySet().stream().sorted().collect(Collectors.toList());
|
88 |
List<Integer> sortedFofoIds = customRetailerMap.keySet().stream().sorted().collect(Collectors.toList());
|
| 89 |
|
89 |
|
| 90 |
for (int fofoId : sortedFofoIds) {
|
90 |
for (int fofoId : sortedFofoIds) {
|
| 91 |
CreditAccount creditAccount = creditAccountMap.get(fofoId);
|
91 |
// Per-partner isolation: one partner's bad data (null limit/util in the
|
| 92 |
BulkCreditSummary bulkSummary = bulkSummaryMap.get(fofoId);
|
92 |
// compareTo, a getFirstBillingDate/getCurrentRisk failure, etc.) must not
|
| 93 |
BigDecimal utilizationAmount = bulkSummary != null ? bulkSummary.getUtilization() : BigDecimal.ZERO;
|
93 |
// abort the whole read phase and silently update zero partners. Mirrors the
|
| 94 |
|
- |
|
| 95 |
PartnerDailyInvestment partnerDailyInvestment;
|
94 |
// REQUIRES_NEW isolation the write phase (updateSinglePartnerLimit) already has.
|
| 96 |
try {
|
95 |
try {
|
| 97 |
partnerDailyInvestment = partnerInvestmentService.getInvestment(fofoId, 0);
|
96 |
CreditAccount creditAccount = creditAccountMap.get(fofoId);
|
| 98 |
} catch (ProfitMandiBusinessException e) {
|
97 |
BulkCreditSummary bulkSummary = bulkSummaryMap.get(fofoId);
|
| 99 |
LOGGER.error("Failed to get investment for fofoId={}: {}", fofoId, e.getMessage());
|
98 |
BigDecimal utilizationAmount = bulkSummary != null ? bulkSummary.getUtilization() : BigDecimal.ZERO;
|
| - |
|
99 |
|
| 100 |
continue;
|
100 |
PartnerDailyInvestment partnerDailyInvestment = partnerInvestmentService.getInvestment(fofoId, 0);
|
| 101 |
}
|
- |
|
| 102 |
|
101 |
|
| 103 |
BigDecimal suggestedAmount = getSuggestedAmount(creditAccount, partnerDailyInvestment, utilizationAmount, fofoSidbiLimitMap.get(fofoId));
|
102 |
BigDecimal suggestedAmount = getSuggestedAmount(creditAccount, partnerDailyInvestment, utilizationAmount, fofoSidbiLimitMap.get(fofoId));
|
| 104 |
SDCreditRequirement existing = sdCreditRequirementMap.get(fofoId);
|
103 |
SDCreditRequirement existing = sdCreditRequirementMap.get(fofoId);
|
| 105 |
|
104 |
|
| 106 |
if (existing == null) {
|
105 |
if (existing == null) {
|
| 107 |
// New partner — needs a record
|
106 |
// New partner — needs a record
|
| 108 |
changedPartners.add(new PartnerLimitUpdateData(
|
107 |
changedPartners.add(new PartnerLimitUpdateData(
|
| 109 |
fofoId, suggestedAmount, utilizationAmount,
|
108 |
fofoId, suggestedAmount, utilizationAmount,
|
| 110 |
suggestedAmount.subtract(utilizationAmount),
|
109 |
suggestedAmount.subtract(utilizationAmount),
|
| 111 |
CreditRisk.HIGH_RISK, true));
|
110 |
CreditRisk.HIGH_RISK, true));
|
| 112 |
continue;
|
111 |
continue;
|
| 113 |
}
|
112 |
}
|
| 114 |
|
113 |
|
| 115 |
LocalDateTime firstBillingDate = transactionRepository.getFirstBillingDate(fofoId);
|
114 |
LocalDateTime firstBillingDate = transactionRepository.getFirstBillingDate(fofoId);
|
| 116 |
CreditRisk newRisk = sdCreditService.getCurrentRisk(existing, firstBillingDate);
|
115 |
CreditRisk newRisk = sdCreditService.getCurrentRisk(existing, firstBillingDate);
|
| 117 |
|
116 |
|
| 118 |
BigDecimal currentLimit = existing.isHardLimit() ? existing.getLimit() : existing.getSuggestedLimit();
|
117 |
BigDecimal currentLimit = existing.isHardLimit() ? existing.getLimit() : existing.getSuggestedLimit();
|
| 119 |
BigDecimal newLimit = existing.isHardLimit() ? existing.getLimit() : suggestedAmount;
|
118 |
BigDecimal newLimit = existing.isHardLimit() ? existing.getLimit() : suggestedAmount;
|
| 120 |
BigDecimal newAvailable = newLimit.subtract(utilizationAmount);
|
119 |
BigDecimal newAvailable = newLimit.subtract(utilizationAmount);
|
| 121 |
|
120 |
|
| 122 |
// Compare: only include if something actually changed
|
121 |
// Compare: only include if something actually changed
|
| 123 |
boolean limitChanged = suggestedAmount.compareTo(existing.getSuggestedLimit()) != 0;
|
122 |
boolean limitChanged = suggestedAmount.compareTo(existing.getSuggestedLimit()) != 0;
|
| 124 |
boolean utilizationChanged = utilizationAmount.compareTo(existing.getUtilizedAmount()) != 0;
|
123 |
boolean utilizationChanged = utilizationAmount.compareTo(existing.getUtilizedAmount()) != 0;
|
| 125 |
boolean riskChanged = !newRisk.equals(existing.getRisk());
|
124 |
boolean riskChanged = !newRisk.equals(existing.getRisk());
|
| 126 |
|
125 |
|
| 127 |
if (limitChanged || utilizationChanged || riskChanged) {
|
126 |
if (limitChanged || utilizationChanged || riskChanged) {
|
| 128 |
LOGGER.info("fofoId={} changed: limit {}→{}, util {}→{}, risk {}→{}",
|
127 |
LOGGER.info("fofoId={} changed: limit {}→{}, util {}→{}, risk {}→{}",
|
| 129 |
fofoId,
|
128 |
fofoId,
|
| 130 |
existing.getSuggestedLimit(), suggestedAmount,
|
129 |
existing.getSuggestedLimit(), suggestedAmount,
|
| 131 |
existing.getUtilizedAmount(), utilizationAmount,
|
130 |
existing.getUtilizedAmount(), utilizationAmount,
|
| 132 |
existing.getRisk(), newRisk);
|
131 |
existing.getRisk(), newRisk);
|
| 133 |
changedPartners.add(new PartnerLimitUpdateData(
|
132 |
changedPartners.add(new PartnerLimitUpdateData(
|
| 134 |
fofoId, suggestedAmount, utilizationAmount, newAvailable, newRisk, false));
|
133 |
fofoId, suggestedAmount, utilizationAmount, newAvailable, newRisk, false));
|
| - |
|
134 |
}
|
| - |
|
135 |
} catch (Exception e) {
|
| - |
|
136 |
LOGGER.error("Partner limit calc failed for fofoId={}, skipping: {}", fofoId, e.getMessage());
|
| 135 |
}
|
137 |
}
|
| 136 |
}
|
138 |
}
|
| 137 |
|
139 |
|
| 138 |
LOGGER.info("Partner limit check: {} total, {} changed", sortedFofoIds.size(), changedPartners.size());
|
140 |
LOGGER.info("Partner limit check: {} total, {} changed", sortedFofoIds.size(), changedPartners.size());
|
| 139 |
return changedPartners;
|
141 |
return changedPartners;
|