This commit is contained in:
2025-10-27 18:24:04 +08:00
parent 6d43b3031c
commit 4559638c3b

View File

@@ -477,56 +477,55 @@ private String handleTF(String input) {
// 提取电话第7个字段
String phone = parts[6];
//每天至多从phoneWithTF中进行一次替换
//每天可以使用 phoneWithTF 中的多个不同号码,每个号码只用一次
if ("13243039070".equals(phone) || "17530176250".equals(phone)) {
String today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date());
String phoneUsageKey = "phone_usage:" + today;
String usedPhonesKey = "phone_used_today:" + today;
if (stringRedisTemplate != null) {
try {
// 获取今天已经使用过的号码
String usedPhone = stringRedisTemplate.opsForValue().get(phoneUsageKey);
// 获取今天已经使用过的号码集合
Set<String> usedPhonesToday = stringRedisTemplate.opsForSet().members(usedPhonesKey);
if (usedPhone != null) {
// 今天已经替换过了,使用已记录的号码
phone = usedPhone;
} else {
// 今天还没有替换过,从列表中选择一个可用的号码
// 获取最近使用过的所有号码保留7天
String usedPhonesKey = "used_phones_history";
Set<String> usedPhones = stringRedisTemplate.opsForSet().members(usedPhonesKey);
// 找出还没使用过的号码
List<String> availablePhones = new ArrayList<>();
for (String p : phoneWithTF) {
if (usedPhones == null || !usedPhones.contains(p)) {
availablePhones.add(p);
}
// 找出今天还没使用过的号码
List<String> availablePhones = new ArrayList<>();
for (String p : phoneWithTF) {
if (usedPhonesToday == null || !usedPhonesToday.contains(p)) {
availablePhones.add(p);
}
}
if (!availablePhones.isEmpty()) {
// 随机选择一个今天还没用过的号码
phone = availablePhones.get(new Random().nextInt(availablePhones.size()));
// 记录今天使用的号码24小时后自动过期
stringRedisTemplate.opsForSet().add(usedPhonesKey, phone);
stringRedisTemplate.expire(usedPhonesKey, 1, TimeUnit.DAYS);
} else {
// phoneWithTF 中的号码今天都用完了,使用备用号码(轮换使用)
String backupUsedKey = "backup_phone_used_today:" + today;
Set<String> backupUsedToday = stringRedisTemplate.opsForSet().members(backupUsedKey);
if (!availablePhones.isEmpty()) {
// 随机选择一个可用的号码
phone = availablePhones.get(new Random().nextInt(availablePhones.size()));
// 检查两个备用号码哪个还没用过
if (backupUsedToday == null || !backupUsedToday.contains("15639125541")) {
phone = "15639125541";
} else if (!backupUsedToday.contains("13243039070")) {
phone = "13243039070";
} else {
// 所有号码都用过了,使用备用号码(轮换使用
String lastBackupKey = "last_backup_phone";
// 两个备用号码今天都用过了,继续轮换使用
String lastBackupKey = "last_backup_phone:" + today;
String lastBackup = stringRedisTemplate.opsForValue().get(lastBackupKey);
if ("15639125541".equals(lastBackup)) {
phone = "13243039070";
} else {
phone = "15639125541";
}
stringRedisTemplate.opsForValue().set(lastBackupKey, phone);
// 清空历史记录,重新开始
stringRedisTemplate.delete(usedPhonesKey);
}
// 记录今天使用的号码24小时过期
stringRedisTemplate.opsForValue().set(phoneUsageKey, phone, 1, TimeUnit.DAYS);
// 添加到历史记录(不过期,手动管理)
if (availablePhones.contains(phone)) {
stringRedisTemplate.opsForSet().add(usedPhonesKey, phone);
}
// 记录今天使用的备用号码
stringRedisTemplate.opsForSet().add(backupUsedKey, phone);
stringRedisTemplate.expire(backupUsedKey, 1, TimeUnit.DAYS);
stringRedisTemplate.opsForValue().set("last_backup_phone:" + today, phone, 1, TimeUnit.DAYS);
}
} catch (Exception e) {
// Redis操作失败使用随机号码作为降级方案