Browse Source

[fix]全部接口支持按地点保存到不同数据库

master
贾荣国Home 3 years ago
parent
commit
50051deb12
  1. 11
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application.Contracts/RouteConsts.cs
  2. 10
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Parts/PartAppService.cs
  3. 35
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/PurchaseOrders/PurchaseOrderAppService.cs
  4. 21
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Receipts/ReceiptAppService.cs
  5. 14
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Suppliers/SuppliersAppService.cs
  6. 34
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/UnplannedReceipts/UnplannedReceiptAppService.cs
  7. 183
      WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/MultiTenantConnectionStringResolver.cs
  8. 32
      WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/WebApiHttpApiHostModule.cs
  9. 17
      WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/appsettings.json
  10. 2
      WebApiService/src/Win_in.Sfs.Scp.v1.Domain/Parts/TA_PART.cs

11
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application.Contracts/RouteConsts.cs

@ -11,15 +11,4 @@ namespace Win_in.Sfs.Scp.WebApi
public const string Supplier = "api/scp/supplier";
public const string UnplannedReceipt = "api/scp/unplanned-receipt";
}
public class SiteConsts
{
public static readonly Dictionary<string, Guid> SiteDict = new Dictionary<string, Guid>
{
{"T8",Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0")},
{"T9",Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d")}
};
}
}

10
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Parts/PartAppService.cs

@ -16,8 +16,10 @@ using Volo.Abp.Uow;
using Win_in.Sfs.Scp.v1.Domain;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Clients;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Validation;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
using Volo.Abp.TenantManagement;
namespace Win_in.Sfs.Scp.WebApi
{
@ -32,16 +34,19 @@ namespace Win_in.Sfs.Scp.WebApi
{
private readonly IPartRepository _partRepository;
private readonly ITaPartRepository _taPartRepository;
private readonly ITenantStore _tenantStore;
private readonly string _validSites = Validator.VALID_SITES;
public PartAppService(
IPartRepository repository
, ITaPartRepository taPartRepository
, ITenantStore tenantStore
, IConfiguration configuration
) : base(repository)
{
_partRepository = repository;
_taPartRepository = taPartRepository;
_tenantStore = tenantStore;
_validSites = configuration["ValidSites"];
}
@ -118,10 +123,9 @@ namespace Win_in.Sfs.Scp.WebApi
//使用AutoMapper执行类型转换
var taPart = ObjectMapper.Map<Part, TA_PART>(entity);
var tenantId = SiteConsts.SiteDict.GetValueOrDefault(taPart.Site);
using (CurrentTenant.Change(tenantId,taPart.Site))
var tenant =await _tenantStore.FindAsync(taPart.Site);
using (CurrentTenant.Change(tenant.Id,tenant.Name))
{
taPart.TenantId = tenantId;
//根据传入数据新增或修改SCP数据
await _taPartRepository.UpsertAsync(taPart);
}

35
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/PurchaseOrders/PurchaseOrderAppService.cs

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TenantManagement;
using Volo.Abp.Validation;
using Win_in.Sfs.Scp.v1.Domain;
@ -24,6 +26,7 @@ namespace Win_in.Sfs.Scp.WebApi
private readonly ITbPoRepository _tbPoRepository;
private readonly ITbPoDetailRepository _tbPoDetailRepository;
private readonly ITaVenderRepository _taVenderRepository;
private readonly ITenantStore _tenantStore;
private readonly string _validSites = Validator.VALID_SITES;
public PurchaseOrderAppService(
@ -31,6 +34,7 @@ namespace Win_in.Sfs.Scp.WebApi
, ITbPoRepository tbPoRepository
, ITbPoDetailRepository tbPoDetailRepository
, ITaVenderRepository taVenderRepository
, ITenantStore tenantStore
, IConfiguration configuration
) : base(repository)
{
@ -38,6 +42,7 @@ namespace Win_in.Sfs.Scp.WebApi
_tbPoRepository = tbPoRepository;
_tbPoDetailRepository = tbPoDetailRepository;
_taVenderRepository = taVenderRepository;
_tenantStore = tenantStore;
_validSites = configuration["ValidSites"];
}
@ -123,21 +128,25 @@ namespace Win_in.Sfs.Scp.WebApi
{
//使用AutoMapper执行类型转换
var tbPo = ObjectMapper.Map<PurchaseOrder, TB_PO>(entity);
//根据传入数据新增或修改SCP数据
await _tbPoRepository.UpsertAsync(tbPo);
var poDetails = entity.Details;
foreach (var poDetail in poDetails)
var tenant =await _tenantStore.FindAsync(tbPo.Site);
using (CurrentTenant.Change(tenant.Id, tenant.Name))
{
//使用AutoMapper执行类型转换
var tbPoDetail = ObjectMapper.Map<PurchaseOrderDetail, TB_PO_DETAIL>(poDetail);
//接口数据中没有Site,从主表中获取
tbPoDetail.Site = tbPo.Site;
tbPoDetail.BeginTime = tbPo.BeginTime;
tbPoDetail.EndTime = tbPo.EndTime;
//根据传入数据新增或修改SCP数据
await _tbPoDetailRepository.UpsertAsync(tbPoDetail);
await _tbPoRepository.UpsertAsync(tbPo);
var poDetails = entity.Details;
foreach (var poDetail in poDetails)
{
//使用AutoMapper执行类型转换
var tbPoDetail = ObjectMapper.Map<PurchaseOrderDetail, TB_PO_DETAIL>(poDetail);
//接口数据中没有Site,从主表中获取
tbPoDetail.Site = tbPo.Site;
tbPoDetail.BeginTime = tbPo.BeginTime;
tbPoDetail.EndTime = tbPo.EndTime;
//根据传入数据新增或修改SCP数据
await _tbPoDetailRepository.UpsertAsync(tbPoDetail);
}
}
}
}

21
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Receipts/ReceiptAppService.cs

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.Validation;
using Win_in.Sfs.Scp.v1.Domain;
@ -25,6 +26,7 @@ namespace Win_in.Sfs.Scp.WebApi
private readonly ITbReceiptRepository _tbReceiptRepository;
private readonly ITbReceiptDetailRepository _tbReceiptDetailRepository;
private readonly ITaVenderRepository _taVenderRepository;
private readonly ITenantStore _tenantStore;
private readonly string _validSites = Validator.VALID_SITES;
public ReceiptAppService(
@ -32,6 +34,7 @@ namespace Win_in.Sfs.Scp.WebApi
,ITbReceiptRepository tbReceiptRepository
,ITbReceiptDetailRepository tbReceiptDetailRepository
,ITaVenderRepository taVenderRepository
, ITenantStore tenantStore
, IConfiguration configuration
) : base(repository)
{
@ -39,6 +42,7 @@ namespace Win_in.Sfs.Scp.WebApi
_tbReceiptRepository = tbReceiptRepository;
_tbReceiptDetailRepository = tbReceiptDetailRepository;
_taVenderRepository = taVenderRepository;
_tenantStore = tenantStore;
_validSites = configuration["ValidSites"];
}
@ -117,14 +121,19 @@ namespace Win_in.Sfs.Scp.WebApi
private async Task UpsertTbReceiptAndTbReceiptDetailAsync(Receipt entity)
{
var tbReceipt = ObjectMapper.Map<Receipt, TB_RECEIVE_QAD>(entity);
//根据传入数据新增或修改SCP数据
await _tbReceiptRepository.UpsertAsync(tbReceipt);
//使用AutoMapper执行类型转换
var tbReceipt = ObjectMapper.Map<Receipt, TB_RECEIVE_QAD>(entity);
var tbReceiveDetail = ObjectMapper.Map<Receipt, TB_RECEIVE_DETAIL_QAD>(entity);
//根据传入数据新增或修改SCP数据
await _tbReceiptDetailRepository.UpsertAsync(tbReceiveDetail);
var tenant = await _tenantStore.FindAsync(tbReceipt.Site);
using (CurrentTenant.Change(tenant.Id, tenant.Name))
{
//根据传入数据新增或修改SCP数据
await _tbReceiptRepository.UpsertAsync(tbReceipt);
//根据传入数据新增或修改SCP数据
await _tbReceiptDetailRepository.UpsertAsync(tbReceiveDetail);
}
}
}
}

14
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/Suppliers/SuppliersAppService.cs

@ -8,6 +8,7 @@ using FluentValidation;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.Validation;
using Win_in.Sfs.Scp.v1.Domain;
@ -25,16 +26,19 @@ namespace Win_in.Sfs.Scp.WebApi
{
private readonly ISupplierRepository _supplierRepository;
private readonly ITaVenderRepository _taVenderRepository;
private readonly ITenantStore _tenantStore;
private readonly string _validSites = Validator.VALID_SITES;
public SuppliersAppService(
ISupplierRepository repository
,ITaVenderRepository taVenderRepository
,ITenantStore tenantStore
, IConfiguration configuration
) : base(repository)
{
_supplierRepository = repository;
_taVenderRepository = taVenderRepository;
_tenantStore = tenantStore;
_validSites = configuration["ValidSites"];
}
@ -111,8 +115,14 @@ namespace Win_in.Sfs.Scp.WebApi
{
//使用AutoMapper执行类型转换
var taVender = ObjectMapper.Map<Supplier, TA_VENDER>(entity);
//根据传入数据新增或修改SCP数据
await _taVenderRepository.UpsertAsync(taVender);
var tenant =await _tenantStore.FindAsync(taVender.Site);
using (CurrentTenant.Change(tenant.Id, tenant.Name))
{
//根据传入数据新增或修改SCP数据
await _taVenderRepository.UpsertAsync(taVender);
}
}
}
}

34
WebApiService/src/Win_in.Sfs.Scp.WebApi.Application/UnplannedReceipts/UnplannedReceiptAppService.cs

@ -8,6 +8,7 @@ using FluentValidation;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.Validation;
using Win_in.Sfs.Scp.v1.Domain;
@ -20,12 +21,14 @@ namespace Win_in.Sfs.Scp.WebApi
[Authorize]
[Route(RouteConsts.UnplannedReceipt)]
[ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)]
public class UnplannedReceiptAppService : ReadOnlyAppService<UnplannedReceipt, UnplannedReceiptDTO, Guid,RequestDTO>, IUnplannedReceiptAppService
public class UnplannedReceiptAppService :
ReadOnlyAppService<UnplannedReceipt, UnplannedReceiptDTO, Guid, RequestDTO>, IUnplannedReceiptAppService
{
private readonly IUnplannedReceiptRepository _unplannedReceiptRepository;
private readonly ITbReceiptRepository _tbReceiptRepository;
private readonly ITbReceiptDetailRepository _tbReceiptDetailRepository;
private readonly ITaVenderRepository _taVenderRepository;
private readonly ITenantStore _tenantStore;
private readonly string _validSites = Validator.VALID_SITES;
public UnplannedReceiptAppService(
@ -33,6 +36,7 @@ namespace Win_in.Sfs.Scp.WebApi
, ITbReceiptRepository tbReceiptRepository
, ITbReceiptDetailRepository tbReceiptDetailRepository
, ITaVenderRepository taVenderRepository
, ITenantStore tenantStore
, IConfiguration configuration
) : base(repository)
{
@ -40,6 +44,7 @@ namespace Win_in.Sfs.Scp.WebApi
_tbReceiptRepository = tbReceiptRepository;
_tbReceiptDetailRepository = tbReceiptDetailRepository;
_taVenderRepository = taVenderRepository;
_tenantStore = tenantStore;
_validSites = configuration["ValidSites"];
}
@ -78,15 +83,16 @@ namespace Win_in.Sfs.Scp.WebApi
/// <returns></returns>
[HttpPost]
[Route("")]
public virtual async Task<ActionResult<UnplannedReceiptDTO>> CreateAsync(UnplannedReceiptCreateDTO receiptCreateDTO)
public virtual async Task<ActionResult<UnplannedReceiptDTO>> CreateAsync(
UnplannedReceiptCreateDTO receiptCreateDTO)
{
var entity = ObjectMapper.Map<UnplannedReceiptCreateDTO, UnplannedReceipt>(receiptCreateDTO);
try
{
Validator.CheckSite(_validSites,entity.Site);
Validator.CheckSite(_validSites,entity.Company);
Validator.CheckSupplierCode(_taVenderRepository,entity.Site, entity.Address);
Validator.CheckSite(_validSites, entity.Site);
Validator.CheckSite(_validSites, entity.Company);
Validator.CheckSupplierCode(_taVenderRepository, entity.Site, entity.Address);
await UpsertTbReceiptAndTbReceiptDetailAsync(entity);
await CurrentUnitOfWork.SaveChangesAsync();
@ -118,14 +124,20 @@ namespace Win_in.Sfs.Scp.WebApi
private async Task UpsertTbReceiptAndTbReceiptDetailAsync(UnplannedReceipt entity)
{
var tbReceipt = ObjectMapper.Map<UnplannedReceipt, TB_RECEIVE_QAD>(entity);
//根据传入数据新增或修改SCP数据
await _tbReceiptRepository.UpsertAsync(tbReceipt);
//使用AutoMapper执行类型转换
var tbReceipt = ObjectMapper.Map<UnplannedReceipt, TB_RECEIVE_QAD>(entity);
var tbReceiveDetail = ObjectMapper.Map<UnplannedReceipt, TB_RECEIVE_DETAIL_QAD>(entity);
//根据传入数据新增或修改SCP数据
await _tbReceiptDetailRepository.UpsertAsync(tbReceiveDetail);
var tenant = await _tenantStore.FindAsync(tbReceipt.Site);
using (CurrentTenant.Change(tenant.Id, tenant.Name))
{
//根据传入数据新增或修改SCP数据
await _tbReceiptRepository.UpsertAsync(tbReceipt);
//根据传入数据新增或修改SCP数据
await _tbReceiptDetailRepository.UpsertAsync(tbReceiveDetail);
}
}
}
}

183
WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/MultiTenantConnectionStringResolver.cs

@ -1,183 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace Win_in.Sfs.Scp.WebApi;
public class AAA:DefaultConnectionStringResolver
{
public AAA(IOptionsSnapshot<AbpDbConnectionOptions> options) : base(options)
{
}
public override Task<string> ResolveAsync(string connectionStringName = null)
{
return base.ResolveAsync(connectionStringName);
}
}
[Dependency(ReplaceServices = true)]
public class MultiTenantConnectionStringResolver : DefaultConnectionStringResolver
{
private readonly ICurrentTenant _currentTenant;
private readonly IServiceProvider _serviceProvider;
public MultiTenantConnectionStringResolver(
IOptionsSnapshot<AbpDbConnectionOptions> options,
ICurrentTenant currentTenant,
IServiceProvider serviceProvider)
: base(options)
{
_currentTenant = currentTenant;
_serviceProvider = serviceProvider;
}
public override async Task<string> ResolveAsync(string connectionStringName = null)
{
if (_currentTenant.Id == null)
{
//No current tenant, fallback to default logic
return await base.ResolveAsync(connectionStringName);
}
var tenant = await FindTenantConfigurationAsync(_currentTenant.Id.Value);
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
{
//Tenant has not defined any connection string, fallback to default logic
return await base.ResolveAsync(connectionStringName);
}
var tenantDefaultConnectionString = tenant.ConnectionStrings.Default;
//Requesting default connection string...
if (connectionStringName == null ||
connectionStringName == ConnectionStrings.DefaultConnectionStringName)
{
//Return tenant's default or global default
return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
? tenantDefaultConnectionString
: Options.ConnectionStrings.Default;
}
//Requesting specific connection string...
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
if (!connString.IsNullOrWhiteSpace())
{
//Found for the tenant
return connString;
}
//Fallback to the mapped database for the specific connection string
var database = Options.Databases.GetMappedDatabaseOrNull(connectionStringName);
if (database != null && database.IsUsedByTenants)
{
connString = tenant.ConnectionStrings.GetOrDefault(database.DatabaseName);
if (!connString.IsNullOrWhiteSpace())
{
//Found for the tenant
return connString;
}
}
//Fallback to tenant's default connection string if available
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
{
return tenantDefaultConnectionString;
}
return await base.ResolveAsync(connectionStringName);
}
[Obsolete("Use ResolveAsync method.")]
public override string Resolve(string connectionStringName = null)
{
if (_currentTenant.Id == null)
{
//No current tenant, fallback to default logic
return base.Resolve(connectionStringName);
}
var tenant = FindTenantConfiguration(_currentTenant.Id.Value);
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
{
//Tenant has not defined any connection string, fallback to default logic
return base.Resolve(connectionStringName);
}
var tenantDefaultConnectionString = tenant.ConnectionStrings.Default;
//Requesting default connection string...
if (connectionStringName == null ||
connectionStringName == ConnectionStrings.DefaultConnectionStringName)
{
//Return tenant's default or global default
return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
? tenantDefaultConnectionString
: Options.ConnectionStrings.Default;
}
//Requesting specific connection string...
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
if (!connString.IsNullOrWhiteSpace())
{
//Found for the tenant
return connString;
}
//Fallback to tenant's default connection string if available
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
{
return tenantDefaultConnectionString;
}
//Try to find the specific connection string for given name
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
if (!connStringInOptions.IsNullOrWhiteSpace())
{
return connStringInOptions;
}
//Fallback to the global default connection string
var defaultConnectionString = Options.ConnectionStrings.Default;
if (!defaultConnectionString.IsNullOrWhiteSpace())
{
return defaultConnectionString;
}
throw new AbpException("No connection string defined!");
}
protected virtual async Task<TenantConfiguration> FindTenantConfigurationAsync(Guid tenantId)
{
using (var serviceScope = _serviceProvider.CreateScope())
{
var tenantStore = serviceScope
.ServiceProvider
.GetRequiredService<ITenantStore>();
return await tenantStore.FindAsync(tenantId);
}
}
[Obsolete("Use FindTenantConfigurationAsync method.")]
protected virtual TenantConfiguration FindTenantConfiguration(Guid tenantId)
{
using (var serviceScope = _serviceProvider.CreateScope())
{
var tenantStore = serviceScope
.ServiceProvider
.GetRequiredService<ITenantStore>();
return tenantStore.Find(tenantId);
}
}
}

32
WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/WebApiHttpApiHostModule.cs

@ -81,38 +81,6 @@ namespace Win_in.Sfs.Scp.WebApi
private void ConfigureMultiTenancy(IConfiguration configuration)
{
Configure<AbpMultiTenancyOptions>(options => { options.IsEnabled = true; });
// Configure<AbpDefaultTenantStoreOptions>(configuration);
// Configure<AbpDefaultTenantStoreOptions>(options =>
// {
// options.Tenants = new[]
// {
// new TenantConfiguration(
// Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0"), //Id
// "T8" //Name
// )
// {
// //tenant1 有单独的数据库连接字符串
// ConnectionStrings =
// {
// {V1ScpConsts.ConnectionStringName, "Server=127.0.0.1;Database=SCP_HZ;User ID=sa;Password=Microsoft2008;connection timeout=600;"}
// }
// }
// ,
// new TenantConfiguration(
// Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d"), //Id
// "T9" //Name
// )
// {
// //tenant2 有单独的数据库连接字符串
// ConnectionStrings =
// {
// {V1ScpConsts.ConnectionStringName, "Server=127.0.0.1;Database=SCP_TJ;User ID=sa;Password=Microsoft2008;connection timeout=600;"}
// }
// }
// };
// });
}
private static void ConfigureAuthorization(ServiceConfigurationContext context, IConfiguration configuration)

17
WebApiService/src/Win_in.Sfs.Scp.WebApi.HttpApi.Host/appsettings.json

@ -34,23 +34,6 @@
}
}
},
"Tenants": [
{
"Id": "446a5211-3d72-4339-9adc-845151f8ada0",
"Name": "T8",
"ConnectionStrings": {
"V1Scp": "Server=127.0.0.1;Database=SCP_HZ;User ID=sa;Password=Microsoft2008;connection timeout=600;"
}
},
{
"Id": "25388015-ef1c-4355-9c18-f6b6ddbaf89d",
"Name": "T9",
"ConnectionStrings": {
"V1Scp": "Server=127.0.0.1;Database=SCP_TJ;User ID=sa;Password=Microsoft2008;connection timeout=600;"
}
}
],
"AlwaysAllowAuthorization": true,
"ValidSites": "T8,T9"
}

2
WebApiService/src/Win_in.Sfs.Scp.v1.Domain/Parts/TA_PART.cs

@ -42,6 +42,6 @@ namespace Win_in.Sfs.Scp.v1.Domain
public bool? Ischeck { get; set; }
public Guid? TenantId { get; set; }
public Guid? TenantId { get; }
}
}
Loading…
Cancel
Save