using SqlSugar; using System.Collections.Generic; using System.Linq; namespace TProduct.DAL { /// /// 数据库访问基类 /// /// 实体类类型 public partial class BaseLiteSqlDAL where T : Entity.BaseEntity, new() { /// /// ID /// protected const string IDKey = "id"; /// /// 构造函数 /// public BaseLiteSqlDAL() { this.TableName = EntityHelper.GetTableName(); } /// /// 对象的表名 /// public virtual string TableName { get; set; } /// /// 数据库连接 /// public virtual ISqlSugarClient Connection { get { var connection = ConnectionFactory.CreateConnection(); return connection; } } #region Insert /// /// 插入指定对象到数据库中 /// public virtual long Insert(T entity) { using (ISqlSugarClient db = Connection) { return db.Insertable(entity).ExecuteReturnSnowflakeId(); } } /// /// 插入指定对象集合到数据库中 /// public virtual bool Inserts(List list) { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var snowflakeIds = db.Insertable(list).ExecuteReturnSnowflakeIdList(); if (snowflakeIds.Count < list.Count) { db.Ado.RollbackTran(); return default; } if (snowflakeIds == null || snowflakeIds.Count < 1) { db.Ado.RollbackTran(); return default; } db.Ado.CommitTran(); return true; } catch { db.Ado.RollbackTran(); throw; } } } /// /// 插入并返回 /// public virtual List InsertsR(List list) { if (list == null || list.Count() < 1) return default; using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var snowflakeIds = db.Insertable(list).ExecuteReturnSnowflakeIdList(); if (snowflakeIds.Count < list.Count) { db.Ado.RollbackTran(); return default; } if (snowflakeIds == null || snowflakeIds.Count < 1) { db.Ado.RollbackTran(); return default; } db.Ado.CommitTran(); return snowflakeIds; } catch { db.Ado.RollbackTran(); throw; } } } #endregion #region Update /// /// 更新对象属性到数据库中 /// public virtual bool Update(T entity) { using (ISqlSugarClient db = Connection) { var result = db.Updateable(entity).ExecuteCommandHasChange(); return result; } } /// /// 更新部分 /// public virtual bool UpdatePart(T entity, List pros) { using (ISqlSugarClient db = Connection) { var result = db.Updateable(entity).UpdateColumns(pros.ToArray()).ExecuteCommandHasChange();//更新单 条根据ID return result; } } /// /// 更新指定对象集合到数据库中 /// public virtual bool Updates(List list) { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var result = db.Updateable(list).ExecuteCommandHasChange(); db.Ado.CommitTran(); return result; } catch { db.Ado.RollbackTran(); throw; } } } /// /// 批量更新部分 /// public virtual bool UpdatesPart(List list, List pros) { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var result = db.Updateable(list).UpdateColumns(pros.ToArray()).ExecuteCommandHasChange(); db.Ado.CommitTran(); return result; } catch { db.Ado.RollbackTran(); throw; } } } #endregion #region Delete /// /// 从数据库中删除指定对象 /// public virtual bool Delete(T entity) { using (ISqlSugarClient db = Connection) { var result = db.Deleteable(entity).ExecuteCommand(); return true; } } /// /// 从数据库中删除指定对象集合 /// public virtual bool Deletes(List list) { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var result = db.Deleteable(list).ExecuteCommand(); db.Ado.CommitTran(); return true; } catch { db.Ado.RollbackTran(); throw; } } } /// /// 根据指定对象的ID,从数据库中删除指定对象 /// public virtual bool DeleteByID(long ID) { using (ISqlSugarClient db = Connection) { var result = db.Deleteable(ID).ExecuteCommandHasChange(); return result; } } /// /// 通过id列表删除 /// public virtual bool DeleteByIds(List ids) { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var result = db.Deleteable(ids).ExecuteCommandHasChange(); //批量删除 db.Ado.CommitTran(); return result; } catch { db.Ado.RollbackTran(); throw; } } } /// /// 从数据库中删除所有对象 /// public virtual bool DeleteAll() { using (ISqlSugarClient db = Connection) { try { db.Ado.BeginTran(); var result = db.Deleteable().ExecuteCommandHasChange(); db.Ado.CommitTran(); return result; } catch { db.Ado.RollbackTran(); throw; } } } #endregion #region Query /// /// 返回数据库所有的对象集合 /// public virtual List GetAll() { using (ISqlSugarClient db = Connection) { return db.Queryable()?.ToList(); } } /// /// 查询数据库,返回指定ID的对象 /// public virtual T GetByID(long id) { using (ISqlSugarClient db = Connection) { return db.Queryable().Single(x => x.ID == id); } } /// /// 通拓id列表获取 /// /// public virtual List GetByIds(List ids) { using (ISqlSugarClient db = Connection) { return db.Queryable().Where(x => ids.Contains(x.ID))?.ToList(); } } #endregion #region Count /// /// 获取数量 /// public virtual long GetCount() { using (ISqlSugarClient db = Connection) { return db.Queryable().Count(); } } #endregion #region 分页数据 /// /// 分页获取数据 /// public virtual List GetPageList(int PageIndex, int PageSize, ref int Total) { using (var conn = Connection) { int skip = 1; if (PageIndex > 0) { skip = (PageIndex - 1) * PageSize + 1; } using (ISqlSugarClient db = Connection) { var page = db.Queryable().ToPageList(PageIndex, PageSize, ref Total); return page; } } } #endregion /// /// 表是否存在 /// /// public bool IsExistTable() { using (ISqlSugarClient db = Connection) { //怎么获取sqlsugar实体类中的表名 //((SugarTable)).TableName; return db.DbMaintenance.IsAnyTable(this.TableName); } } /// /// 列是否存在 /// /// /// public bool IsExistColumn(string columnName) { using (ISqlSugarClient db = Connection) { return db.DbMaintenance.IsAnyColumn(this.TableName, columnName); } } } }