Subversion Repositories SmartDukaan

Rev

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

Rev 35843 Rev 35852
Line 1403... Line 1403...
1403
 
1403
 
1404
        // Total called = distinct fofoIds + distinct numbers without fofoId mapping
1404
        // Total called = distinct fofoIds + distinct numbers without fofoId mapping
1405
        return calledFofoIds.size() + calledNumbersWithoutFofoId.size();
1405
        return calledFofoIds.size() + calledNumbersWithoutFofoId.size();
1406
    }
1406
    }
1407
 
1407
 
-
 
1408
    @Override
-
 
1409
    public List<List<String>> getAllCallDataByDate(LocalDate date) throws Exception {
-
 
1410
        List<List<String>> rows = new ArrayList<>();
-
 
1411
 
-
 
1412
        // Add header row
-
 
1413
        rows.add(Arrays.asList("RBM Name", "Partner Name", "Code", "Remark", "Call Status", "Call Duration", "Call Date Time", "Recording URL"));
-
 
1414
 
-
 
1415
        // Get all call logs for the date
-
 
1416
        List<AgentCallLog> allCallLogs = agentCallLogRepository.findAllByDate(date);
-
 
1417
        LOGGER.info("getAllCallDataByDate: Found {} call logs for date {}", allCallLogs.size(), date);
-
 
1418
 
-
 
1419
        if (allCallLogs.isEmpty()) {
-
 
1420
            return rows;
-
 
1421
        }
-
 
1422
 
-
 
1423
        // Get unique authIds from call logs
-
 
1424
        Set<Long> authIds = allCallLogs.stream()
-
 
1425
                .map(AgentCallLog::getAuthId)
-
 
1426
                .collect(Collectors.toSet());
-
 
1427
 
-
 
1428
        // Get auth users for RBM names
-
 
1429
        List<Integer> authIdInts = authIds.stream().map(Long::intValue).collect(Collectors.toList());
-
 
1430
        Map<Integer, AuthUser> authUserMap = authRepository.selectByIds(authIdInts).stream()
-
 
1431
                .collect(Collectors.toMap(AuthUser::getId, au -> au, (a, b) -> a));
-
 
1432
 
-
 
1433
        // Build a map of normalized customer number -> fofoId
-
 
1434
        Map<String, Integer> customerToFofoIdMap = new HashMap<>();
-
 
1435
        Set<String> normalizedNumbers = new HashSet<>();
-
 
1436
 
-
 
1437
        for (AgentCallLog callLog : allCallLogs) {
-
 
1438
            String customerNumber = callLog.getCustomerNumber();
-
 
1439
            if (customerNumber != null) {
-
 
1440
                String normalized = customerNumber.startsWith("+91") ? customerNumber.substring(3) : customerNumber;
-
 
1441
                normalizedNumbers.add(normalized);
-
 
1442
            }
-
 
1443
        }
-
 
1444
 
-
 
1445
        // For each normalized number, find fofoId
-
 
1446
        for (String mobile : normalizedNumbers) {
-
 
1447
            Integer fofoId = findFofoIdByMobile(mobile);
-
 
1448
            if (fofoId != null) {
-
 
1449
                customerToFofoIdMap.put(mobile, fofoId);
-
 
1450
            }
-
 
1451
        }
-
 
1452
 
-
 
1453
        // Get unique fofoIds for retailer lookup
-
 
1454
        Set<Integer> fofoIds = new HashSet<>(customerToFofoIdMap.values());
-
 
1455
        Map<Integer, CustomRetailer> retailerMap = Collections.emptyMap();
-
 
1456
        if (!fofoIds.isEmpty()) {
-
 
1457
            try {
-
 
1458
                retailerMap = retailerService.getFofoRetailers(new ArrayList<>(fofoIds));
-
 
1459
            } catch (ProfitMandiBusinessException e) {
-
 
1460
                LOGGER.error("Error fetching fofo stores", e);
-
 
1461
            }
-
 
1462
        }
-
 
1463
 
-
 
1464
        // Get remarks for these fofoIds on this date
-
 
1465
        Map<Integer, List<PartnerCollectionRemark>> fofoRemarkMap = new HashMap<>();
-
 
1466
        if (!fofoIds.isEmpty()) {
-
 
1467
            List<PartnerCollectionRemark> dateRemarks = partnerCollectionRemarkRepository
-
 
1468
                    .selectAllByFofoIdsOnDate(new ArrayList<>(fofoIds), date);
-
 
1469
            for (PartnerCollectionRemark remark : dateRemarks) {
-
 
1470
                fofoRemarkMap.computeIfAbsent(remark.getFofoId(), k -> new ArrayList<>()).add(remark);
-
 
1471
            }
-
 
1472
        }
-
 
1473
 
-
 
1474
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm a");
-
 
1475
 
-
 
1476
        // Build rows
-
 
1477
        for (AgentCallLog callLog : allCallLogs) {
-
 
1478
            String customerNumber = callLog.getCustomerNumber();
-
 
1479
            if (customerNumber == null) {
-
 
1480
                continue;
-
 
1481
            }
-
 
1482
 
-
 
1483
            // Get RBM Name
-
 
1484
            AuthUser authUser = authUserMap.get((int) callLog.getAuthId());
-
 
1485
            String rbmName = authUser != null ? authUser.getFullName() : "Unknown";
-
 
1486
 
-
 
1487
            String normalized = customerNumber.startsWith("+91") ? customerNumber.substring(3) : customerNumber;
-
 
1488
            Integer fofoId = customerToFofoIdMap.get(normalized);
-
 
1489
 
-
 
1490
            String partyName = "Unknown";
-
 
1491
            String code = "-";
-
 
1492
 
-
 
1493
            if (fofoId != null) {
-
 
1494
                CustomRetailer retailer = retailerMap.get(fofoId);
-
 
1495
                if (retailer != null) {
-
 
1496
                    partyName = retailer.getBusinessName();
-
 
1497
                    code = retailer.getCode();
-
 
1498
                } else {
-
 
1499
                    partyName = "Unknown (" + fofoId + ")";
-
 
1500
                }
-
 
1501
            } else {
-
 
1502
                partyName = "Unknown (" + normalized + ")";
-
 
1503
            }
-
 
1504
 
-
 
1505
            // Get remark if available
-
 
1506
            String remarkValue = "-";
-
 
1507
            if (fofoId != null && fofoRemarkMap.containsKey(fofoId)) {
-
 
1508
                List<PartnerCollectionRemark> remarks = fofoRemarkMap.get(fofoId);
-
 
1509
                if (!remarks.isEmpty()) {
-
 
1510
                    PartnerCollectionRemark remark = remarks.get(0);
-
 
1511
                    remarkValue = remark.getRemark() != null ? remark.getRemark().getValue() : "-";
-
 
1512
                    if (remark.getMessage() != null && !remark.getMessage().isEmpty()) {
-
 
1513
                        remarkValue = remarkValue + " - " + remark.getMessage();
-
 
1514
                    }
-
 
1515
                }
-
 
1516
            }
-
 
1517
 
-
 
1518
            // Call status from customerStatus field
-
 
1519
            String callStatus = callLog.getCustomerStatus() != null ? callLog.getCustomerStatus() : "-";
-
 
1520
            String callDuration = callLog.getCallDuration() != null ? callLog.getCallDuration() : "-";
-
 
1521
 
-
 
1522
            String callDateTime = "-";
-
 
1523
            if (callLog.getCallDate() != null && callLog.getCallTime() != null) {
-
 
1524
                LocalDateTime callDateTimeObj = LocalDateTime.of(callLog.getCallDate(), callLog.getCallTime());
-
 
1525
                callDateTime = callDateTimeObj.format(dateTimeFormatter);
-
 
1526
            }
-
 
1527
 
-
 
1528
            String recordingUrl = callLog.getRecordingUrl() != null ? callLog.getRecordingUrl() : "-";
-
 
1529
 
-
 
1530
            rows.add(Arrays.asList(rbmName, partyName, code, remarkValue, callStatus, callDuration, callDateTime, recordingUrl));
-
 
1531
        }
-
 
1532
 
-
 
1533
        return rows;
-
 
1534
    }
-
 
1535
 
1408
}
1536
}