细节优化

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
@RequiredArgsConstructor
@Slf4j
@Profile("dev")
@Profile("prod")
public class DailyReport {
private final TeamService teamService;
private final DeptService deptService;
@ -215,7 +215,7 @@ public class DailyReport {
LocalDate date,
int sumReg) {
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 -> {
int currentPageSize = page == totalPages - 1
@ -253,27 +253,27 @@ public class DailyReport {
sumReg = (sumReg == 0) ? 100 : 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 -> {
return paginationFutures.stream()
.map(CompletableFuture::join)
.flatMap(memberPagination -> {
return paginationFutures.stream().map(CompletableFuture::join).flatMap(memberPagination -> {
List<TeamMember> members = memberPagination.getList();
log.info("members size:{}", members.size());
return members.stream();
})
.collect(Collectors.toList());
}).collect(Collectors.toList());
});
CompletableFuture<List<TeamMember>> filteredMembersFuture = aggregatedMembersFuture.thenApplyAsync(members -> {
return members.stream()
.filter(member -> member.getDeposit() != null && member.getDeposit().compareTo(BigDecimal.ZERO) == 0)
.collect(Collectors.toList());
}, asyncTaskExecutor);
CompletableFuture<List<TeamMember>> filteredMembersFuture = aggregatedMembersFuture
.thenApplyAsync(members -> members.stream()
.filter(member -> member.getDeposit() != null && member.getDeposit()
.compareTo(BigDecimal.ZERO) == 0)
.collect(Collectors.toList()), asyncTaskExecutor);
CompletableFuture<List<TeamMember>> membersWithFailedPayFuture = filteredMembersFuture.thenComposeAsync(membersWithoutDep -> {
List<CompletableFuture<TeamMember>> payRecordFutures = membersWithoutDep.stream().map(memberWithoutDep -> {
CompletableFuture<List<TeamMember>> membersWithFailedPayFuture = filteredMembersFuture
.thenComposeAsync(membersWithoutDep -> {
List<CompletableFuture<TeamMember>> payRecordFutures = membersWithoutDep.stream()
.map(memberWithoutDep -> {
PayRecordsListReq req = PayRecordsListReq.builder()
.startDate(date)
.endDate(date)
@ -281,14 +281,16 @@ public class DailyReport {
.memberName(memberWithoutDep.getName())
.build();
return fetchPaginationPayRecordWithRetry(account, req).thenApplyAsync(pagination -> {
if (CollUtil.isNotEmpty(pagination.getList())
&& pagination.getList().stream().noneMatch(payRecord -> payRecord.getPayStatus() == 2)) {
if (CollUtil.isNotEmpty(pagination.getList()) && pagination.getList()
.stream()
.noneMatch(payRecord -> payRecord.getPayStatus() == 2)) {
return memberWithoutDep;
} else {
return null;
}
}, asyncTaskExecutor);
}).toList();
})
.toList();
return CompletableFuture.allOf(payRecordFutures.toArray(new CompletableFuture[0]))
.thenApply(v -> payRecordFutures.stream()
@ -297,15 +299,19 @@ public class DailyReport {
.collect(Collectors.toList()));
}, asyncTaskExecutor);
CompletableFuture<List<TeamMember>> notificationFuture = membersWithFailedPayFuture.thenApplyAsync(membersWithoutDep -> {
CompletableFuture<List<TeamMember>> notificationFuture = membersWithFailedPayFuture
.thenApplyAsync(membersWithoutDep -> {
if (CollUtil.isNotEmpty(membersWithoutDep)) {
Map<String, List<TeamMember>> groupByTopAgentName = membersWithoutDep.stream()
.collect(Collectors.groupingBy(TeamMember::getTopAgentName));
groupByTopAgentName.forEach((accountName, accountMembers) -> {
String notification = telegramMessageService.buildFailedPayMessage(accountName, accountMembers);
String notification = telegramMessageService
.buildFailedPayMessage(accountName, accountMembers);
UserWithRolesAndAccountsResp currUser = accountUsernameToUserMap.get(accountName);
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);
}
});
@ -316,7 +322,8 @@ public class DailyReport {
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(() -> {
List<TeamMember> allTeamMembers = accountFutureList.stream()
.map(CompletableFuture::join)
@ -331,10 +338,13 @@ public class DailyReport {
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())) {
telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser.getReportIds(), combinedNotification.toString());
telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser
.getReportIds(), combinedNotification.toString());
}
}, asyncTaskExecutor).exceptionally(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) {
return CompletableFuture.supplyAsync(() -> completableFutureWebClientService.fetchDataForAccount(
account, ApiPathConstants.PAY_RECORDS_LIST_URL, req,
new ParameterizedTypeReference<ApiResponse<Pagination<List<PayRecord>>>>() {
}), asyncTaskExecutor).thenCompose(future -> future)
.exceptionallyCompose(ex -> {
private CompletableFuture<Pagination<List<PayRecord>>> fetchPaginationPayRecordWithRetry(AccountResp account,
PayRecordsListReq req) {
return CompletableFuture.supplyAsync(() -> completableFutureWebClientService
.fetchDataForAccount(account, ApiPathConstants.PAY_RECORDS_LIST_URL, req, new ParameterizedTypeReference<ApiResponse<Pagination<List<PayRecord>>>>() {
}), asyncTaskExecutor).thenCompose(future -> future).exceptionallyCompose(ex -> {
log.error("Error fetching pay records, retrying...", ex);
return fetchPaginationPayRecordWithRetry(account, req);
});
@ -422,7 +431,7 @@ public class DailyReport {
}
String percent = getPercent(totalNewFirstDeposit, totalNewMember);
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});
}
}, asyncTaskExecutor))
@ -433,7 +442,7 @@ public class DailyReport {
// rows 列表进行排序
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])});
String message = TableFormatter.formatTableAsHtml(rows);
telegramMessageService.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, message);