add - 添加Repository仓储模块

dev
wenjy 4 weeks ago
parent e106fc862b
commit 8b3603d0ec

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2026 WenJY
* CLR4.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
{
/// <summary>
/// Sql连接配置
/// </summary>
public class SqlConfig
{
/// <summary>
/// Sql 配置ID实体通过该ID关联数据源
/// </summary>
public string configId { get; set; }
/// <summary>
/// 数据库类型MySql-0;SqlServer-1;Sqlite-2;Oracle-3
/// </summary>
public int dbType { get; set; }
/// <summary>
/// 是否启用true-是false-否
/// </summary>
public bool isFlag { get; set; }
/// <summary>
/// 连接字符串
/// </summary>
public string connStr { get; set; }
}
}

@ -8,7 +8,10 @@
<ItemGroup>
<Folder Include="Dto\" />
<Folder Include="Configs\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.214" />
</ItemGroup>
</Project>

@ -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
* CLR4.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<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,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sln.Wcs.Model\Sln.Wcs.Model.csproj" />
</ItemGroup>
</Project>

@ -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
* CLR4.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
{
/// <summary>
/// 注册SqlSugar
/// </summary>
/// <param name="services"></param>
public static void AddSqlSugarSetup(this IServiceCollection services)
{
services.AddSingleton<ISqlSugarClient>(x =>
{
var configuration = x.GetService<IConfiguration>();
List<SqlConfig> sqlConfigs = JsonConvert.DeserializeObject<List<SqlConfig>>(configuration["sqlConfigs"]);
var connectConfigList = new List<ConnectionConfig>();
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;
});
}
}
}

@ -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
* CLR4.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<T> : IBaseService<T> where T : class, new()
{
public readonly Repository<T> _rep;
public BaseServiceImpl(Repository<T> rep)
{
_rep = rep;
}
/// <summary>
/// 添加实体信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
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}");
}
}
/// <summary>
/// 批量添加实体集合
/// </summary>
/// <param name="lisT"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public bool Insert(List<T> 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}");
}
}
/// <summary>
/// 根据id 删除信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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}");
}
}
/// <summary>
/// 根据实体删除信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public bool Delete(T model)
{
if (model == null)
{
throw new ArgumentNullException($"根据实体删除信息异常:实体参数为空");
}
try
{
return _rep.DeleteById(model);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据实体删除信息异常:{ex.Message}");
}
}
/// <summary>
/// 根据实体集合批量删除信息
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Deletes(List<T> entitys)
{
if (entitys == null)
{
throw new ArgumentNullException($"根据实体集合批量删除信息异常:实体集合参数为空");
}
try
{
return _rep.Delete(entitys);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据实体集合批量删除信息异常:{ex.Message}");
}
}
/// <summary>
/// 根据实体更新信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Update(T model)
{
if (model == null)
{
throw new ArgumentNullException($"根据实体更新信息异常:实体参数为空");
}
try
{
return _rep.Update(model);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据实体更新信息异常:{ex.Message}");
}
}
/// <summary>
/// 批量更新实体集合信息
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Update(List<T> entitys)
{
if (entitys == null)
{
throw new ArgumentNullException($"批量更新实体集合信息异常:实体集合参数为空");
}
try
{
return _rep.UpdateRange(entitys);
}
catch (Exception ex)
{
throw new InvalidOperationException($"批量更新实体集合信息异常:{ex.Message}");
}
}
/// <summary>
/// 根据Where条件更新实体信息
/// </summary>
/// <param name="entity"></param>
/// <param name="strWhere"></param>
/// <returns></returns>
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}");
}
}
/// <summary>
/// 根据实体更新指定列
/// </summary>
/// <param name="entity"></param>
/// <param name="lstColumns"></param>
/// <param name="lstIgnoreColumns"></param>
/// <param name="strWhere"></param>
/// <returns></returns>
public bool Update(T entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null,
string strWhere = "")
{
try
{
IUpdateable<T> 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}");
}
}
/// <summary>
/// 查询所有信息
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<T> Query()
{
try
{
return _rep.GetList();
}
catch (Exception ex)
{
throw new InvalidOperationException($"查询所有信息异常:{ex.Message}");
}
}
/// <summary>
/// 根据Id查询实体
/// </summary>
/// <param name="objId"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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}");
}
}
/// <summary>
/// 根据表达式查询
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<T> Query(Expression<Func<T, bool>> whereExpression)
{
if (whereExpression == null)
{
throw new ArgumentNullException($"根据表达式查询实体信息异常:表达式参数为空");
}
try
{
return _rep.GetList(whereExpression);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据表达式查询实体信息异常:{ex.Message}");
}
}
/// <summary>
/// 根据表达式排序查询
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpression"></param>
/// <param name="isAsc"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<T> Query(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> 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}");
}
}
}
}

@ -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<T> where T : class
{
/// <summary>
/// 添加实体信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
bool Insert(T model);
/// <summary>
/// 批量添加实体集合
/// </summary>
/// <param name="lisT"></param>
/// <returns></returns>
bool Insert(List<T> lisT);
/// <summary>
/// 根据id 删除信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
bool DeleteById(object id);
/// <summary>
/// 根据实体删除信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
bool Delete(T model);
/// <summary>
/// 根据实体集合批量删除信息
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
bool Deletes(List<T> entitys);
/// <summary>
/// 根据实体更新信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
bool Update(T model);
/// <summary>
/// 批量更新实体集合信息
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
bool Update(List<T> entitys);
/// <summary>
/// 根据Where条件更新实体信息
/// </summary>
/// <param name="entity"></param>
/// <param name="strWhere"></param>
/// <returns></returns>
bool Update(T entity, string strWhere);
/// <summary>
/// 根据实体更新指定列
/// </summary>
/// <param name="entity"></param>
/// <param name="lstColumns"></param>
/// <param name="lstIgnoreColumns"></param>
/// <param name="strWhere"></param>
/// <returns></returns>
bool Update(T entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string strWhere = "");
/// <summary>
/// 查询所有信息
/// </summary>
/// <returns></returns>
List<T> Query();
/// <summary>
/// 根据Id查询实体
/// </summary>
/// <param name="objId"></param>
/// <returns></returns>
T Query(object objId);
/// <summary>
/// 根据表达式查询
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
List<T> Query(Expression<Func<T, bool>> whereExpression);
/// <summary>
/// 根据表达式排序查询
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="orderByExpression">排序条件</param>
/// <param name="isAsc">是否正序</param>
/// <returns></returns>
List<T> Query(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderByExpression,
bool isAsc = true);
}
}

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

@ -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<IConfiguration>();
var log = serviceProvider.GetService<SerilogHelper>();
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();
}
}

@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\Sln.Wcs.HikRoBotApi\Sln.Wcs.HikRoBotApi.csproj" />
<ProjectReference Include="..\Sln.Wcs.HikRoBotSdk\Sln.Wcs.HikRoBotSdk.csproj" />
<ProjectReference Include="..\Sln.Wcs.Repository\Sln.Wcs.Repository.csproj" />
<ProjectReference Include="..\Sln.Wcs.Serilog\Sln.Wcs.Serilog.csproj" />
</ItemGroup>

Loading…
Cancel
Save