细节优化

This commit is contained in:
zayac 2024-06-10 12:32:26 +08:00
parent 708182e67f
commit c0a15b6580

View File

@ -66,7 +66,7 @@ import java.util.stream.IntStream;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@Profile("dev") @Profile("prod")
public class DailyReport { public class DailyReport {
private final TeamService teamService; private final TeamService teamService;
private final DeptService deptService; private final DeptService deptService;
@ -215,7 +215,7 @@ public class DailyReport {
LocalDate date, LocalDate date,
int sumReg) { int sumReg) {
int pageSize = 100; int pageSize = 100;
int totalPages = (int) Math.ceil((double) sumReg / pageSize); int totalPages = (int)Math.ceil((double)sumReg / pageSize);
return IntStream.range(0, totalPages).mapToObj(page -> { return IntStream.range(0, totalPages).mapToObj(page -> {
int currentPageSize = page == totalPages - 1 int currentPageSize = page == totalPages - 1
@ -253,27 +253,27 @@ public class DailyReport {
sumReg = (sumReg == 0) ? 100 : sumReg; sumReg = (sumReg == 0) ? 100 : sumReg;
List<CompletableFuture<MemberPagination<List<TeamMember>>>> paginationFutures = createFuturesForPagination(account, date, sumReg); List<CompletableFuture<MemberPagination<List<TeamMember>>>> paginationFutures = createFuturesForPagination(account, date, sumReg);
CompletableFuture<Void> allPaginationFutures = CompletableFuture.allOf(paginationFutures.toArray(new CompletableFuture[0])); CompletableFuture<Void> allPaginationFutures = CompletableFuture.allOf(paginationFutures
.toArray(new CompletableFuture[0]));
CompletableFuture<List<TeamMember>> aggregatedMembersFuture = allPaginationFutures.thenApply(v -> { CompletableFuture<List<TeamMember>> aggregatedMembersFuture = allPaginationFutures.thenApply(v -> {
return paginationFutures.stream() return paginationFutures.stream().map(CompletableFuture::join).flatMap(memberPagination -> {
.map(CompletableFuture::join)
.flatMap(memberPagination -> {
List<TeamMember> members = memberPagination.getList(); List<TeamMember> members = memberPagination.getList();
log.info("members size:{}", members.size()); log.info("members size:{}", members.size());
return members.stream(); return members.stream();
}) }).collect(Collectors.toList());
.collect(Collectors.toList());
}); });
CompletableFuture<List<TeamMember>> filteredMembersFuture = aggregatedMembersFuture.thenApplyAsync(members -> { CompletableFuture<List<TeamMember>> filteredMembersFuture = aggregatedMembersFuture
return members.stream() .thenApplyAsync(members -> members.stream()
.filter(member -> member.getDeposit() != null && member.getDeposit().compareTo(BigDecimal.ZERO) == 0) .filter(member -> member.getDeposit() != null && member.getDeposit()
.collect(Collectors.toList()); .compareTo(BigDecimal.ZERO) == 0)
}, asyncTaskExecutor); .collect(Collectors.toList()), asyncTaskExecutor);
CompletableFuture<List<TeamMember>> membersWithFailedPayFuture = filteredMembersFuture.thenComposeAsync(membersWithoutDep -> { CompletableFuture<List<TeamMember>> membersWithFailedPayFuture = filteredMembersFuture
List<CompletableFuture<TeamMember>> payRecordFutures = membersWithoutDep.stream().map(memberWithoutDep -> { .thenComposeAsync(membersWithoutDep -> {
List<CompletableFuture<TeamMember>> payRecordFutures = membersWithoutDep.stream()
.map(memberWithoutDep -> {
PayRecordsListReq req = PayRecordsListReq.builder() PayRecordsListReq req = PayRecordsListReq.builder()
.startDate(date) .startDate(date)
.endDate(date) .endDate(date)
@ -281,14 +281,16 @@ public class DailyReport {
.memberName(memberWithoutDep.getName()) .memberName(memberWithoutDep.getName())
.build(); .build();
return fetchPaginationPayRecordWithRetry(account, req).thenApplyAsync(pagination -> { return fetchPaginationPayRecordWithRetry(account, req).thenApplyAsync(pagination -> {
if (CollUtil.isNotEmpty(pagination.getList()) if (CollUtil.isNotEmpty(pagination.getList()) && pagination.getList()
&& pagination.getList().stream().noneMatch(payRecord -> payRecord.getPayStatus() == 2)) { .stream()
.noneMatch(payRecord -> payRecord.getPayStatus() == 2)) {
return memberWithoutDep; return memberWithoutDep;
} else { } else {
return null; return null;
} }
}, asyncTaskExecutor); }, asyncTaskExecutor);
}).toList(); })
.toList();
return CompletableFuture.allOf(payRecordFutures.toArray(new CompletableFuture[0])) return CompletableFuture.allOf(payRecordFutures.toArray(new CompletableFuture[0]))
.thenApply(v -> payRecordFutures.stream() .thenApply(v -> payRecordFutures.stream()
@ -297,15 +299,19 @@ public class DailyReport {
.collect(Collectors.toList())); .collect(Collectors.toList()));
}, asyncTaskExecutor); }, asyncTaskExecutor);
CompletableFuture<List<TeamMember>> notificationFuture = membersWithFailedPayFuture.thenApplyAsync(membersWithoutDep -> { CompletableFuture<List<TeamMember>> notificationFuture = membersWithFailedPayFuture
.thenApplyAsync(membersWithoutDep -> {
if (CollUtil.isNotEmpty(membersWithoutDep)) { if (CollUtil.isNotEmpty(membersWithoutDep)) {
Map<String, List<TeamMember>> groupByTopAgentName = membersWithoutDep.stream() Map<String, List<TeamMember>> groupByTopAgentName = membersWithoutDep.stream()
.collect(Collectors.groupingBy(TeamMember::getTopAgentName)); .collect(Collectors.groupingBy(TeamMember::getTopAgentName));
groupByTopAgentName.forEach((accountName, accountMembers) -> { groupByTopAgentName.forEach((accountName, accountMembers) -> {
String notification = telegramMessageService.buildFailedPayMessage(accountName, accountMembers); String notification = telegramMessageService
.buildFailedPayMessage(accountName, accountMembers);
UserWithRolesAndAccountsResp currUser = accountUsernameToUserMap.get(accountName); UserWithRolesAndAccountsResp currUser = accountUsernameToUserMap.get(accountName);
if (currUser != null && DisEnableStatusEnum.ENABLE.equals(currUser.getNeedNotify())) { if (currUser != null && DisEnableStatusEnum.ENABLE.equals(currUser.getNeedNotify())) {
String botToken = StrUtil.isEmpty(currUser.getBotToken()) ? ministerUser.getBotToken() : currUser.getBotToken(); String botToken = StrUtil.isEmpty(currUser.getBotToken())
? ministerUser.getBotToken()
: currUser.getBotToken();
telegramMessageService.sendMessage(botToken, currUser.getReportIds(), notification); telegramMessageService.sendMessage(botToken, currUser.getReportIds(), notification);
} }
}); });
@ -316,7 +322,8 @@ public class DailyReport {
accountFutureList.add(notificationFuture); accountFutureList.add(notificationFuture);
}); });
CompletableFuture<Void> allAccountFutures = CompletableFuture.allOf(accountFutureList.toArray(new CompletableFuture[0])); CompletableFuture<Void> allAccountFutures = CompletableFuture.allOf(accountFutureList
.toArray(new CompletableFuture[0]));
allAccountFutures.thenRunAsync(() -> { allAccountFutures.thenRunAsync(() -> {
List<TeamMember> allTeamMembers = accountFutureList.stream() List<TeamMember> allTeamMembers = accountFutureList.stream()
.map(CompletableFuture::join) .map(CompletableFuture::join)
@ -331,10 +338,13 @@ public class DailyReport {
combinedNotification.append(notification).append("\n"); combinedNotification.append(notification).append("\n");
}); });
telegramMessageService.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, combinedNotification.toString()); telegramMessageService
.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, combinedNotification
.toString());
if (DisEnableStatusEnum.ENABLE.equals(ministerUser.getNeedNotify())) { if (DisEnableStatusEnum.ENABLE.equals(ministerUser.getNeedNotify())) {
telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser.getReportIds(), combinedNotification.toString()); telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser
.getReportIds(), combinedNotification.toString());
} }
}, asyncTaskExecutor).exceptionally(ex -> { }, asyncTaskExecutor).exceptionally(ex -> {
log.error("Error collecting and processing data", ex); log.error("Error collecting and processing data", ex);
@ -342,12 +352,11 @@ public class DailyReport {
}); });
} }
private CompletableFuture<Pagination<List<PayRecord>>> fetchPaginationPayRecordWithRetry(AccountResp account, PayRecordsListReq req) { private CompletableFuture<Pagination<List<PayRecord>>> fetchPaginationPayRecordWithRetry(AccountResp account,
return CompletableFuture.supplyAsync(() -> completableFutureWebClientService.fetchDataForAccount( PayRecordsListReq req) {
account, ApiPathConstants.PAY_RECORDS_LIST_URL, req, return CompletableFuture.supplyAsync(() -> completableFutureWebClientService
new ParameterizedTypeReference<ApiResponse<Pagination<List<PayRecord>>>>() { .fetchDataForAccount(account, ApiPathConstants.PAY_RECORDS_LIST_URL, req, new ParameterizedTypeReference<ApiResponse<Pagination<List<PayRecord>>>>() {
}), asyncTaskExecutor).thenCompose(future -> future) }), asyncTaskExecutor).thenCompose(future -> future).exceptionallyCompose(ex -> {
.exceptionallyCompose(ex -> {
log.error("Error fetching pay records, retrying...", ex); log.error("Error fetching pay records, retrying...", ex);
return fetchPaginationPayRecordWithRetry(account, req); return fetchPaginationPayRecordWithRetry(account, req);
}); });
@ -422,7 +431,7 @@ public class DailyReport {
} }
String percent = getPercent(totalNewFirstDeposit, totalNewMember); String percent = getPercent(totalNewFirstDeposit, totalNewMember);
synchronized (rows) { synchronized (rows) {
rows.add(new String[]{accountResp.getPlatformName(), String.valueOf(totalNewMember), String rows.add(new String[] {accountResp.getPlatformName(), String.valueOf(totalNewMember), String
.valueOf(totalNewFirstDeposit), percent}); .valueOf(totalNewFirstDeposit), percent});
} }
}, asyncTaskExecutor)) }, asyncTaskExecutor))
@ -433,7 +442,7 @@ public class DailyReport {
// rows 列表进行排序 // rows 列表进行排序
rows.sort(Comparator.comparing((String[] row) -> row[0].length()).thenComparing(row -> row[0])); rows.sort(Comparator.comparing((String[] row) -> row[0].length()).thenComparing(row -> row[0]));
rows.add(new String[]{"总计", String.valueOf(totals[0]), String.valueOf(totals[1]), rows.add(new String[] {"总计", String.valueOf(totals[0]), String.valueOf(totals[1]),
getPercent(totals[1], totals[0])}); getPercent(totals[1], totals[0])});
String message = TableFormatter.formatTableAsHtml(rows); String message = TableFormatter.formatTableAsHtml(rows);
telegramMessageService.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, message); telegramMessageService.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, message);