修复部分bug
完善部分功能
This commit is contained in:
parent
0a4013b330
commit
f0418a8038
@ -17,6 +17,7 @@
|
||||
package com.zayac.admin.schedule;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zayac.admin.common.enums.DisEnableStatusEnum;
|
||||
import com.zayac.admin.req.AgentDataVisualListReq;
|
||||
import com.zayac.admin.req.team.TeamInfoReq;
|
||||
@ -32,6 +33,7 @@ import com.zayac.admin.system.model.entity.UserDO;
|
||||
import com.zayac.admin.system.model.req.AgentStatsReq;
|
||||
import com.zayac.admin.system.model.resp.AccountResp;
|
||||
import com.zayac.admin.system.service.*;
|
||||
import com.zayac.admin.utils.TableFormatter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -40,6 +42,7 @@ import java.text.NumberFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@ -60,7 +63,8 @@ public class DailyReport {
|
||||
@Scheduled(cron = "0 40 11,14,17,21 * * ?")
|
||||
public void teamAccountDailyReport() {
|
||||
LocalDateTime nowDateTime = LocalDateTime.now();
|
||||
sendDailyReport(LocalDate.now(), nowDateTime, nowDateTime, false);
|
||||
LocalDate nowDate = LocalDate.now();
|
||||
sendDailyReport(nowDate, nowDate.atStartOfDay(), nowDateTime, false);
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 15 0 * * ?")
|
||||
@ -76,53 +80,56 @@ public class DailyReport {
|
||||
|
||||
userIds.forEach(userId -> {
|
||||
UserDO ministerUser = userService.getById(userId);
|
||||
List<AccountResp> accounts = getTeamAccounts(ministerUser.getId());
|
||||
List<UserDO> deptUsers = userService.getByDeptId(DisEnableStatusEnum.ENABLE, ministerUser.getDeptId());
|
||||
|
||||
accounts.forEach(accountResp -> {
|
||||
Team team = getLatestTeamInfo(accountResp, startDateTime, endDateTime);
|
||||
sendMinisterMessage(ministerUser, team.getList(), endDateTime);
|
||||
});
|
||||
if (ministerUser.getNeedNotify() == DisEnableStatusEnum.ENABLE) {
|
||||
List<String[]> rows = generateRows(ministerUser.getId(), startDateTime, endDateTime);
|
||||
String table = TableFormatter.formatTable(rows);
|
||||
telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser.getCountGroupId(), table);
|
||||
telegramMessageService
|
||||
.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, table);
|
||||
}
|
||||
|
||||
AgentDataVisualListReq agentDataVisualListReq = AgentDataVisualListReq.builder().build();
|
||||
//获取部长对应下级所有用户
|
||||
deptUsers.forEach(deptUser -> {
|
||||
String message = getDeptUserMessage(deptUser, agentDataVisualListReq, reportDate, saveStatics);
|
||||
if (deptUser.getNeedNotify() == DisEnableStatusEnum.ENABLE) {
|
||||
telegramMessageService.sendMessage(deptUser.getBotToken(), deptUser.getCountGroupId(), message);
|
||||
String botToken = StrUtil.isEmpty(deptUser.getBotToken()) ? ministerUser.getBotToken() : deptUser.getBotToken();
|
||||
telegramMessageService.sendMessage(botToken, deptUser.getCountGroupId(), message);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private List<AccountResp> getTeamAccounts(Long userId) {
|
||||
return accountService.getAccountsByUserId(userId, DisEnableStatusEnum.ENABLE)
|
||||
.stream()
|
||||
.filter(AccountResp::getIsTeam)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Team getLatestTeamInfo(AccountResp accountResp, LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
||||
TeamInfoReq teamInfoReq = TeamInfoReq.builder().startDate(startDateTime).endDate(endDateTime).build();
|
||||
return teamService.getLatestTeamInfoAsync(accountResp, teamInfoReq).join();
|
||||
}
|
||||
|
||||
private void sendMinisterMessage(UserDO ministerUser, List<TeamAccount> list, LocalDateTime endDateTime) {
|
||||
if (ministerUser.getNeedNotify() == DisEnableStatusEnum.ENABLE) {
|
||||
int totalNewMember = list.stream().mapToInt(TeamAccount::getSubMemberNum).sum();
|
||||
int totalNewFirstDeposit = list.stream().mapToInt(TeamAccount::getFirstDepositNum).sum();
|
||||
String percent = getPercent(totalNewFirstDeposit, totalNewMember);
|
||||
String message = String
|
||||
.format("截至%s 注册:*%d*,新增:*%d* 转化率: *%s*", endDateTime, totalNewMember, totalNewFirstDeposit, percent);
|
||||
telegramMessageService.sendMessage(ministerUser.getBotToken(), ministerUser.getCountGroupId(), message);
|
||||
}
|
||||
}
|
||||
// private void buildMinisterMessage(String platformName, List<TeamAccount> list, StringBuilder notification, int[] totals) {
|
||||
// int totalNewMember = list.stream().mapToInt(TeamAccount::getSubMemberNum).sum();
|
||||
// int totalNewFirstDeposit = list.stream().mapToInt(TeamAccount::getFirstDepositNum).sum();
|
||||
// totals[0] += totalNewMember;
|
||||
// totals[1] += totalNewFirstDeposit;
|
||||
// String percent = getPercent(totalNewFirstDeposit, totalNewMember);
|
||||
// List<String[]> rows = generateRows(ministerUser.getId(), startDateTime, endDateTime);
|
||||
// String table = TableFormatter.formatTable(rows);
|
||||
// //notification.append(String.format("%-3s %-4d %-4d %-5s\n", platformName, totalNewMember, totalNewFirstDeposit, percent));
|
||||
// notification.append(String.format("%-10s %-8d %-8d %-10s\n",
|
||||
// padRight(platformName, 10), totalNewMember, totalNewFirstDeposit, percent));
|
||||
// }
|
||||
|
||||
//获取需要发送给用户的信息
|
||||
private String getDeptUserMessage(UserDO deptUser,
|
||||
AgentDataVisualListReq agentDataVisualListReq,
|
||||
LocalDate reportDate,
|
||||
boolean saveStatics) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
List<AccountResp> currUserAccounts = getTeamAccounts(deptUser.getId());
|
||||
// 主线注册新增不用报数,过滤
|
||||
List<AccountResp> currUserAccounts = accountService.getAccountsByUserId(deptUser.getId(), DisEnableStatusEnum.ENABLE)
|
||||
.stream()
|
||||
.filter(accountResp -> !accountResp.getIsTeam())
|
||||
.toList();
|
||||
|
||||
currUserAccounts.forEach(currAccount -> {
|
||||
CompletableFuture<AgentDataVisualList> future = agentDataVisualListService
|
||||
@ -161,4 +168,34 @@ public class DailyReport {
|
||||
percentInstance.setMinimumFractionDigits(2);
|
||||
return percentInstance.format(d1 / d2);
|
||||
}
|
||||
|
||||
public static String padRight(String s, int n) {
|
||||
int length = s.length() + (int) s.chars().filter(ch -> ch > 0xFF).count();
|
||||
return String.format("%-" + (n - length + s.length()) + "s", s);
|
||||
}
|
||||
|
||||
private List<String[]> generateRows(Long userId, LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
||||
List<String[]> rows = new ArrayList<>();
|
||||
rows.add(new String[]{"平台", "注册", "新增", "转化率"});
|
||||
rows.add(new String[]{"----", "----", "----", "----"});
|
||||
|
||||
List<AccountResp> accounts = accountService.getAccountsByUserId(userId, DisEnableStatusEnum.ENABLE)
|
||||
.stream()
|
||||
.filter(AccountResp::getIsTeam)
|
||||
.toList();
|
||||
int[] totals = {0, 0};
|
||||
|
||||
accounts.forEach(accountResp -> {
|
||||
Team team = getLatestTeamInfo(accountResp, startDateTime, endDateTime);
|
||||
int totalNewMember = team.getList().stream().mapToInt(TeamAccount::getSubMemberNum).sum();
|
||||
int totalNewFirstDeposit = team.getList().stream().mapToInt(TeamAccount::getFirstDepositNum).sum();
|
||||
totals[0] += totalNewMember;
|
||||
totals[1] += totalNewFirstDeposit;
|
||||
String percent = getPercent(totalNewFirstDeposit, totalNewMember);
|
||||
rows.add(new String[]{accountResp.getPlatformName(), String.valueOf(totalNewMember), String.valueOf(totalNewFirstDeposit), percent});
|
||||
});
|
||||
|
||||
rows.add(new String[]{"总注册", String.valueOf(totals[0]), String.valueOf(totals[1]), getPercent(totals[1], totals[0])});
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
@ -70,20 +70,21 @@ public class TelegramTeamMessageSchedule {
|
||||
});
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> processUser(UserDO user, LocalDate nowDate, LocalDateTime nowDateTime) {
|
||||
List<AccountResp> accounts = accountService.getAccountsByUserId(user.getId(), DisEnableStatusEnum.ENABLE)
|
||||
private CompletableFuture<Void> processUser(UserDO minister, LocalDate nowDate, LocalDateTime nowDateTime) {
|
||||
List<AccountResp> accounts = accountService.getAccountsByUserId(minister.getId(), DisEnableStatusEnum.ENABLE)
|
||||
.stream()
|
||||
.filter(AccountResp::getIsTeam)
|
||||
.toList();
|
||||
|
||||
List<CompletableFuture<Void>> futures = accounts.stream()
|
||||
.map(account -> processTeamAccount(account, nowDate, nowDateTime))
|
||||
.map(account -> processTeamAccount(minister, account, nowDate, nowDateTime))
|
||||
.toList();
|
||||
|
||||
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> processTeamAccount(AccountResp account,
|
||||
private CompletableFuture<Void> processTeamAccount(UserDO minister,
|
||||
AccountResp account,
|
||||
LocalDate nowDate,
|
||||
LocalDateTime nowDateTime) {
|
||||
TeamInfoReq teamInfoReq = TeamInfoReq.builder()
|
||||
@ -102,9 +103,9 @@ public class TelegramTeamMessageSchedule {
|
||||
.collect(Collectors.toMap(TeamAccount::getAgentName, Function.identity()));
|
||||
|
||||
CompletableFuture<Void> registrationProcess = registrationService
|
||||
.processRegistration(account, currentTeamInfo, prevTeamInfo, nowDate, teamAccountMap);
|
||||
.processRegistration(minister, account, currentTeamInfo, prevTeamInfo, nowDate, teamAccountMap);
|
||||
CompletableFuture<Void> depositProcess = depositService
|
||||
.processDeposits(account, currentTeamInfo, prevTeamInfo, nowDate, nowDateTime);
|
||||
.processDeposits(minister, account, currentTeamInfo, prevTeamInfo, nowDate, nowDateTime);
|
||||
|
||||
return CompletableFuture.allOf(registrationProcess, depositProcess)
|
||||
.thenRun(() -> teamService.updateTeamInfo(account, currentTeamInfo));
|
||||
|
@ -18,6 +18,7 @@ package com.zayac.admin.service;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zayac.admin.common.enums.DisEnableStatusEnum;
|
||||
import com.zayac.admin.constant.ApiPathConstants;
|
||||
import com.zayac.admin.req.MemberDetailsReq;
|
||||
import com.zayac.admin.req.PayRecordsListReq;
|
||||
@ -56,7 +57,8 @@ public class DepositService {
|
||||
private final TelegramMessageService telegramMessageService;
|
||||
private final AccountService accountService;
|
||||
|
||||
public CompletableFuture<Void> processDeposits(AccountResp account,
|
||||
public CompletableFuture<Void> processDeposits(UserDO minister,
|
||||
AccountResp account,
|
||||
Team currentTeam,
|
||||
Team previousTeam,
|
||||
LocalDate nowDate,
|
||||
@ -121,6 +123,10 @@ public class DepositService {
|
||||
.buildDepositMessage(agentName, targetTeamAccount.getNewDepositNum(), depositResults
|
||||
.toString(), targetTeamAccount.getFirstDepositNum());
|
||||
UserDO currUser = accountService.getUserByAccountUsername(agentName);
|
||||
if (currUser != null && DisEnableStatusEnum.ENABLE.equals(currUser.getNeedNotify())) {
|
||||
String botToken = StrUtil.isEmpty(currUser.getBotToken()) ? minister.getBotToken() : currUser.getBotToken();
|
||||
telegramMessageService.sendMessage(botToken, currUser.getGroupId(), notification);
|
||||
}
|
||||
telegramMessageService
|
||||
.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, notification);
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package com.zayac.admin.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zayac.admin.common.enums.DisEnableStatusEnum;
|
||||
import com.zayac.admin.constant.ApiPathConstants;
|
||||
import com.zayac.admin.req.team.TeamMemberReq;
|
||||
import com.zayac.admin.resp.MemberPagination;
|
||||
@ -41,11 +43,11 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class RegistrationService {
|
||||
private final CompletableFutureWebClientService completableFutureWebClientService;
|
||||
private final TelegramMessageService notificationService;
|
||||
private final TelegramMessageService telegramMessageService;
|
||||
private final AccountService accountService;
|
||||
|
||||
public CompletableFuture<Void> processRegistration(AccountResp account,
|
||||
public CompletableFuture<Void> processRegistration(UserDO minister,
|
||||
AccountResp account,
|
||||
Team currentTeamInfo,
|
||||
Team prevTeamInfo,
|
||||
LocalDate nowDate,
|
||||
@ -72,9 +74,10 @@ public class RegistrationService {
|
||||
String notification = telegramMessageService
|
||||
.buildRegistrationMessage(accountName, accountMembers, teamAccountMap.get(accountName));
|
||||
UserDO currUser = accountService.getUserByAccountUsername(accountName);
|
||||
// if (currUser != null && DisEnableStatusEnum.ENABLE.equals(currUser.getNeedNotify())) {
|
||||
// telegramMessageService.sendMessage(currUser.getBotToken(), currUser.getGroupId(), notification);
|
||||
// }
|
||||
if (currUser != null && DisEnableStatusEnum.ENABLE.equals(currUser.getNeedNotify())) {
|
||||
String botToken = StrUtil.isEmpty(currUser.getBotToken()) ? minister.getBotToken() : currUser.getBotToken();
|
||||
telegramMessageService.sendMessage(botToken, currUser.getGroupId(), notification);
|
||||
}
|
||||
telegramMessageService
|
||||
.sendMessage("6013830443:AAHUOS4v6Ln19ziZkH-L28-HZQLJrGcvhto", 6054562838L, notification);
|
||||
});
|
||||
|
@ -33,7 +33,10 @@ public class TelegramMessageService {
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
public void sendMessage(String botToken, Long targetId, String message) {
|
||||
String fullMessage = String.format("%s|%s|%s", botToken, targetId, escapeMarkdown(message));
|
||||
if (!containsHTMLCharacters(message)) {
|
||||
message = escapeMarkdown(message);
|
||||
}
|
||||
String fullMessage = String.format("%s|%s|%s", botToken, targetId, message);
|
||||
this.rabbitTemplate.convertAndSend("message_queue", fullMessage);
|
||||
}
|
||||
|
||||
@ -47,6 +50,14 @@ public class TelegramMessageService {
|
||||
return text;
|
||||
}
|
||||
|
||||
private static boolean containsHTMLCharacters(String text) {
|
||||
List<String> htmlTags = Arrays.asList("<b>", "<i>", "<a>", "<code>", "<pre>");
|
||||
if (htmlTags.stream().anyMatch(text::contains)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String buildRegistrationMessage(String accountName,
|
||||
List<TeamMember> accountMembers,
|
||||
TeamAccount teamAccount) {
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.zayac.admin.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TableFormatter {
|
||||
|
||||
public static String formatMarkdownTable(List<String[]> rows) {
|
||||
int[] colWidths = calculateColumnWidths(rows);
|
||||
StringBuilder table = new StringBuilder("```\n");
|
||||
for (String[] row : rows) {
|
||||
table.append(formatRow(row, colWidths)).append("\n");
|
||||
}
|
||||
table.append("```");
|
||||
return table.toString();
|
||||
}
|
||||
|
||||
public static String formatTable(List<String[]> rows) {
|
||||
int[] colWidths = calculateColumnWidths(rows);
|
||||
StringBuilder table = new StringBuilder();
|
||||
for (String[] row : rows) {
|
||||
table.append(formatRow(row, colWidths)).append("\n");
|
||||
}
|
||||
return table.toString();
|
||||
}
|
||||
|
||||
public static String formatTableAsHtml(List<String[]> rows) {
|
||||
int[] colWidths = calculateColumnWidths(rows);
|
||||
StringBuilder table = new StringBuilder("<pre>\n");
|
||||
for (String[] row : rows) {
|
||||
table.append(formatRow(row, colWidths)).append("\n");
|
||||
}
|
||||
table.append("</pre>");
|
||||
return table.toString();
|
||||
}
|
||||
|
||||
private static int[] calculateColumnWidths(List<String[]> rows) {
|
||||
int[] colWidths = new int[rows.get(0).length];
|
||||
for (String[] row : rows) {
|
||||
for (int i = 0; i < row.length; i++) {
|
||||
colWidths[i] = Math.max(colWidths[i], getStringWidth(row[i]));
|
||||
}
|
||||
}
|
||||
return colWidths;
|
||||
}
|
||||
|
||||
private static String formatRow(String[] row, int[] colWidths) {
|
||||
StringBuilder formattedRow = new StringBuilder();
|
||||
for (int i = 0; i < row.length; i++) {
|
||||
formattedRow.append(padRight(row[i], colWidths[i])).append(" ");
|
||||
}
|
||||
return formattedRow.toString().trim();
|
||||
}
|
||||
|
||||
private static int getStringWidth(String s) {
|
||||
return s.codePoints().map(ch -> ch > 0xFF ? 2 : 1).sum();
|
||||
}
|
||||
|
||||
private static String padRight(String s, int n) {
|
||||
int width = getStringWidth(s);
|
||||
int padding = n - width;
|
||||
return s + " ".repeat(Math.max(0, padding));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user