From d71a95bd658d0987dcbc3f9530b64dc8772d1ca0 Mon Sep 17 00:00:00 2001 From: stevelee Date: Wed, 27 Apr 2022 16:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=A9=E6=96=99=E8=84=9A?= =?UTF-8?q?=E6=89=8B=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPA.SaaS.Material.Api.sln | 8 +- .../BPA.SaaS.Material.Api.Bootstrap.csproj | 42 ++--- .../SetupBootstrap.cs | 163 ++++++++++++++++++ .../SetupRabbitMqConfig.cs | 67 +++++++ .../SetupRedisConfig.cs | 47 +++++ .../SetupServiceSDK.cs | 47 +++++ .../BPA.SaaS.Material.Api.DTO.csproj | 7 +- src/BPA.SaaS.Material.Api.DTO/UnitDto.cs | 26 +++ src/BPA.SaaS.Material.Api.DTO/UnitDtoQuery.cs | 35 ++++ .../BPA.SaaS.Material.Api.Entity.csproj | 19 +- src/BPA.SaaS.Material.Api.Entity/BPA_Unit.cs | 24 +++ .../BPA.SaaS.Material.Api.IRepository.csproj | 5 +- .../IUnitRepository.cs | 48 ++++++ .../BPA.SaaS.Material.Api.IService.csproj | 2 +- .../IUnitService.cs | 49 ++++++ .../BPA.SaaS.Material.Api.Model.csproj | 37 ++-- .../MaterialApiConfig.cs | 42 +++++ .../MaterialDbSugarClient.cs | 14 ++ .../BPA.SaaS.Material.Api.Repository.csproj | 34 ++-- .../UnitRepository.cs | 58 +++++++ .../BPA.SaaS.Material.Api.SDK.csproj | 23 +-- .../MaterialWebApiSDK.cs | 14 ++ .../BPA.SaaS.Material.Api.Service.csproj | 7 +- .../UnitService.cs | 45 +++++ .../BPA.SaaS.Material.Api.UnitTest.csproj | 25 ++- .../BaseUnitTest.cs | 52 ++++++ .../BPA.SaaS.Material.Api.WebApi.csproj | 2 +- src/BPA.SaaS.Material.Api.WebApi/Program.cs | 34 +++- .../Properties/launchSettings.json | 27 +-- .../UnitController.cs | 25 +++ .../appsettings.Development.json | 8 - .../appsettings.json | 9 - 32 files changed, 915 insertions(+), 130 deletions(-) create mode 100644 src/BPA.SaaS.Material.Api.Bootstrap/SetupBootstrap.cs create mode 100644 src/BPA.SaaS.Material.Api.Bootstrap/SetupRabbitMqConfig.cs create mode 100644 src/BPA.SaaS.Material.Api.Bootstrap/SetupRedisConfig.cs create mode 100644 src/BPA.SaaS.Material.Api.Bootstrap/SetupServiceSDK.cs create mode 100644 src/BPA.SaaS.Material.Api.DTO/UnitDto.cs create mode 100644 src/BPA.SaaS.Material.Api.DTO/UnitDtoQuery.cs create mode 100644 src/BPA.SaaS.Material.Api.Entity/BPA_Unit.cs create mode 100644 src/BPA.SaaS.Material.Api.IRepository/IUnitRepository.cs create mode 100644 src/BPA.SaaS.Material.Api.IService/IUnitService.cs create mode 100644 src/BPA.SaaS.Material.Api.Model/MaterialApiConfig.cs create mode 100644 src/BPA.SaaS.Material.Api.Model/MaterialDbSugarClient.cs create mode 100644 src/BPA.SaaS.Material.Api.Repository/UnitRepository.cs create mode 100644 src/BPA.SaaS.Material.Api.SDK/MaterialWebApiSDK.cs create mode 100644 src/BPA.SaaS.Material.Api.Service/UnitService.cs create mode 100644 src/BPA.SaaS.Material.Api.UnitTest/BaseUnitTest.cs create mode 100644 src/BPA.SaaS.Material.Api.WebApi/UnitController.cs delete mode 100644 src/BPA.SaaS.Material.Api.WebApi/appsettings.Development.json delete mode 100644 src/BPA.SaaS.Material.Api.WebApi/appsettings.json diff --git a/BPA.SaaS.Material.Api.sln b/BPA.SaaS.Material.Api.sln index 49073e9..0642da2 100644 --- a/BPA.SaaS.Material.Api.sln +++ b/BPA.SaaS.Material.Api.sln @@ -43,11 +43,6 @@ ProjectSection(SolutionItems) = preProject src\nuget.config = src\nuget.config EndProjectSection EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".pack", ".pack", "{5CE18647-CEFB-483A-B384-99D4FB47DA29}" -ProjectSection(SolutionItems) = preProject -src\.pack\pack.sh = src\.pack\pack.sh -EndProjectSection -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -114,9 +109,8 @@ Global {4F48E97A-2DC3-4720-98F7-25935258D79C} = {0265A6CF-0D40-4A59-B6DF-2F25099DA95B} {CFDFD099-D03F-441A-A3CD-28C7320742C5} = {71207855-D2BE-48D3-86AD-D1E5BC389E64} {FBE0E584-BF1F-498D-BBFC-66B790EAFC7C} = {FEDACFF6-C6AF-4C34-9575-9456856AEACC} - {5CE18647-CEFB-483A-B384-99D4FB47DA29} = {0265A6CF-0D40-4A59-B6DF-2F25099DA95B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7A177DC6-A13E-4628-97F6-40DC7573252A} EndGlobalSection -EndGlobal \ No newline at end of file +EndGlobal diff --git a/src/BPA.SaaS.Material.Api.Bootstrap/BPA.SaaS.Material.Api.Bootstrap.csproj b/src/BPA.SaaS.Material.Api.Bootstrap/BPA.SaaS.Material.Api.Bootstrap.csproj index 36e924d..7b79432 100644 --- a/src/BPA.SaaS.Material.Api.Bootstrap/BPA.SaaS.Material.Api.Bootstrap.csproj +++ b/src/BPA.SaaS.Material.Api.Bootstrap/BPA.SaaS.Material.Api.Bootstrap.csproj @@ -1,22 +1,24 @@ - + - - net6.0 - enable - enable - - - - - - - - - - - - - - - + + net6.0 + enable + enable + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Bootstrap/SetupBootstrap.cs b/src/BPA.SaaS.Material.Api.Bootstrap/SetupBootstrap.cs new file mode 100644 index 0000000..d0e6b83 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Bootstrap/SetupBootstrap.cs @@ -0,0 +1,163 @@ +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using Autofac; +using BPA.Component.ApolloClient; +using BPA.Component.CAP; +using BPA.Component.Extensions; +using BPA.Component.LogClient.Extensions; +using BPA.Component.SDKCommon; +using BPA.Component.WebApiExtensions.Extensions; +using BPA.SaaS.Material.Api.Model; +using BPA.SaaS.Material.Api.Repository; +using BPA.SaaS.Material.Api.Service; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using IPAddress = System.Net.IPAddress; + +namespace BPA.SaaS.Material.Api.Bootstrap; + +/// +/// 启动配置 +/// +public static class SetupBootstrap +{ + private static IConfiguration _configuration; + + /// + /// 添加配置中心 + /// + /// + public static IConfiguration SetupConfiguration(this IConfiguration configuration) + { + _configuration = configuration; + + // 初始化化 apollo配置 + return configuration.AddApolloConfiguration(); + } + + /// + /// DI Inject + /// + public static IServiceCollection AddBPAMiddlewareServices(this IServiceCollection services, string[] args) + { + if (services == null) throw new ArgumentNullException(nameof(services)); + + services.AddApollo(); + services.AddBPALog(); + services.AddRedis(); + services.AddRabbitMQ(); + services.AddHttpContextAccessor(); + services.AddControllers(options => { options.AddDefaultMvcOptions(); }); + services.AddEndpointsApiExplorer(); + services.AddServiceSDK(_configuration); + services.AddHealthChecks(); + + // services.AddCap(x => + // { + // var config = services.GetPreConfig(args); + // x.UseRabbitMQ(mqOptions => + // { + // mqOptions.UserName = config.RabbitMqConfig.UserName; + // mqOptions.Password = config.RabbitMqConfig.Password; + // mqOptions.Port = config.RabbitMqConfig.Port; + // mqOptions.HostName = config.RabbitMqConfig.HostName; + // mqOptions.VirtualHost = config.RabbitMqConfig.VirtualHost; + // }); + // x.UseMySql(config.BasicMySqlDb.MasterDbConnectionString); + // }); + + services.AddSwaggerGen(options => + { + options.SwaggerDoc("v1", new OpenApiInfo + { + Title = AppDomain.CurrentDomain.FriendlyName, + Description = AppDomain.CurrentDomain.FriendlyName, + Version = "v1" + }); + options.CustomSchemaIds(type => type.GetDescribe(true, "BPA.", "BPA.SaaS.")); + }); + + return services; + } + + /// + /// DI Register + /// + public static ContainerBuilder SetupConfigureContainer(this ContainerBuilder builder) + { + // 注入DB + builder.RegisterType().InstancePerLifetimeScope(); + + // 服务层 + builder.RegisterAssemblyTypes(typeof(UnitService).Assembly) + .Where(t => t.Name.EndsWith("Service")) + .AsImplementedInterfaces(); + + // 仓储层 + builder.RegisterAssemblyTypes(typeof(UnitRepository).Assembly) + .Where(t => t.Name.EndsWith("Repository")) + .AsImplementedInterfaces(); + + return builder; + } + + /// + /// pre run + /// + /// + public static void PreHeatRun(this IServiceProvider serviceProvider) + { + serviceProvider.GetService(); + serviceProvider.GetService()?.DbMaintenance.GetTableInfoList(); + serviceProvider.PreHeatRunRedis(); + serviceProvider.PreHeatRunRabbitMQ(); + serviceProvider.SetDebug(true); + serviceProvider.GetService(); + } + + /// + /// APP健康检查 + /// + /// + public static void UseBpaHealthChecks(this IApplicationBuilder app) + { + var ips = new List(); + foreach (var network in NetworkInterface.GetAllNetworkInterfaces()) + { + var ipAddress = network.GetIPProperties() + .UnicastAddresses + .FirstOrDefault(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address)) + ?.Address.ToString(); + if (ipAddress == null) + continue; + ips.Add(new {name = network.Name, ip = ipAddress}); + } + + var ipsJson = ips.ToJson(); + + app.UseHealthChecks("/health", new HealthCheckOptions + { + ResponseWriter = (httpContext, _) => httpContext.Response.WriteAsync(ipsJson, Encoding.UTF8) + }); + } + + private static MaterialApiConfig GetPreConfig(this IServiceCollection services, string[] args) + { + var tmpBuilder = WebApplication.CreateBuilder(args); + tmpBuilder.Configuration.SetupConfiguration(); + tmpBuilder.Services.AddApollo(); + tmpBuilder.Services.AddBPALog(); + var tmpApp = tmpBuilder.Build(); + var config = tmpApp.Services.GetService(); + tmpApp.StopAsync().ConfigureAwait(false).GetAwaiter(); + tmpApp.DisposeAsync().ConfigureAwait(false).GetAwaiter(); + tmpBuilder = null; + tmpApp = null; + return config; + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Bootstrap/SetupRabbitMqConfig.cs b/src/BPA.SaaS.Material.Api.Bootstrap/SetupRabbitMqConfig.cs new file mode 100644 index 0000000..ea87607 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Bootstrap/SetupRabbitMqConfig.cs @@ -0,0 +1,67 @@ +using BPA.Component.RabbitMQClient.Extensions; +using BPA.Component.RabbitMQClient.Provider; +using BPA.SaaS.Material.Api.Model; +using Microsoft.Extensions.DependencyInjection; + +namespace BPA.SaaS.Material.Api.Bootstrap +{ + /// + /// RabbitMQ配置 + /// + public static class SetupRabbitMqConfig + { + /// + /// 使用消息队列 + /// + /// + /// + public static IServiceCollection AddRabbitMQ(this IServiceCollection services) + { + //注入 RabbitMQProvider +#pragma warning disable CS8603 + services.AddRabbitMQProvider(sp => sp.GetService()?.RabbitMqConfig); +#pragma warning restore CS8603 + // //注入 业务日志 生产者 单例 + // services.UseRabbitMQProducer(); + // //注入 消息 生产者 单例 + // services.UseRabbitMQProducer(); + + return services; + } + + // public static IServiceCollection UseCap(this IServiceCollection services, Func configFunc) + // { + // if (configFunc == null) throw new ArgumentNullException(nameof(configFunc)); + // services.AddSingleton(sp => { return configFunc(sp); }); + // return services; + // } + + + /// + /// 预热消息队列 + /// + /// + /// + public static IServiceProvider PreHeatRunRabbitMQ(this IServiceProvider serviceProvider) + { + //获取rabbitMqProvider + var rabbitMqProvider = serviceProvider.UseRabbitMQProvider(); + + // //创建交换机配置对象 + // var createBizLogExchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.CreateBizLogExchange); + // //启用交换机 + // createBizLogExchangeConfig.UseExchange(); + // //启用生产者 + // createBizLogExchangeConfig.UseProducer(); + + // //创建交换机配置对象 + // var addMessageExchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.BatchAddMessageExchange, ExchangeType.Fanout); + // //启用交换机 + // addMessageExchangeConfig.UseExchange(); + // //启用生产者 + // addMessageExchangeConfig.UseProducer(); + + return serviceProvider; + } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Bootstrap/SetupRedisConfig.cs b/src/BPA.SaaS.Material.Api.Bootstrap/SetupRedisConfig.cs new file mode 100644 index 0000000..a9b6152 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Bootstrap/SetupRedisConfig.cs @@ -0,0 +1,47 @@ +using BPA.Component.RedisClient; +using BPA.SaaS.Material.Api.Model; +using Microsoft.Extensions.DependencyInjection; + +namespace BPA.SaaS.Material.Api.Bootstrap; + +/// +/// redis DI配置 +/// +public static class SetupRedisConfig +{ + /// + /// 注入redis + /// + /// + public static void AddRedis(this IServiceCollection services) + { + services.UseRedisSingleton(sp => GetRedisSingletonConnectionString(sp.GetService()!)); + } + + /// + /// 预热redis + /// + /// + public static void PreHeatRunRedis(this IServiceProvider serviceProvider) + { + var config = serviceProvider.GetService(); + + // 添加配置改变时的事件 + if (config != null) + config.OnConfigChange += (stockWebApiConfig, key, _, _, _) => + { + if (nameof(stockWebApiConfig.RedisConfig) != key) return; + var conStr = GetRedisSingletonConnectionString(stockWebApiConfig); + + //单个redis时 配置变化 触发生成新的实例 + RedisConfigExtensions.OnRedisSingletonConnectionStringChange(conStr); + }; + } + + /// + /// 获取单例 Redis 连接字符串 + /// + /// + /// + private static string GetRedisSingletonConnectionString(MaterialApiConfig config) => config.RedisConfig[new Random().Next(0, config.RedisConfig.Count)]; +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Bootstrap/SetupServiceSDK.cs b/src/BPA.SaaS.Material.Api.Bootstrap/SetupServiceSDK.cs new file mode 100644 index 0000000..7a4e8d0 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Bootstrap/SetupServiceSDK.cs @@ -0,0 +1,47 @@ +using BPA.Component.DTOCommon.Langs; +using BPA.Component.SDKCommon; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace BPA.SaaS.Material.Api.Bootstrap; + +/// +/// 其他SDK服务 +/// +public static class SetupServiceSDK +{ + /// + /// 注入其他SDK + /// + /// + /// + public static void AddServiceSDK(this IServiceCollection services, IConfiguration configuration) + { + // 初始化sdk配置和语言获取 + services.AddApiSDK(_ => /*sp.GetService()?.HttpContext?.Request.GetLanguage() ??*/ LangType.Zn); + + // 获取当前API启动时定义的环境 + var environment = configuration.GetValue("ASPNETCORE_ENVIRONMENT"); + + // 环境选择 + switch (environment) + { + case "Development": + // 开发本地连集群 + // services.AddApiSDK(_ => "http://[IP]:[port]"); + // 开发集群环境内 + // services.UseApiSDK(_ => "http://serviceName"); + break; + + + case "Production": + // services.AddApiSDK(_ => "http://[domainName].com:[80]/[serviceName]"); + break; + + + case "Testing": + // services.AddApiSDK(_ => "http://test.[domainName].com:[80]/[serviceName]/"); + break; + } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.DTO/BPA.SaaS.Material.Api.DTO.csproj b/src/BPA.SaaS.Material.Api.DTO/BPA.SaaS.Material.Api.DTO.csproj index d5a0aaa..c3e83d9 100644 --- a/src/BPA.SaaS.Material.Api.DTO/BPA.SaaS.Material.Api.DTO.csproj +++ b/src/BPA.SaaS.Material.Api.DTO/BPA.SaaS.Material.Api.DTO.csproj @@ -1,11 +1,14 @@ - + net6.0 enable enable + 1.0.1 + - + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.DTO/UnitDto.cs b/src/BPA.SaaS.Material.Api.DTO/UnitDto.cs new file mode 100644 index 0000000..82b6558 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.DTO/UnitDto.cs @@ -0,0 +1,26 @@ +namespace BPA.SaaS.Material.Api.DTO +{ + public class UnitDto + { + /// + /// 设备类型Id + /// + public string Id { get; set; } + + /// + /// 设备类型名称 + /// + public string Name { get; set; } + + /// + /// 设备类型备注 + /// + public string Remark { get; set; } + + // TODO:状态需要用枚举 + /// + /// 状态 + /// + public int Status { get; set; } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.DTO/UnitDtoQuery.cs b/src/BPA.SaaS.Material.Api.DTO/UnitDtoQuery.cs new file mode 100644 index 0000000..da0f072 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.DTO/UnitDtoQuery.cs @@ -0,0 +1,35 @@ +namespace BPA.SaaS.Material.Api.DTO +{ + public class UnitDtoQuery + { + /// + /// TypeID + /// + public string TypeID { get; set; } + + /// + /// 类型编号 + /// + public string Id { get; set; } + + /// + /// 类型名称 + /// + public string Name { get; set; } + + /// + /// 状态 + /// + public string Status { get; set; } + + /// + /// 备注 + /// + public string Remark { get; set; } + + /// + /// 创建时间 + /// + public DateTime? CreateAt { get; set; } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Entity/BPA.SaaS.Material.Api.Entity.csproj b/src/BPA.SaaS.Material.Api.Entity/BPA.SaaS.Material.Api.Entity.csproj index 0b15548..2cfefb0 100644 --- a/src/BPA.SaaS.Material.Api.Entity/BPA.SaaS.Material.Api.Entity.csproj +++ b/src/BPA.SaaS.Material.Api.Entity/BPA.SaaS.Material.Api.Entity.csproj @@ -1,11 +1,12 @@ - + - - net6.0 - enable - enable - - - - + + net6.0 + enable + enable + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Entity/BPA_Unit.cs b/src/BPA.SaaS.Material.Api.Entity/BPA_Unit.cs new file mode 100644 index 0000000..001869f --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Entity/BPA_Unit.cs @@ -0,0 +1,24 @@ +using BPA.Common.Entity; +using SqlSugar; + +namespace BPA.SaaS.Material.Api.Entity +{ + /// + /// 原料单位表 + /// + [SugarTable("BPA_Uint")] + public class BPA_Unit : BaseGroupIdEntity + { + /// + /// 单位名称 + /// + [SugarColumn(ColumnDataType = "Nvarchar(200)", IsNullable = false)] + public string Name { get; set; } + + /// + /// 备注 + /// + [SugarColumn(ColumnDataType = "Nvarchar(500)", IsNullable = true)] + public string Remark { get; set; } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.IRepository/BPA.SaaS.Material.Api.IRepository.csproj b/src/BPA.SaaS.Material.Api.IRepository/BPA.SaaS.Material.Api.IRepository.csproj index e20e8bf..63a17d8 100644 --- a/src/BPA.SaaS.Material.Api.IRepository/BPA.SaaS.Material.Api.IRepository.csproj +++ b/src/BPA.SaaS.Material.Api.IRepository/BPA.SaaS.Material.Api.IRepository.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,7 +6,8 @@ enable - + + diff --git a/src/BPA.SaaS.Material.Api.IRepository/IUnitRepository.cs b/src/BPA.SaaS.Material.Api.IRepository/IUnitRepository.cs new file mode 100644 index 0000000..98f08c7 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.IRepository/IUnitRepository.cs @@ -0,0 +1,48 @@ +using BPA.Component.DTOCommon.BaseDTOs; +using BPA.SaaS.Material.Api.DTO; + +namespace BPA.SaaS.Material.Api.IRepository; + +public interface IUnitRepository +{ + /// + /// 获取类型列表 + /// + /// + Task>> GetUnitList(); + + /// + /// 类型查询 + /// + /// + /// + Task> PostType(UnitDtoQuery dto); + + /// + /// 添加商品类型 + /// + /// + /// + Task> AddType(UnitDto dto); + + /// + /// 修改类型 + /// + /// + /// + Task> UpdateType(UnitDto dto); + + /// + /// 删除类型 + /// + /// + /// + Task> DeleteType(string id); + + /// + /// 批量删除类型 + /// + /// + /// + Task> BatchDeleteType(string[] Ids); +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.IService/BPA.SaaS.Material.Api.IService.csproj b/src/BPA.SaaS.Material.Api.IService/BPA.SaaS.Material.Api.IService.csproj index 4caa14d..abf0a3b 100644 --- a/src/BPA.SaaS.Material.Api.IService/BPA.SaaS.Material.Api.IService.csproj +++ b/src/BPA.SaaS.Material.Api.IService/BPA.SaaS.Material.Api.IService.csproj @@ -1,4 +1,4 @@ - + net6.0 diff --git a/src/BPA.SaaS.Material.Api.IService/IUnitService.cs b/src/BPA.SaaS.Material.Api.IService/IUnitService.cs new file mode 100644 index 0000000..15a99bd --- /dev/null +++ b/src/BPA.SaaS.Material.Api.IService/IUnitService.cs @@ -0,0 +1,49 @@ +using BPA.Component.DTOCommon.BaseDTOs; +using BPA.SaaS.Material.Api.DTO; + +namespace BPA.SaaS.Material.Api.IService +{ + public interface IUnitService + { + /// + /// 获取类型列表 + /// + /// + Task>> GetUnitList(); + + /// + /// 类型查询 + /// + /// + /// + Task> PostType(UnitDtoQuery dto); + + /// + /// 添加商品类型 + /// + /// + /// + Task> AddType(UnitDto dto); + + /// + /// 修改类型 + /// + /// + /// + Task> UpdateType(UnitDto dto); + + /// + /// 删除类型 + /// + /// + /// + Task> DeleteType(string id); + + /// + /// 批量删除类型 + /// + /// + /// + Task> BatchDeleteType(string[] Ids); + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Model/BPA.SaaS.Material.Api.Model.csproj b/src/BPA.SaaS.Material.Api.Model/BPA.SaaS.Material.Api.Model.csproj index f8f853c..d1451b8 100644 --- a/src/BPA.SaaS.Material.Api.Model/BPA.SaaS.Material.Api.Model.csproj +++ b/src/BPA.SaaS.Material.Api.Model/BPA.SaaS.Material.Api.Model.csproj @@ -1,17 +1,24 @@ - + - - net6.0 - enable - enable - - - - - - - - - - + + net6.0 + enable + enable + + + 1701;1702;0618;8618 + + + 1701;1702;0618 + + + + + + + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Model/MaterialApiConfig.cs b/src/BPA.SaaS.Material.Api.Model/MaterialApiConfig.cs new file mode 100644 index 0000000..9fba108 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Model/MaterialApiConfig.cs @@ -0,0 +1,42 @@ +using BPA.Component.ApolloClient; +using BPA.Component.DbClient; +using BPA.Component.RabbitMQClient.Connection; +using Com.Ctrip.Framework.Apollo; +using Microsoft.Extensions.Configuration; + +namespace BPA.SaaS.Material.Api.Model; + +/// +/// BasicWebApiConfig +/// +// ReSharper disable once ClassNeverInstantiated.Global +public class MaterialApiConfig : ApolloBPAConfig +{ + /// + /// BasicMySqlDb + /// + [AutoWrite] + public new DbConfig MaterialMySqlDb { get; set; } + + /// + /// RedisConfig + /// + [AutoWrite] + public new List RedisConfig { set; get; } + + /// + /// 是否打印SQL + /// + [AutoWrite] + public bool IsLogSql { get; set; } = true; + + /// + /// RabbitMQ配置 + /// + [AutoWrite] + public new RabbitMqConnectionConfig RabbitMqConfig { get; set; } + + public MaterialApiConfig(ApolloConfigurationManager apolloConfigurationManager, IConfiguration configuration) : base(apolloConfigurationManager, configuration) + { + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Model/MaterialDbSugarClient.cs b/src/BPA.SaaS.Material.Api.Model/MaterialDbSugarClient.cs new file mode 100644 index 0000000..78308dd --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Model/MaterialDbSugarClient.cs @@ -0,0 +1,14 @@ +using BPA.Component.DbClient; +using Microsoft.Extensions.Logging; + +namespace BPA.SaaS.Material.Api.Model; + +public class MaterialDbSugarClient : BaseDbClient +{ + public MaterialDbSugarClient(ILogger logger, MaterialApiConfig config) + : base(BuildConnectionConfig(config.MaterialMySqlDb), + config.IsLogSql, + logger) + { + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Repository/BPA.SaaS.Material.Api.Repository.csproj b/src/BPA.SaaS.Material.Api.Repository/BPA.SaaS.Material.Api.Repository.csproj index 6b7ba16..88140ec 100644 --- a/src/BPA.SaaS.Material.Api.Repository/BPA.SaaS.Material.Api.Repository.csproj +++ b/src/BPA.SaaS.Material.Api.Repository/BPA.SaaS.Material.Api.Repository.csproj @@ -1,17 +1,21 @@ - + - - net6.0 - enable - enable - - - - - - - - - - + + net6.0 + enable + enable + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Repository/UnitRepository.cs b/src/BPA.SaaS.Material.Api.Repository/UnitRepository.cs new file mode 100644 index 0000000..087271b --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Repository/UnitRepository.cs @@ -0,0 +1,58 @@ +using BPA.Component.DbClient; +using BPA.Component.DbClient.RepositoryModel; +using BPA.Component.DTOCommon.BaseDTOs; +using BPA.Component.Extensions; +using BPA.SaaS.Material.Api.DTO; +using BPA.SaaS.Material.Api.Entity; +using BPA.SaaS.Material.Api.IRepository; +using BPA.SaaS.Material.Api.Model; + +namespace BPA.SaaS.Material.Api.Repository; + +public class UnitRepository : BaseDbRepository, IUnitRepository +{ + public UnitRepository(MaterialDbSugarClient dbClient) : base(dbClient) + { + } + + /// + /// 获取产品列表 + /// + /// + public async Task>> GetUnitList() => + BaseResult> + .BuildOK((await SimpleClient.AsQueryable().Where(a => a.IsDeleted).ToListAsync()) + .MapToList() + .ToList()); + + /// + /// + /// + /// + /// + /// + public Task> PostType(UnitDtoQuery dto) + { + throw new NotImplementedException(); + } + + public Task> AddType(UnitDto dto) + { + throw new NotImplementedException(); + } + + public Task> UpdateType(UnitDto dto) + { + throw new NotImplementedException(); + } + + public Task> DeleteType(string id) + { + throw new NotImplementedException(); + } + + public Task> BatchDeleteType(string[] Ids) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.SDK/BPA.SaaS.Material.Api.SDK.csproj b/src/BPA.SaaS.Material.Api.SDK/BPA.SaaS.Material.Api.SDK.csproj index 65e26fe..767a0bb 100644 --- a/src/BPA.SaaS.Material.Api.SDK/BPA.SaaS.Material.Api.SDK.csproj +++ b/src/BPA.SaaS.Material.Api.SDK/BPA.SaaS.Material.Api.SDK.csproj @@ -1,13 +1,14 @@ - + - - net6.0 - enable - enable - - - - - - + + net6.0 + enable + enable + + + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.SDK/MaterialWebApiSDK.cs b/src/BPA.SaaS.Material.Api.SDK/MaterialWebApiSDK.cs new file mode 100644 index 0000000..30afb43 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.SDK/MaterialWebApiSDK.cs @@ -0,0 +1,14 @@ +using BPA.Component.SDKCommon; + +namespace BPA.SaaS.Material.Api.SDK; + +public class MaterialWebApiSDK : BaseApiSDK +{ + /// + /// BasicWebApiSDK + /// + /// + public MaterialWebApiSDK(IServiceProvider serviceProvider) : base(serviceProvider) + { + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.Service/BPA.SaaS.Material.Api.Service.csproj b/src/BPA.SaaS.Material.Api.Service/BPA.SaaS.Material.Api.Service.csproj index 68d8f15..68a4aee 100644 --- a/src/BPA.SaaS.Material.Api.Service/BPA.SaaS.Material.Api.Service.csproj +++ b/src/BPA.SaaS.Material.Api.Service/BPA.SaaS.Material.Api.Service.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,8 +6,11 @@ enable - + + + + diff --git a/src/BPA.SaaS.Material.Api.Service/UnitService.cs b/src/BPA.SaaS.Material.Api.Service/UnitService.cs new file mode 100644 index 0000000..09e167f --- /dev/null +++ b/src/BPA.SaaS.Material.Api.Service/UnitService.cs @@ -0,0 +1,45 @@ +using BPA.Component.DTOCommon.BaseDTOs; +using BPA.SaaS.Material.Api.DTO; +using BPA.SaaS.Material.Api.IService; + +namespace BPA.SaaS.Material.Api.Service +{ + public class UnitService : IUnitService + { + + + public UnitService() + { + } + + public Task>> GetUnitList() + { + throw new NotImplementedException(); + } + + public Task> PostType(UnitDtoQuery dto) + { + throw new NotImplementedException(); + } + + public Task> AddType(UnitDto dto) + { + throw new NotImplementedException(); + } + + public Task> UpdateType(UnitDto dto) + { + throw new NotImplementedException(); + } + + public Task> DeleteType(string id) + { + throw new NotImplementedException(); + } + + public Task> BatchDeleteType(string[] Ids) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.UnitTest/BPA.SaaS.Material.Api.UnitTest.csproj b/src/BPA.SaaS.Material.Api.UnitTest/BPA.SaaS.Material.Api.UnitTest.csproj index e1015fb..c835ef1 100644 --- a/src/BPA.SaaS.Material.Api.UnitTest/BPA.SaaS.Material.Api.UnitTest.csproj +++ b/src/BPA.SaaS.Material.Api.UnitTest/BPA.SaaS.Material.Api.UnitTest.csproj @@ -1,7 +1,20 @@ - - -net6.0 -enable -enable - + + + net6.0 + enable + enable + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.UnitTest/BaseUnitTest.cs b/src/BPA.SaaS.Material.Api.UnitTest/BaseUnitTest.cs new file mode 100644 index 0000000..080e693 --- /dev/null +++ b/src/BPA.SaaS.Material.Api.UnitTest/BaseUnitTest.cs @@ -0,0 +1,52 @@ +using Autofac; +using Autofac.Extensions.DependencyInjection; +using BPA.Component.ApolloClient; +using BPA.SaaS.Material.Api.Bootstrap; +using BPA.SaaS.Material.Api.Model; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace BPA.SaaS.Material.Api.UnitTest +{ + /// + /// 测试基类 + /// + public class BaseUnitTest + { +#pragma warning disable CS8618 + private static ILifetimeScope lifetimeScope; +#pragma warning restore CS8618 + + public BaseUnitTest() + { + var builder = new ConfigurationBuilder(); + builder.AddEnvironmentVariables(); + Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); + Environment.SetEnvironmentVariable("APOLLO_META_SERVER_URL", "http://10.2.1.21:28080"); + Environment.SetEnvironmentVariable("APOLLO_COMMON_NAMESPACE", "DEV.Common"); + Environment.SetEnvironmentVariable("APP_NAME", "BPA.SaaS.Basic.Api"); + var configuration = builder.Build(); + configuration.SetupConfiguration(); + + var services = new ServiceCollection(); + services.AddApollo(); + services.AddBPAMiddlewareServices(new[] {"null arg"}); + + var factory = new AutofacServiceProviderFactory(); + var containerBuild = factory.CreateBuilder(services); + containerBuild.SetupConfigureContainer(); + containerBuild.RegisterBuildCallback(scope => lifetimeScope = scope); + var serviceProvider = factory.CreateServiceProvider(containerBuild); + serviceProvider.PreHeatRun(); + + // var db = serviceProvider.CreateScope().ServiceProvider.GetService(); + } + + /// + /// 从容器中获取对象 + /// + /// + /// + protected static T GetService() where T : notnull => lifetimeScope.Resolve(); + } +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.WebApi/BPA.SaaS.Material.Api.WebApi.csproj b/src/BPA.SaaS.Material.Api.WebApi/BPA.SaaS.Material.Api.WebApi.csproj index 10404b5..ad8a930 100644 --- a/src/BPA.SaaS.Material.Api.WebApi/BPA.SaaS.Material.Api.WebApi.csproj +++ b/src/BPA.SaaS.Material.Api.WebApi/BPA.SaaS.Material.Api.WebApi.csproj @@ -1,4 +1,4 @@ - + net6.0 diff --git a/src/BPA.SaaS.Material.Api.WebApi/Program.cs b/src/BPA.SaaS.Material.Api.WebApi/Program.cs index e2d1548..d790aab 100644 --- a/src/BPA.SaaS.Material.Api.WebApi/Program.cs +++ b/src/BPA.SaaS.Material.Api.WebApi/Program.cs @@ -1,8 +1,38 @@ +using Autofac; +using Autofac.Extensions.DependencyInjection; +using BPA.Component.WebApiExtensions.Extensions; + using BPA.SaaS.Material.Api.Bootstrap; +using Microsoft.AspNetCore.Mvc; + var builder = WebApplication.CreateBuilder(args); -builder.Services.AddControllers(); +builder.Configuration.SetupConfiguration(); + +// DI +builder.Services.AddBPAMiddlewareServices(args); + +// DI config +builder.Services.Configure(options => { options.AddDefaultBehaviorOptions(); }); + +// Use Autofac +builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); +builder.Host.ConfigureContainer(containerBuilder => { containerBuilder.SetupConfigureContainer(); }); var app = builder.Build(); +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +// Use module +app.UseRouting(); app.UseHttpsRedirection(); app.UseAuthorization(); +app.UseBpaHealthChecks(); app.MapControllers(); -app.Run(); +app.UseGlobalErrorHandler(); + +// PreRun +((IApplicationBuilder) app).ApplicationServices.PreHeatRun(); + +app.Run(); \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.WebApi/Properties/launchSettings.json b/src/BPA.SaaS.Material.Api.WebApi/Properties/launchSettings.json index 2127f25..c7bd62e 100644 --- a/src/BPA.SaaS.Material.Api.WebApi/Properties/launchSettings.json +++ b/src/BPA.SaaS.Material.Api.WebApi/Properties/launchSettings.json @@ -1,30 +1,17 @@ { "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:11305", - "sslPort": 44315 - } - }, "profiles": { - "BPA.SaaS.Stock.Api": { + "BPA.SaaS.Basic.Api": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "weatherforecast", - "applicationUrl": "https://localhost:7047;http://localhost:5047", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5051", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Development", + "APOLLO_META_SERVER_URL": "http://10.2.1.21:28080", + "APOLLO_COMMON_NAMESPACE": "DEV.Common", + "APP_NAME": "BPA.SaaS.Material.Api" } } } diff --git a/src/BPA.SaaS.Material.Api.WebApi/UnitController.cs b/src/BPA.SaaS.Material.Api.WebApi/UnitController.cs new file mode 100644 index 0000000..a5ae35c --- /dev/null +++ b/src/BPA.SaaS.Material.Api.WebApi/UnitController.cs @@ -0,0 +1,25 @@ +using BPA.Component.DTOCommon.BaseDTOs; +using BPA.SaaS.Material.Api.DTO; +using BPA.SaaS.Material.Api.IService; +using Microsoft.AspNetCore.Mvc; + +namespace BPA.SaaS.Material.Api.WebApi; + +[ApiController] +[Route("api/[controller]/[action]")] +public class UnitController : ControllerBase +{ + private readonly IUnitService _unitService; + + public UnitController(IUnitService unitService) + { + _unitService = unitService; + } + + /// + /// 获取单位列表 + /// + /// + [HttpGet] + public Task>> GetUnitList() => _unitService.GetUnitList(); +} \ No newline at end of file diff --git a/src/BPA.SaaS.Material.Api.WebApi/appsettings.Development.json b/src/BPA.SaaS.Material.Api.WebApi/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/src/BPA.SaaS.Material.Api.WebApi/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/src/BPA.SaaS.Material.Api.WebApi/appsettings.json b/src/BPA.SaaS.Material.Api.WebApi/appsettings.json deleted file mode 100644 index 10f68b8..0000000 --- a/src/BPA.SaaS.Material.Api.WebApi/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -}