change - Add SqlSugar

master
wenjy 1 year ago
parent ec7fd0f6f6
commit 97f16b5377

@ -0,0 +1,28 @@
using Microsoft.Extensions.Options;
using System;
namespace Durkee.Mes.Api.Config
{
/// <summary>
/// 系统配置
/// </summary>
public class AppConfig : IOptions<AppConfig>
{
/// <summary>
/// 日志文件路径
/// </summary>
public string logPath { get; set; }
/// <summary>
/// MES 数据库连接字符串
/// </summary>
public string mesConnStr { get; set; }
/// <summary>
/// MCS 数据库连接字符串
/// </summary>
public string mcsConnStr { get; set; }
public AppConfig Value => this;
}
}

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
</ItemGroup>
</Project>

@ -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
{
/// <summary>
/// 主键标识
///</summary>
[SugarColumn(ColumnName = "OBJ_ID", IsPrimaryKey = true, IsIdentity = true)]
public int objId { get; set; }
/// <summary>
/// BOM编号
/// </summary>
[SugarColumn(ColumnName = "BOM_CODE")]
public string bomCode { get; set; }
/// <summary>
/// 物料编号
/// </summary>
[SugarColumn(ColumnName = "MATERIAL_CODE")]
public string materialCode { get; set; }
/// <summary>
/// 物料名称
/// </summary>
[SugarColumn(ColumnName = "MATERIAL_NAME")]
public string materialName { get; set; }
/// <summary>
/// 物料类别
/// </summary>
[SugarColumn(ColumnName = "MATERIAL_TYPE")]
public string materialType { get; set; }
/// <summary>
/// 标准数量
/// </summary>
[SugarColumn(ColumnName = "STANDARD_AMOUNT")]
public int standardAmount { get; set; }
/// <summary>
/// 父级编号
/// </summary>
[SugarColumn(ColumnName = "PARENT_ID")]
public string parentId { get; set; }
/// <summary>
/// 工厂编号
/// </summary>
[SugarColumn(ColumnName = "PLANT_CODE")]
public string plantCode { get; set; }
/// <summary>
/// 产线/工位
/// </summary>
[SugarColumn(ColumnName = "PRODUCT_LINE_CODE")]
public string productLineCode { get; set; }
/// <summary>
/// 是否标识
/// </summary>
[SugarColumn(ColumnName = "IS_FLAG")]
public int isFlag { get; set; }
}
}

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.112" />
</ItemGroup>
</Project>

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.112" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj" />
<ProjectReference Include="..\Durkee.Mes.Api.Model\Durkee.Mes.Api.Model.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,25 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
namespace Durkee.Mes.Api.Repository
{
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能
public Repository(ISqlSugarClient db)
{
itenant = db.AsTenant();//用来处理事务
base.Context = db.AsTenant().GetConnectionScopeWithAttr<T>();//获取子Db
//如果不想通过注入多个仓储
//用到ChangeRepository或者Db.GetMyRepository需要看标题4写法
}
}
}

@ -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<BaseBomInfo> GetBomInfos();
}
}

@ -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<BaseBomInfo> _bomInfoRepository;
public BaseBomInfoServiceImpl(Repository<BaseBomInfo> bomInfoRepository)
{
_bomInfoRepository = bomInfoRepository;
}
public List<BaseBomInfo> GetBomInfos()
{
try
{
var info = _bomInfoRepository.GetList();
return info;
}
catch (Exception ex)
{
//logHelper.Error("获取BOM集合异常", ex);
return null;
}
}
}
}

@ -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

@ -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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
private IBaseBomInfoService _service;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IBaseBomInfoService service)
{
_logger = logger;
_service = service;
}
[HttpGet]
public IEnumerable<WeatherForecast> 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;
}
}
}

@ -12,5 +12,10 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj" />
<ProjectReference Include="..\Durkee.Mes.Api.Repository\Durkee.Mes.Api.Repository.csproj" />
</ItemGroup>
</Project>

@ -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) =>

@ -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<AppConfig>(provider =>
{
var configuration = provider.GetService<IConfiguration>();
return configuration.GetSection("AppConfig").Get<AppConfig>();
});
//注册SqlSugar
services.AddSingleton<ISqlSugarClient>(x =>
{
var appConfig = x.GetService<AppConfig>();
SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
{
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<Repository<BaseBomInfo>>();
services.AddSingleton<IBaseBomInfoService, BaseBomInfoServiceImpl>();
}
// 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<AppConfig>();
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}");
}
}
}

@ -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"
}
}

Loading…
Cancel
Save