using Business.Core.Attributes; using Business.Core.Utilities; using Business.Domain; using Business.EntityFrameworkCore; using Business.MultiTenancy; using Bussiness.MongoDB; using GZY.Quartz.MUI.Extensions; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using MongoDB.Driver; using NLog; using Quartz; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using Volo.Abp; using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.Caching; using Volo.Abp.EntityFrameworkCore.MySQL; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.VirtualFileSystem; namespace Business { [DependsOn( typeof(AbpAutofacModule), typeof(AbpEntityFrameworkCoreMySQLModule), typeof(BusinessHttpApiModule), typeof(BusinessApplicationModule), typeof(BusinessEntityFrameworkCoreModule), typeof(BussinessMongoDbModule), typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpAspNetCoreSerilogModule) )] public class BusinessHostModule : AbpModule { private const string DefaultCorsPolicyName = "Default"; public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); var hostingEnvironment = context.Services.GetHostingEnvironment(); //ConfigureConventionalControllers(); ConfigureMultiTenancy(); ConfigureAuthentication(context, configuration); ConfigureLocalization(); ConfigureCache(configuration); ConfigureVirtualFileSystem(context, hostingEnvironment); //ConfigureRedis(context, configuration, hostingEnvironment); ConfigureCors(context, configuration); ConfigureSwaggerServices(context, configuration); ConfigureQuartz(context, configuration); ConfigureMongoDB(configuration); if (hostingEnvironment.IsDevelopment()) { Configure(options => { options.SendExceptionsDetailsToClients = true; }); } } private void ConfigureConventionalControllers() { Configure(options => { options.ConventionalControllers.Create(typeof(BusinessApplicationModule).Assembly); }); } private void ConfigureMultiTenancy() { Configure(options => { options.IsEnabled = true; }); } private void ConfigureQuartz(ServiceConfigurationContext context, IConfiguration configuration) { //程序启动执行一次日志分表检查,可以自己初始化避免需要部署脚本 LogManager.Configuration.Install(new NLog.Config.InstallationContext());//每天0点执行一次 context.Services.AddQuartz(q => { //q.UseMicrosoftDependencyInjectionScopedJobFactory(); //// Just use the name of your job that you created in the Jobs folder. //var jobKey = new JobKey("SyncDataJob"); //q.AddJob(opts => opts.WithIdentity(jobKey)); //q.AddTrigger(opts => opts // .ForJob(jobKey) // .WithIdentity("SyncDataJob-trigger") // .WithCronSchedule("0 38 10 * * ?") // .WithDescription("定时同步MySQL基础数据到MongoDB")); //var NLogJobKey = new JobKey("NLogJob"); //q.AddJob(opts => opts.WithIdentity(NLogJobKey)); //q.AddTrigger(opts => opts // .ForJob(NLogJobKey) // .WithIdentity("NLogJob-trigger") // .WithCronSchedule("0 01 01 * * ?") // .WithDescription("定时创建NLog日志按月分表")); //var WMSJobKey = new JobKey("WMSJob"); //q.AddJob(opts => opts.WithIdentity(WMSJobKey)); //q.AddTrigger(opts => opts // .ForJob(WMSJobKey) // .WithIdentity("WMSJob-trigger") // .WithCronSchedule("0 32 15 * * ?") // .WithDescription("定时同步WMS物料订单等基础数据到MySQL")); //var ProductionScheduleJobKey = new JobKey("ProductionScheduleJob"); //q.AddJob(opts => opts.WithIdentity(ProductionScheduleJobKey)); //q.AddTrigger(opts => opts // .ForJob(ProductionScheduleJobKey) // .WithIdentity("ProductionScheduleJob-trigger") // .WithCronSchedule("0 01 01 * * ?") // .WithDescription("定时排产任务")); }); context.Services.AddQuartzServer(options => { // when shutting down we want jobs to complete gracefully options.WaitForJobsToComplete = true; }); context.Services.AddQuartzUI(); context.Services.AddQuartzClassJobs(); //添加本地调度任务访问 context.Services.AddQuartzHostedService(options => { // when shutting down we want jobs to complete gracefully options.WaitForJobsToComplete = true; }); } /// /// MongoDB依赖注入 /// /// /// private void ConfigureMongoDB(IConfiguration configuration) { //[Index(nameof(bom_number), nameof(item_number), nameof(version), nameof(tenant_id), nameof(factory_id), IsUnique = true)] var indexModel_mo_ic_bom = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.bom_number). Ascending(_ => _.item_number). Ascending(_ => _.version). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id) , new CreateIndexOptions { Name = "bom_number_1_item_number_1_version_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_ic_bom.Indexes.CreateOne(indexModel_mo_ic_bom); MongoHelper.CreatIndexAsync(indexModel_mo_ic_bom); //[Index(nameof(bom_number), nameof(item_number), nameof(version), nameof(tenant_id), nameof(factory_id), IsUnique = true)] var indexModel_mo_ic_bom_child = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.bom_number). Ascending(_ => _.item_number). Ascending(_ => _.version). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id) , new CreateIndexOptions { Name = "bom_number_1_item_number_1_version_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_ic_bom_child.Indexes.CreateOne(indexModel_mo_ic_bom_child); MongoHelper.CreatIndexAsync(indexModel_mo_ic_bom_child); //[Index(nameof(number), nameof(fversion), nameof(tenant_id), nameof(factory_id), IsUnique = true)] var indexModel_mo_ic_item = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.number). Ascending(_ => _.fversion). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id) , new CreateIndexOptions { Name = "number_1_fversion_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_ic_item.Indexes.CreateOne(indexModel_mo_ic_item); MongoHelper.CreatIndexAsync(indexModel_mo_ic_item); //[Index(nameof(icitem_number), nameof(fversion), nameof(tenant_id), nameof(factory_id), IsUnique = true)] var indexModel_mo_ic_item_stock = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.icitem_number). Ascending(_ => _.fversion). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id). Ascending(_ => _.bang_id) , new CreateIndexOptions { Name = "icitem_number_1_fversion_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_ic_item_stock.Indexes.CreateOne(indexModel_mo_ic_item_stock); MongoHelper.CreatIndexAsync(indexModel_mo_ic_item_stock); //[Index(nameof(po_billno), nameof(polist_row), nameof(tenant_id), nameof(factory_id), IsUnique = true)] /*var indexModel_mo_srm_po_list = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.po_billno). Ascending(_ => _.polist_row). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id). Ascending(_ => _.bang_id) , new CreateIndexOptions { Name = "po_billno_1_polist_row_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_srm_po_list.Indexes.CreateOne(indexModel_mo_srm_po_list); MongoHelper.CreatIndexAsync(indexModel_mo_srm_po_list);*/ //[Index(nameof(po_billno), nameof(tenant_id), nameof(factory_id), IsUnique = true)] CreateIndexModel indexModel_mo_srm_po_main = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.po_billno). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id). Ascending(_ => _.bang_id) , new CreateIndexOptions { Name = "po_billno_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_srm_po_main.Indexes.CreateOne(indexModel_mo_srm_po_main); MongoHelper.CreatIndexAsync(indexModel_mo_srm_po_main); /*//[Index(nameof(pr_billno), nameof(tenant_id), nameof(factory_id), IsUnique = true)] CreateIndexModel indexModel_mo_srm_pr_main = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.pr_billno). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id). Ascending(_ => _.bang_id) , new CreateIndexOptions { Name = "pr_billno_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_srm_pr_main.Indexes.CreateOne(indexModel_mo_srm_pr_main); MongoHelper.CreatIndexAsync(indexModel_mo_srm_pr_main);*/ //[Index(nameof(number), nameof(supplier_number), nameof(tenant_id), nameof(factory_id), IsUnique = true)] CreateIndexModel indexModel_mo_srm_purchase = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.number). Ascending(_ => _.supplier_number). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id). Ascending(_ => _.bang_id) , new CreateIndexOptions { Name = "number_1_supplier_number_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_srm_purchase.Indexes.CreateOne(indexModel_mo_srm_purchase); MongoHelper.CreatIndexAsync(indexModel_mo_srm_purchase); //[Index(nameof(supplier_no), nameof(tenant_id), nameof(factory_id), IsUnique = true)] CreateIndexModel indexModel_mo_srm_supplier = new CreateIndexModel( Builders.IndexKeys. Ascending(_ => _.supplier_no). Ascending(_ => _.tenant_id). Ascending(_ => _.company_id). Ascending(_ => _.factory_id) , new CreateIndexOptions { Name = "supplier_no_1_tenant_id_1_factory_id_1", Sparse = true, Background = true, Unique = true, //唯一值索引 }); //mongoCollection_mo_srm_supplier.Indexes.CreateOne(indexModel_mo_srm_supplier); MongoHelper.CreatIndexAsync(indexModel_mo_srm_supplier); } private void ConfigureCache(IConfiguration configuration) { Configure(options => { options.KeyPrefix = "dopbiz:"; }); } private void ConfigureVirtualFileSystem(ServiceConfigurationContext context, IWebHostEnvironment webHostEnvironment) { //var hostingEnvironment = context.Services.GetHostingEnvironment(); if (webHostEnvironment.IsDevelopment()) { Configure(options => { options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(webHostEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Business.Domain")); options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(webHostEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Business.Application.Contracts")); options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(webHostEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Business.Application")); }); } } private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = false; options.Audience = "BusinessService"; }); } private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration) { if (configuration["UseSwagger"] == "true") { context.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "Business Service API", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "请输入JWT令牌,例如:Bearer 12345abcdef", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "Bearer" }); options.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, new List() } }); }); } } private void ConfigureLocalization() { Configure(options => { options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština")); options.Languages.Add(new LanguageInfo("en", "en", "English")); options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português")); options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe")); options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文")); }); } private void ConfigureRedis( ServiceConfigurationContext context, IConfiguration configuration, IWebHostEnvironment hostingEnvironment) { context.Services.AddStackExchangeRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; }); if (!hostingEnvironment.IsDevelopment()) { var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); context.Services .AddDataProtection() .PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys"); } } private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddCors(options => { options.AddPolicy(DefaultCorsPolicyName, builder => { builder .WithOrigins( configuration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var configuration = context.GetConfiguration(); app.UseCorrelationId(); app.UseStaticFiles(); app.UseRouting(); app.UseCors(DefaultCorsPolicyName); app.UseAuthentication(); app.UseAbpClaimsMap(); if (MultiTenancyConsts.IsEnabled) { app.UseMultiTenancy(); } app.UseAbpRequestLocalization(); if (configuration["UseSwagger"] == "true") { app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Business Service API"); }); } app.UseAuditing(); app.UseAbpSerilogEnrichers(); app.UseUnitOfWork(); app.UseConfiguredEndpoints(); app.UseQuartz(); //AsyncHelper.RunSync(async () => //{ // using (var scope = context.ServiceProvider.CreateScope()) // { // await scope.ServiceProvider // .GetRequiredService() // .SeedAsync(); // } //}); } } }