企微前后台联调。

This commit is contained in:
2026-04-24 20:54:14 +08:00
parent 1f7b1e6d55
commit 1627e486a2
7 changed files with 26 additions and 14 deletions

View File

@@ -73,8 +73,9 @@ public class QweiDepartmentController {
@PostMapping("/sync")
@Operation(summary = "同步企微部门并入库")
public ResponseEntity<Map<String, Object>> sync() {
Map<String, Object> result = qweiDepartmentService.syncFromQiWei();
public ResponseEntity<Map<String, Object>> sync(
@RequestParam(required = false) String tenantId) {
Map<String, Object> result = qweiDepartmentService.syncFromQiWei(tenantId);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);

View File

@@ -74,8 +74,9 @@ public class QweiUserController {
@PostMapping("/sync")
@Operation(summary = "同步企微用户并入库")
public ResponseEntity<Map<String, Object>> sync() {
Map<String, Object> result = qweiUserService.syncFromQiWei();
public ResponseEntity<Map<String, Object>> sync(
@RequestParam(required = false) String tenantId) {
Map<String, Object> result = qweiUserService.syncFromQiWei(tenantId);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);

View File

@@ -19,6 +19,10 @@ public interface IQweiDepartmentService extends IService<QweiDepartment> {
String deptName,
Long parentDeptId);
Map<String, Object> syncFromQiWei();
default Map<String, Object> syncFromQiWei() {
return syncFromQiWei(null);
}
Map<String, Object> syncFromQiWei(String tenantId);
}

View File

@@ -20,6 +20,10 @@ public interface IQweiUserService extends IService<QweiUser> {
Long mainDepartmentId,
Integer status);
Map<String, Object> syncFromQiWei();
default Map<String, Object> syncFromQiWei() {
return syncFromQiWei(null);
}
Map<String, Object> syncFromQiWei(String tenantId);
}

View File

@@ -167,11 +167,11 @@ public class QweiDepartmentServiceImpl extends ServiceImpl<QweiDepartmentMapper,
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> syncFromQiWei() {
public Map<String, Object> syncFromQiWei(String tenantIdParam) {
Map<String, Object> result = new HashMap<>();
try {
QiWeiConfig cfg = loadQiWeiConfig();
String tenantId = resolveTenantId(null);
String tenantId = resolveTenantId(tenantIdParam);
// 确保后续对 qwei_department 的查询/写入都带 tenant_id 条件
TenantContextHolder.setTenantId(tenantId);
String corpId = requireNonBlank(cfg.getCorpid(), "qiwei_config.corpid不能为空");

View File

@@ -173,11 +173,11 @@ public class QweiUserServiceImpl extends ServiceImpl<QweiUserMapper, QweiUser> i
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> syncFromQiWei() {
public Map<String, Object> syncFromQiWei(String tenantIdParam) {
Map<String, Object> result = new HashMap<>();
String tenantId = null;
try {
tenantId = resolveTenantId(null);
tenantId = resolveTenantId(tenantIdParam);
TenantContextHolder.setTenantId(tenantId);
QiWeiConfig cfg = loadQiWeiConfig();

View File

@@ -49,9 +49,10 @@ public class QweiDepartmentControllerTest {
serviceResult.put("inserted", 3);
serviceResult.put("updated", 5);
serviceResult.put("total", 8);
when(qweiDepartmentService.syncFromQiWei()).thenReturn(serviceResult);
when(qweiDepartmentService.syncFromQiWei("TENANT_TEST")).thenReturn(serviceResult);
mockMvc.perform(post("/api/qweiDepartment/sync")
.param("tenantId", "TENANT_TEST")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
@@ -59,7 +60,7 @@ public class QweiDepartmentControllerTest {
.andExpect(jsonPath("$.updated").value(5))
.andExpect(jsonPath("$.total").value(8));
verify(qweiDepartmentService, times(1)).syncFromQiWei();
verify(qweiDepartmentService, times(1)).syncFromQiWei("TENANT_TEST");
}
@Test
@@ -67,14 +68,15 @@ public class QweiDepartmentControllerTest {
Map<String, Object> serviceResult = new HashMap<>();
serviceResult.put("success", false);
serviceResult.put("message", "同步异常:测试错误");
when(qweiDepartmentService.syncFromQiWei()).thenReturn(serviceResult);
when(qweiDepartmentService.syncFromQiWei("TENANT_TEST")).thenReturn(serviceResult);
mockMvc.perform(post("/api/qweiDepartment/sync")
.param("tenantId", "TENANT_TEST")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.success").value(false));
verify(qweiDepartmentService, times(1)).syncFromQiWei();
verify(qweiDepartmentService, times(1)).syncFromQiWei("TENANT_TEST");
}
}