From 97f16b5377a423cae8c9c20733a87e9a000e62f0 Mon Sep 17 00:00:00 2001 From: wenjy Date: Thu, 26 Oct 2023 17:54:44 +0800 Subject: [PATCH] change - Add SqlSugar --- Durkee.Mes.Api.Config/AppConfig.cs | 28 ++++++++ .../Durkee.Mes.Api.Config.csproj | 12 ++++ Durkee.Mes.Api.Model/BaseBomInfo.cs | 72 +++++++++++++++++++ .../Durkee.Mes.Api.Model.csproj | 11 +++ .../Durkee.Mes.Api.Repository.csproj | 18 +++++ Durkee.Mes.Api.Repository/Repository.cs | 25 +++++++ .../service/IBaseBomInfoService.cs | 12 ++++ .../service/Impl/BaseBomInfoServiceImpl.cs | 31 ++++++++ Durkee.Mes.Api.sln | 18 +++++ .../Controllers/WeatherForecastController.cs | 34 ++++++--- Durkee.Mes.Api/Durkee.Mes.Api.csproj | 5 ++ Durkee.Mes.Api/Program.cs | 2 - Durkee.Mes.Api/Startup.cs | 61 ++++++++++++++-- Durkee.Mes.Api/appsettings.json | 7 +- 14 files changed, 318 insertions(+), 18 deletions(-) create mode 100644 Durkee.Mes.Api.Config/AppConfig.cs create mode 100644 Durkee.Mes.Api.Config/Durkee.Mes.Api.Config.csproj create mode 100644 Durkee.Mes.Api.Model/BaseBomInfo.cs create mode 100644 Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj create mode 100644 Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj create mode 100644 Durkee.Mes.Api.Repository/Repository.cs create mode 100644 Durkee.Mes.Api.Repository/service/IBaseBomInfoService.cs create mode 100644 Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs diff --git a/Durkee.Mes.Api.Config/AppConfig.cs b/Durkee.Mes.Api.Config/AppConfig.cs new file mode 100644 index 0000000..5cf8c12 --- /dev/null +++ b/Durkee.Mes.Api.Config/AppConfig.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Options; +using System; + +namespace Durkee.Mes.Api.Config +{ + /// + /// 系统配置 + /// + public class AppConfig : IOptions + { + /// + /// 日志文件路径 + /// + public string logPath { get; set; } + + /// + /// MES 数据库连接字符串 + /// + public string mesConnStr { get; set; } + + /// + /// MCS 数据库连接字符串 + /// + public string mcsConnStr { get; set; } + + public AppConfig Value => this; + } +} diff --git a/Durkee.Mes.Api.Config/Durkee.Mes.Api.Config.csproj b/Durkee.Mes.Api.Config/Durkee.Mes.Api.Config.csproj new file mode 100644 index 0000000..d53daaa --- /dev/null +++ b/Durkee.Mes.Api.Config/Durkee.Mes.Api.Config.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + true + + + + + + + diff --git a/Durkee.Mes.Api.Model/BaseBomInfo.cs b/Durkee.Mes.Api.Model/BaseBomInfo.cs new file mode 100644 index 0000000..6665d6e --- /dev/null +++ b/Durkee.Mes.Api.Model/BaseBomInfo.cs @@ -0,0 +1,72 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Security.Principal; +using System.Text; + +namespace Durkee.Mes.Api.Model +{ + [SugarTable("BASE_BOMINFO","mes")] + public class BaseBomInfo + { + /// + /// 主键标识 + /// + [SugarColumn(ColumnName = "OBJ_ID", IsPrimaryKey = true, IsIdentity = true)] + public int objId { get; set; } + + /// + /// BOM编号 + /// + [SugarColumn(ColumnName = "BOM_CODE")] + public string bomCode { get; set; } + + /// + /// 物料编号 + /// + [SugarColumn(ColumnName = "MATERIAL_CODE")] + public string materialCode { get; set; } + + /// + /// 物料名称 + /// + [SugarColumn(ColumnName = "MATERIAL_NAME")] + public string materialName { get; set; } + + /// + /// 物料类别 + /// + [SugarColumn(ColumnName = "MATERIAL_TYPE")] + public string materialType { get; set; } + + /// + /// 标准数量 + /// + [SugarColumn(ColumnName = "STANDARD_AMOUNT")] + public int standardAmount { get; set; } + + /// + /// 父级编号 + /// + [SugarColumn(ColumnName = "PARENT_ID")] + public string parentId { get; set; } + + /// + /// 工厂编号 + /// + [SugarColumn(ColumnName = "PLANT_CODE")] + public string plantCode { get; set; } + + /// + /// 产线/工位 + /// + [SugarColumn(ColumnName = "PRODUCT_LINE_CODE")] + public string productLineCode { get; set; } + + /// + /// 是否标识 + /// + [SugarColumn(ColumnName = "IS_FLAG")] + public int isFlag { get; set; } + } +} diff --git a/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj b/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj new file mode 100644 index 0000000..82a7fbe --- /dev/null +++ b/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj b/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj new file mode 100644 index 0000000..9d040d7 --- /dev/null +++ b/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp3.1 + true + + + + + + + + + + + + + diff --git a/Durkee.Mes.Api.Repository/Repository.cs b/Durkee.Mes.Api.Repository/Repository.cs new file mode 100644 index 0000000..5ef0bc1 --- /dev/null +++ b/Durkee.Mes.Api.Repository/Repository.cs @@ -0,0 +1,25 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Durkee.Mes.Api.Repository +{ + public class Repository : SimpleClient where T : class, new() + { + public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能 + public Repository(ISqlSugarClient db) + { + + itenant = db.AsTenant();//用来处理事务 + base.Context = db.AsTenant().GetConnectionScopeWithAttr();//获取子Db + + + //如果不想通过注入多个仓储 + //用到ChangeRepository或者Db.GetMyRepository需要看标题4写法 + + + } + + } +} \ No newline at end of file diff --git a/Durkee.Mes.Api.Repository/service/IBaseBomInfoService.cs b/Durkee.Mes.Api.Repository/service/IBaseBomInfoService.cs new file mode 100644 index 0000000..178aed9 --- /dev/null +++ b/Durkee.Mes.Api.Repository/service/IBaseBomInfoService.cs @@ -0,0 +1,12 @@ +using Durkee.Mes.Api.Model; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Durkee.Mes.Api.Repository.service +{ + public interface IBaseBomInfoService + { + List GetBomInfos(); + } +} diff --git a/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs b/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs new file mode 100644 index 0000000..80409b9 --- /dev/null +++ b/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs @@ -0,0 +1,31 @@ +using Durkee.Mes.Api.Model; +using Microsoft.IdentityModel.Logging; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Durkee.Mes.Api.Repository.service.Impl +{ + public class BaseBomInfoServiceImpl : IBaseBomInfoService + { + private Repository _bomInfoRepository; + public BaseBomInfoServiceImpl(Repository bomInfoRepository) + { + _bomInfoRepository = bomInfoRepository; + } + + public List GetBomInfos() + { + try + { + var info = _bomInfoRepository.GetList(); + return info; + } + catch (Exception ex) + { + //logHelper.Error("获取BOM集合异常", ex); + return null; + } + } + } +} diff --git a/Durkee.Mes.Api.sln b/Durkee.Mes.Api.sln index f172e5d..3fb184f 100644 --- a/Durkee.Mes.Api.sln +++ b/Durkee.Mes.Api.sln @@ -5,6 +5,12 @@ VisualStudioVersion = 17.5.33502.453 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api", "Durkee.Mes.Api\Durkee.Mes.Api.csproj", "{30BF9893-8860-4267-BDDC-C1D220DF702C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Config", "Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj", "{10B17840-AFCA-4547-AC77-B0F9B2B89C2A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Repository", "Durkee.Mes.Api.Repository\Durkee.Mes.Api.Repository.csproj", "{A6857806-816A-40A3-AFEC-F05F876899BF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Model", "Durkee.Mes.Api.Model\Durkee.Mes.Api.Model.csproj", "{28400313-5B3D-412C-94F4-3DCCF5FDAEE3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +21,18 @@ Global {30BF9893-8860-4267-BDDC-C1D220DF702C}.Debug|Any CPU.Build.0 = Debug|Any CPU {30BF9893-8860-4267-BDDC-C1D220DF702C}.Release|Any CPU.ActiveCfg = Release|Any CPU {30BF9893-8860-4267-BDDC-C1D220DF702C}.Release|Any CPU.Build.0 = Release|Any CPU + {10B17840-AFCA-4547-AC77-B0F9B2B89C2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10B17840-AFCA-4547-AC77-B0F9B2B89C2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10B17840-AFCA-4547-AC77-B0F9B2B89C2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10B17840-AFCA-4547-AC77-B0F9B2B89C2A}.Release|Any CPU.Build.0 = Release|Any CPU + {A6857806-816A-40A3-AFEC-F05F876899BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A6857806-816A-40A3-AFEC-F05F876899BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A6857806-816A-40A3-AFEC-F05F876899BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A6857806-816A-40A3-AFEC-F05F876899BF}.Release|Any CPU.Build.0 = Release|Any CPU + {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs index 8eb7392..271e007 100644 --- a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs +++ b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs @@ -1,4 +1,7 @@ -using Microsoft.AspNetCore.Mvc; +using Durkee.Mes.Api.Config; +using Durkee.Mes.Api.Repository; +using Durkee.Mes.Api.Repository.service; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -11,6 +14,7 @@ namespace Durkee.Mes.Api.Controllers [Route("[controller]")] public class WeatherForecastController : ControllerBase { + private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" @@ -18,23 +22,33 @@ namespace Durkee.Mes.Api.Controllers private readonly ILogger _logger; - public WeatherForecastController(ILogger logger) + private IBaseBomInfoService _service; + + public WeatherForecastController(ILogger logger, IBaseBomInfoService service) { _logger = logger; + _service = service; } [HttpGet] public IEnumerable Get() { - var rng = new Random(); - return Enumerable.Range(1, 5).Select(index => new WeatherForecast + try { - Date = DateTime.Now.AddDays(index), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }) - .ToArray(); - + var info = _service.GetBomInfos(); + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + }catch(Exception ex) + { + _logger.LogError("异常", ex); + } + return null; } } } diff --git a/Durkee.Mes.Api/Durkee.Mes.Api.csproj b/Durkee.Mes.Api/Durkee.Mes.Api.csproj index 1bcb835..5bb8ee9 100644 --- a/Durkee.Mes.Api/Durkee.Mes.Api.csproj +++ b/Durkee.Mes.Api/Durkee.Mes.Api.csproj @@ -12,5 +12,10 @@ + + + + + diff --git a/Durkee.Mes.Api/Program.cs b/Durkee.Mes.Api/Program.cs index d7914eb..2dc26ce 100644 --- a/Durkee.Mes.Api/Program.cs +++ b/Durkee.Mes.Api/Program.cs @@ -13,8 +13,6 @@ namespace Durkee.Mes.Api public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); - - Log.CloseAndFlush(); } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/Durkee.Mes.Api/Startup.cs b/Durkee.Mes.Api/Startup.cs index 75650c1..c3c7da7 100644 --- a/Durkee.Mes.Api/Startup.cs +++ b/Durkee.Mes.Api/Startup.cs @@ -1,12 +1,20 @@ +using Durkee.Mes.Api.Config; +using Durkee.Mes.Api.Model; +using Durkee.Mes.Api.Repository; +using Durkee.Mes.Api.Repository.service; +using Durkee.Mes.Api.Repository.service.Impl; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.IdentityModel.Logging; using Microsoft.OpenApi.Models; using Serilog; using Serilog.Events; +using SqlSugar; using System; +using System.Collections.Generic; using System.IO; namespace Durkee.Mes.Api @@ -30,6 +38,46 @@ namespace Durkee.Mes.Api { swagger.SwaggerDoc("v1", new OpenApiInfo { Title = "ſMES Web Api", Version = "v1" }); }); + + //ע + services.AddSingleton(provider => + { + var configuration = provider.GetService(); + return configuration.GetSection("AppConfig").Get(); + }); + + //עSqlSugar + services.AddSingleton(x => + { + var appConfig = x.GetService(); + SqlSugarScope Db = new SqlSugarScope(new List() + { + new ConnectionConfig() + { + ConfigId = "mes", + DbType = DbType.Oracle, + ConnectionString = appConfig.mesConnStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + }, + new ConnectionConfig() + { + ConfigId = Guid.NewGuid(), + DbType = DbType.Oracle, + ConnectionString = appConfig.mcsConnStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + } + }, + db => + { + db.Aop.OnLogExecuting = (sql, pars) => { }; + }); + + return Db; + }); + services.AddSingleton>(); + services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -50,12 +98,14 @@ namespace Durkee.Mes.Api //c.DisplayRequestDuration(); // ʾʱ䣨ѡ }); + //Serilogм app.UseSerilogRequestLogging(); - //Log.Logger = new LoggerConfiguration() - // .MinimumLevel.Information().WriteTo.Console() - // .WriteTo.File($"Logs/{DateTime.UtcNow:yyyyMMdd}/.txt", rollingInterval: RollingInterval.Day) - // .CreateLogger(); - var logPath = $"E://־Ϣ/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; + + #region ͨļȡ־λ + var appConfig = app.ApplicationServices.GetService(); + var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; + #endregion + Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console() .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information, fileSizeLimitBytes: 5*1024) .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error, fileSizeLimitBytes: 5 * 1024) @@ -76,6 +126,7 @@ namespace Durkee.Mes.Api }); Log.Information($"Ŀʼ,־·{logPath}"); + } } } diff --git a/Durkee.Mes.Api/appsettings.json b/Durkee.Mes.Api/appsettings.json index d9d9a9b..011f0e2 100644 --- a/Durkee.Mes.Api/appsettings.json +++ b/Durkee.Mes.Api/appsettings.json @@ -6,5 +6,10 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "AppConfig": { + "logPath": "E:/代码生成/日志信息", + "mesConnStr": "Data Source=175.27.215.92/helowin;User ID=aucma_mes;Password=aucma", + "mcsConnStr": "Data Source=175.27.215.92/helowin;User ID=aucma_scada;Password=aucma" + } }