From d4fd12fd967c5ddd49528e7780b564e11f77ab7c Mon Sep 17 00:00:00 2001 From: "qianhao.xu" Date: Mon, 23 Sep 2024 13:25:25 +0800 Subject: [PATCH] XUEHUA --- DOAN.Repository/BaseRepository.cs | 611 ++++++++++++------------ DOAN.Service/PBL/InventorylogService.cs | 1 + 2 files changed, 311 insertions(+), 301 deletions(-) diff --git a/DOAN.Repository/BaseRepository.cs b/DOAN.Repository/BaseRepository.cs index 4f982b6..dc332a7 100644 --- a/DOAN.Repository/BaseRepository.cs +++ b/DOAN.Repository/BaseRepository.cs @@ -17,331 +17,340 @@ namespace DOAN.Repository /// public class BaseRepository : SimpleClient where T : class, new() { - public ITenant itenant = null;//多租户事务 - public BaseRepository(ISqlSugarClient context = null) : base(context) - { - //通过特性拿到ConfigId - var configId = typeof(T).GetCustomAttribute()?.configId; - if (configId != null) - { - Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择 - } - else - { - Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0 - } - //Context = DbScoped.SugarScope.GetConnectionScopeWithAttr(); - itenant = DbScoped.SugarScope;//设置租户接口 - } - #region add + public string XUEHUA + { - /// - /// 插入实体 - /// - /// - /// - public int Add(T t, bool ignoreNull = true) - { - return Context.Insertable(t).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand(); + get { return SnowFlakeSingle.Instance.NextId().ToString(); } + + } + + public ITenant itenant = null;//多租户事务 + public BaseRepository(ISqlSugarClient context = null) : base(context) + { + //通过特性拿到ConfigId + var configId = typeof(T).GetCustomAttribute()?.configId; + if (configId != null) + { + Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择 + } + else + { + Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0 + } + //Context = DbScoped.SugarScope.GetConnectionScopeWithAttr(); + itenant = DbScoped.SugarScope;//设置租户接口 + } - public int Insert(List t) - { - return InsertRange(t) ? 1 : 0; - } - public int Insert(T parm, Expression> iClumns = null, bool ignoreNull = true) - { - return Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand(); - } - public IInsertable Insertable(T t) - { - return Context.Insertable(t); - } - #endregion add + #region add - #region update - //public IUpdateable Updateable(T entity) - //{ - // return Context.Updateable(entity); - //} + /// + /// 插入实体 + /// + /// + /// + public int Add(T t, bool ignoreNull = true) + { + return Context.Insertable(t).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand(); + } - /// - /// 实体根据主键更新 - /// - /// - /// - /// - public int Update(T entity, bool ignoreNullColumns = false, object data = null) - { - return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns) - .EnableDiffLogEventIF(data.IsNotEmpty(), data).ExecuteCommand(); - } + public int Insert(List t) + { + return InsertRange(t) ? 1 : 0; + } + public int Insert(T parm, Expression> iClumns = null, bool ignoreNull = true) + { + return Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand(); + } + public IInsertable Insertable(T t) + { + return Context.Insertable(t); + } + #endregion add - /// - /// 实体根据主键更新指定字段 - /// return Update(new SysUser(){ Status = 1 }, t => new { t.NickName, }, true); - /// - /// - /// - /// - /// - public int Update(T entity, Expression> expression, bool ignoreAllNull = false) - { - return Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand(); - } + #region update + //public IUpdateable Updateable(T entity) + //{ + // return Context.Updateable(entity); + //} - /// - /// 根据指定条件更新指定列 eg:Update(new SysUser(){ Status = 1 }, it => new { it.Status }, f => f.Userid == 1)); - /// 只更新Status列,条件是包含 - /// - /// 实体类 - /// 要更新列的表达式 - /// where表达式 - /// - public int Update(T entity, Expression> expression, Expression> where) - { - return Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand(); - } + /// + /// 实体根据主键更新 + /// + /// + /// + /// + public int Update(T entity, bool ignoreNullColumns = false, object data = null) + { + return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns) + .EnableDiffLogEventIF(data.IsNotEmpty(), data).ExecuteCommand(); + } - /// - /// 更新指定列 eg:Update(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" }); - /// - /// - /// - /// - public int Update(Expression> where, Expression> columns) - { - return Context.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand(); - } - #endregion update + /// + /// 实体根据主键更新指定字段 + /// return Update(new SysUser(){ Status = 1 }, t => new { t.NickName, }, true); + /// + /// + /// + /// + /// + public int Update(T entity, Expression> expression, bool ignoreAllNull = false) + { + return Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand(); + } - public DbResult UseTran(Action action) - { - try - { - var result = Context.Ado.UseTran(() => action()); - return result; - } - catch (Exception ex) - { - Context.Ado.RollbackTran(); - Console.WriteLine(ex.Message); - throw; - } - } + /// + /// 根据指定条件更新指定列 eg:Update(new SysUser(){ Status = 1 }, it => new { it.Status }, f => f.Userid == 1)); + /// 只更新Status列,条件是包含 + /// + /// 实体类 + /// 要更新列的表达式 + /// where表达式 + /// + public int Update(T entity, Expression> expression, Expression> where) + { + return Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand(); + } - /// - /// - /// - /// - /// 增删改查方法 - /// - public DbResult UseTran(ISqlSugarClient client, Action action) - { - try - { - var result = client.AsTenant().UseTran(() => action()); - return result; - } - catch (Exception ex) - { - Console.WriteLine("事务异常" + ex.Message); - client.AsTenant().RollbackTran(); - throw; - } - } + /// + /// 更新指定列 eg:Update(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" }); + /// + /// + /// + /// + public int Update(Expression> where, Expression> columns) + { + return Context.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand(); + } + #endregion update - /// - /// 使用事务 - /// - /// - /// - public bool UseTran2(Action action) + public DbResult UseTran(Action action) + { + try { - Console.WriteLine("---事务开始---"); var result = Context.Ado.UseTran(() => action()); - Console.WriteLine("---事务结束---"); - return result.IsSuccess; - } - - #region delete - public IDeleteable Deleteable() - { - return Context.Deleteable(); - } - - public int Delete(object id, string title = "") - { - return Context.Deleteable(id).EnableDiffLogEventIF(title.IsNotEmpty(), title).ExecuteCommand(); - } - public int DeleteTable() - { - return Context.Deleteable().ExecuteCommand(); - } - public bool Truncate() - { - return Context.DbMaintenance.TruncateTable(); - } - #endregion delete - - #region query - - public bool Any(Expression> expression) - { - return Context.Queryable().Where(expression).Any(); - } - - public ISugarQueryable Queryable() - { - return Context.Queryable(); - } - - public List SqlQueryToList(string sql, object obj = null) - { - return Context.Ado.SqlQuery(sql, obj); - } - - /// - /// 根据主值查询单条数据 - /// - /// 主键值 - /// 泛型实体 - public T GetId(object pkValue) - { - return Context.Queryable().InSingle(pkValue); - } - /// - /// 根据条件查询分页数据 - /// - /// - /// - /// - public PagedInfo GetPages(Expression> where, PagerInfo parm) - { - var source = Context.Queryable().Where(where); - - return source.ToPage(parm); - } - - /// - /// 分页获取数据 - /// - /// 条件表达式 - /// - /// - /// - /// - public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, OrderByType orderEnum = OrderByType.Asc) - { - var source = Context - .Queryable() - .Where(where) - .OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc) - .OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc); - - return source.ToPage(parm); - } - - public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, string orderByType) - { - return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc); - } - - /// - /// 查询所有数据(无分页,请慎用) - /// - /// - public List GetAll(bool useCache = false, int cacheSecond = 3600) - { - return Context.Queryable().WithCacheIF(useCache, cacheSecond).ToList(); - } - - #endregion query - - /// - /// 此方法不带output返回值 - /// var list = new List(); - /// list.Add(new SugarParameter(ParaName, ParaValue)); input - /// - /// - /// - /// - public DataTable UseStoredProcedureToDataTable(string procedureName, List parameters) - { - return Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters); - } - - /// - /// 带output返回值 - /// var list = new List(); - /// list.Add(new SugarParameter(ParaName, ParaValue, true)); output - /// list.Add(new SugarParameter(ParaName, ParaValue)); input - /// - /// - /// - /// - public (DataTable, List) UseStoredProcedureToTuple(string procedureName, List parameters) - { - var result = (Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters), parameters); return result; } + catch (Exception ex) + { + Context.Ado.RollbackTran(); + Console.WriteLine(ex.Message); + throw; + } } /// - /// 分页查询扩展 + /// /// - public static class QueryableExtension + /// + /// 增删改查方法 + /// + public DbResult UseTran(ISqlSugarClient client, Action action) { - /// - /// 读取列表 - /// - /// - /// 查询表单式 - /// 分页参数 - /// - public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) + try { - var page = new PagedInfo(); - var total = 0; - page.PageSize = parm.PageSize; - page.PageIndex = parm.PageNum; - if (parm.Sort.IsNotEmpty()) - { - source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc); - } - page.Result = source - //.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}") - .ToPageList(parm.PageNum, parm.PageSize, ref total); - page.TotalNum = total; - return page; + var result = client.AsTenant().UseTran(() => action()); + return result; } - - /// - /// 转指定实体类Dto - /// - /// - /// - /// - /// - /// - public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) + catch (Exception ex) { - var page = new PagedInfo(); - var total = 0; - page.PageSize = parm.PageSize; - page.PageIndex = parm.PageNum; - if (parm.Sort.IsNotEmpty()) - { - source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc); - } - var result = source - //.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}") - .ToPageList(parm.PageNum, parm.PageSize, ref total); - - page.TotalNum = total; - page.Result = result.Adapt>(); - return page; + Console.WriteLine("事务异常" + ex.Message); + client.AsTenant().RollbackTran(); + throw; } } + + /// + /// 使用事务 + /// + /// + /// + public bool UseTran2(Action action) + { + Console.WriteLine("---事务开始---"); + var result = Context.Ado.UseTran(() => action()); + Console.WriteLine("---事务结束---"); + return result.IsSuccess; + } + + #region delete + public IDeleteable Deleteable() + { + return Context.Deleteable(); + } + + public int Delete(object id, string title = "") + { + return Context.Deleteable(id).EnableDiffLogEventIF(title.IsNotEmpty(), title).ExecuteCommand(); + } + public int DeleteTable() + { + return Context.Deleteable().ExecuteCommand(); + } + public bool Truncate() + { + return Context.DbMaintenance.TruncateTable(); + } + #endregion delete + + #region query + + public bool Any(Expression> expression) + { + return Context.Queryable().Where(expression).Any(); + } + + public ISugarQueryable Queryable() + { + return Context.Queryable(); + } + + public List SqlQueryToList(string sql, object obj = null) + { + return Context.Ado.SqlQuery(sql, obj); + } + + /// + /// 根据主值查询单条数据 + /// + /// 主键值 + /// 泛型实体 + public T GetId(object pkValue) + { + return Context.Queryable().InSingle(pkValue); + } + /// + /// 根据条件查询分页数据 + /// + /// + /// + /// + public PagedInfo GetPages(Expression> where, PagerInfo parm) + { + var source = Context.Queryable().Where(where); + + return source.ToPage(parm); + } + + /// + /// 分页获取数据 + /// + /// 条件表达式 + /// + /// + /// + /// + public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, OrderByType orderEnum = OrderByType.Asc) + { + var source = Context + .Queryable() + .Where(where) + .OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc) + .OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc); + + return source.ToPage(parm); + } + + public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, string orderByType) + { + return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc); + } + + /// + /// 查询所有数据(无分页,请慎用) + /// + /// + public List GetAll(bool useCache = false, int cacheSecond = 3600) + { + return Context.Queryable().WithCacheIF(useCache, cacheSecond).ToList(); + } + + #endregion query + + /// + /// 此方法不带output返回值 + /// var list = new List(); + /// list.Add(new SugarParameter(ParaName, ParaValue)); input + /// + /// + /// + /// + public DataTable UseStoredProcedureToDataTable(string procedureName, List parameters) + { + return Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters); + } + + /// + /// 带output返回值 + /// var list = new List(); + /// list.Add(new SugarParameter(ParaName, ParaValue, true)); output + /// list.Add(new SugarParameter(ParaName, ParaValue)); input + /// + /// + /// + /// + public (DataTable, List) UseStoredProcedureToTuple(string procedureName, List parameters) + { + var result = (Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters), parameters); + return result; + } +} + +/// +/// 分页查询扩展 +/// +public static class QueryableExtension +{ + /// + /// 读取列表 + /// + /// + /// 查询表单式 + /// 分页参数 + /// + public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) + { + var page = new PagedInfo(); + var total = 0; + page.PageSize = parm.PageSize; + page.PageIndex = parm.PageNum; + if (parm.Sort.IsNotEmpty()) + { + source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc); + } + page.Result = source + //.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}") + .ToPageList(parm.PageNum, parm.PageSize, ref total); + page.TotalNum = total; + return page; + } + + /// + /// 转指定实体类Dto + /// + /// + /// + /// + /// + /// + public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) + { + var page = new PagedInfo(); + var total = 0; + page.PageSize = parm.PageSize; + page.PageIndex = parm.PageNum; + if (parm.Sort.IsNotEmpty()) + { + source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc); + } + var result = source + //.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}") + .ToPageList(parm.PageNum, parm.PageSize, ref total); + + page.TotalNum = total; + page.Result = result.Adapt>(); + return page; + } +} } diff --git a/DOAN.Service/PBL/InventorylogService.cs b/DOAN.Service/PBL/InventorylogService.cs index e97e7df..2a5619b 100644 --- a/DOAN.Service/PBL/InventorylogService.cs +++ b/DOAN.Service/PBL/InventorylogService.cs @@ -52,6 +52,7 @@ namespace DOAN.Service.PBL /// public Inventorylog AddInventorylog(Inventorylog model) { + model.Id = XUEHUA; return Insertable(model).ExecuteReturnEntity(); }