public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar, WarehouseMeta warehouseMeta) { var warehouseId = Guid.NewGuid().ToString(); var warehouse = new Domain.Models.Warehouse { Id = warehouseId, ConcurrencyStamp = warehouseId, Name = warehouseMeta.Name.Trim(), Description = warehouseMeta.Description?.Trim(), Address = warehouseMeta.Address?.Trim(), UnsignName = warehouseMeta.Name?.StripVietnameseChars().ToUpper(), IsActive = warehouseMeta.IsActive, TenantId = tenantId, CreatorId = creatorId, CreatorFullName = creatorFullName }; var result = await _warehouseRepository.Insert(warehouse); if (result <= 0) { return(new ActionResultResponse <string>(result, _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong))); } // Thêm phương pháp tính tồn. await _warehouseConfigService.Save(new WarehouseConfigMeta { TenantId = tenantId, Value = ((int)warehouseMeta.InventoryCalculatorMethod).ToString(), Key = ClassHelper.GetPropertyNameAsKey <WarehouseConfigSetting>("CalculatorMethod"), WarehouseId = warehouseId, UserId = creatorId, FullName = creatorFullName, LanguageId = CultureInfo.CurrentCulture.Name, DisplayName = "Calculator method" }); #region insert WarehouseManagerConfigs. if (warehouseMeta.WarehouseManagerConfigs != null && warehouseMeta.WarehouseManagerConfigs.Count > 0 && warehouseMeta.WarehouseManagerConfigs.Any()) { var resultInsertWarehouseManagerConfig = await InsertWarehouseManagerConfig(); if (resultInsertWarehouseManagerConfig.Code <= 0) { await RollbackInsert(); return(resultInsertWarehouseManagerConfig); } } #endregion return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Add new warehouse successful."), string.Empty, warehouseId)); #region Local functions async Task <ActionResultResponse <string> > InsertWarehouseManagerConfig() { var warehouseManagerConfigs = new List <WarehouseManagerConfig>(); foreach (var warehouseManagerConfig in warehouseMeta.WarehouseManagerConfigs) { var warehouseManagerConfigInsert = new WarehouseManagerConfig { WarehouseId = warehouseId, TenantId = tenantId, UserId = warehouseManagerConfig.UserId.Trim(), FullName = warehouseManagerConfig.FullName.Trim(), Avatar = warehouseManagerConfig.Avatar?.Trim(), PhoneNumber = warehouseManagerConfig.PhoneNumber?.Trim(), UnsignName = warehouseManagerConfig.FullName?.StripVietnameseChars().ToUpper(), Permissions = warehouseManagerConfig.Permissions, CreatorId = creatorId, CreatorFullName = creatorFullName }; warehouseManagerConfigs.Add(warehouseManagerConfigInsert); } var resultWarehouseManagerConfig = await _warehouseManagerConfigRepository.Inserts(warehouseManagerConfigs); if (resultWarehouseManagerConfig > 0) { return(new ActionResultResponse <string>(resultWarehouseManagerConfig, _warehouseResourceService.GetString("Add new warehouse manager configs successful."))); } await RollbackInsertWarehouseManagerConfig(); await RollbackInsert(); return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString("Can not insert warehouse manager configs. Please contact with administrator."))); } async Task RollbackInsert() { await _warehouseRepository.ForceDelete(warehouseId); } async Task RollbackInsertWarehouseManagerConfig() { await _warehouseManagerConfigRepository.DeleteByWarehouseId(warehouseId); } #endregion Local functions }