using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SqlSugar;
using System.Configuration;
using System.Linq.Expressions;
using System.Data;
using Newtonsoft.Json;
namespace CYSoft.WebMain_X2.Areas.Tools.SqlSugarTools
{
public sealed class SqlSugarDBHelper
{
public static SqlSugarClient GetContext(bool isWithNoLockQuery = true)
{
var moreSettings = new ConnMoreSettings();
moreSettings.IsAutoRemoveDataCache = true;
moreSettings.IsWithNoLockQuery = isWithNoLockQuery;
var context = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ConfigurationManager.ConnectionStrings["DefaultDBConnectionString"].ToString(),
DbType = SqlSugar.DbType.SqlServer,
IsAutoCloseConnection = false,
InitKeyType = InitKeyType.SystemTable,
MoreSettings = moreSettings
});
return context;
}
///
/// 对单表单条记录新增或更新
///
///
/// 实体
/// 主键字段值
///
public static bool AddOrUpdateEntity
{
using (var Db = GetContext())
{
var model = Db.Queryable
if (model != null)
{
return Db.Updateable(entity).ExecuteCommand() > 0;
}
return Db.Insertable(entity).ExecuteCommand() > 0;
}
}
///
/// 对单表多条记录进行新增或更新
///
///
///
/// 主键字段名
///
public static bool AddOrUpdateRange
{
if (entities != null && entities.Any())
{
using (var Db = GetContext())
{
var updateList = new List
var insertList = new List
//start--获取传入的子表的主键值集合,根据此集合从数据库中拉取子表数据,若subEntities包含取出的数据为要更新的子表数据,否则为要新增的子表数据
List
//keyValue = entities.Select(p => p.GetType().GetProperty(keyName).GetValue(p).ToString()).ToList();
foreach (var item in entities)
{
keyValue.Add(item.GetType().GetProperty(keyName).GetValue(item, null).ToString());
}
var models = Db.Queryable
if (models != null)
{
updateList.AddRange(entities.Where(p => models.Select(g => g.GetType().GetProperty(keyName).GetValue(g, null)).Contains(p.GetType().GetProperty(keyName).GetValue(p, null).ToString())));
insertList.AddRange(entities.Where(p => !models.Select(g => g.GetType().GetProperty(keyName).GetValue(g, null)).Contains(p.GetType().GetProperty(keyName).GetValue(p, null).ToString())));
}
else
{
insertList.AddRange(entities);
}
//end
var result = Db.Ado.UseTran(() =>
{
if (insertList.Count > 0)
Db.Insertable(insertList.ToArray()).ExecuteCommand();
if (updateList.Count > 0)
Db.Updateable(updateList).ExecuteCommand();
});
return result.Data;
}
}
return false;
}
///
/// 对主从表进行新增或修改
///
///
///
/// 主表实体集合
/// 主表主键值
/// 子表实体集合
/// 子表主键名
///
public static bool AddOrUpdateSuperAndSubEntities
where T1 : class, new()
where T2 : class, new()
{
using (var Db = GetContext())
{
//根据主表主键从数据库拉取数据,包含数据库中取出的数据加入updateListMain,否则加入insertListMain
var updateListMain = new List
var insertListMain = new List
List
foreach (var item in superEntity)
{
mainkeyValue.Add(item.GetType().GetProperty(superKeyValue).GetValue(item, null).ToString());
}
var mainModels = Db.Queryable
updateListMain.AddRange(superEntity.Where(p => mainModels.Select(g => g.GetType().GetProperty(superKeyValue).GetValue(g, null)).Contains(p.GetType().GetProperty(superKeyValue).GetValue(p, null).ToString())));
insertListMain.AddRange(superEntity.Where(p => !mainModels.Select(g => g.GetType().GetProperty(superKeyValue).GetValue(g, null)).Contains(p.GetType().GetProperty(superKeyValue).GetValue(p, null).ToString())));
//主表结束
//根据子表主键从数据库拉取数据,包含数据库中取出的数据加入updateList,否则加入insertList
var updateList = new List
var insertList = new List
List
foreach (var item in subEntities)
{
keyValue.Add(item.GetType().GetProperty(subKeyName).GetValue(item, null).ToString());
}
var models = Db.Queryable
if (models != null)
{
updateList.AddRange(subEntities.Where(p => models.Select(g => g.GetType().GetProperty(subKeyName).GetValue(g, null)).Contains(p.GetType().GetProperty(subKeyName).GetValue(p, null).ToString())));
insertList.AddRange(subEntities.Where(p => !models.Select(g => g.GetType().GetProperty(subKeyName).GetValue(g, null)).Contains(p.GetType().GetProperty(subKeyName).GetValue(p, null).ToString())));
}
else
{
insertList.AddRange(subEntities);
}
//子表结束
var result = Db.Ado.UseTran(() =>
{
if (updateListMain.Count > 0)
Db.Updateable(updateListMain).ExecuteCommand();
if (insertListMain.Count > 0)
Db.Insertable(insertListMain.ToArray()).ExecuteCommand();
if (updateList.Count > 0)
Db.Updateable(updateList).ExecuteCommand();
if (insertList.Count > 0)
Db.Insertable(insertList.ToArray()).ExecuteCommand();
});
return result.Data;
}
}
///
/// 对主从表进行新增或修改
///
///
///
/// 主表实体
/// 主表主键值
/// 子表实体集合
/// 子表主键名
///
public static bool AddOrUpdateSuperAndSub
where T1 : class, new()
where T2 : class, new()
{
using (var Db = GetContext())
{
//根据主表主键从数据库拉取数据,包含数据库中取出的数据加入updateListMain,否则加入insertListMain
var mainModel = Db.Queryable
//主表结束
//根据子表主键从数据库拉取数据,包含数据库中取出的数据加入updateList,否则加入insertList
var updateList = new List
var insertList = new List
List
foreach (var item in subEntities)
{
keyValue.Add(item.GetType().GetProperty(subKeyName).GetValue(item, null).ToString());
}
var models = Db.Queryable
if (models != null)
{
updateList.AddRange(subEntities.Where(p => models.Select(g => g.GetType().GetProperty(subKeyName).GetValue(g, null)).Contains(p.GetType().GetProperty(subKeyName).GetValue(p, null).ToString())));
insertList.AddRange(subEntities.Where(p => !models.Select(g => g.GetType().GetProperty(subKeyName).GetValue(g, null)).Contains(p.GetType().GetProperty(subKeyName).GetValue(p, null).ToString())));
}
else
{
insertList.AddRange(subEntities);
}
//子表结束
var result = Db.Ado.UseTran(() =>
{
if (mainModel != null)
{
Db.Updateable(superEntity).ExecuteCommand();
}
else
{
Db.Insertable(superEntity).ExecuteCommand();
}
if (updateList.Count > 0)
Db.Updateable(updateList).ExecuteCommand();
if (insertList.Count > 0)
Db.Insertable(insertList.ToArray()).ExecuteCommand();
});
return result.Data;
}
}
///
/// 根据主键删除单条记录
///
///
///
public static bool DeleteEntity
{
using (var Db = GetContext())
{
return Db.Deleteable
}
}
///
/// 根据主键删除多条记录
///
///
///
///
public static bool DeleteEntities
{
using (var Db = GetContext())
{
return Db.Deleteable
}
}
///
/// 根据主键删除主从表信息
///
///
///
///
///
///
public static bool DeleteSuperAndSubEntities
where T : class, new()
where T1 : class, new()
{
using (var Db = GetContext())
{
var result = Db.Ado.UseTran(() =>
{
Db.Deleteable
Db.Deleteable
});
return result.Data;
}
}
///
/// 根据主键删除主表信息,并根据主表ID删除从表信息
///
///
///
///
/// 主表主键字段名
///
public static bool DeleteSuperAndSubEntities
where T : class, new()
where T1 : class, new()
{
using (var Db = GetContext())
{
var result = Db.Ado.UseTran(() =>
{
Db.Deleteable
foreach (string keyValue in superKey)
{
var sql = string.Format("sub.{0}=@superKey ", superKeyName);
var subList = Db.Queryable
Db.Deleteable
}
});
return result.Data;
}
}
///
/// 单表分页查询
///
///
/// 查询条件
/// 当前页
/// 每页显示条数
/// 总记录数
///
public static List
{
using (var Db = GetContext())
{
var list = Db.Queryable
return list;
}
}
public static List
{
using (var Db = GetContext())
{
var list = Db.Queryable
return list;
}
}
public static T FindOne
{
using (var Db = GetContext())
{
var model = Db.Queryable
return model;
}
}
///
/// 判断记录是否存在
///
///
///
///
public static bool Exsits
{
using (var Db = GetContext())
{
return Db.Queryable
}
}
#region 执行SQL语句/存储过程,返回表/实体
///
/// 执行SQL语句,返回实体
///
///
/// SQL语句
/// 参数
///
public static List
{
using (var Db = GetContext())
{
List
return dt;
};
}
//执行SQL语句,返回表
public static DataTable getDateBySQL(string sql, List
{
using (var Db = GetContext())
{
DataTable dt = Db.Ado.GetDataTable(sql, par);
return dt;
};
}
///
/// 执行存储过程,返回表
///
/// 过程名称
/// 参数
///
public static DataTable getDateByPro(string pro, List
{
using (var Db = GetContext())
{
DataTable dt = Db.Ado.UseStoredProcedure().GetDataTable(pro, par);
return dt;
};
}
// 执行存储过程,返回实体
public static List
{
using (var Db = GetContext())
{
string sql = @"exec " + pro;
foreach (SugarParameter sp in par)
{
sql += @" " + sp.ParameterName + "='" + sp.Value + "',";
}
sql = sql.Remove(sql.LastIndexOf(","));
List
return dt;
};
}
#endregion
///
/// 分页查询,返回实体集
/// 表转成dynamic
/// string str = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
/// List>();
/// List
/// string strd = Newtonsoft.Json.JsonConvert.SerializeObject(list);
/// DataTable dts=Newtonsoft.Json.JsonConvert.DeserializeObject
/// 或者DataTable dts=Newtonsoft.Json.JsonConvert.DeserializeObject
///
///
/// 执行类型。0 SQL语句,1 存储过程,2 视图
/// 对的SQL,存储过程名称,视图名称
/// 对应的SQL参数
/// 排序字段名
/// 当前页数,从0页开始
/// 每一页的数据行数
/// 总页数
///
public static List
{
using (var Db = GetContext())
{
//得到所有数据集
List
if (ExecuteType == 0)
listT = getDateBySQL
else if (ExecuteType == 1)
listT = getDateByPro
else if (ExecuteType == 1)
listT = getDateByPro
//开始分页
int count = listT.Count();//总行数
var Page = (int)Math.Ceiling((Decimal)count / pageSize);//总页数
totalCount = Page;
var Skip = pageIndex * pageSize;//当前页从第一行开始行数
var Take = (pageIndex + 1) * pageSize;//当前页的结尾行数
if (pageIndex * pageSize > count / 2)//页码大于一半用倒序
{
listT = listT.OrderByDescending(i => i.GetType().GetProperty(orderBy).GetValue(i, null)).ToList
var Mod = count % pageSize;//总页数,取整
if (pageIndex * pageSize >= count)
{
Skip = 0; Take = Mod == 0 ? pageSize : Mod;
}
else
{
Skip = (Page - pageIndex - 1) * pageSize + Mod;
}
}
else
{
listT = listT.OrderBy(i => i.GetType().GetProperty(orderBy).GetValue(i, null)).ToList
}
List
list = list.Take(pageSize).ToList
return list;
}
}
}
}