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

1、迁移企微活码的接口迁移到qw-api模块中

yys 1 день назад
Родитель
Сommit
5fe79257a7

+ 94 - 11
fs-company/src/main/java/com/fs/company/controller/qw/QwContactWayController.java

@@ -1,5 +1,8 @@
 package com.fs.company.controller.qw;
 
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fs.common.annotation.Log;
 import com.fs.common.core.controller.BaseController;
@@ -7,6 +10,7 @@ import com.fs.common.core.domain.AjaxResult;
 import com.fs.common.core.domain.R;
 import com.fs.common.core.page.TableDataInfo;
 import com.fs.common.enums.BusinessType;
+import com.fs.common.utils.SecurityUtils;
 import com.fs.common.utils.ServletUtils;
 import com.fs.common.utils.TimeUtils;
 import com.fs.common.utils.poi.ExcelUtil;
@@ -17,10 +21,14 @@ import com.fs.qw.domain.QwContactWay;
 import com.fs.qw.param.QwStatisticsParam;
 import com.fs.qw.service.IQwContactWayService;
 import com.fs.qw.service.IQwInformationService;
+import com.fs.qwApi.config.OpenQwConfig;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
+import java.net.SocketTimeoutException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -31,6 +39,7 @@ import java.util.stream.Collectors;
  * @author fs
  * @date 2024-07-05
  */
+@Slf4j
 @RestController
 @RequestMapping("/qw/contactWay")
 public class QwContactWayController extends BaseController
@@ -42,10 +51,13 @@ public class QwContactWayController extends BaseController
     @Autowired
     private IQwInformationService qwInformationService;
 
+    /** HTTP调用超时时间(秒) */
+    @Value("${qw.api.timeout:30}")
+    private int apiTimeout;
+
     /**
      * 查询企微活码列表
      */
-
     @GetMapping("/list")
     public TableDataInfo list(QwContactWay qwContactWay)
     {
@@ -60,9 +72,27 @@ public class QwContactWayController extends BaseController
     @GetMapping("/sync/{corpId}")
     public R sync(@PathVariable("corpId")String corpId)
     {
-
-        return qwContactWayService.selectQwContactWaysync(corpId);
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qw/contactWay/sync/" + corpId + "?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), R.class);
+            } else {
+                log.error("同步企微活码失败,HTTP状态码: {}", response.getStatus());
+                return R.error("同步企微活码失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("同步企微活码异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return R.error("同步企微活码超时,请稍后重试");
+            }
+            return R.error("同步企微活码失败: " + e.getMessage());
+        }
     }
+
     /**
      * 导出企微活码列表
      */
@@ -71,7 +101,6 @@ public class QwContactWayController extends BaseController
     @GetMapping("/export")
     public AjaxResult export(QwContactWay qwContactWay)
     {
-
         List<QwContactWay> list = qwContactWayService.selectQwContactWayList(qwContactWay);
         ExcelUtil<QwContactWay> util = new ExcelUtil<QwContactWay>(QwContactWay.class);
         return util.exportExcel(list, "企微活码数据");
@@ -97,7 +126,26 @@ public class QwContactWayController extends BaseController
     {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         qwContactWay.setCompanyId(loginUser.getCompany().getCompanyId());
-        return qwContactWayService.insertQwContactWay(qwContactWay);
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qw/contactWay/add?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .body(JSON.toJSONString(qwContactWay))
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), R.class);
+            } else {
+                log.error("新增企微活码失败,HTTP状态码: {}", response.getStatus());
+                return R.error("新增企微活码失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("新增企微活码异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return R.error("新增企微活码超时,请稍后重试");
+            }
+            return R.error("新增企微活码失败: " + e.getMessage());
+        }
     }
 
     /**
@@ -108,7 +156,26 @@ public class QwContactWayController extends BaseController
     @PutMapping
     public R edit(@RequestBody QwContactWay qwContactWay)
     {
-        return qwContactWayService.updateQwContactWay(qwContactWay);
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qw/contactWay/edit?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .body(JSON.toJSONString(qwContactWay))
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), R.class);
+            } else {
+                log.error("修改企微活码失败,HTTP状态码: {}", response.getStatus());
+                return R.error("修改企微活码失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("修改企微活码异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return R.error("修改企微活码超时,请稍后重试");
+            }
+            return R.error("修改企微活码失败: " + e.getMessage());
+        }
     }
 
     /**
@@ -116,17 +183,34 @@ public class QwContactWayController extends BaseController
      */
     @PreAuthorize("@ss.hasPermi('qw:contactWay:remove')")
     @Log(title = "企微活码", businessType = BusinessType.DELETE)
-	@DeleteMapping("/{ids}")
+    @DeleteMapping("/{ids}")
     public R remove(@PathVariable Long[] ids)
     {
-
-        return qwContactWayService.deleteQwContactWayByIds(ids);
+        Long tenantId = SecurityUtils.getTenantId();
+        String url = OpenQwConfig.api + "/qw/contactWay/delete?tenantId=" + tenantId;
+        try {
+            HttpResponse response = HttpRequest.post(url)
+                    .body(JSON.toJSONString(ids))
+                    .timeout(apiTimeout * 1000)
+                    .execute();
+            if (response.getStatus() == 200) {
+                return JSON.parseObject(response.body(), R.class);
+            } else {
+                log.error("删除企微活码失败,HTTP状态码: {}", response.getStatus());
+                return R.error("删除企微活码失败,服务返回状态码: " + response.getStatus());
+            }
+        } catch (Exception e) {
+            log.error("删除企微活码异常, url={}", url, e);
+            if (e.getCause() instanceof SocketTimeoutException) {
+                return R.error("删除企微活码超时,请稍后重试");
+            }
+            return R.error("删除企微活码失败: " + e.getMessage());
+        }
     }
 
     @GetMapping("/statistics")
     public R statistics(QwStatisticsParam param)
     {
-//        List<QwWayStatisticsListVO> list= qwContactWayService.QwWayStatisticsListVO(param);
         TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
         timeEntity.setCompanyId(param.getWayId());
         Integer cycleNum = timeEntity.getCycleNum();
@@ -142,7 +226,6 @@ public class QwContactWayController extends BaseController
         List<Integer> deleteNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("deleteNum")).collect(Collectors.toList());
         List<Integer> num = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("num")).collect(Collectors.toList());
         return R.ok().put("list",jsonObjectList).put("dates",dates).put("addNum",addNum).put("deleteNum",deleteNum).put("num",num);
-
     }
 
 

+ 105 - 0
fs-qw-api/src/main/java/com/fs/app/controller/QwContactWayController.java

@@ -0,0 +1,105 @@
+package com.fs.app.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.fs.common.core.controller.BaseController;
+import com.fs.common.core.domain.R;
+import com.fs.framework.datasource.TenantDataSourceUtil;
+import com.fs.qw.domain.QwContactWay;
+import com.fs.qw.service.IQwContactWayService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * 企微活码Controller(API端)
+ * 由Company端通过HTTP远程调用
+ *
+ * @author fs
+ */
+@Slf4j
+@RestController
+@RequestMapping("/qw/contactWay")
+public class QwContactWayController extends BaseController {
+
+    @Autowired
+    private IQwContactWayService qwContactWayService;
+
+    @Autowired
+    private TenantDataSourceUtil tenantDataSourceUtil;
+
+    /**
+     * 同步企微活码
+     */
+    @PostMapping("/sync/{corpId}")
+    public R sync(@PathVariable("corpId") String corpId, @RequestParam("tenantId") Long tenantId) {
+        try {
+            log.info("[QwContactWay] 同步企微活码,tenantId={}, corpId={}", tenantId, corpId);
+            return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
+                return qwContactWayService.selectQwContactWaysync(corpId);
+            });
+        } catch (IllegalArgumentException e) {
+            log.error("[QwContactWay] 同步企微活码失败,租户不存在或已禁用,tenantId={}", tenantId, e);
+            return R.error("租户不存在或已禁用");
+        } catch (Exception e) {
+            log.error("[QwContactWay] 同步企微活码异常,tenantId={}, corpId={}", tenantId, corpId, e);
+            return R.error("同步企微活码失败: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 新增企微活码
+     */
+    @PostMapping("/add")
+    public R add(@RequestBody QwContactWay qwContactWay, @RequestParam("tenantId") Long tenantId) {
+        try {
+            log.info("[QwContactWay] 新增企微活码,tenantId={}", tenantId);
+            return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
+                return qwContactWayService.insertQwContactWay(qwContactWay);
+            });
+        } catch (IllegalArgumentException e) {
+            log.error("[QwContactWay] 新增企微活码失败,租户不存在或已禁用,tenantId={}", tenantId, e);
+            return R.error("租户不存在或已禁用");
+        } catch (Exception e) {
+            log.error("[QwContactWay] 新增企微活码异常,tenantId={}", tenantId, e);
+            return R.error("新增企微活码失败: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 修改企微活码
+     */
+    @PostMapping("/edit")
+    public R edit(@RequestBody QwContactWay qwContactWay, @RequestParam("tenantId") Long tenantId) {
+        try {
+            log.info("[QwContactWay] 修改企微活码,tenantId={}, id={}", tenantId, qwContactWay.getId());
+            return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
+                return qwContactWayService.updateQwContactWay(qwContactWay);
+            });
+        } catch (IllegalArgumentException e) {
+            log.error("[QwContactWay] 修改企微活码失败,租户不存在或已禁用,tenantId={}", tenantId, e);
+            return R.error("租户不存在或已禁用");
+        } catch (Exception e) {
+            log.error("[QwContactWay] 修改企微活码异常,tenantId={}", tenantId, e);
+            return R.error("修改企微活码失败: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 删除企微活码
+     */
+    @PostMapping("/delete")
+    public R delete(@RequestBody Long[] ids, @RequestParam("tenantId") Long tenantId) {
+        try {
+            log.info("[QwContactWay] 删除企微活码,tenantId={}, ids={}", tenantId, JSON.toJSONString(ids));
+            return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
+                return qwContactWayService.deleteQwContactWayByIds(ids);
+            });
+        } catch (IllegalArgumentException e) {
+            log.error("[QwContactWay] 删除企微活码失败,租户不存在或已禁用,tenantId={}", tenantId, e);
+            return R.error("租户不存在或已禁用");
+        } catch (Exception e) {
+            log.error("[QwContactWay] 删除企微活码异常,tenantId={}", tenantId, e);
+            return R.error("删除企微活码失败: " + e.getMessage());
+        }
+    }
+}