From 8b3603d0ec6abe72018cc68bed0c955a362ae916 Mon Sep 17 00:00:00 2001 From: wenjy Date: Thu, 19 Mar 2026 11:30:58 +0800 Subject: [PATCH] =?UTF-8?q?add=20-=20=E6=B7=BB=E5=8A=A0Repository=E4=BB=93?= =?UTF-8?q?=E5=82=A8=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sln.Wcs.Model/Configs/SqlConfig.cs | 56 +++ Sln.Wcs.Model/Sln.Wcs.Model.csproj | 5 +- Sln.Wcs.Repository/Repository.cs | 45 +++ Sln.Wcs.Repository/Sln.Wcs.Repository.csproj | 18 + Sln.Wcs.Repository/SqlsugarSetup.cs | 77 ++++ Sln.Wcs.Repository/base/BaseServiceImpl.cs | 371 +++++++++++++++++++ Sln.Wcs.Repository/base/IBaseService.cs | 109 ++++++ Sln.Wcs.sln | 6 + Sln.Wcs/Program.cs | 9 +- Sln.Wcs/Sln.Wcs.csproj | 1 + 10 files changed, 695 insertions(+), 2 deletions(-) create mode 100644 Sln.Wcs.Model/Configs/SqlConfig.cs create mode 100644 Sln.Wcs.Repository/Repository.cs create mode 100644 Sln.Wcs.Repository/Sln.Wcs.Repository.csproj create mode 100644 Sln.Wcs.Repository/SqlsugarSetup.cs create mode 100644 Sln.Wcs.Repository/base/BaseServiceImpl.cs create mode 100644 Sln.Wcs.Repository/base/IBaseService.cs diff --git a/Sln.Wcs.Model/Configs/SqlConfig.cs b/Sln.Wcs.Model/Configs/SqlConfig.cs new file mode 100644 index 0000000..8a50848 --- /dev/null +++ b/Sln.Wcs.Model/Configs/SqlConfig.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2026 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:Sln.Wcs.Model.Configs +* 唯一标识:f0583d35-2981-40a9-a8e7-bdd3d62730e2 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2026-03-19 11:15:30 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace Sln.Wcs.Model.Configs +{ + /// + /// Sql连接配置 + /// + public class SqlConfig + { + /// + /// Sql 配置ID,实体通过该ID关联数据源 + /// + public string configId { get; set; } + + /// + /// 数据库类型,MySql-0;SqlServer-1;Sqlite-2;Oracle-3 + /// + public int dbType { get; set; } + + /// + /// 是否启用:true-是;false-否 + /// + public bool isFlag { get; set; } + + /// + /// 连接字符串 + /// + public string connStr { get; set; } + } +} diff --git a/Sln.Wcs.Model/Sln.Wcs.Model.csproj b/Sln.Wcs.Model/Sln.Wcs.Model.csproj index 040a94a..c7bcc6e 100644 --- a/Sln.Wcs.Model/Sln.Wcs.Model.csproj +++ b/Sln.Wcs.Model/Sln.Wcs.Model.csproj @@ -8,7 +8,10 @@ - + + + + diff --git a/Sln.Wcs.Repository/Repository.cs b/Sln.Wcs.Repository/Repository.cs new file mode 100644 index 0000000..4c82bfc --- /dev/null +++ b/Sln.Wcs.Repository/Repository.cs @@ -0,0 +1,45 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2026 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:Sln.Wcs.Repository +* 唯一标识:ba49de90-3c8c-447a-889a-a5fc34514709 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2026-03-19 11:27:11 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace Sln.Wcs.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写法 + } + } +} diff --git a/Sln.Wcs.Repository/Sln.Wcs.Repository.csproj b/Sln.Wcs.Repository/Sln.Wcs.Repository.csproj new file mode 100644 index 0000000..a8b302e --- /dev/null +++ b/Sln.Wcs.Repository/Sln.Wcs.Repository.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/Sln.Wcs.Repository/SqlsugarSetup.cs b/Sln.Wcs.Repository/SqlsugarSetup.cs new file mode 100644 index 0000000..77d6671 --- /dev/null +++ b/Sln.Wcs.Repository/SqlsugarSetup.cs @@ -0,0 +1,77 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Identity.Client; +using Newtonsoft.Json; +using Sln.Wcs.Model.Configs; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2026 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:Sln.Wcs.Repository +* 唯一标识:705f0413-0af8-472c-adea-cf7b42540220 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2026-03-19 11:17:45 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace Sln.Wcs.Repository +{ + public static class SqlsugarSetup + { + /// + /// 注册SqlSugar + /// + /// + public static void AddSqlSugarSetup(this IServiceCollection services) + { + services.AddSingleton(x => + { + var configuration = x.GetService(); + List sqlConfigs = JsonConvert.DeserializeObject>(configuration["sqlConfigs"]); + var connectConfigList = new List(); + if (sqlConfigs != null) + { + foreach (var item in sqlConfigs) + { + if (item.isFlag) + { + var config = new ConnectionConfig() + { + ConfigId = item.configId, + DbType = (DbType)item.dbType, + ConnectionString = item.connStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + }; + connectConfigList.Add(config); + } + } + } + + SqlSugarScope Db = + new SqlSugarScope(connectConfigList, db => { db.Aop.OnLogExecuting = (sql, pars) => { }; }); + + return Db; + }); + } + } +} \ No newline at end of file diff --git a/Sln.Wcs.Repository/base/BaseServiceImpl.cs b/Sln.Wcs.Repository/base/BaseServiceImpl.cs new file mode 100644 index 0000000..6540215 --- /dev/null +++ b/Sln.Wcs.Repository/base/BaseServiceImpl.cs @@ -0,0 +1,371 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2026 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:Sln.Wcs.Repository.base +* 唯一标识:d7a87113-4748-4683-8291-a0a724f94e44 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2026-03-19 11:28:36 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace Sln.Wcs.Repository.@base +{ + public class BaseServiceImpl : IBaseService where T : class, new() + { + public readonly Repository _rep; + + public BaseServiceImpl(Repository rep) + { + _rep = rep; + } + + /// + /// 添加实体信息 + /// + /// + /// + /// + /// + public bool Insert(T model) + { + if (model == null) + { + throw new ArgumentNullException($"添加实体信息异常:实体参数为空"); + } + + try + { + return _rep.CopyNew().Insert(model); + } + catch (Exception ex) + { + throw new InvalidOperationException($"添加实体信息异常:{ex.Message}"); + } + } + + /// + /// 批量添加实体集合 + /// + /// + /// + /// + /// + public bool Insert(List lisT) + { + if (lisT == null) + { + throw new ArgumentNullException($"批量添加实体集合异常:实体集合参数为空"); + } + + try + { + // _rep.AsTenant().BeginTran(); + var info = _rep.CopyNew().InsertRange(lisT); + // _rep.AsTenant().CommitTran(); + return true; + } + catch (Exception ex) + { + // _rep.AsTenant().RollbackTran(); + throw new InvalidOperationException($"批量添加实体集合异常:{ex.Message}"); + } + } + + /// + /// 根据id 删除信息 + /// + /// + /// + /// + public bool DeleteById(object id) + { + if (id == null) + { + throw new ArgumentNullException($"根据id删除信息异常:Id参数为空"); + } + + try + { + return _rep.DeleteById(id); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据id删除信息异常:{ex.Message}"); + } + } + + /// + /// 根据实体删除信息 + /// + /// + /// + /// + /// + public bool Delete(T model) + { + if (model == null) + { + throw new ArgumentNullException($"根据实体删除信息异常:实体参数为空"); + } + + try + { + return _rep.DeleteById(model); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据实体删除信息异常:{ex.Message}"); + } + } + + /// + /// 根据实体集合批量删除信息 + /// + /// + /// + /// + public bool Deletes(List entitys) + { + if (entitys == null) + { + throw new ArgumentNullException($"根据实体集合批量删除信息异常:实体集合参数为空"); + } + + try + { + return _rep.Delete(entitys); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据实体集合批量删除信息异常:{ex.Message}"); + } + } + + /// + /// 根据实体更新信息 + /// + /// + /// + /// + public bool Update(T model) + { + if (model == null) + { + throw new ArgumentNullException($"根据实体更新信息异常:实体参数为空"); + } + + try + { + return _rep.Update(model); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据实体更新信息异常:{ex.Message}"); + } + } + + /// + /// 批量更新实体集合信息 + /// + /// + /// + /// + public bool Update(List entitys) + { + if (entitys == null) + { + throw new ArgumentNullException($"批量更新实体集合信息异常:实体集合参数为空"); + } + + try + { + return _rep.UpdateRange(entitys); + } + catch (Exception ex) + { + throw new InvalidOperationException($"批量更新实体集合信息异常:{ex.Message}"); + } + } + + /// + /// 根据Where条件更新实体信息 + /// + /// + /// + /// + public bool Update(T entity, string strWhere) + { + if (entity == null) + { + throw new ArgumentNullException($"根据Where条件更新实体信息异常:实体参数为空"); + } + + if (string.IsNullOrEmpty(strWhere)) + { + throw new ArgumentNullException($"根据Where条件更新实体信息异常:Where参数为空"); + } + + try + { + return _rep.AsUpdateable(entity).Where(strWhere).ExecuteCommandHasChange(); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据Where条件更新实体信息异常:{ex.Message}"); + } + } + + /// + /// 根据实体更新指定列 + /// + /// + /// + /// + /// + /// + public bool Update(T entity, List lstColumns = null, List lstIgnoreColumns = null, + string strWhere = "") + { + try + { + IUpdateable up = _rep.AsUpdateable(entity); + if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) + { + up = up.IgnoreColumns(lstIgnoreColumns.ToArray()); + } + + if (lstColumns != null && lstColumns.Count > 0) + { + up = up.UpdateColumns(lstColumns.ToArray()); + } + + if (!string.IsNullOrEmpty(strWhere)) + { + up = up.Where(strWhere); + } + + return up.ExecuteCommandHasChange(); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据实体更新指定列异常:{ex.Message}"); + } + } + + /// + /// 查询所有信息 + /// + /// + /// + public List Query() + { + try + { + return _rep.GetList(); + } + catch (Exception ex) + { + throw new InvalidOperationException($"查询所有信息异常:{ex.Message}"); + } + } + + /// + /// 根据Id查询实体 + /// + /// + /// + /// + public T Query(object objId) + { + if (objId == null) + { + throw new ArgumentNullException($"根据Id查询实体信息异常:Id参数为空"); + } + + try + { + return _rep.GetById(objId); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据Id查询实体信息异常:{ex.Message}"); + } + } + + /// + /// 根据表达式查询 + /// + /// + /// + /// + public List Query(Expression> whereExpression) + { + if (whereExpression == null) + { + throw new ArgumentNullException($"根据表达式查询实体信息异常:表达式参数为空"); + } + + try + { + return _rep.GetList(whereExpression); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据表达式查询实体信息异常:{ex.Message}"); + } + } + + /// + /// 根据表达式排序查询 + /// + /// + /// + /// + /// + /// + public List Query(Expression> whereExpression, Expression> orderByExpression, + bool isAsc = true) + { + if (whereExpression == null) + { + throw new ArgumentNullException($"根据表达式排序查询信息异常:条件表达式参数为空"); + } + + if (orderByExpression == null) + { + throw new ArgumentNullException($"根据表达式排序查询信息异常:排序表达式参数为空"); + } + + try + { + return _rep.AsQueryable() + .OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc) + .WhereIF(whereExpression != null, whereExpression).ToList(); + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据表达式排序查询信息异常:{ex.Message}"); + } + } + } +} diff --git a/Sln.Wcs.Repository/base/IBaseService.cs b/Sln.Wcs.Repository/base/IBaseService.cs new file mode 100644 index 0000000..ada987d --- /dev/null +++ b/Sln.Wcs.Repository/base/IBaseService.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Sln.Wcs.Repository.@base +{ + public interface IBaseService where T : class + { + /// + /// 添加实体信息 + /// + /// + /// + bool Insert(T model); + + /// + /// 批量添加实体集合 + /// + /// + /// + bool Insert(List lisT); + + /// + /// 根据id 删除信息 + /// + /// + /// + bool DeleteById(object id); + + /// + /// 根据实体删除信息 + /// + /// + /// + bool Delete(T model); + + /// + /// 根据实体集合批量删除信息 + /// + /// + /// + bool Deletes(List entitys); + + /// + /// 根据实体更新信息 + /// + /// + /// + bool Update(T model); + + /// + /// 批量更新实体集合信息 + /// + /// + /// + bool Update(List entitys); + + /// + /// 根据Where条件更新实体信息 + /// + /// + /// + /// + bool Update(T entity, string strWhere); + + /// + /// 根据实体更新指定列 + /// + /// + /// + /// + /// + /// + bool Update(T entity, List lstColumns = null, List lstIgnoreColumns = null, string strWhere = ""); + + /// + /// 查询所有信息 + /// + /// + List Query(); + + /// + /// 根据Id查询实体 + /// + /// + /// + T Query(object objId); + + /// + /// 根据表达式查询 + /// + /// + /// + List Query(Expression> whereExpression); + + /// + /// 根据表达式排序查询 + /// + /// 查询条件 + /// 排序条件 + /// 是否正序 + /// + List Query(Expression> whereExpression, Expression> orderByExpression, + bool isAsc = true); + } +} diff --git a/Sln.Wcs.sln b/Sln.Wcs.sln index 129f7e6..f28fc54 100644 --- a/Sln.Wcs.sln +++ b/Sln.Wcs.sln @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sln.Wcs.ElevatorSdk", "SLn. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sln.Wcs.Serilog", "Sln.Wcs.Serilog\Sln.Wcs.Serilog.csproj", "{5EF250AE-58B8-4C39-8F36-A579EA252A5C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sln.Wcs.Repository", "Sln.Wcs.Repository\Sln.Wcs.Repository.csproj", "{549AF273-88BE-4316-88F8-CAD82BC5F1E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -56,6 +58,10 @@ Global {5EF250AE-58B8-4C39-8F36-A579EA252A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU {5EF250AE-58B8-4C39-8F36-A579EA252A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU {5EF250AE-58B8-4C39-8F36-A579EA252A5C}.Release|Any CPU.Build.0 = Release|Any CPU + {549AF273-88BE-4316-88F8-CAD82BC5F1E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {549AF273-88BE-4316-88F8-CAD82BC5F1E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {549AF273-88BE-4316-88F8-CAD82BC5F1E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {549AF273-88BE-4316-88F8-CAD82BC5F1E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Sln.Wcs/Program.cs b/Sln.Wcs/Program.cs index d276f28..ed29ed0 100644 --- a/Sln.Wcs/Program.cs +++ b/Sln.Wcs/Program.cs @@ -1,8 +1,11 @@ using System.Reflection; using Com.Ctrip.Framework.Apollo; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using Sln.Wcs.HikRoBotSdk; +using Sln.Wcs.Model.Configs; +using Sln.Wcs.Repository; using Sln.Wcs.Serilog; namespace Sln.Wcs @@ -21,6 +24,7 @@ namespace Sln.Wcs serviceProvider.UseSerilogExtensions(); var config = serviceProvider.GetService(); var log = serviceProvider.GetService(); + log.Info($"系统启动成功,日志存放位置:{config["logPath"]}"); @@ -54,6 +58,7 @@ namespace Sln.Wcs Assembly[] assemblies = { Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Common.dll")), + Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Repository.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotApi.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotSdk.dll")), }; @@ -65,6 +70,8 @@ namespace Sln.Wcs .WithTransientLifetime()); services.AddSingleton(typeof(SerilogHelper)); + + services.AddSqlSugarSetup(); } } diff --git a/Sln.Wcs/Sln.Wcs.csproj b/Sln.Wcs/Sln.Wcs.csproj index 0a8fea9..f3da775 100644 --- a/Sln.Wcs/Sln.Wcs.csproj +++ b/Sln.Wcs/Sln.Wcs.csproj @@ -28,6 +28,7 @@ +