Selaa lähdekoodia

多租户组件搬迁

yq 2 vuotta sitten
vanhempi
commit
08c53e1d3b

+ 32 - 0
base-modules/service-system/service-system-api/src/main/java/com/usky/system/RemoteTenantService.java

@@ -0,0 +1,32 @@
+package com.usky.system;
+
+import com.usky.common.core.bean.ApiResult;
+import com.usky.system.factory.RemoteUserFallbackFactory;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+
+import java.util.List;
+
+/**
+ * @author yq
+ * @date 2022/7/12 13:13
+ */
+@FeignClient(contextId = "remoteTenantService", value = "usky-system", fallbackFactory = RemoteUserFallbackFactory.class)
+public interface RemoteTenantService {
+
+    /**
+     * 获得所有租户
+     *
+     * @return 租户编号数组
+     */
+    @GetMapping("/getTenantIds")
+    ApiResult<List<Integer>> getTenantIds();
+
+    /**
+     * 校验租户是否合法
+     *
+     * @param id 租户编号
+     */
+    @GetMapping("/validTenant")
+    ApiResult<Void> validTenant(Integer id);
+}

+ 39 - 0
base-modules/service-system/service-system-api/src/main/java/com/usky/system/factory/RemoteTenantFallbackFactory.java

@@ -0,0 +1,39 @@
+package com.usky.system.factory;
+
+import com.usky.common.core.bean.ApiResult;
+import com.usky.system.RemoteTenantService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.openfeign.FallbackFactory;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @author yq
+ * @date 2022/7/12 13:14
+ */
+@Component
+public class RemoteTenantFallbackFactory implements FallbackFactory<RemoteTenantService> {
+    private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);
+
+    @Override
+    public RemoteTenantService create(Throwable throwable)
+    {
+        log.error("租户服务调用失败:{}", throwable.getMessage());
+        return new RemoteTenantService()
+        {
+
+            @Override
+            public ApiResult<List<Integer>> getTenantIds() {
+                return ApiResult.error("500","获取租户失败:" + throwable.getMessage());
+            }
+
+            @Override
+            public ApiResult<Void> validTenant(Integer id) {
+                return ApiResult.error("500","校验租失败:" + throwable.getMessage());
+            }
+        };
+    }
+}
+

+ 13 - 0
base-modules/service-system/service-system-api/src/main/java/com/usky/system/model/LoginUser.java

@@ -60,6 +60,19 @@ public class LoginUser implements Serializable
      */
     private SysUserVO sysUser;
 
+    public Integer getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(Integer tenantId) {
+        this.tenantId = tenantId;
+    }
+
+    /**
+     * 租户编号
+     */
+    private Integer tenantId;
+
     public String getToken()
     {
         return token;

+ 46 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/api/SysTenantControllerApi.java

@@ -0,0 +1,46 @@
+package com.usky.system.controller.api;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.usky.common.core.bean.ApiResult;
+import com.usky.common.core.constants.CommonConst;
+import com.usky.common.core.exception.BusinessException;
+import com.usky.system.RemoteTenantService;
+import com.usky.system.domain.SysTenant;
+import com.usky.system.service.SysTenantService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author yq
+ * @date 2022/7/12 13:21
+ */
+@RequestMapping("/tenantApi")
+@RestController
+public class SysTenantControllerApi implements RemoteTenantService {
+
+    @Autowired
+    private SysTenantService sysTenantService;
+    @Override
+    public ApiResult<List<Integer>> getTenantIds() {
+        LambdaQueryWrapper<SysTenant> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.select(SysTenant::getId);
+        return ApiResult.success(sysTenantService.list(queryWrapper).stream().map(SysTenant::getId).collect(Collectors.toList()));
+    }
+
+    @Override
+    public ApiResult<Void> validTenant(Integer id) {
+        SysTenant tenant = sysTenantService.getById(id);
+        if (tenant == null) {
+             throw new BusinessException("租户信息为空");
+        }
+        if (tenant.getStatus().equals(CommonConst.FALSE_NUM_STR)) {
+            throw new BusinessException("租户已被停用");
+        }
+        return ApiResult.success();
+    }
+}