| Line 1622... |
Line 1622... |
| 1622 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
1622 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
| 1623 |
}
|
1623 |
}
|
| 1624 |
List<TargetSlabEntity> targetSlabs = offerTargetSlabRepository.selectEntitiesByOfferId(offerId);
|
1624 |
List<TargetSlabEntity> targetSlabs = offerTargetSlabRepository.selectEntitiesByOfferId(offerId);
|
| 1625 |
targetSlabs.sort(Comparator.comparing(TargetSlabEntity::getPayoutTarget));
|
1625 |
targetSlabs.sort(Comparator.comparing(TargetSlabEntity::getPayoutTarget));
|
| 1626 |
|
1626 |
|
| - |
|
1627 |
// Group slabs by unique payout_target (multiple item_criteria share the same target value)
|
| - |
|
1628 |
LinkedHashMap<Integer, List<TargetSlabEntity>> groupedByTarget = new LinkedHashMap<>();
|
| - |
|
1629 |
for (TargetSlabEntity slab : targetSlabs) {
|
| - |
|
1630 |
groupedByTarget.computeIfAbsent(slab.getPayoutTarget(), k -> new ArrayList<>()).add(slab);
|
| - |
|
1631 |
}
|
| - |
|
1632 |
List<Integer> uniqueTargets = new ArrayList<>(groupedByTarget.keySet());
|
| - |
|
1633 |
|
| 1627 |
if (newTargets.size() != targetSlabs.size()) {
|
1634 |
if (newTargets.size() != uniqueTargets.size()) {
|
| 1628 |
throw new ProfitMandiBusinessException(
|
1635 |
throw new ProfitMandiBusinessException(
|
| 1629 |
"Target count mismatch. Expected " + targetSlabs.size() + " but got " + newTargets.size(),
|
1636 |
"Target count mismatch. Expected " + uniqueTargets.size() + " but got " + newTargets.size(),
|
| 1630 |
"Target count mismatch", "");
|
1637 |
"Target count mismatch", "");
|
| 1631 |
}
|
1638 |
}
|
| - |
|
1639 |
|
| - |
|
1640 |
// Update each group: all slabs sharing the same old target get the new target value
|
| 1632 |
for (int i = 0; i < targetSlabs.size(); i++) {
|
1641 |
for (int i = 0; i < uniqueTargets.size(); i++) {
|
| 1633 |
targetSlabs.get(i).setPayoutTarget(newTargets.get(i));
|
1642 |
int oldTarget = uniqueTargets.get(i);
|
| 1634 |
genericRepository.persist(targetSlabs.get(i));
|
1643 |
int newTarget = newTargets.get(i);
|
| - |
|
1644 |
if (oldTarget != newTarget) {
|
| - |
|
1645 |
for (TargetSlabEntity slab : groupedByTarget.get(oldTarget)) {
|
| - |
|
1646 |
genericRepository.updateById(TargetSlabEntity.class, "payoutTarget", newTarget, slab.getId());
|
| - |
|
1647 |
}
|
| - |
|
1648 |
}
|
| 1635 |
}
|
1649 |
}
|
| 1636 |
LOGGER.info("Updated targets for offer {} to {}", offerId, newTargets);
|
1650 |
LOGGER.info("Updated targets for offer {} to {}", offerId, newTargets);
|
| 1637 |
}
|
1651 |
}
|
| 1638 |
}
|
1652 |
}
|