change - 添加Service基类

pull/3/head
wenjy 9 months ago
parent 57e0d17164
commit ee9bc11e99

@ -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<BaseMaterialInfo>
{
/// <summary>

@ -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<BaseUser>
{
/// <summary>
/// 获取用户信息

@ -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<BaseMaterialInfo>, IBaseMaterialService
{
private Repository<BaseMaterialInfo> _repository;
private ILogger<BaseMaterialServiceImpl> _logger;
public BaseMaterialServiceImpl(Repository<BaseMaterialInfo> repository, ILogger<BaseMaterialServiceImpl> logger)
public BaseMaterialServiceImpl(Repository<BaseMaterialInfo> repository, ILogger<BaseMaterialServiceImpl> 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<BaseMaterialInfo> 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)
{

@ -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<BaseUser>,IBaseUserService
{
private readonly ILogger<BaseUser> _logger;
private readonly Repository<BaseUser> _rep;
public BaseUserServiceImpl(ILogger<BaseUser> logger, Repository<BaseUser> rep)
public BaseUserServiceImpl(Repository<BaseUser> rep,ILogger<BaseUser> logger) :base(rep)
{
_logger = logger;
_rep = rep;
}
@ -46,7 +44,7 @@ namespace SlnMesnac.Repository.service.Impl
List<BaseUser> 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;

@ -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
* CLR4.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<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.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.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,106 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace SlnMesnac.Repository.service.@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);
}
}
Loading…
Cancel
Save