From ee9bc11e996d8dd1b41f378f2e8b0e9609d7bd12 Mon Sep 17 00:00:00 2001 From: wenjy Date: Mon, 8 Apr 2024 09:59:12 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E6=B7=BB=E5=8A=A0Service=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/IBaseMaterialService.cs | 3 +- .../service/IBaseUserService.cs | 3 +- .../service/Impl/BaseMaterialServiceImpl.cs | 16 +- .../service/Impl/BaseUserServiceImpl.cs | 18 +- .../service/base/BaseServiceImpl.cs | 358 ++++++++++++++++++ .../service/base/IBaseService.cs | 106 ++++++ 6 files changed, 483 insertions(+), 21 deletions(-) create mode 100644 SlnMesnac.Repository/service/base/BaseServiceImpl.cs create mode 100644 SlnMesnac.Repository/service/base/IBaseService.cs diff --git a/SlnMesnac.Repository/service/IBaseMaterialService.cs b/SlnMesnac.Repository/service/IBaseMaterialService.cs index ac4f62f..5e3e59e 100644 --- a/SlnMesnac.Repository/service/IBaseMaterialService.cs +++ b/SlnMesnac.Repository/service/IBaseMaterialService.cs @@ -1,4 +1,5 @@ using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; using System; using System.Collections.Generic; using System.Text; @@ -27,7 +28,7 @@ using System.Text; #endregion << 版 本 注 释 >> namespace SlnMesnac.Repository.service { - public interface IBaseMaterialService + public interface IBaseMaterialService : IBaseService { /// diff --git a/SlnMesnac.Repository/service/IBaseUserService.cs b/SlnMesnac.Repository/service/IBaseUserService.cs index bac80e5..d1ece41 100644 --- a/SlnMesnac.Repository/service/IBaseUserService.cs +++ b/SlnMesnac.Repository/service/IBaseUserService.cs @@ -1,4 +1,5 @@ using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; using System; using System.Collections.Generic; using System.Text; @@ -27,7 +28,7 @@ using System.Text; #endregion << 版 本 注 释 >> namespace SlnMesnac.Repository.service { - public interface IBaseUserService + public interface IBaseUserService:IBaseService { /// /// 获取用户信息 diff --git a/SlnMesnac.Repository/service/Impl/BaseMaterialServiceImpl.cs b/SlnMesnac.Repository/service/Impl/BaseMaterialServiceImpl.cs index 7487cd3..82c5e2f 100644 --- a/SlnMesnac.Repository/service/Impl/BaseMaterialServiceImpl.cs +++ b/SlnMesnac.Repository/service/Impl/BaseMaterialServiceImpl.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; +using SlnMesnac.Repository.service.@base; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- @@ -30,15 +31,12 @@ using System.Text; #endregion << 版 本 注 释 >> namespace SlnMesnac.Repository.service.Impl { - public class BaseMaterialServiceImpl : IBaseMaterialService + public class BaseMaterialServiceImpl : BaseServiceImpl, IBaseMaterialService { - private Repository _repository; - private ILogger _logger; - public BaseMaterialServiceImpl(Repository repository, ILogger logger) + public BaseMaterialServiceImpl(Repository repository, ILogger logger):base(repository) { - _repository = repository; _logger = logger; } @@ -52,7 +50,7 @@ namespace SlnMesnac.Repository.service.Impl BaseMaterialInfo materialInfo = null; try { - materialInfo = _repository.GetFirst(x => x.MaterialCode == materialCode); + materialInfo = base._rep.GetFirst(x => x.MaterialCode == materialCode); } catch (Exception ex) { @@ -71,7 +69,7 @@ namespace SlnMesnac.Repository.service.Impl BaseMaterialInfo materialInfo = null; try { - materialInfo = _repository.GetFirst(x => x.SAPMaterialCode == sapMaterialCode); + materialInfo = base._rep.GetFirst(x => x.SAPMaterialCode == sapMaterialCode); } catch (Exception ex) { @@ -89,7 +87,7 @@ namespace SlnMesnac.Repository.service.Impl List materialInfos = null; try { - materialInfos = _repository.GetList(); + materialInfos = base._rep.GetList(); } catch (Exception ex) { @@ -120,7 +118,7 @@ namespace SlnMesnac.Repository.service.Impl exp = exp.And(x => x.MinorTypeID == minorTypeId); } - materialInfos = _repository.GetList(exp); + materialInfos = base._rep.GetList(exp); } catch (Exception ex) { diff --git a/SlnMesnac.Repository/service/Impl/BaseUserServiceImpl.cs b/SlnMesnac.Repository/service/Impl/BaseUserServiceImpl.cs index d2d99ef..ee2b511 100644 --- a/SlnMesnac.Repository/service/Impl/BaseUserServiceImpl.cs +++ b/SlnMesnac.Repository/service/Impl/BaseUserServiceImpl.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; using System; using System.Collections.Generic; using System.Text; @@ -28,16 +29,13 @@ using System.Text; #endregion << 版 本 注 释 >> namespace SlnMesnac.Repository.service.Impl { - public class BaseUserServiceImpl : IBaseUserService + public class BaseUserServiceImpl : BaseServiceImpl,IBaseUserService { private readonly ILogger _logger; - private readonly Repository _rep; - - public BaseUserServiceImpl(ILogger logger, Repository rep) + public BaseUserServiceImpl(Repository rep,ILogger logger) :base(rep) { _logger = logger; - _rep = rep; } @@ -46,7 +44,7 @@ namespace SlnMesnac.Repository.service.Impl List users = null; try { - users = _rep.GetList(); + users = base._rep.GetList(); } catch (Exception ex) { @@ -60,15 +58,15 @@ namespace SlnMesnac.Repository.service.Impl bool result = false; try { - _rep.AsTenant().BeginTran(); + base._rep.AsTenant().BeginTran(); - result = _rep.InsertRange(users); + result = base._rep.InsertRange(users); - _rep.AsTenant().CommitTran(); + base._rep.AsTenant().CommitTran(); } catch (Exception ex) { - _rep.AsTenant().RollbackTran(); + base._rep.AsTenant().RollbackTran(); _logger.LogError($"用户信息添加异常:{ex.Message}"); } return result; diff --git a/SlnMesnac.Repository/service/base/BaseServiceImpl.cs b/SlnMesnac.Repository/service/base/BaseServiceImpl.cs new file mode 100644 index 0000000..2227110 --- /dev/null +++ b/SlnMesnac.Repository/service/base/BaseServiceImpl.cs @@ -0,0 +1,358 @@ +using Microsoft.Extensions.Logging; +using SlnMesnac.Repository.service.@base; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Repository.service.Impl +* 唯一标识:70cc8c3a-2c3b-4034-894b-f1a4f04aa21e +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-08 09:49:07 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Repository.service.@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.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.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/SlnMesnac.Repository/service/base/IBaseService.cs b/SlnMesnac.Repository/service/base/IBaseService.cs new file mode 100644 index 0000000..029ac99 --- /dev/null +++ b/SlnMesnac.Repository/service/base/IBaseService.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Text; + +namespace SlnMesnac.Repository.service.@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); + } +}