Просмотр исходного кода

1、迁移企微获客链接的接口迁移到qw-api模块中

yys 1 месяц назад
Родитель
Сommit
a216a7f62f

+ 54 - 89
fs-company/src/main/java/com/fs/company/controller/qw/QwCustomerLinkController.java

@@ -1,31 +1,30 @@
 package com.fs.company.controller.qw;
 
-import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.fs.common.result.Result;
+import com.fs.common.utils.SecurityUtils;
 import com.fs.qw.domain.QwCustomerLink;
 import com.fs.qw.domain.QwCustomerLinkChannel;
 import com.fs.qw.domain.QwCustomerLinkUser;
 import com.fs.qw.dto.QwCustomerLinkChannelReq;
 import com.fs.qw.dto.QwCustomerLinkReq;
-import com.fs.qw.dto.QwCustomerLinkUserDto;
 import com.fs.qw.service.IQwCustomerLinkChannelService;
 import com.fs.qw.service.IQwCustomerLinkService;
 import com.fs.qw.service.IQwCustomerLinkUserService;
-import com.fs.qwApi.domain.QwLinkCreateResult;
-import com.fs.qwApi.param.QwLinkCreateParam;
-import com.fs.qwApi.service.QwApiService;
+import com.fs.qwApi.config.OpenQwConfig;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.transaction.annotation.Transactional;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.List;
-import java.util.stream.Collectors;
+import java.net.SocketTimeoutException;
 
 /**
  * <p>
@@ -35,6 +34,7 @@ import java.util.stream.Collectors;
  * @author zhangqin
  * @since 2025-12-03
  */
+@Slf4j
 @RestController
 @RequestMapping("/qwCustomerLink")
 public class QwCustomerLinkController {
@@ -45,8 +45,10 @@ public class QwCustomerLinkController {
     private IQwCustomerLinkUserService qwCustomerLinkUserService;
     @Autowired
     private IQwCustomerLinkChannelService qwCustomerLinkChannelService;
-    @Autowired
-    private QwApiService qwApiService;
+
+    /** HTTP调用超时时间(秒) */
+    @Value("${qw.api.timeout:30}")
+    private int apiTimeout;
 
     /**
      * 分页查询主链
@@ -89,96 +91,59 @@ public class QwCustomerLinkController {
     }
 
     /**
-     * 新增/编辑 主链
+     * 新增/编辑 主链(远程调用fs-qw-api)
      */
     @PostMapping("/createOrUpdate")
-    @Transactional(rollbackFor = Exception.class)
     public Result<Void> create(@RequestBody QwCustomerLinkReq qwGroupLiveCode) {
-        QwCustomerLink bean = BeanUtil.toBean(qwGroupLiveCode, QwCustomerLink.class);
-
-        QwLinkCreateParam qwLinkCreateParam = new QwLinkCreateParam();
-        qwLinkCreateParam.setLink_name(qwGroupLiveCode.getLinkName());
-        qwLinkCreateParam.setSkip_verify(qwGroupLiveCode.getSkipVerify());
-        QwLinkCreateParam.Range range = new QwLinkCreateParam.Range();
-
-        range.setUser_list(qwGroupLiveCode.getLinkUser().stream().map(QwCustomerLinkUserDto::getQwUserId).collect(Collectors.toList()));
-        qwLinkCreateParam.setRange(range);
-
-        boolean success = false;
-        if (qwGroupLiveCode.getId() != null) {
-            QwCustomerLink byId = qwCustomerLinkService.getById(qwGroupLiveCode.getId());
-            qwLinkCreateParam.setLink_id(byId.getLinkId());
-            // qwApiService.linkUpdate(qwLinkCreateParam, qwGroupLiveCode.getCorpId());
-            success = qwCustomerLinkService.updateById(bean);
-        } else {
-            QwLinkCreateResult qwLinkCreateResult = qwApiService.linkCreate(qwLinkCreateParam, qwGroupLiveCode.getCorpId());
-            bean.setLinkId(qwLinkCreateResult.getLinkId());
-            bean.setUrl(qwLinkCreateResult.getUrl());
-/*            bean.setLinkId(IdUtil.randomUUID());
-            bean.setUrl("https://work.weixin.qq.com/ca/" + IdUtil.randomUUID())*/;
-            success = qwCustomerLinkService.save(bean);
-        }
-
-        if (success) {
-            qwCustomerLinkUserService.remove(new LambdaQueryWrapper<QwCustomerLinkUser>().eq(QwCustomerLinkUser::getSysLinkId, bean.getId()));
-            qwCustomerLinkUserService.saveBatch(qwGroupLiveCode.getLinkUser().stream()
-                    .map(e -> {
-                        QwCustomerLinkUser user = new QwCustomerLinkUser();
-                        user.setSysLinkId(bean.getId());
-                        user.setQwUserId(e.getQwUserId());
-                        user.setSysQwUserId(e.getSysQwUserId());
-                        user.setQwUserName(e.getQwUserName());
-                        return user;
-                    })
-                    .collect(Collectors.toList()));
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qwCustomerLink/createOrUpdate?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .body(JSON.toJSONString(qwGroupLiveCode))
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), Result.class);
+            } else {
+                log.error("新增/编辑主链失败,HTTP状态码: {}", response.getStatus());
+                return Result.error("新增/编辑主链失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("新增/编辑主链异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return Result.error("新增/编辑主链超时,请稍后重试");
+            }
+            return Result.error("新增/编辑主链失败: " + e.getMessage());
         }
-        return success ? Result.success() : Result.error("创建失败");
     }
 
     /**
-     * 新增渠道链接
+     * 新增渠道链接(远程调用fs-qw-api)
      */
     @PostMapping("/channel/create")
     public Result<Void> createChannel(@RequestBody QwCustomerLinkChannelReq linkChannelReq) {
-
-        // 主链
-        QwCustomerLink qwCustomerLink = qwCustomerLinkService.getById(linkChannelReq.getSysLinkId());
-        // 主链关联的企微用户
-        List<QwCustomerLinkUser> linkUsers = qwCustomerLinkUserService.list(new LambdaQueryWrapper<QwCustomerLinkUser>()
-                .eq(QwCustomerLinkUser::getSysLinkId, qwCustomerLink.getId()));
-        List<String> userIdList = linkUsers.stream()
-                .map(QwCustomerLinkUser::getQwUserId)
-                .collect(Collectors.toList());
-
-        List<QwCustomerLinkChannel> channels = linkChannelReq.getChannelList().stream()
-                .map(channelDto -> {
-                    QwLinkCreateParam param = new QwLinkCreateParam();
-                    param.setLink_name(channelDto.getChannelName());
-                    param.setSkip_verify(qwCustomerLink.getSkipVerify().equals(1));
-                    QwLinkCreateParam.Range range = new QwLinkCreateParam.Range();
-                    range.setUser_list(userIdList);
-                    param.setRange(range);
-
-                    // QwLinkCreateResult linkResult = qwApiService.linkCreate(param, qwCustomerLink.getCorpId());
-
-                    QwCustomerLinkChannel channel = new QwCustomerLinkChannel();
-                    channel.setChannelId(channelDto.getChannelId());
-                    channel.setChannelName(channelDto.getChannelName());
-                    channel.setSysLinkId(linkChannelReq.getSysLinkId());
-                    channel.setLinkName(channelDto.getChannelName());
-                    // channel.setLinkId(linkResult.getLinkId());
-                    // channel.setUrl(linkResult.getUrl());
-
-                    channel.setLinkId(IdUtil.randomUUID());
-                    channel.setUrl("https://work.weixin.qq.com/ca/" + IdUtil.randomUUID());
-                    return channel;
-                })
-                .collect(Collectors.toList());
-
-        return qwCustomerLinkChannelService.saveBatch(channels) ? Result.success() : Result.error("创建失败");
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qwCustomerLink/channel/create?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .body(JSON.toJSONString(linkChannelReq))
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), Result.class);
+            } else {
+                log.error("新增渠道链接失败,HTTP状态码: {}", response.getStatus());
+                return Result.error("新增渠道链接失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("新增渠道链接异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return Result.error("新增渠道链接超时,请稍后重试");
+            }
+            return Result.error("新增渠道链接失败: " + e.getMessage());
+        }
     }
 
-
     /**
      * 删除 主链
      */

+ 49 - 1
fs-qw-api/src/main/java/com/fs/app/controller/QwCustomerLinkController.java

@@ -1,15 +1,17 @@
 package com.fs.app.controller;
 
 import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.util.IdUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.fs.common.core.controller.BaseController;
 import com.fs.common.result.Result;
 import com.fs.framework.datasource.TenantDataSourceUtil;
 import com.fs.qw.domain.QwCustomerLink;
+import com.fs.qw.domain.QwCustomerLinkChannel;
 import com.fs.qw.domain.QwCustomerLinkUser;
+import com.fs.qw.dto.QwCustomerLinkChannelReq;
 import com.fs.qw.dto.QwCustomerLinkReq;
 import com.fs.qw.dto.QwCustomerLinkUserDto;
+import com.fs.qw.service.IQwCustomerLinkChannelService;
 import com.fs.qw.service.IQwCustomerLinkService;
 import com.fs.qw.service.IQwCustomerLinkUserService;
 import com.fs.qwApi.domain.QwLinkCreateResult;
@@ -20,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
 import java.util.stream.Collectors;
 
 /**
@@ -40,6 +43,9 @@ public class QwCustomerLinkController extends BaseController {
     @Autowired
     private IQwCustomerLinkUserService qwCustomerLinkUserService;
 
+    @Autowired
+    private IQwCustomerLinkChannelService qwCustomerLinkChannelService;
+
     @Autowired
     private QwApiService qwApiService;
 
@@ -92,4 +98,46 @@ public class QwCustomerLinkController extends BaseController {
             return success ? Result.success() : Result.error("创建失败");
         });
     }
+
+    /**
+     * 新增渠道链接
+     */
+    @PostMapping("/channel/create")
+    public Result<Void> createChannel(@RequestBody QwCustomerLinkChannelReq linkChannelReq, @RequestParam("tenantId") Long tenantId) {
+        log.info("[QwCustomerLink] 新增渠道链接,tenantId={}", tenantId);
+        return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
+            // 主链
+            QwCustomerLink qwCustomerLink = qwCustomerLinkService.getById(linkChannelReq.getSysLinkId());
+            // 主链关联的企微用户
+            List<QwCustomerLinkUser> linkUsers = qwCustomerLinkUserService.list(new LambdaQueryWrapper<QwCustomerLinkUser>()
+                    .eq(QwCustomerLinkUser::getSysLinkId, qwCustomerLink.getId()));
+            List<String> userIdList = linkUsers.stream()
+                    .map(QwCustomerLinkUser::getQwUserId)
+                    .collect(Collectors.toList());
+
+            List<QwCustomerLinkChannel> channels = linkChannelReq.getChannelList().stream()
+                    .map(channelDto -> {
+                        QwLinkCreateParam param = new QwLinkCreateParam();
+                        param.setLink_name(channelDto.getChannelName());
+                        param.setSkip_verify(qwCustomerLink.getSkipVerify().equals(1));
+                        QwLinkCreateParam.Range range = new QwLinkCreateParam.Range();
+                        range.setUser_list(userIdList);
+                        param.setRange(range);
+
+                        QwLinkCreateResult linkResult = qwApiService.linkCreate(param, qwCustomerLink.getCorpId());
+
+                        QwCustomerLinkChannel channel = new QwCustomerLinkChannel();
+                        channel.setChannelId(channelDto.getChannelId());
+                        channel.setChannelName(channelDto.getChannelName());
+                        channel.setSysLinkId(linkChannelReq.getSysLinkId());
+                        channel.setLinkName(channelDto.getChannelName());
+                        channel.setLinkId(linkResult.getLinkId());
+                        channel.setUrl(linkResult.getUrl());
+                        return channel;
+                    })
+                    .collect(Collectors.toList());
+
+            return qwCustomerLinkChannelService.saveBatch(channels) ? Result.success() : Result.error("创建失败");
+        });
+    }
 }