|
|
|
|
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<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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|