Kaynağa Gözat

Merge branch 'master' of http://47.111.81.118:3000/uskycloud/usky-cloud into fu-dev

fuyuchuan 7 ay önce
ebeveyn
işleme
3eb3145058

+ 16 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/SysUserTenantController.java

@@ -5,10 +5,14 @@ import com.usky.common.core.bean.ApiResult;
 import com.usky.common.core.exception.BusinessErrorCode;
 import com.usky.common.log.annotation.Log;
 import com.usky.common.log.enums.BusinessType;
+import com.usky.common.security.utils.SecurityUtils;
 import com.usky.system.domain.SysTenant;
+import com.usky.system.domain.SysUser;
 import com.usky.system.domain.SysUserTenant;
 import com.usky.system.service.SysUserTenantService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -62,5 +66,17 @@ public class SysUserTenantController extends BaseController {
     {
         return ApiResult.success(sysUserTenantService.getTenantByUser(userId));
     }
+
+    /**
+     * 修改用户默认租户
+     */
+    @Log(title = "修改用户默认租户", businessType = BusinessType.UPDATE)
+    @Transactional
+    @PutMapping
+    public ApiResult<Void> edit(@Validated @RequestBody SysUserTenant userTenant)
+    {
+        sysUserTenantService.updateUserTenant(userTenant);
+        return ApiResult.success();
+    }
 }
 

+ 9 - 1
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/TokenController.java

@@ -58,6 +58,9 @@ public class TokenController {
     @Autowired
     private MceSettingService mceSettingService;
 
+    @Autowired
+    private SysUserTenantService sysUserTenantService;
+
     @Resource
     private HttpServletRequest request;
     @Resource
@@ -78,9 +81,14 @@ public class TokenController {
         // 用户登录
         SysUser userInfo = sysLoginService.appLogin(form.getUsername(), form.getPassword(), form.getTenantId(),
                 form.getPhone(), form.getVerify(), form.getMethod());
+        Integer isDefaulTenant = sysUserTenantService.getIsDefaultByUser(userInfo.getUserId());
         LoginUser sysUser = new LoginUser();
         SysUserVO sysUserVO = BeanMapperUtils.map(userInfo, SysUserVO.class);
-        sysUserVO.setTenantId(userInfo.getTenantId());
+        if (null != form.getMethod() && form.getMethod().equals("switch")){
+            sysUserVO.setTenantId(userInfo.getTenantId());
+        }else {
+            sysUserVO.setTenantId(isDefaulTenant);
+        }
         sysUser.setSysUser(sysUserVO);
         Set<String> rolePermission = permissionService.getRolePermission(userInfo.getUserId());
         sysUser.setRoles(rolePermission);

+ 6 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/domain/SysTenant.java

@@ -121,4 +121,10 @@ public class SysTenant implements Serializable {
      */
     @TableField(exist = false)
     private Integer originalTenantId;
+
+    /**
+     * 是否为默认租户(默认0; 0:否,1:是)
+     */
+    @TableField(exist = false)
+    private Boolean isDefault;
 }

+ 12 - 4
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/SysUserTenantService.java

@@ -3,6 +3,7 @@ package com.usky.system.service;
 import com.usky.system.domain.SysTenant;
 import com.usky.system.domain.SysUserTenant;
 import com.usky.common.mybatis.core.CrudService;
+import com.usky.system.service.vo.TenantPlatformVo;
 
 import java.util.List;
 
@@ -20,7 +21,7 @@ public interface SysUserTenantService extends CrudService<SysUserTenant> {
      *
      * @param sysUserTenant
      */
-    public void insertInviteUser(SysUserTenant sysUserTenant);
+    void insertInviteUser(SysUserTenant sysUserTenant);
 
     /**
      * 企业解绑用户
@@ -28,15 +29,22 @@ public interface SysUserTenantService extends CrudService<SysUserTenant> {
      * @param tenantId 租户ID
      * @param userId 用户ID
      */
-    public void deleteUserTenant(Integer tenantId,Long userId);
+    void deleteUserTenant(Integer tenantId,Long userId);
 
     /**
      * 根据用户查询企业下拉框
      */
-    public List<SysTenant> getTenantByUser(Long userId);
+    List<SysTenant> getTenantByUser(Long userId);
 
     /**
      * 根据用户查询绑定状态
      */
-    public Boolean getIdByUser(Integer tenantId,Long userId);
+    Boolean getIdByUser(Integer tenantId,Long userId);
+
+    /**
+     * 根据用户查询默认租户ID
+     */
+    Integer getIsDefaultByUser(Long userId);
+
+    void updateUserTenant(SysUserTenant userTenant);
 }

+ 36 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysUserTenantServiceImpl.java

@@ -1,6 +1,7 @@
 package com.usky.system.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.common.core.exception.BusinessException;
@@ -13,6 +14,7 @@ import com.usky.system.mapper.SysUserTenantMapper;
 import com.usky.system.service.SysTenantService;
 import com.usky.system.service.SysUserTenantService;
 import com.usky.common.mybatis.core.AbstractCrudService;
+import com.usky.system.service.vo.TenantPlatformVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -94,6 +96,15 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
             queryWrapper1.in(SysTenant::getId, tenantIdList)
                     .eq(SysTenant::getStatus, 0);
             tenantList = sysTenantService.list(queryWrapper1);
+            if (CollectionUtils.isNotEmpty(tenantList)) {
+                for (int j = 0; j < userTenants.size(); j++) {
+                    for (int k = 0; k < tenantList.size(); k++) {
+                        if (userTenants.get(j).getTenantId().equals(tenantList.get(k).getId())){
+                            tenantList.get(k).setIsDefault(userTenants.get(j).getIsDefault());
+                        }
+                    }
+                }
+            }
         }
         return tenantList;
     }
@@ -107,4 +118,29 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
         List<SysUserTenant> userTenants = this.list(queryWrapper);
         return userTenants.get(0).getIsDefault();
     }
+
+    @Override
+    @Transactional
+    public Integer getIsDefaultByUser(Long userId) {
+        LambdaQueryWrapper<SysUserTenant> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.eq(SysUserTenant::getUserId, userId)
+                .eq(SysUserTenant::getIsDefault, 1);
+        List<SysUserTenant> userTenants = this.list(queryWrapper);
+        return userTenants.get(0).getTenantId();
+    }
+
+    @Override
+    @Transactional
+    public void updateUserTenant(SysUserTenant userTenant) {
+        LambdaUpdateWrapper<SysUserTenant> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(SysUserTenant::getIsDefault, 0)
+                .eq(SysUserTenant::getUserId, userTenant.getUserId())
+                .eq(SysUserTenant::getIsDefault, 1);
+        this.update(updateWrapper);
+        LambdaUpdateWrapper<SysUserTenant> updateWrapper1 = new LambdaUpdateWrapper<>();
+        updateWrapper1.set(SysUserTenant::getIsDefault, 1)
+                .eq(SysUserTenant::getUserId, userTenant.getUserId())
+                .eq(SysUserTenant::getTenantId, userTenant.getTenantId());
+        this.update(updateWrapper1);
+    }
 }