Subversion Repositories SmartDukaan

Rev

Rev 36359 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36359 Rev 36596
Line 1744... Line 1744...
1744
        }
1744
        }
1745
 
1745
 
1746
        return rows;
1746
        return rows;
1747
    }
1747
    }
1748
 
1748
 
-
 
1749
    @Override
-
 
1750
    public List<List<String>> getAllRbmCallTargetRawData() throws Exception {
-
 
1751
        List<List<String>> rows = new ArrayList<>();
-
 
1752
 
-
 
1753
        // Get all L1 RBM positions
-
 
1754
        List<Position> allRbmPositions = positionRepository
-
 
1755
                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM).stream()
-
 
1756
                .filter(x -> EscalationType.L1.equals(x.getEscalationType()))
-
 
1757
                .collect(Collectors.toList());
-
 
1758
 
-
 
1759
        List<Integer> l1AuthIds = allRbmPositions.stream()
-
 
1760
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
-
 
1761
 
-
 
1762
        if (l1AuthIds.isEmpty()) {
-
 
1763
            return rows;
-
 
1764
        }
-
 
1765
 
-
 
1766
        // Get auth user map
-
 
1767
        Map<Integer, AuthUser> authUserMap = authRepository.selectByIds(l1AuthIds).stream()
-
 
1768
                .collect(Collectors.toMap(AuthUser::getId, au -> au));
-
 
1769
 
-
 
1770
        // Get partner assignments
-
 
1771
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
-
 
1772
 
-
 
1773
        Map<Integer, List<Integer>> rbmToFofoIdsMap = new HashMap<>();
-
 
1774
        Set<Integer> allFofoIds = new HashSet<>();
-
 
1775
        for (int rbmAuthId : l1AuthIds) {
-
 
1776
            AuthUser au = authUserMap.get(rbmAuthId);
-
 
1777
            if (au != null && storeGuyMap.containsKey(au.getEmailId())) {
-
 
1778
                List<Integer> fofoIds = new ArrayList<>(storeGuyMap.get(au.getEmailId()));
-
 
1779
                allFofoIds.addAll(fofoIds);
-
 
1780
                rbmToFofoIdsMap.put(rbmAuthId, fofoIds);
-
 
1781
            }
-
 
1782
        }
-
 
1783
 
-
 
1784
        if (allFofoIds.isEmpty()) {
-
 
1785
            return rows;
-
 
1786
        }
-
 
1787
 
-
 
1788
        // Get fofo stores for filtering and name resolution
-
 
1789
        Map<Integer, FofoStore> fofoStoresMap = new HashMap<>();
-
 
1790
        try {
-
 
1791
            fofoStoresMap = fofoStoreRepository.selectByRetailerIds(new ArrayList<>(allFofoIds)).stream()
-
 
1792
                    .collect(Collectors.toMap(FofoStore::getId, x -> x, (a, b) -> a));
-
 
1793
        } catch (ProfitMandiBusinessException e) {
-
 
1794
            LOGGER.error("Error fetching fofo stores for all raw data", e);
-
 
1795
        }
-
 
1796
 
-
 
1797
        // Batch fetch collection rank map
-
 
1798
        LocalDateTime startDate = LocalDate.now().atStartOfDay();
-
 
1799
        Map<Integer, Integer> allCollectionRankMap = new HashMap<>();
-
 
1800
        try {
-
 
1801
            allCollectionRankMap = partnerCollectionService.getCollectionRankMap(new ArrayList<>(allFofoIds), startDate);
-
 
1802
        } catch (ProfitMandiBusinessException e) {
-
 
1803
            LOGGER.error("Error fetching collection rank map for all raw data", e);
-
 
1804
        }
-
 
1805
 
-
 
1806
        // MTD billing data
-
 
1807
        LocalDate firstOfMonth = LocalDate.now().withDayOfMonth(1);
-
 
1808
        LocalDate endOfMonth = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).plusDays(1);
-
 
1809
        List<RbmWeeklyBillingModel> mtdBillingData = getWeeklyBillingDataForMonth(firstOfMonth, endOfMonth);
-
 
1810
        Set<Integer> allMtdBilledFofoIds = mtdBillingData.stream()
-
 
1811
                .filter(RbmWeeklyBillingModel::isMtdBilled)
-
 
1812
                .map(RbmWeeklyBillingModel::getFofoId)
-
 
1813
                .collect(Collectors.toSet());
-
 
1814
 
-
 
1815
        // Batch fetch partner collection remarks for escalation filtering
-
 
1816
        Map<Integer, PartnerCollectionRemark> allPartnerCollectionRemarks = new HashMap<>();
-
 
1817
        if (!allFofoIds.isEmpty()) {
-
 
1818
            List<Integer> allRemarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(new ArrayList<>(allFofoIds));
-
 
1819
            if (!allRemarkIds.isEmpty()) {
-
 
1820
                allPartnerCollectionRemarks = partnerCollectionRemarkRepository.selectByIds(allRemarkIds).stream()
-
 
1821
                        .collect(Collectors.toMap(PartnerCollectionRemark::getFofoId, x -> x, (a, b) -> a));
-
 
1822
            }
-
 
1823
        }
-
 
1824
 
-
 
1825
        // Resolve partner names/codes in batch
-
 
1826
        Map<Integer, CustomRetailer> retailerMap = Collections.emptyMap();
-
 
1827
        try {
-
 
1828
            retailerMap = retailerService.getFofoRetailers(new ArrayList<>(allFofoIds));
-
 
1829
        } catch (ProfitMandiBusinessException e) {
-
 
1830
            LOGGER.error("Error fetching fofo retailers for all raw data", e);
-
 
1831
        }
-
 
1832
 
-
 
1833
        // Get positions for L1 check
-
 
1834
        Map<Integer, List<Position>> positionsByAuthId = positionRepository.selectPositionByAuthIds(l1AuthIds).stream()
-
 
1835
                .collect(Collectors.groupingBy(Position::getAuthUserId));
-
 
1836
 
-
 
1837
        // Track processed fofoIds to avoid duplicates (a party assigned to L1 RBM
-
 
1838
        // should not appear again under another RBM who also has it via L2 position)
-
 
1839
        Set<Integer> processedFofoIds = new HashSet<>();
-
 
1840
 
-
 
1841
        // Process each L1 RBM
-
 
1842
        for (int rbmAuthId : l1AuthIds) {
-
 
1843
            AuthUser authUser = authUserMap.get(rbmAuthId);
-
 
1844
            if (authUser == null || !storeGuyMap.containsKey(authUser.getEmailId())) {
-
 
1845
                continue;
-
 
1846
            }
-
 
1847
 
-
 
1848
            List<Integer> fofoIdList = rbmToFofoIdsMap.getOrDefault(rbmAuthId, Collections.emptyList());
-
 
1849
 
-
 
1850
            // Filter escalated partners for L1 RBMs
-
 
1851
            List<Position> positions = positionsByAuthId.getOrDefault(rbmAuthId, Collections.emptyList());
-
 
1852
            boolean isRBMAndL1 = positions.stream()
-
 
1853
                    .anyMatch(position ->
-
 
1854
                            ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId()
-
 
1855
                                    && EscalationType.L1.equals(position.getEscalationType()));
-
 
1856
 
-
 
1857
            List<Integer> fofoIds = fofoIdList;
-
 
1858
            if (isRBMAndL1) {
-
 
1859
                Map<Integer, PartnerCollectionRemark> partnerRemarks = new HashMap<>();
-
 
1860
                for (Integer fofoId : fofoIdList) {
-
 
1861
                    if (allPartnerCollectionRemarks.containsKey(fofoId)) {
-
 
1862
                        partnerRemarks.put(fofoId, allPartnerCollectionRemarks.get(fofoId));
-
 
1863
                    }
-
 
1864
                }
-
 
1865
                Map<Integer, PartnerCollectionRemark> finalPartnerRemarks = partnerRemarks;
-
 
1866
                fofoIds = fofoIdList.stream()
-
 
1867
                        .filter(fofoId -> {
-
 
1868
                            if (!finalPartnerRemarks.containsKey(fofoId)) return true;
-
 
1869
                            PartnerCollectionRemark pcr = finalPartnerRemarks.get(fofoId);
-
 
1870
                            return !(CollectionRemark.RBM_L2_ESCALATION.equals(pcr.getRemark())
-
 
1871
                                    || CollectionRemark.SALES_ESCALATION.equals(pcr.getRemark()));
-
 
1872
                        })
-
 
1873
                        .collect(Collectors.toList());
-
 
1874
            }
-
 
1875
 
-
 
1876
            // Filter to only external, ACTIVE or REVIVAL stores
-
 
1877
            Map<Integer, FofoStore> finalFofoStoresMap = fofoStoresMap;
-
 
1878
            List<Integer> validFofoIds = fofoIds.stream()
-
 
1879
                    .filter(fofoId -> {
-
 
1880
                        FofoStore store = finalFofoStoresMap.get(fofoId);
-
 
1881
                        if (store == null || store.isInternal()) return false;
-
 
1882
                        return ActivationType.ACTIVE.equals(store.getActivationType())
-
 
1883
                                || ActivationType.REVIVAL.equals(store.getActivationType());
-
 
1884
                    })
-
 
1885
                    .collect(Collectors.toList());
-
 
1886
 
-
 
1887
            String rbmName = authUser.getFullName();
-
 
1888
 
-
 
1889
            // Categorize each partner and add row (skip if already processed under another RBM)
-
 
1890
            for (Integer fofoId : validFofoIds) {
-
 
1891
                if (processedFofoIds.contains(fofoId)) {
-
 
1892
                    continue; // Already added under another L1 RBM
-
 
1893
                }
-
 
1894
 
-
 
1895
                int rank = allCollectionRankMap.getOrDefault(fofoId, 5);
-
 
1896
                boolean hasZeroBilling = !allMtdBilledFofoIds.contains(fofoId);
-
 
1897
 
-
 
1898
                String status;
-
 
1899
                if (rank == 1) {
-
 
1900
                    status = "Plan Today";
-
 
1901
                } else if (rank == 2) {
-
 
1902
                    status = "Carry Forward";
-
 
1903
                } else if (hasZeroBilling) {
-
 
1904
                    status = "Zero Billing";
-
 
1905
                } else if (rank == 3) {
-
 
1906
                    status = "Untouched";
-
 
1907
                } else {
-
 
1908
                    continue; // Skip Future Plan and Normal — only include calling target parties
-
 
1909
                }
-
 
1910
 
-
 
1911
                processedFofoIds.add(fofoId);
-
 
1912
 
-
 
1913
                CustomRetailer retailer = retailerMap.get(fofoId);
-
 
1914
                String partnerName = retailer != null ? retailer.getBusinessName() : "Unknown (" + fofoId + ")";
-
 
1915
                String partnerCode = retailer != null ? retailer.getCode() : "-";
-
 
1916
 
-
 
1917
                rows.add(Arrays.asList(partnerName, partnerCode, status, rbmName));
-
 
1918
            }
-
 
1919
        }
-
 
1920
 
-
 
1921
        return rows;
-
 
1922
    }
-
 
1923
 
1749
    /**
1924
    /**
1750
     * Get count of distinct partners called today based on call logs.
1925
     * Get count of distinct partners called today based on call logs.
1751
     * Maps customerNumber from call log to fofoId using retailer_contact and address.
1926
     * Maps customerNumber from call log to fofoId using retailer_contact and address.
1752
     * If same fofoId is called multiple times, counts only once.
1927
     * If same fofoId is called multiple times, counts only once.
1753
     * Numbers without fofoId mapping are also counted (by distinct customer number).
1928
     * Numbers without fofoId mapping are also counted (by distinct customer number).