mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 02:08:43 +08:00 
			
		
		
		
	【新增】【优化】新建租户时,自动创建对应的管理员账号、角色等基础信息
This commit is contained in:
		| @@ -40,7 +40,7 @@ public class RoleController { | ||||
|     @ApiOperation("创建角色") | ||||
|     @PreAuthorize("@ss.hasPermission('system:role:create')") | ||||
|     public CommonResult<Long> createRole(@Valid @RequestBody RoleCreateReqVO reqVO) { | ||||
|         return success(roleService.createRole(reqVO)); | ||||
|         return success(roleService.createRole(reqVO, null)); | ||||
|     } | ||||
|  | ||||
|     @PutMapping("/update") | ||||
| @@ -88,7 +88,7 @@ public class RoleController { | ||||
|     public CommonResult<List<RoleSimpleRespVO>> getSimpleRoles() { | ||||
|         // 获得角色列表,只要开启状态的 | ||||
|         List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         // 排序后,返回个诶前端 | ||||
|         // 排序后,返回给前端 | ||||
|         list.sort(Comparator.comparing(RoleDO::getSort)); | ||||
|         return success(RoleConvert.INSTANCE.convertList02(list)); | ||||
|     } | ||||
|   | ||||
| @@ -28,9 +28,6 @@ public class RoleBaseVO { | ||||
|     @NotNull(message = "显示顺序不能为空") | ||||
|     private Integer sort; | ||||
|  | ||||
|     @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "见 RoleTypeEnum 枚举") | ||||
|     private Integer type; | ||||
|  | ||||
|     @ApiModelProperty(value = "备注", example = "我是一个角色") | ||||
|     private String remark; | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,18 @@ | ||||
| ### 创建租户 /admin-api/system/tenant/create | ||||
| POST {{baseUrl}}/system/tenant/create | ||||
| Content-Type: application/json | ||||
| Authorization: Bearer {{token}} | ||||
| tenant-id: {{adminTenentId}} | ||||
|  | ||||
| { | ||||
|   "name": "芋道", | ||||
|   "contactName": "芋艿", | ||||
|   "contactMobile": "15601691300", | ||||
|   "status": 0, | ||||
|   "domain": "https://www.iocoder.cn", | ||||
|   "packageId": 110, | ||||
|   "expireTime": 1699545600000, | ||||
|   "accountCount": 20, | ||||
|   "username": "admin", | ||||
|   "password": "123321" | ||||
| } | ||||
| @@ -1,11 +1,9 @@ | ||||
| package cn.iocoder.yudao.module.system.controller.admin.tenant; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageRespVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.*; | ||||
| import cn.iocoder.yudao.module.system.convert.tenant.TenantPackageConvert; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; | ||||
| import cn.iocoder.yudao.module.system.service.tenant.TenantPackageService; | ||||
| @@ -82,4 +80,12 @@ public class TenantPackageController { | ||||
|         return success(TenantPackageConvert.INSTANCE.convertPage(pageResult)); | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/get-simple-list") | ||||
|     @ApiOperation(value = "获取租户套餐精简信息列表", notes = "只包含被开启的租户套餐,主要用于前端的下拉选项") | ||||
|     public CommonResult<List<TenantPackageSimpleRespVO>> getTenantPackageList() { | ||||
|         // 获得角色列表,只要开启状态的 | ||||
|         List<TenantPackageDO> list = tenantPackageService.getTenantPackageListByStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         return success(TenantPackageConvert.INSTANCE.convertList02(list)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,21 @@ | ||||
| package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages; | ||||
|  | ||||
| import io.swagger.annotations.ApiModel; | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
|  | ||||
| import javax.validation.constraints.NotNull; | ||||
|  | ||||
| @ApiModel("管理后台 - 租户套餐精简 Response VO") | ||||
| @Data | ||||
| public class TenantPackageSimpleRespVO { | ||||
|  | ||||
|     @ApiModelProperty(value = "套餐编号", required = true, example = "1024") | ||||
|     @NotNull(message = "套餐编号不能为空") | ||||
|     private Long id; | ||||
|  | ||||
|     @ApiModelProperty(value = "套餐名", required = true, example = "VIP") | ||||
|     @NotNull(message = "套餐名不能为空") | ||||
|     private String name; | ||||
|  | ||||
| } | ||||
| @@ -2,7 +2,10 @@ package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant; | ||||
|  | ||||
| import lombok.*; | ||||
| import io.swagger.annotations.*; | ||||
| import org.hibernate.validator.constraints.URL; | ||||
|  | ||||
| import javax.validation.constraints.*; | ||||
| import java.util.Date; | ||||
|  | ||||
| /** | ||||
| * 租户 Base VO,提供给添加、修改、详细的子 VO 使用 | ||||
| @@ -22,8 +25,24 @@ public class TenantBaseVO { | ||||
|     @ApiModelProperty(value = "联系手机", example = "15601691300") | ||||
|     private String contactMobile; | ||||
|  | ||||
|     @ApiModelProperty(value = "租户状态(0正常 1停用)", required = true, example = "1") | ||||
|     @NotNull(message = "租户状态(0正常 1停用)不能为空") | ||||
|     @ApiModelProperty(value = "租户状态", required = true, example = "1") | ||||
|     @NotNull(message = "租户状态") | ||||
|     private Integer status; | ||||
|  | ||||
|     @ApiModelProperty(value = "绑定域名", example = "https://www.iocoder.cn") | ||||
|     @URL(message = "绑定域名的地址非 URL 格式") | ||||
|     private String domain; | ||||
|  | ||||
|     @ApiModelProperty(value = "租户套餐编号", required = true, example = "1024") | ||||
|     @NotNull(message = "租户套餐编号不能为空") | ||||
|     private Long packageId; | ||||
|  | ||||
|     @ApiModelProperty(value = "过期时间", required = true) | ||||
|     @NotNull(message = "过期时间不能为空") | ||||
|     private Date expireTime; | ||||
|  | ||||
|     @ApiModelProperty(value = "账号数量", required = true, example = "1024") | ||||
|     @NotNull(message = "账号数量不能为空") | ||||
|     private Integer accountCount; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -2,6 +2,12 @@ package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant; | ||||
|  | ||||
| import lombok.*; | ||||
| import io.swagger.annotations.*; | ||||
| import org.hibernate.validator.constraints.Length; | ||||
|  | ||||
| import javax.validation.constraints.NotBlank; | ||||
| import javax.validation.constraints.NotEmpty; | ||||
| import javax.validation.constraints.Pattern; | ||||
| import javax.validation.constraints.Size; | ||||
|  | ||||
| @ApiModel("管理后台 - 租户创建 Request VO") | ||||
| @Data | ||||
| @@ -9,4 +15,15 @@ import io.swagger.annotations.*; | ||||
| @ToString(callSuper = true) | ||||
| public class TenantCreateReqVO extends TenantBaseVO { | ||||
|  | ||||
|     @ApiModelProperty(value = "用户账号", required = true, example = "yudao") | ||||
|     @NotBlank(message = "用户账号不能为空") | ||||
|     @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成") | ||||
|     @Size(min = 4, max = 30, message = "用户账号长度为 4-30 个字符") | ||||
|     private String username; | ||||
|  | ||||
|     @ApiModelProperty(value = "密码", required = true, example = "123456") | ||||
|     @NotEmpty(message = "密码不能为空") | ||||
|     @Length(min = 4, max = 16, message = "密码长度为 4-16 位") | ||||
|     private String password; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| package cn.iocoder.yudao.module.system.controller.admin.user.vo.user; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.validation.Mobile; | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
| import org.hibernate.validator.constraints.Length; | ||||
|  | ||||
| import javax.validation.constraints.Email; | ||||
| import javax.validation.constraints.NotBlank; | ||||
| @@ -42,7 +42,7 @@ public class UserBaseVO { | ||||
|     private String email; | ||||
|  | ||||
|     @ApiModelProperty(value = "手机号码", example = "15601691300") | ||||
|     @Length(min = 11, max = 11, message = "手机号长度必须 11 位") | ||||
|     @Mobile | ||||
|     private String mobile; | ||||
|  | ||||
|     @ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SexEnum 枚举类") | ||||
|   | ||||
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.convert.permission; | ||||
|  | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.*; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | ||||
| import cn.iocoder.yudao.module.system.service.permission.bo.RoleCreateReqBO; | ||||
| import org.mapstruct.Mapper; | ||||
| import org.mapstruct.factory.Mappers; | ||||
|  | ||||
| @@ -22,4 +23,6 @@ public interface RoleConvert { | ||||
|  | ||||
|     List<RoleExcelVO> convertList03(List<RoleDO> list); | ||||
|  | ||||
|     RoleDO convert(RoleCreateReqBO bean); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,11 +1,12 @@ | ||||
| package cn.iocoder.yudao.module.system.convert.tenant; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantExcelVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import org.mapstruct.Mapper; | ||||
| import org.mapstruct.factory.Mappers; | ||||
|  | ||||
| @@ -33,4 +34,12 @@ public interface TenantConvert { | ||||
|  | ||||
|     List<TenantExcelVO> convertList02(List<TenantDO> list); | ||||
|  | ||||
|     default UserCreateReqVO convert02(TenantCreateReqVO bean) { | ||||
|         UserCreateReqVO reqVO = new UserCreateReqVO(); | ||||
|         reqVO.setUsername(bean.getUsername()); | ||||
|         reqVO.setPassword(bean.getPassword()); | ||||
|         reqVO.setNickname(bean.getContactName()).setMobile(bean.getContactMobile()); | ||||
|         return reqVO; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,8 +1,10 @@ | ||||
| package cn.iocoder.yudao.module.system.convert.tenant; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleSimpleRespVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageRespVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageSimpleRespVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; | ||||
| import org.mapstruct.Mapper; | ||||
| @@ -30,4 +32,6 @@ public interface TenantPackageConvert { | ||||
|  | ||||
|     PageResult<TenantPackageRespVO> convertPage(PageResult<TenantPackageDO> page); | ||||
|  | ||||
|     List<TenantPackageSimpleRespVO> convertList02(List<TenantPackageDO> list); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | ||||
| import cn.iocoder.yudao.framework.mybatis.core.type.JsonLongSetTypeHandler; | ||||
| import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; | ||||
| import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; | ||||
| import com.baomidou.mybatisplus.annotation.TableField; | ||||
| import com.baomidou.mybatisplus.annotation.TableId; | ||||
| import com.baomidou.mybatisplus.annotation.TableName; | ||||
| @@ -50,7 +51,7 @@ public class RoleDO extends BaseDO { | ||||
|     /** | ||||
|      * 角色类型 | ||||
|      * | ||||
|      * 枚举 | ||||
|      * 枚举 {@link RoleTypeEnum} | ||||
|      */ | ||||
|     private Integer type; | ||||
|     /** | ||||
|   | ||||
| @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.Tenant | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; | ||||
| import org.apache.ibatis.annotations.Mapper; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * 租户套餐 Mapper | ||||
|  * | ||||
| @@ -24,4 +26,7 @@ public interface TenantPackageMapper extends BaseMapperX<TenantPackageDO> { | ||||
|                 .orderByDesc(TenantPackageDO::getId)); | ||||
|     } | ||||
|  | ||||
|     default List<TenantPackageDO> selectListByStatus(Integer status) { | ||||
|         return selectList(TenantPackageDO::getStatus, status); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -224,8 +224,6 @@ public class PermissionServiceImpl implements PermissionService { | ||||
|                 UserRoleDO::getRoleId); | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     @Override | ||||
|     public void assignUserRole(Long userId, Set<Long> roleIds) { | ||||
|         // 获得角色拥有角色编号 | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleUp | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | ||||
| import org.springframework.lang.Nullable; | ||||
|  | ||||
| import javax.validation.Valid; | ||||
| import java.util.Collection; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| @@ -28,16 +29,17 @@ public interface RoleService { | ||||
|      * 创建角色 | ||||
|      * | ||||
|      * @param reqVO 创建角色信息 | ||||
|      * @param type 角色类型 | ||||
|      * @return 角色编号 | ||||
|      */ | ||||
|     Long createRole(RoleCreateReqVO reqVO); | ||||
|     Long createRole(@Valid RoleCreateReqVO reqVO, Integer type); | ||||
|  | ||||
|     /** | ||||
|      * 更新角色 | ||||
|      * | ||||
|      * @param reqVO 更新角色信息 | ||||
|      */ | ||||
|     void updateRole(RoleUpdateReqVO reqVO); | ||||
|     void updateRole(@Valid RoleUpdateReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 删除角色 | ||||
|   | ||||
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.permission; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import cn.hutool.core.collection.CollectionUtil; | ||||
| import cn.hutool.core.util.ObjectUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||||
| @@ -120,12 +121,12 @@ public class RoleServiceImpl implements RoleService { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Long createRole(RoleCreateReqVO reqVO) { | ||||
|     public Long createRole(RoleCreateReqVO reqVO, Integer type) { | ||||
|         // 校验角色 | ||||
|         checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null); | ||||
|         // 插入到数据库 | ||||
|         RoleDO role = RoleConvert.INSTANCE.convert(reqVO); | ||||
|         role.setType(RoleTypeEnum.CUSTOM.getType()); | ||||
|         role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType())); | ||||
|         role.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         role.setDataScope(DataScopeEnum.ALL.getScope()); // 默认可查看所有数据。原因是,可能一些项目不需要项目权限 | ||||
|         roleMapper.insert(role); | ||||
|   | ||||
| @@ -0,0 +1,49 @@ | ||||
| package cn.iocoder.yudao.module.system.service.permission.bo; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| import javax.validation.constraints.NotBlank; | ||||
| import javax.validation.constraints.NotNull; | ||||
| import javax.validation.constraints.Size; | ||||
|  | ||||
| /** | ||||
|  * 角色创建 Request BO | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Data | ||||
| public class RoleCreateReqBO { | ||||
|  | ||||
|     /** | ||||
|      * 租户编号 | ||||
|      */ | ||||
|     @NotNull(message = "租户编号不能为空") | ||||
|     private Long tenantId; | ||||
|  | ||||
|     /** | ||||
|      * 角色名称 | ||||
|      */ | ||||
|     @NotBlank(message = "角色名称不能为空") | ||||
|     @Size(max = 30, message = "角色名称长度不能超过30个字符") | ||||
|     private String name; | ||||
|  | ||||
|     /** | ||||
|      * 角色标志 | ||||
|      */ | ||||
|     @NotBlank(message = "角色标志不能为空") | ||||
|     @Size(max = 100, message = "角色标志长度不能超过100个字符") | ||||
|     private String code; | ||||
|  | ||||
|     /** | ||||
|      * 显示顺序 | ||||
|      */ | ||||
|     @NotNull(message = "显示顺序不能为空") | ||||
|     private Integer sort; | ||||
|  | ||||
|     /** | ||||
|      * 角色类型 | ||||
|      */ | ||||
|     @NotNull(message = "角色类型不能为空") | ||||
|     private Integer type; | ||||
|  | ||||
| } | ||||
| @@ -63,4 +63,20 @@ public interface TenantPackageService { | ||||
|      */ | ||||
|     PageResult<TenantPackageDO> getTenantPackagePage(TenantPackagePageReqVO pageReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 校验租户套餐 | ||||
|      * | ||||
|      * @param id 编号 | ||||
|      * @return 租户套餐 | ||||
|      */ | ||||
|     TenantPackageDO validTenantPackage(Long id); | ||||
|  | ||||
|     /** | ||||
|      * 获得指定状态的租户套餐列表 | ||||
|      * | ||||
|      * @param status 状态 | ||||
|      * @return 租户套餐 | ||||
|      */ | ||||
|     List<TenantPackageDO> getTenantPackageListByStatus(Integer status); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.yudao.module.system.service.tenant; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; | ||||
| @@ -7,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.Tenant | ||||
| import cn.iocoder.yudao.module.system.convert.tenant.TenantPackageConvert; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantPackageMapper; | ||||
| import org.springframework.context.annotation.Lazy; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
|  | ||||
| @@ -15,8 +17,7 @@ import java.util.Collection; | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_PACKAGE_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_PACKAGE_USED; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
|  | ||||
| /** | ||||
|  * 租户套餐 Service 实现类 | ||||
| @@ -31,6 +32,7 @@ public class TenantPackageServiceImpl implements TenantPackageService { | ||||
|     private TenantPackageMapper tenantPackageMapper; | ||||
|  | ||||
|     @Resource | ||||
|     @Lazy // 避免循环依赖的报错 | ||||
|     private TenantService tenantService; | ||||
|  | ||||
|     @Override | ||||
| @@ -88,4 +90,21 @@ public class TenantPackageServiceImpl implements TenantPackageService { | ||||
|         return tenantPackageMapper.selectPage(pageReqVO); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public TenantPackageDO validTenantPackage(Long id) { | ||||
|         TenantPackageDO tenantPackage = tenantPackageMapper.selectById(id); | ||||
|         if (tenantPackage == null) { | ||||
|             throw exception(TENANT_PACKAGE_NOT_EXISTS); | ||||
|         } | ||||
|         if (tenantPackage.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) { | ||||
|             throw exception(TENANT_PACKAGE_DISABLE, tenantPackage.getName()); | ||||
|         } | ||||
|         return tenantPackage; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<TenantPackageDO> getTenantPackageListByStatus(Integer status) { | ||||
|         return tenantPackageMapper.selectListByStatus(status); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,11 @@ | ||||
| package cn.iocoder.yudao.module.system.service.tenant; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||||
| import cn.iocoder.yudao.framework.common.util.date.DateUtils; | ||||
| import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantExportReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO; | ||||
| @@ -9,15 +13,22 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantUp | ||||
| import cn.iocoder.yudao.module.system.convert.tenant.TenantConvert; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper; | ||||
| import cn.iocoder.yudao.module.system.enums.permission.RoleCodeEnum; | ||||
| import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; | ||||
| import cn.iocoder.yudao.module.system.service.permission.PermissionService; | ||||
| import cn.iocoder.yudao.module.system.service.permission.RoleService; | ||||
| import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.Collection; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
|  | ||||
| /** | ||||
|  * 租户 Service 实现类 | ||||
| @@ -31,6 +42,15 @@ public class TenantServiceImpl implements TenantService { | ||||
|     @Resource | ||||
|     private TenantMapper tenantMapper; | ||||
|  | ||||
|     @Resource | ||||
|     private TenantPackageService tenantPackageService; | ||||
|     @Resource | ||||
|     private AdminUserService userService; | ||||
|     @Resource | ||||
|     private RoleService roleService; | ||||
|     @Resource | ||||
|     private PermissionService permissionService; | ||||
|  | ||||
|     @Override | ||||
|     public List<Long> getTenantIds() { | ||||
|         List<TenantDO> tenants = tenantMapper.selectList(); | ||||
| @@ -38,18 +58,62 @@ public class TenantServiceImpl implements TenantService { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void validTenant(Long id) { | ||||
|         TenantDO tenant = tenantMapper.selectById(id); | ||||
|         if (tenant == null) { | ||||
|             throw exception(TENANT_NOT_EXISTS); | ||||
|         } | ||||
|         if (tenant.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) { | ||||
|             throw exception(TENANT_DISABLE, tenant.getName()); | ||||
|         } | ||||
|         if (DateUtils.isExpired(tenant.getExpireTime())) { | ||||
|             throw exception(TENANT_EXPIRE, tenant.getName()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public Long createTenant(TenantCreateReqVO createReqVO) { | ||||
|         // 插入 | ||||
|         // 校验套餐被禁用 | ||||
|         tenantPackageService.validTenantPackage(createReqVO.getPackageId()); | ||||
|  | ||||
|         // 创建租户 | ||||
|         TenantDO tenant = TenantConvert.INSTANCE.convert(createReqVO); | ||||
|         tenantMapper.insert(tenant); | ||||
|  | ||||
|         TenantUtils.execute(tenant.getId(), () -> { | ||||
|             // 创建角色 | ||||
|             Long roleId = createRole(); | ||||
|             // 创建用户,并分配角色 | ||||
|             Long userId = createUser(roleId, createReqVO); | ||||
|             // 修改租户的管理员 | ||||
|             tenantMapper.updateById(new TenantDO().setId(tenant.getId()).setContactUserId(userId)); | ||||
|         }); | ||||
|         // 返回 | ||||
|         return tenant.getId(); | ||||
|     } | ||||
|  | ||||
|     private Long createUser(Long roleId, TenantCreateReqVO createReqVO) { | ||||
|         // 创建用户 | ||||
|         Long userId = userService.createUser(TenantConvert.INSTANCE.convert02(createReqVO)); | ||||
|         // 分配角色 | ||||
|         permissionService.assignUserRole(userId, Collections.singleton(roleId)); | ||||
|         return userId; | ||||
|     } | ||||
|  | ||||
|     private Long createRole() { | ||||
|         RoleCreateReqVO reqVO = new RoleCreateReqVO(); | ||||
|         reqVO.setName(RoleCodeEnum.ADMIN.name()).setCode(RoleCodeEnum.ADMIN.getKey()).setSort(0); | ||||
|         return roleService.createRole(reqVO, RoleTypeEnum.SYSTEM.getType()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void updateTenant(TenantUpdateReqVO updateReqVO) { | ||||
|         // 校验存在 | ||||
|         this.validateTenantExists(updateReqVO.getId()); | ||||
|         // 校验套餐被禁用 | ||||
|         tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); | ||||
|  | ||||
|         // 更新 | ||||
|         TenantDO updateObj = TenantConvert.INSTANCE.convert(updateReqVO); | ||||
|         tenantMapper.updateById(updateObj); | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||||
|  | ||||
| import javax.validation.Valid; | ||||
| import java.io.InputStream; | ||||
| import java.util.*; | ||||
|  | ||||
| @@ -24,14 +25,14 @@ public interface AdminUserService { | ||||
|      * @param reqVO 用户信息 | ||||
|      * @return 用户编号 | ||||
|      */ | ||||
|     Long createUser(UserCreateReqVO reqVO); | ||||
|     Long createUser(@Valid UserCreateReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 修改用户 | ||||
|      * | ||||
|      * @param reqVO 用户信息 | ||||
|      */ | ||||
|     void updateUser(UserUpdateReqVO reqVO); | ||||
|     void updateUser(@Valid UserUpdateReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 更新用户的最后登陆信息 | ||||
| @@ -47,7 +48,7 @@ public interface AdminUserService { | ||||
|      * @param id 用户编号 | ||||
|      * @param reqVO 用户个人信息 | ||||
|      */ | ||||
|     void updateUserProfile(Long id, UserProfileUpdateReqVO reqVO); | ||||
|     void updateUserProfile(Long id, @Valid UserProfileUpdateReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 修改用户个人密码 | ||||
| @@ -55,7 +56,7 @@ public interface AdminUserService { | ||||
|      * @param id 用户编号 | ||||
|      * @param reqVO 更新用户个人密码 | ||||
|      */ | ||||
|     void updateUserPassword(Long id, UserProfileUpdatePasswordReqVO reqVO); | ||||
|     void updateUserPassword(Long id, @Valid UserProfileUpdatePasswordReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 更新用户头像 | ||||
|   | ||||
| @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.user; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import cn.hutool.core.io.IoUtil; | ||||
| import cn.hutool.core.util.IdUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.exception.ServiceException; | ||||
| @@ -258,7 +257,7 @@ public class AdminUserServiceImpl implements AdminUserService { | ||||
|         // 校验邮箱唯一 | ||||
|         this.checkEmailUnique(id, email); | ||||
|         // 校验部门处于开启状态 | ||||
|         deptService.validDepts(Collections.singleton(deptId)); | ||||
|         deptService.validDepts(CollectionUtils.singleton(deptId)); | ||||
|         // 校验岗位处于开启状态 | ||||
|         postService.validPosts(postIds); | ||||
|     } | ||||
|   | ||||
| @@ -72,10 +72,9 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|             o.setCode("role_code"); | ||||
|             o.setName("role_name"); | ||||
|             o.setRemark("remark"); | ||||
|             o.setType(RoleTypeEnum.CUSTOM.getType()); | ||||
|             o.setSort(1); | ||||
|         }); | ||||
|         Long roleId = sysRoleService.createRole(reqVO); | ||||
|         Long roleId = sysRoleService.createRole(reqVO, null); | ||||
|  | ||||
|         //断言 | ||||
|         assertNotNull(roleId); | ||||
| @@ -96,7 +95,6 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|             o.setId(roleId); | ||||
|             o.setCode("role_code"); | ||||
|             o.setName("update_name"); | ||||
|             o.setType(RoleTypeEnum.SYSTEM.getType()); | ||||
|             o.setSort(999); | ||||
|         }); | ||||
|         sysRoleService.updateRole(reqVO); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV