|
@@ -10,6 +10,9 @@ import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.qw.domain.QwCompany;
|
|
import com.fs.qw.domain.QwCompany;
|
|
|
import com.fs.qw.param.QwUpdateContactWayParam;
|
|
import com.fs.qw.param.QwUpdateContactWayParam;
|
|
|
import com.fs.qw.service.IQwCompanyService;
|
|
import com.fs.qw.service.IQwCompanyService;
|
|
|
|
|
+import com.fs.common.config.RedisTenantContext;
|
|
|
|
|
+import com.fs.common.utils.spring.SpringUtils;
|
|
|
|
|
+import com.fs.qw.mapper.QwCompanyMapper;
|
|
|
import com.fs.qwApi.Result.*;
|
|
import com.fs.qwApi.Result.*;
|
|
|
import com.fs.qwApi.config.QwApiConfig;
|
|
import com.fs.qwApi.config.QwApiConfig;
|
|
|
import com.fs.qwApi.domain.*;
|
|
import com.fs.qwApi.domain.*;
|
|
@@ -36,6 +39,7 @@ import org.springframework.stereotype.Service;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -58,6 +62,9 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private QwCompanyMapper qwCompanyMapper;
|
|
|
|
|
+
|
|
|
private static final OkHttpClient client = new OkHttpClient.Builder()
|
|
private static final OkHttpClient client = new OkHttpClient.Builder()
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
|
@@ -1071,10 +1078,56 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
HttpResponse response = httpClient.execute(httpPost);
|
|
HttpResponse response = httpClient.execute(httpPost);
|
|
|
String reJson = EntityUtils.toString(response.getEntity());
|
|
String reJson = EntityUtils.toString(response.getEntity());
|
|
|
QwPermanentCodeResult qwPermanentCodeResult = JSON.parseObject(reJson, QwPermanentCodeResult.class);
|
|
QwPermanentCodeResult qwPermanentCodeResult = JSON.parseObject(reJson, QwPermanentCodeResult.class);
|
|
|
|
|
+
|
|
|
|
|
+ if (qwPermanentCodeResult != null && qwPermanentCodeResult.getErrcode() == 0 && qwPermanentCodeResult.getAuth_corp_info() != null) {
|
|
|
|
|
+ String corpName = qwPermanentCodeResult.getAuth_corp_info().getCorp_name();
|
|
|
|
|
+// String authCorpId = qwPermanentCodeResult.getAuth_corp_info().getCorpid();
|
|
|
|
|
+ String permanentCode = qwPermanentCodeResult.getPermanent_code();
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 根据 corpName 匹配主库主体
|
|
|
|
|
+ QwCompany masterCompany = qwCompanyMapper.selectQwCompanyByCorpName(corpName);
|
|
|
|
|
+ if (masterCompany != null) {
|
|
|
|
|
+ String corpId = masterCompany.getCorpId();
|
|
|
|
|
+ masterCompany.setPermanentCode(permanentCode);
|
|
|
|
|
+ qwCompanyMapper.updateQwCompany(masterCompany);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 更新 Redis 缓存 (主库系统缓存)
|
|
|
|
|
+ redisCache.setCacheObject("qwCompany:permanentCode" + corpId, permanentCode, 1, TimeUnit.HOURS);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 同步到租户库及租户 Redis 缓存
|
|
|
|
|
+ Long tenantId = masterCompany.getTenantId();
|
|
|
|
|
+ if (tenantId != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 切换 Redis 租户上下文
|
|
|
|
|
+ RedisTenantContext.setTenantId(tenantId);
|
|
|
|
|
+ redisCache.setCacheObject("qwCompany:permanentCode" + corpId, permanentCode, 1, TimeUnit.HOURS);
|
|
|
|
|
+
|
|
|
|
|
+ // 通过反射切换租户数据库,避免循环依赖
|
|
|
|
|
+ Object manager = SpringUtils.getBean("tenantDataSourceManager");
|
|
|
|
|
+ Method method = manager.getClass().getMethod("ensureSwitchByTenantId", Long.class);
|
|
|
|
|
+ method.invoke(manager, tenantId);
|
|
|
|
|
+
|
|
|
|
|
+ qwCompanyMapper.updatePermanentCodeByCorpId(corpId, permanentCode);
|
|
|
|
|
+ log.info("getPermanentCode: 已同步租户库及租户Redis永久授权码, tenantId={}, corpId={}", tenantId, corpId);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("getPermanentCode: 同步租户库永久授权码失败 tenantId={}", corpId, e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ RedisTenantContext.clear();
|
|
|
|
|
+ Object manager = SpringUtils.getBean("tenantDataSourceManager");
|
|
|
|
|
+ Method method = manager.getClass().getMethod("clear");
|
|
|
|
|
+ method.invoke(manager);
|
|
|
|
|
+ } catch (Exception ignore) {}
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("getPermanentCode: 未在主库找到名称为 {} 的企微主体,跳过数据库更新", corpName);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
return qwPermanentCodeResult;
|
|
return qwPermanentCodeResult;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ log.error("getPermanentCode error", e);
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|