public async Task <IActionResult> OnPostSaveAsync([FromBody] CreateOrUpdate_TenantInfoDto dto) { if (!ModelState.IsValid) { return(Ok(false)); } bool isNameExists = await this._tenantInfoService.CheckNameExisted(dto.Id, dto.Name); if (isNameExists) { return(Ok(false, $"{dto.Name} 名称已存在")); } bool isTenantKeyExists = await this._tenantInfoService.CheckTenantKeyExisted(dto.Id, dto.TenantKey); if (isTenantKeyExists) { return(Ok(false, $"{dto.TenantKey} 匹配规则已存在")); } try { await _tenantInfoService.CreateOrUpdateTenantInfoAsync(dto); return(Ok(true, $"{(dto.Id > 0 ? "编辑" : "新增")}租户成功!")); } catch (Exception ex) { return(Ok(false, ex.Message)); } }
/// <summary> /// 创建默认 TenantInfo 信息 /// </summary> /// <param name="httpContext">如果为 null,则自动从 IHttpContextAccessor 中获取</param> /// <returns></returns> public async Task <TenantInfoDto> CreateInitTenantInfoAsync(HttpContext httpContext) { httpContext = httpContext ?? _httpContextAccessor.Value.HttpContext; var urlData = httpContext.Request; var host = urlData.Host.Host; CreateOrUpdate_TenantInfoDto createOrUpdate_TenantInfoDto = new CreateOrUpdate_TenantInfoDto(0, host, host, true, "系统初始化自动创建"); return(await CreateOrUpdateTenantInfoAsync(createOrUpdate_TenantInfoDto)); }
/// <summary> /// 创建租户信息 /// </summary> /// <param name="createOrUpdate_TenantInfoDto"></param> /// <param name="throwIfExisted">如果 Name 或 TenantKey 已存在,则抛出异常</param> /// <exception cref="NcfTenantException">Name 或 TenantKey 已存在</exception> /// <returns></returns> public async Task <TenantInfoDto> CreateOrUpdateTenantInfoAsync(CreateOrUpdate_TenantInfoDto createOrUpdate_TenantInfoDto, bool throwIfExisted = false) { createOrUpdate_TenantInfoDto.TenantKey = createOrUpdate_TenantInfoDto.TenantKey.ToUpper(); var tenantInfo = await GetObjectAsync(z => z.Id != createOrUpdate_TenantInfoDto.Id && (z.Name == createOrUpdate_TenantInfoDto.Name || z.TenantKey == createOrUpdate_TenantInfoDto.TenantKey)); if (tenantInfo != null) { if (throwIfExisted) { throw new NcfTenantException($"已存在名为 {createOrUpdate_TenantInfoDto.Name} 或 关键条件为 {createOrUpdate_TenantInfoDto.TenantKey} 的租户记录!"); } else { return(base.Mapper.Map <TenantInfoDto>(tenantInfo)); } } if (createOrUpdate_TenantInfoDto.Id > 0) { //编辑 tenantInfo = await GetObjectAsync(z => z.Id == createOrUpdate_TenantInfoDto.Id, null); if (tenantInfo == null) { throw new NcfTenantException($"租户信息不存在!Id:{createOrUpdate_TenantInfoDto.Id}"); } tenantInfo.Update(createOrUpdate_TenantInfoDto); } else { //新增 tenantInfo = new TenantInfo(createOrUpdate_TenantInfoDto.Name, createOrUpdate_TenantInfoDto.Enable, createOrUpdate_TenantInfoDto.TenantKey); } await SaveObjectAsync(tenantInfo); await tenantInfo.ClearCache(_serviceProvider);//所有涉及到租户信息的修改,都清除租户信息,重新更新 return(base.Mapper.Map <TenantInfoDto>(tenantInfo)); }