用户同步

This commit is contained in:
2026-05-19 08:35:55 +08:00
parent 7c9ee9101e
commit b73f5505bf
10 changed files with 334 additions and 2 deletions

View File

@@ -3,23 +3,37 @@ package com.rj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.dto.hxr.HxrLbUserSelectResponse;
import com.rj.entity.LbUser;
import com.rj.mapper.LbUserMapper;
import com.rj.service.HxrAdminUserService;
import com.rj.service.ILbUserService;
import com.rj.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Slf4j
@Service
public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> implements ILbUserService {
private static final DateTimeFormatter DATETIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Autowired
private HxrAdminUserService hxrAdminUserService;
@Override
public Map<String, Object> add(LbUser entity) {
Map<String, Object> result = new HashMap<>();
@@ -29,6 +43,12 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
result.put("message", "id不能为空");
return result;
}
if (entity.getTenantId() == null || entity.getTenantId().trim().isEmpty()) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
entity.setTenantId(entity.getTenantId().trim());
if (this.getById(entity.getId()) != null) {
result.put("success", false);
result.put("message", "该用户 id 已存在");
@@ -107,6 +127,7 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
public Map<String, Object> pageQuery(
Integer current,
Integer size,
String tenantId,
Long pid,
String username,
String nickname,
@@ -131,6 +152,9 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
}
LambdaQueryWrapper<LbUser> w = new LambdaQueryWrapper<>();
if (tenantId != null && !tenantId.trim().isEmpty()) {
w.eq(LbUser::getTenantId, tenantId.trim());
}
if (pid != null) {
w.eq(LbUser::getPid, pid);
}
@@ -276,4 +300,127 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
return null;
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> syncFromHxrAdmin(String tenantId) {
Map<String, Object> result = new HashMap<>();
try {
if (tenantId == null || tenantId.trim().isEmpty()) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
String tid = tenantId.trim();
String previousTenantId = TenantContextHolder.getTenantId();
TenantContextHolder.setTenantId(tid);
try {
HxrLbUserSelectResponse firstBody = null;
int page = 1;
int totalSynced = 0;
while (true) {
Optional<HxrLbUserSelectResponse> opt = hxrAdminUserService.fetchUserSelectPage(page);
if (opt.isEmpty()) {
if (page == 1) {
result.put("success", false);
result.put("message", "未拉取到用户(请检查 hxr.admin cookie/phpsid、网络或后台返回");
result.put("synced", 0);
return result;
}
result.put("success", false);
result.put("message", "" + page + " 页拉取失败,已成功同步前 "
+ (page - 1) + " 页共 " + totalSynced + "");
result.put("synced", totalSynced);
if (firstBody != null) {
result.put("remoteCount", firstBody.count());
}
result.put("pages", page - 1);
return result;
}
HxrLbUserSelectResponse body = opt.get();
if (firstBody == null) {
firstBody = body;
}
List<LbUser> rows = body.data();
if (rows == null || rows.isEmpty()) {
break;
}
List<LbUser> entities = new ArrayList<>(rows.size());
for (LbUser row : rows) {
if (row.getId() == null) {
continue;
}
row.setTenantId(tid);
applyDefaults(row);
entities.add(row);
}
int upserted = upsertBatch(entities);
if (upserted < 0) {
result.put("success", false);
result.put("message", "" + page + " 页保存失败,已成功同步前 "
+ (page - 1) + " 页共 " + totalSynced + "");
result.put("synced", totalSynced);
result.put("remoteCount", firstBody.count());
result.put("pages", page - 1);
return result;
}
totalSynced += upserted;
if (rows.size() < HxrAdminUserService.USER_SELECT_PAGE_SIZE) {
break;
}
page++;
}
result.put("success", true);
result.put("message", totalSynced == 0 ? "接口成功,无用户数据" : "同步完成");
result.put("synced", totalSynced);
result.put("remoteCount", firstBody != null ? firstBody.count() : 0);
result.put("pages", page);
return result;
} finally {
if (previousTenantId != null) {
TenantContextHolder.setTenantId(previousTenantId);
} else {
TenantContextHolder.clear();
}
}
} catch (Exception e) {
result.put("success", false);
result.put("message", "同步异常:" + e.getMessage());
result.put("synced", 0);
log.error("syncFromHxrAdmin failed", e);
return result;
}
}
/**
* @return 实际 upsert 条数;无有效 id 时返回 0失败返回 -1
*/
private int upsertBatch(List<LbUser> entities) {
if (entities == null || entities.isEmpty()) {
return 0;
}
Map<Long, LbUser> deduped = new LinkedHashMap<>();
for (LbUser entity : entities) {
if (entity.getId() != null) {
deduped.put(entity.getId(), entity);
}
}
if (deduped.isEmpty()) {
return 0;
}
List<LbUser> rows = new ArrayList<>(deduped.values());
try {
baseMapper.upsertBatch(rows);
return rows.size();
} catch (Exception e) {
log.error("lb_user upsertBatch failed, size={}", rows.size(), e);
return -1;
}
}
}