补充自动同步的手机号码
This commit is contained in:
@@ -27,6 +27,8 @@ import java.util.UUID;
|
||||
public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMapper, LbDepartmentUser>
|
||||
implements ILbDepartmentUserService {
|
||||
|
||||
private static final int PURCHASE_APPLY_BATCH_SIZE = 200;
|
||||
|
||||
@Autowired
|
||||
private LbDailyUserTradeMapper lbDailyUserTradeMapper;
|
||||
@Autowired
|
||||
@@ -259,6 +261,8 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
|
||||
.ge(LbDailyUserTrade::getReportDate, startDate)
|
||||
.le(LbDailyUserTrade::getReportDate, endDate)
|
||||
.isNotNull(LbDailyUserTrade::getNickname)
|
||||
.orderByDesc(LbDailyUserTrade::getUpdatedAt)
|
||||
.orderByDesc(LbDailyUserTrade::getCreatedAt)
|
||||
);
|
||||
if (tradeList == null || tradeList.isEmpty()) {
|
||||
result.put("success", true);
|
||||
@@ -271,51 +275,74 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
|
||||
return result;
|
||||
}
|
||||
|
||||
List<LbDepartmentUser> existedUsers = this.list(
|
||||
new LambdaQueryWrapper<LbDepartmentUser>()
|
||||
.select(LbDepartmentUser::getName)
|
||||
);
|
||||
Set<String> existingNameSet = new HashSet<>();
|
||||
List<LbDepartmentUser> existedUsers = this.list();
|
||||
Map<String, LbDepartmentUser> existingUserMap = new HashMap<>();
|
||||
for (LbDepartmentUser user : existedUsers) {
|
||||
if (user.getName() != null && !user.getName().trim().isEmpty()) {
|
||||
existingNameSet.add(user.getName().trim());
|
||||
existingUserMap.put(user.getName().trim(), user);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, LbDailyUserTrade> latestTradeByNickname = new HashMap<>();
|
||||
List<LbDepartmentUser> toInsert = new ArrayList<>();
|
||||
List<LbDepartmentUser> toUpdate = new ArrayList<>();
|
||||
int skippedCount = 0;
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Set<String> candidateNicknameSet = new HashSet<>();
|
||||
for (LbDailyUserTrade trade : tradeList) {
|
||||
String nickname = trade.getNickname() == null ? null : trade.getNickname().trim();
|
||||
if (nickname == null || nickname.isEmpty()) {
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
if (existingNameSet.contains(nickname)) {
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
candidateNicknameSet.add(nickname);
|
||||
// tradeList 已按更新时间倒序,首次写入即最新数据
|
||||
latestTradeByNickname.putIfAbsent(nickname, trade);
|
||||
}
|
||||
|
||||
LbDepartmentUser entity = new LbDepartmentUser();
|
||||
entity.setId(UUID.randomUUID().toString());
|
||||
Map<String, LbPurchaseApply> phoneMap = buildPurchaseApplyPhoneMap(candidateNicknameSet);
|
||||
for (Map.Entry<String, LbDailyUserTrade> entry : latestTradeByNickname.entrySet()) {
|
||||
String nickname = entry.getKey();
|
||||
LbDailyUserTrade trade = entry.getValue();
|
||||
LbDepartmentUser entity = existingUserMap.get(nickname);
|
||||
boolean isInsert = false;
|
||||
if (entity == null) {
|
||||
entity = new LbDepartmentUser();
|
||||
entity.setId(UUID.randomUUID().toString());
|
||||
entity.setCreateTime(now);
|
||||
isInsert = true;
|
||||
}
|
||||
entity.setTenantId(trade.getTenantId());
|
||||
entity.setUserId(trade.getUserId());
|
||||
entity.setName(nickname);
|
||||
// 若手机号为空,则尝试从进货申请表补齐
|
||||
entity.setPhone(findPhoneFromPurchaseApply(trade.getTenantId(), nickname));
|
||||
entity.setCreateTime(now);
|
||||
entity.setParentId(trade.getPromoterId());
|
||||
entity.setParentName(trade.getPromoterName());
|
||||
|
||||
LbPurchaseApply purchaseApply = phoneMap.get(buildPhoneKey(trade.getTenantId(), nickname));
|
||||
if (purchaseApply != null && purchaseApply.getApplyPhone() != null) {
|
||||
String phone = purchaseApply.getApplyPhone().trim();
|
||||
entity.setPhone(phone.isEmpty() ? null : phone);
|
||||
entity.setJoinDate(purchaseApply.getApplyDate());
|
||||
}
|
||||
entity.setUpdateTime(now);
|
||||
toInsert.add(entity);
|
||||
existingNameSet.add(nickname);
|
||||
if (isInsert) {
|
||||
toInsert.add(entity);
|
||||
} else {
|
||||
toUpdate.add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (!toInsert.isEmpty()) {
|
||||
this.saveBatch(toInsert);
|
||||
}
|
||||
if (!toUpdate.isEmpty()) {
|
||||
this.updateBatchById(toUpdate);
|
||||
}
|
||||
|
||||
result.put("success", true);
|
||||
result.put("message", "同步完成");
|
||||
result.put("insertedCount", toInsert.size());
|
||||
result.put("updatedCount", toUpdate.size());
|
||||
result.put("skippedCount", skippedCount);
|
||||
result.put("sourceCount", tradeList.size());
|
||||
result.put("startDate", startDate);
|
||||
@@ -328,24 +355,44 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
|
||||
}
|
||||
}
|
||||
|
||||
private String findPhoneFromPurchaseApply(String tenantId, String applyUser) {
|
||||
if (applyUser == null || applyUser.trim().isEmpty()) {
|
||||
return null;
|
||||
private Map<String, LbPurchaseApply> buildPurchaseApplyPhoneMap(Set<String> applyUserSet) {
|
||||
Map<String, LbPurchaseApply> phoneMap = new HashMap<>();
|
||||
if (applyUserSet == null || applyUserSet.isEmpty()) {
|
||||
return phoneMap;
|
||||
}
|
||||
LambdaQueryWrapper<LbPurchaseApply> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (tenantId != null && !tenantId.trim().isEmpty()) {
|
||||
queryWrapper.eq(LbPurchaseApply::getTenantId, tenantId.trim());
|
||||
|
||||
List<String> applyUserList = new ArrayList<>(applyUserSet);
|
||||
for (int i = 0; i < applyUserList.size(); i += PURCHASE_APPLY_BATCH_SIZE) {
|
||||
int end = Math.min(i + PURCHASE_APPLY_BATCH_SIZE, applyUserList.size());
|
||||
List<String> batchUsers = applyUserList.subList(i, end);
|
||||
|
||||
List<LbPurchaseApply> purchaseApplyList = lbPurchaseApplyMapper.selectList(
|
||||
new LambdaQueryWrapper<LbPurchaseApply>()
|
||||
.in(LbPurchaseApply::getApplyUser, batchUsers)
|
||||
.isNotNull(LbPurchaseApply::getApplyPhone)
|
||||
.orderByDesc(LbPurchaseApply::getUpdateTime)
|
||||
.orderByDesc(LbPurchaseApply::getCreateTime)
|
||||
);
|
||||
for (LbPurchaseApply item : purchaseApplyList) {
|
||||
if (item.getApplyUser() == null || item.getApplyPhone() == null) {
|
||||
continue;
|
||||
}
|
||||
String applyUser = item.getApplyUser().trim();
|
||||
String phone = item.getApplyPhone().trim();
|
||||
if (applyUser.isEmpty() || phone.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
String key = buildPhoneKey(item.getTenantId(), applyUser);
|
||||
// 已按更新时间倒序,首次写入即最新手机号
|
||||
phoneMap.putIfAbsent(key, item);
|
||||
}
|
||||
}
|
||||
queryWrapper.eq(LbPurchaseApply::getApplyUser, applyUser.trim())
|
||||
.isNotNull(LbPurchaseApply::getApplyPhone)
|
||||
.orderByDesc(LbPurchaseApply::getUpdateTime)
|
||||
.orderByDesc(LbPurchaseApply::getCreateTime)
|
||||
.last("limit 1");
|
||||
LbPurchaseApply purchaseApply = lbPurchaseApplyMapper.selectOne(queryWrapper);
|
||||
if (purchaseApply == null || purchaseApply.getApplyPhone() == null) {
|
||||
return null;
|
||||
}
|
||||
String phone = purchaseApply.getApplyPhone().trim();
|
||||
return phone.isEmpty() ? null : phone;
|
||||
return phoneMap;
|
||||
}
|
||||
|
||||
private String buildPhoneKey(String tenantId, String applyUser) {
|
||||
String tenantPart = tenantId == null ? "" : tenantId.trim();
|
||||
String userPart = applyUser == null ? "" : applyUser.trim();
|
||||
return tenantPart + "#" + userPart;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user