|
|
@@ -1,14 +1,20 @@
|
|
|
package com.fs.qw.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.qw.domain.QwCompany;
|
|
|
import com.fs.qw.mapper.QwCompanyMapper;
|
|
|
+import com.fs.qw.param.QwCompanyTenantSetParam;
|
|
|
import com.fs.qw.service.IQwCompanyService;
|
|
|
import com.fs.qw.vo.QwOptionsVO;
|
|
|
+import com.fs.tenant.domain.TenantInfo;
|
|
|
+import com.fs.tenant.mapper.TenantInfoMapper;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
@@ -21,6 +27,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
* @author fs
|
|
|
* @date 2024-10-09
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class QwCompanyServiceImpl implements IQwCompanyService
|
|
|
{
|
|
|
@@ -138,4 +145,121 @@ public class QwCompanyServiceImpl implements IQwCompanyService
|
|
|
public List<String> selectQwCompanyListFormCorpId() {
|
|
|
return qwCompanyMapper.selectQwCompanyListFormCorpId();
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantInfoMapper tenantInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R setTenant(QwCompanyTenantSetParam param) {
|
|
|
+ Long qwCompanyId = param.getQwCompanyId();
|
|
|
+ Long tenantId = param.getTenantId();
|
|
|
+ if (qwCompanyId == null || tenantId == null) {
|
|
|
+ return R.error("未选择所配置的企微主体/租户");
|
|
|
+ }
|
|
|
+ TenantInfo tenantInfo = tenantInfoMapper.selectTenantInfoById(tenantId.toString());
|
|
|
+ if (tenantInfo == null) {
|
|
|
+ return R.error("所配置租户不存在");
|
|
|
+ }
|
|
|
+ QwCompany qwCompany = qwCompanyMapper.selectQwCompanyById(qwCompanyId);
|
|
|
+ if (qwCompany == null) {
|
|
|
+ return R.error("所选企微主体不存在");
|
|
|
+ }
|
|
|
+ qwCompany.setTenantId(tenantId);
|
|
|
+ try {
|
|
|
+ if (!upsertQwCompanyInTenantDatabase(tenantInfo, qwCompany)) {
|
|
|
+ return R.error("更新租户企微主体失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("更新租户企微主体失败:{}",e.getMessage());
|
|
|
+ return R.error("更新租户企微主体失败:"+e.getMessage());
|
|
|
+ }
|
|
|
+ updateQwCompany(qwCompany);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在租户库中按 corpId 同步企微主体:已存在则更新,否则新增。
|
|
|
+ * 直接走 Mapper,避免复用 updateQwCompany 时用租户库主键覆盖按 corpId 缓存的 Redis。
|
|
|
+ */
|
|
|
+ private boolean upsertQwCompanyInTenantDatabase(TenantInfo tenantInfo, QwCompany masterCompany) {
|
|
|
+ if (!switchToTenantDataSource(tenantInfo)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ boolean flag = false;
|
|
|
+ try {
|
|
|
+ String corpId = masterCompany.getCorpId();
|
|
|
+ QwCompany tenantRow = qwCompanyMapper.selectQwCompanyByCorpId(corpId);
|
|
|
+ Long tenantPk = tenantInfo.getId();
|
|
|
+ if (tenantRow != null) {
|
|
|
+ tenantRow.setId(tenantPk);
|
|
|
+ tenantRow.setCorpId(masterCompany.getCorpId());
|
|
|
+ tenantRow.setCorpName(masterCompany.getCorpName());
|
|
|
+ tenantRow.setOpenSecret(masterCompany.getOpenSecret());
|
|
|
+ tenantRow.setOpenCorpId(masterCompany.getOpenCorpId());
|
|
|
+ tenantRow.setServerAgentId(masterCompany.getServerAgentId());
|
|
|
+ tenantRow.setServerBookCorpId(masterCompany.getServerBookCorpId());
|
|
|
+ tenantRow.setServerBookSecret(masterCompany.getServerBookSecret());
|
|
|
+ tenantRow.setToken(masterCompany.getToken());
|
|
|
+ tenantRow.setEncodingAesKey(masterCompany.getEncodingAesKey());
|
|
|
+ tenantRow.setProviderSecret(masterCompany.getProviderSecret());
|
|
|
+ tenantRow.setRealmNameUrl(masterCompany.getRealmNameUrl());
|
|
|
+ tenantRow.setNotifyUrl(masterCompany.getNotifyUrl());
|
|
|
+ tenantRow.setUpdateBy("master");
|
|
|
+ tenantRow.setUpdateTime(DateUtils.getNowDate());
|
|
|
+ tenantRow.setAgentId(masterCompany.getAgentId());
|
|
|
+ tenantRow.setPermanentCode(masterCompany.getPermanentCode());
|
|
|
+
|
|
|
+ qwCompanyMapper.updateQwCompany(tenantRow);
|
|
|
+ } else {
|
|
|
+ QwCompany toInsert = new QwCompany();
|
|
|
+ toInsert.setCorpId(masterCompany.getCorpId());
|
|
|
+ toInsert.setCorpName(masterCompany.getCorpName());
|
|
|
+ toInsert.setOpenSecret(masterCompany.getOpenSecret());
|
|
|
+ toInsert.setOpenCorpId(masterCompany.getOpenCorpId());
|
|
|
+ toInsert.setServerAgentId(masterCompany.getServerAgentId());
|
|
|
+ toInsert.setServerBookCorpId(masterCompany.getServerBookCorpId());
|
|
|
+ toInsert.setServerBookSecret(masterCompany.getServerBookSecret());
|
|
|
+ toInsert.setToken(masterCompany.getToken());
|
|
|
+ toInsert.setEncodingAesKey(masterCompany.getEncodingAesKey());
|
|
|
+ toInsert.setProviderSecret(masterCompany.getProviderSecret());
|
|
|
+ toInsert.setRealmNameUrl(masterCompany.getRealmNameUrl());
|
|
|
+ toInsert.setNotifyUrl(masterCompany.getNotifyUrl());
|
|
|
+ toInsert.setCreateBy("master");
|
|
|
+ toInsert.setCreateTime(DateUtils.getNowDate());
|
|
|
+ toInsert.setAgentId(masterCompany.getAgentId());
|
|
|
+ toInsert.setPermanentCode(masterCompany.getPermanentCode());
|
|
|
+ toInsert.setStatus(1L);
|
|
|
+ qwCompanyMapper.insertQwCompany(toInsert);
|
|
|
+ }
|
|
|
+ flag = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步 qw_company 到租户库失败 tenantId={}, corpId={}", tenantInfo.getId(), masterCompany.getCorpId(), e);
|
|
|
+ } finally {
|
|
|
+ clearTenantDataSourceType();
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean switchToTenantDataSource(TenantInfo tenantInfo) {
|
|
|
+ try {
|
|
|
+ Class<?> mgrClass = Class.forName("com.fs.framework.datasource.TenantDataSourceManager");
|
|
|
+ Object manager = applicationContext.getBean(mgrClass);
|
|
|
+ mgrClass.getMethod("switchTenant", TenantInfo.class).invoke(manager, tenantInfo);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("无法切换租户数据源,跳过租户库 qw_company 同步: {}", e.toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clearTenantDataSourceType() {
|
|
|
+ try {
|
|
|
+ Class<?> holderClass = Class.forName("com.fs.framework.datasource.DynamicDataSourceContextHolder");
|
|
|
+ holderClass.getMethod("clearDataSourceType").invoke(null);
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|