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