1804 lines
62 KiB
C#
1804 lines
62 KiB
C#
using JinYuan.DAL;
|
|||
|
|
using JinYuan.DAL.Enums;
|
||
|
|
using JinYuan.DAL.Models;
|
||
|
|
using JinYuan.Helper;
|
||
|
|
using Org.BouncyCastle.Asn1;
|
||
|
|
using SqlSugar;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Data;
|
||
|
|
using System.Data.SqlClient;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Linq.Expressions;
|
||
|
|
using System.Runtime.Remoting.Contexts;
|
||
|
|
using System.Text;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace JinYuan.DAL
|
||
|
|
{
|
||
|
|
|
||
|
|
public class SQLSugarService : ISugarHepler
|
||
|
|
{
|
||
|
|
public void SetConnectionStr(string connectionStr, SqlSugar.DbType dbType)
|
||
|
|
{
|
||
|
|
SQLSugarHelper.connString = connectionStr;
|
||
|
|
SQLSugarHelper.dbType = dbType;
|
||
|
|
SqlSugarDb = SQLSugarHelper.SqlSugarScope;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 数据库连接对象
|
||
|
|
/// </summary>
|
||
|
|
public SqlSugarScope SqlSugarDb { get; set; }
|
||
|
|
|
||
|
|
|
||
|
|
#region 数据库管理
|
||
|
|
/// <summary>
|
||
|
|
/// 添加列
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="column">列信息</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool AddColumn(string tableName, DbColumnInfo column)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.AddColumn(tableName, column);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 添加主键
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="columnName">列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool AddPrimaryKey(string tableName, string columnName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.AddPrimaryKey(tableName, columnName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 备份数据库
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="databaseName">数据库名</param>
|
||
|
|
/// <param name="fullFileName">文件名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool BackupDataBase(string databaseName, string fullFileName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.BackupDataBase(databaseName, fullFileName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 备份表
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="oldTableName">旧表名</param>
|
||
|
|
/// <param name="newTableName">行表名</param>
|
||
|
|
/// <param name="maxBackupDataRows">行数</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.BackupTable(oldTableName, newTableName, maxBackupDataRows);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 创建表
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="columns">列集合</param>
|
||
|
|
/// <param name="isCreatePrimaryKey">是否创建主键</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.CreateTable(tableName, columns, isCreatePrimaryKey);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除列
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="columnName">列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool DropColumn(string tableName, string columnName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.DropColumn(tableName, columnName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除约束
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="constraintName">约束名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool DropConstraint(string tableName, string constraintName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.DropConstraint(tableName, constraintName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除表
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName"></param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool DropTable(string tableName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.DropTable(tableName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 获取列信息
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="isCache">是否缓存</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.GetColumnInfosByTableName(tableName, isCache);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 获取自增列
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<string> GetIsIdentities(string tableName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.GetIsIdentities(tableName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 获取主键
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<string> GetPrimaries(string tableName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.GetPrimaries(tableName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 获取表集合
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="isCache">是否缓存</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<DbTableInfo> GetTableInfoList(bool isCache = true)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.GetTableInfoList(isCache);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 获取视图集合
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="isCache">是否缓存</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<DbTableInfo> GetViewInfoList(bool isCache = true)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.GetViewInfoList(isCache);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测列是否存在
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="column">列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsAnyColumn(string tableName, string column)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsAnyColumn(tableName, column);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测约束
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="constraintName">约束名称</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsAnyConstraint(string constraintName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsAnyConstraint(constraintName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测是否有任何系统表权限
|
||
|
|
/// </summary>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsAnySystemTablePermissions()
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsAnySystemTablePermissions();
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测表是否存在
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="isCache">是否缓存</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsAnyTable(string tableName, bool isCache = true)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsAnyTable(tableName, isCache);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测列是否自增列
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="column">列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsIdentity(string tableName, string column)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsIdentity(tableName, column);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 检测列是否主键
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="column">列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool IsPrimaryKey(string tableName, string column)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.IsPrimaryKey(tableName, column);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 重置列名
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="oldColumnName">旧列名</param>
|
||
|
|
/// <param name="newColumnName">新列名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.RenameColumn(tableName, oldColumnName, newColumnName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 重置表数据
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool TruncateTable(string tableName)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.TruncateTable(tableName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 修改列信息
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="tableName">表名</param>
|
||
|
|
/// <param name="column">列信息</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public bool UpdateColumn(string tableName, DbColumnInfo column)
|
||
|
|
{
|
||
|
|
return SqlSugarDb.DbMaintenance.UpdateColumn(tableName, column);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取数据库时间
|
||
|
|
/// </summary>
|
||
|
|
/// <returns>返回值</returns>
|
||
|
|
public DateTime GetDataBaseTime()
|
||
|
|
{
|
||
|
|
return SqlSugarDb.GetDate();
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 查询
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询所有
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <returns></returns>
|
||
|
|
/// <exception cref="Exception"></exception>
|
||
|
|
public List<T> QueryAll<T>() where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.UpdLock);
|
||
|
|
return up.ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询-返回自定义数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <typeparam name="TResult">返回值类型</typeparam>
|
||
|
|
/// <param name="expression">返回值表达式</param>
|
||
|
|
/// <param name="whereLambda">查询表达式</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public TResult QuerySelect<T, TResult>(Expression<Func<T, TResult>> expression, Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var datas = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock).Where(whereLambda).Select(expression).First();
|
||
|
|
return datas;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询-返回自定义数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <typeparam name="TResult">返回值类型</typeparam>
|
||
|
|
/// <param name="expression">返回值表达式</param>
|
||
|
|
/// <param name="whereLambda">查询表达式</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<TResult> QuerySelectList<T, TResult>(Expression<Func<T, TResult>> expression, Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var datas = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock).Where(whereLambda).Select(expression).ToList();
|
||
|
|
return datas;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 查询单条数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="whereLambda">查询表达式</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public T Query<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda);
|
||
|
|
}
|
||
|
|
return up.First();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 异步查询单条数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="whereLambda">查询表达式</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
/// <exception cref="Exception"></exception>
|
||
|
|
public Task<T> QueryAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
return QueryAsyncImpl<T>(whereLambda);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task<T> QueryAsyncImpl<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
var up = db.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (whereLambda != null) up = up.Where(whereLambda);
|
||
|
|
return await up.FirstAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 根据条件查询排序数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="whereLambda"></param>
|
||
|
|
/// <param name="byType"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public T Query<T>(Expression<Func<T, bool>> whereLambda2 = null, Expression<Func<T, object>> whereLambda = null, OrderByType byType = OrderByType.Desc) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
|
||
|
|
if (whereLambda2 != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda2);
|
||
|
|
}
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.OrderBy(whereLambda, byType);
|
||
|
|
}
|
||
|
|
return up.First();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public T Query<T>(QueryDescriptor query = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (query != null)
|
||
|
|
{
|
||
|
|
if (query.Conditions != null)
|
||
|
|
{
|
||
|
|
var conds = ParseCondition(query.Conditions);
|
||
|
|
up = up.Where(conds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (query.OrderBys != null)
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(query.OrderBys);
|
||
|
|
up = up.OrderBy(orderBys);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return up.First();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="whereLambda">过滤条件</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public List<T> QueryWhereList<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>();
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda);
|
||
|
|
}
|
||
|
|
return up.ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
LogHelper.Instance.WriteError($"查询集合异常:{ex}", "SysErrorLog");
|
||
|
|
return null;
|
||
|
|
//throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="query">过滤条件</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public List<T> QueryList<T>(QueryDescriptor query = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (query != null)
|
||
|
|
{
|
||
|
|
if (query.Conditions != null)
|
||
|
|
{
|
||
|
|
var conds = ParseCondition(query.Conditions);
|
||
|
|
up = up.Where(conds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (query.OrderBys != null)
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(query.OrderBys);
|
||
|
|
up = up.OrderBy(orderBys);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return up.ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询前N条数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="sort"></param>
|
||
|
|
/// <param name="Take"></param>
|
||
|
|
/// <param name="whereLambda"></param>
|
||
|
|
/// <param name="byType"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public List<T> QueryList<T>(string sort, int Take, Expression<Func<T, bool>> whereLambda = null, OrderByType byType = OrderByType.Desc) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda);
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrEmpty(sort))
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(sort, byType);
|
||
|
|
up = up.OrderBy(orderBys).Take(Take);
|
||
|
|
}
|
||
|
|
return up.ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 根据字段查询排列
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="whereLambda"></param>
|
||
|
|
/// <param name="sort"></param>
|
||
|
|
/// <param name="byType"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public List<T> QueryList<T>(string sort, Expression<Func<T, bool>> whereLambda = null, OrderByType byType = OrderByType.Desc) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda);
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrEmpty(sort))
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(sort, byType);
|
||
|
|
up = up.OrderBy(orderBys);
|
||
|
|
}
|
||
|
|
return up.ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 排序转换
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sort"></param>
|
||
|
|
/// <param name="order"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
private string ParseOrderBy(string sort, OrderByType order)
|
||
|
|
{
|
||
|
|
var conds = "";
|
||
|
|
|
||
|
|
switch (order)
|
||
|
|
{
|
||
|
|
case OrderByType.Asc:
|
||
|
|
conds += $"{sort} asc,";
|
||
|
|
break;
|
||
|
|
case OrderByType.Desc:
|
||
|
|
conds += $"{sort} desc,";
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
throw new ArgumentOutOfRangeException();
|
||
|
|
}
|
||
|
|
return conds.TrimEnd(',');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="sql">sql</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public List<T> QuerySqlList<T>(string sql) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.SqlQueryable<T>(sql).ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 分页查询
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="query">过滤条件</param>
|
||
|
|
/// <param name="totalCount">总行数</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public List<T> QueryPageList<T>(QueryDescriptor query, out int totalCount) where T : class, new()
|
||
|
|
{
|
||
|
|
if (query == null)
|
||
|
|
{
|
||
|
|
throw new ArgumentNullException(nameof(query));
|
||
|
|
}
|
||
|
|
var listDatas = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (query.Conditions != null)
|
||
|
|
{
|
||
|
|
var conds = ParseCondition(query.Conditions);
|
||
|
|
listDatas = listDatas.Where(conds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (query.OrderBys != null)
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(query.OrderBys);
|
||
|
|
listDatas = listDatas.OrderBy(orderBys);
|
||
|
|
}
|
||
|
|
|
||
|
|
totalCount = 0;
|
||
|
|
if (query.PageSize == 0)
|
||
|
|
{
|
||
|
|
var datas = listDatas.ToList();
|
||
|
|
totalCount = datas.Count;
|
||
|
|
return datas;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var datas = listDatas.ToPageList(query.PageIndex, query.PageSize, ref totalCount);
|
||
|
|
return datas;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合-多值
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="inFieldName">字段名</param>
|
||
|
|
/// <param name="inValues">数据集合</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<T> In<T>(string inFieldName, List<dynamic> inValues) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Queryable<T>().With(SqlWith.NoLock).In(inFieldName, inValues).ToList();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合-通过多值(主键)
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="values">主键数据集合</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public List<T> In<T>(List<dynamic> values) where T : class, new()
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Queryable<T>().With(SqlWith.NoLock).In(values).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="whereLambda">查询表达式</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public DataTable QueryDataTable<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (whereLambda != null)
|
||
|
|
{
|
||
|
|
up = up.Where(whereLambda);
|
||
|
|
}
|
||
|
|
return up.ToDataTable();
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询集合
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sql">sql</param>
|
||
|
|
/// <returns>实体</returns>
|
||
|
|
public DataTable QueryDataTable(string sql)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Ado.GetDataTable(sql);
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询单个值
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sql">sql</param>
|
||
|
|
/// <returns>单个值</returns>
|
||
|
|
public object QuerySqlScalar(string sql)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Ado.GetScalar(sql);
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 分页查询
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="query">过滤条件</param>
|
||
|
|
/// <param name="totalCount">总行数</param>
|
||
|
|
/// <returns>DataTable</returns>
|
||
|
|
public DataTable QueryDataTablePageList<T>(QueryDescriptor query, out int totalCount) where T : class, new()
|
||
|
|
{
|
||
|
|
if (query == null)
|
||
|
|
{
|
||
|
|
throw new ArgumentNullException(nameof(query));
|
||
|
|
}
|
||
|
|
var listDatas = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
||
|
|
if (query.Conditions != null)
|
||
|
|
{
|
||
|
|
var conds = ParseCondition(query.Conditions);
|
||
|
|
listDatas = listDatas.Where(conds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (query.OrderBys != null)
|
||
|
|
{
|
||
|
|
var orderBys = ParseOrderBy(query.OrderBys);
|
||
|
|
listDatas = listDatas.OrderBy(orderBys);
|
||
|
|
}
|
||
|
|
|
||
|
|
totalCount = 0;
|
||
|
|
var datas = listDatas.ToDataTablePage(query.PageIndex, query.PageSize, ref totalCount);
|
||
|
|
return datas;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 新增
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Add<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = SqlSugarDb.Insertable(entity).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entitys">泛型集合</param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Add<T>(List<T> entitys) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (entitys.Count == 0) return 1;
|
||
|
|
var result = SqlSugarDb.Insertable(entitys).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="keyValues">字典集合(Key:列名 Value:值)</param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Add<T>(Dictionary<string, object> keyValues) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = SqlSugarDb.Insertable<T>(keyValues).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <returns>返回实体</returns>
|
||
|
|
public T AddReturnEntity<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = SqlSugarDb.Insertable(entity).ExecuteReturnEntity();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <returns>返回自增列</returns>
|
||
|
|
public int AddReturnIdentity<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = SqlSugarDb.Insertable(entity).ExecuteReturnIdentity();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <returns>返回bool</returns>
|
||
|
|
public bool AddReturnBool<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = SqlSugarDb.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommand() > 0;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public async Task<bool> AddReturnBoolAsync<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
return await db.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 新增
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entitys">泛型集合</param>
|
||
|
|
/// <returns>返回bool</returns>
|
||
|
|
public bool AddReturnBool<T>(List<T> entitys) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (entitys.Count == 0) return true;
|
||
|
|
var result = SqlSugarDb.Insertable(entitys).ExecuteCommand() > 0;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> AddReturnBoolAsync<T>(List<T> entitys) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
return await db.Insertable(entitys).ExecuteCommandAsync() > 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 修改
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改(主键是更新条件)
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 </param>
|
||
|
|
/// <param name="lstIgnoreColumns">不更新的列</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
///
|
||
|
|
public int Update<T>(T entity, List<string> lstIgnoreColumns = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable(entity);
|
||
|
|
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
|
||
|
|
{
|
||
|
|
up = up.IgnoreColumns(false);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改(主键是更新条件)
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entitys"> 实体对象集合(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 </param>
|
||
|
|
/// <param name="lstIgnoreColumns">不更新的列</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Update<T>(List<T> entitys, List<string> lstIgnoreColumns = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (entitys.Count == 0) return 1;
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable(entitys);
|
||
|
|
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
|
||
|
|
{
|
||
|
|
up = up.IgnoreColumns(lstIgnoreColumns.Contains(lstIgnoreColumns.First()));
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <param name="where"> 条件 </param>
|
||
|
|
/// <param name="lstIgnoreColumns">不更新的列</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Update<T>(T entity, Expression<Func<T, bool>> where, List<string> lstIgnoreColumns = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable(entity);
|
||
|
|
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
|
||
|
|
{
|
||
|
|
up = up.IgnoreColumns(lstIgnoreColumns.Contains(lstIgnoreColumns.First()));
|
||
|
|
}
|
||
|
|
up = up.Where(where);
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="update"> 实体对象 </param>
|
||
|
|
/// <param name="where"> 条件 </param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Update<T>(Expression<Func<T, object>> update, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable<T>().UpdateColumns(update);
|
||
|
|
if (where != null)
|
||
|
|
{
|
||
|
|
up = up.Where(where);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改加1
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="update"></param>
|
||
|
|
/// <param name="where"></param>
|
||
|
|
/// <param name="isLock"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public int Update<T>(Expression<Func<T, bool>> update, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable<T>().SetColumns(update);
|
||
|
|
if (where != null)
|
||
|
|
{
|
||
|
|
up = up.Where(where);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
///异步修改
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="update"></param>
|
||
|
|
/// <param name="where"></param>
|
||
|
|
/// <param name="isLock"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
/// <exception cref="Exception"></exception>
|
||
|
|
public async Task<int> UpdateAsync<T>(Expression<Func<T, bool>> update, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
var up = db.Updateable<T>().SetColumns(update);
|
||
|
|
if (where != null) up = up.Where(where);
|
||
|
|
if (isLock) up = up.With(SqlWith.UpdLock);
|
||
|
|
return await up.ExecuteCommandAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改单条数据
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="entity">实体类</param>
|
||
|
|
/// <param name="IgnoreColumns">指定更新行</param>
|
||
|
|
/// <param name="where">更新表达式</param>
|
||
|
|
/// <param name="isLock">是否加锁</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
///
|
||
|
|
public int UpdateSingle<T>(T entity, Expression<Func<T, object>> IgnoreColumns = null, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable<T>(entity);
|
||
|
|
if (IgnoreColumns != null)
|
||
|
|
{
|
||
|
|
up.UpdateColumns(IgnoreColumns);
|
||
|
|
}
|
||
|
|
if (where != null)
|
||
|
|
{
|
||
|
|
up.Where(where);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 异步更新
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="entity"></param>
|
||
|
|
/// <param name="IgnoreColumns"></param>
|
||
|
|
/// <param name="where"></param>
|
||
|
|
/// <param name="isLock"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
/// <exception cref="Exception"></exception>
|
||
|
|
public async Task<int> UpdateSingleAsync<T>(T entity, Expression<Func<T, object>> IgnoreColumns = null, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
var up = db.Updateable<T>(entity);
|
||
|
|
if (IgnoreColumns != null) up = up.UpdateColumns(IgnoreColumns);
|
||
|
|
if (where != null) up = up.Where(where);
|
||
|
|
if (isLock) up = up.With(SqlWith.UpdLock);
|
||
|
|
return await up.ExecuteCommandAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="keyValues">字典集合(Key:列名 Value:值)</param>
|
||
|
|
/// <param name="where"> 条件 </param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Update<T>(Dictionary<string, T> keyValues, Expression<Func<T, bool>> where = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable<T>(keyValues);
|
||
|
|
if (where != null)
|
||
|
|
{
|
||
|
|
up = up.Where(where);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 批量修改需要更新的列
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entitys"> 实体对象(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 </param>
|
||
|
|
/// <param name="updateColumns">更新指定列</param>
|
||
|
|
/// <param name="wherecolumns">条件(为空则以主键更新,反之需要把wherecolumns中的列加到UpdateColumns中)</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int UpdateColumns<T>(List<T> entitys, Expression<Func<T, object>> updateColumns, Expression<Func<T, object>> wherecolumns = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (entitys.Count == 0) return 1;
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable(entitys).UpdateColumns(updateColumns);
|
||
|
|
if (wherecolumns != null)
|
||
|
|
{
|
||
|
|
up = up.WhereColumns(wherecolumns);
|
||
|
|
}
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 批量更新
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">实体类型</typeparam>
|
||
|
|
/// <param name="entitys">实体集合</param>
|
||
|
|
/// <returns>IUpdateable 对象</returns>
|
||
|
|
public IUpdateable<T> Updateable<T>(List<T> entitys) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Updateable(entitys);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 批量更新(返回 IUpdateable 用于链式操作)
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">实体类型</typeparam>
|
||
|
|
/// <param name="entity">单个实体</param>
|
||
|
|
/// <returns>IUpdateable 对象</returns>
|
||
|
|
public IUpdateable<T> Updateable<T>(T entity) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Updateable(entity);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 修改 通过RowVer及主键Code 更新
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 </param>
|
||
|
|
/// <param name="lstIgnoreColumns">不更新的列</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int UpdateRowVer<T>(T entity, List<string> lstIgnoreColumns = null, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
Type ts = entity.GetType();
|
||
|
|
var rowVerProperty = ts.GetProperty("RowVer");
|
||
|
|
if (rowVerProperty == null)
|
||
|
|
{
|
||
|
|
throw new Exception("Column RowVer Not Exist");
|
||
|
|
}
|
||
|
|
if (rowVerProperty.GetValue(entity, null) == null)
|
||
|
|
{
|
||
|
|
throw new Exception("RowVer Value Is Null");
|
||
|
|
}
|
||
|
|
var codeProperty = ts.GetProperty("Code");
|
||
|
|
if (codeProperty == null)
|
||
|
|
{
|
||
|
|
throw new Exception("Column Code Not Exist");
|
||
|
|
}
|
||
|
|
if (codeProperty.GetValue(entity, null) == null)
|
||
|
|
{
|
||
|
|
throw new Exception("Code Value Is Null");
|
||
|
|
}
|
||
|
|
var rowVerValue = int.Parse(rowVerProperty.GetValue(entity, null).ToString());
|
||
|
|
var codeValue = codeProperty.GetValue(entity, null).ToString();
|
||
|
|
var sqlWhere = $" RowVer={rowVerValue} AND Code='{codeValue}'";
|
||
|
|
rowVerProperty.SetValue(entity, rowVerValue + 1, null);
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable(entity);
|
||
|
|
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
|
||
|
|
{
|
||
|
|
up = up.IgnoreColumns(lstIgnoreColumns.Contains(lstIgnoreColumns.First()));
|
||
|
|
}
|
||
|
|
up = up.Where(sqlWhere);
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//表达式写2列更新2列,其他不会更新
|
||
|
|
/// <summary>
|
||
|
|
/// 修改
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="update"> 实体对象 </param>
|
||
|
|
/// <param name="where"> 更新条件 </param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int UpdateRowVer<T>(Expression<Func<T, object>> update, Dictionary<string, object> where, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!where.ContainsKey("RowVer"))
|
||
|
|
{
|
||
|
|
throw new Exception("Column RowVer Not Exist");
|
||
|
|
}
|
||
|
|
if (where["RowVer"] == null)
|
||
|
|
{
|
||
|
|
throw new Exception("RowVer Value Is Null");
|
||
|
|
}
|
||
|
|
if (update.Body.ToString().IndexOf("RowVer", StringComparison.Ordinal) == -1)
|
||
|
|
{
|
||
|
|
throw new Exception("Column RowVer Update Is Null");
|
||
|
|
}
|
||
|
|
var sqlWhere = "";
|
||
|
|
foreach (var item in where)
|
||
|
|
{
|
||
|
|
sqlWhere += string.IsNullOrWhiteSpace(sqlWhere) ? $" {item.Key}='{item.Value}'" : $" and {item.Key}='{item.Value}'";
|
||
|
|
}
|
||
|
|
IUpdateable<T> up = SqlSugarDb.Updateable<T>().UpdateColumns(update).Where(sqlWhere);
|
||
|
|
if (isLock)
|
||
|
|
{
|
||
|
|
up = up.With(SqlWith.UpdLock);
|
||
|
|
}
|
||
|
|
var result = up.ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 删除
|
||
|
|
/// <summary>
|
||
|
|
/// 初始化表
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="entity"></param>
|
||
|
|
public void Delete<T>() where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
SqlSugarDb.DbMaintenance.TruncateTable<T>();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="primaryKeyValues">主键值</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int DeleteByPrimary<T>(List<object> primaryKeyValues, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = isLock ?
|
||
|
|
SqlSugarDb.Deleteable<T>().In(primaryKeyValues).With(SqlWith.RowLock).ExecuteCommand()
|
||
|
|
: SqlSugarDb.Deleteable<T>().In(primaryKeyValues).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 (必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Delete<T>(T entity, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = isLock ?
|
||
|
|
SqlSugarDb.Deleteable(entity).With(SqlWith.RowLock).ExecuteCommand()
|
||
|
|
: SqlSugarDb.Deleteable(entity).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="entity"> 实体对象 (必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Delete<T>(List<T> entity, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = isLock ?
|
||
|
|
SqlSugarDb.Deleteable(entity).With(SqlWith.RowLock).ExecuteCommand()
|
||
|
|
: SqlSugarDb.Deleteable(entity).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 删除
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="where"> 条件 </param>
|
||
|
|
/// <param name="isLock"> 是否加锁 </param>
|
||
|
|
/// <returns>操作影响的行数</returns>
|
||
|
|
public int Delete<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = isLock ?
|
||
|
|
SqlSugarDb.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommand()
|
||
|
|
: SqlSugarDb.Deleteable<T>().Where(where).ExecuteCommand();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
|
||
|
|
throw new Exception(ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 异步删除
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="where"></param>
|
||
|
|
/// <param name="isLock"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
/// <exception cref="Exception"></exception>
|
||
|
|
public async Task<int> DeleteAsync<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||
|
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||
|
|
{
|
||
|
|
ConnectionString = config.ConnectionString,
|
||
|
|
DbType = config.DbType,
|
||
|
|
IsAutoCloseConnection = true
|
||
|
|
}))
|
||
|
|
{
|
||
|
|
return isLock
|
||
|
|
? await db.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommandAsync()
|
||
|
|
: await db.Deleteable<T>().Where(where).ExecuteCommandAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 通过多值(主键)删除数据集
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
|
||
|
|
/// <param name="inValues">数据集合</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
public int DeleteIn<T>(List<dynamic> inValues) where T : class, new()
|
||
|
|
{
|
||
|
|
return SqlSugarDb.Deleteable<T>().With(SqlWith.RowLock).In(inValues).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
#region 私有方法
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// SqlParameter转换SugarParameter
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="parameters">参数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
private List<SugarParameter> ConvetParameter(List<SqlParameter> parameters)
|
||
|
|
{
|
||
|
|
var listParams = new List<SugarParameter>();
|
||
|
|
foreach (var p in parameters)
|
||
|
|
{
|
||
|
|
var par = new SugarParameter(p.ParameterName, p.Value)
|
||
|
|
{
|
||
|
|
DbType = p.DbType,
|
||
|
|
Direction = p.Direction
|
||
|
|
};
|
||
|
|
if (!string.IsNullOrWhiteSpace(p.TypeName))
|
||
|
|
{
|
||
|
|
par.TypeName = p.TypeName;
|
||
|
|
}
|
||
|
|
listParams.Add(par);
|
||
|
|
}
|
||
|
|
|
||
|
|
return listParams;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 过滤条件转换
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="contitons">过滤条件</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
private List<IConditionalModel> ParseCondition(List<QueryCondition> contitons)
|
||
|
|
{
|
||
|
|
var conds = new List<IConditionalModel>();
|
||
|
|
contitons.Insert(0, new QueryCondition
|
||
|
|
{
|
||
|
|
Operator = QueryOperator.Equal,
|
||
|
|
Key = "1",
|
||
|
|
Value = "1"
|
||
|
|
});
|
||
|
|
|
||
|
|
#region Or条件组装
|
||
|
|
var orConditional = new ConditionalCollections { ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>() };
|
||
|
|
foreach (var con in contitons.Where(m => m.Character == QueryCharacter.Or))
|
||
|
|
{
|
||
|
|
if (orConditional.ConditionalList.Count == 0)
|
||
|
|
{
|
||
|
|
var cond = new KeyValuePair<WhereType, ConditionalModel>
|
||
|
|
(WhereType.And, new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = con.Key,
|
||
|
|
ConditionalType = (ConditionalType)(int)con.Operator,
|
||
|
|
FieldValue = con.Value.ToString()
|
||
|
|
});
|
||
|
|
orConditional.ConditionalList.Add(cond);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var cond = new KeyValuePair<WhereType, ConditionalModel>
|
||
|
|
(WhereType.Or, new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = con.Key,
|
||
|
|
ConditionalType = (ConditionalType)(int)con.Operator,
|
||
|
|
FieldValue = con.Value.ToString()
|
||
|
|
});
|
||
|
|
orConditional.ConditionalList.Add(cond);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region And条件组装
|
||
|
|
foreach (var con in contitons.Where(m => m.Character == QueryCharacter.And))
|
||
|
|
{
|
||
|
|
if (con.Key.Contains(","))
|
||
|
|
{
|
||
|
|
ParseKeyOr(con, orConditional);
|
||
|
|
}
|
||
|
|
else if (con.Operator == QueryOperator.DateRange)
|
||
|
|
{
|
||
|
|
conds.AddRange(ParseRange(con, con.Operator));
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
conds.Add(new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = con.Key,
|
||
|
|
ConditionalType = (ConditionalType)(int)con.Operator,
|
||
|
|
FieldValue = con.Value.ToString()
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
//Or条件是否存在
|
||
|
|
if (orConditional.ConditionalList.Count > 0)
|
||
|
|
{
|
||
|
|
conds.Add(orConditional);
|
||
|
|
}
|
||
|
|
|
||
|
|
return conds;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Key逗号隔开转换Or条件
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="condition">过滤条件</param>
|
||
|
|
/// <param name="conditionalCollections">条件集合</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
private void ParseKeyOr(QueryCondition condition, ConditionalCollections conditionalCollections)
|
||
|
|
{
|
||
|
|
var objectKeys = condition.Key.Split(',');
|
||
|
|
foreach (var objKey in objectKeys)
|
||
|
|
{
|
||
|
|
if (conditionalCollections.ConditionalList.Count == 0)
|
||
|
|
{
|
||
|
|
var cond = new KeyValuePair<WhereType, ConditionalModel>
|
||
|
|
(WhereType.And, new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = objKey,
|
||
|
|
ConditionalType = (ConditionalType)(int)condition.Operator,
|
||
|
|
FieldValue = condition.Value.ToString()
|
||
|
|
});
|
||
|
|
conditionalCollections.ConditionalList.Add(cond);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var cond = new KeyValuePair<WhereType, ConditionalModel>
|
||
|
|
(WhereType.Or, new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = objKey,
|
||
|
|
ConditionalType = (ConditionalType)(int)condition.Operator,
|
||
|
|
FieldValue = condition.Value.ToString()
|
||
|
|
});
|
||
|
|
conditionalCollections.ConditionalList.Add(cond);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 转换区域
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="condition">过滤条件</param>
|
||
|
|
/// <param name="queryOperator">条件类型</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
private List<ConditionalModel> ParseRange(QueryCondition condition, QueryOperator queryOperator)
|
||
|
|
{
|
||
|
|
var objectValue = condition.Value.ToString().Split('|');
|
||
|
|
|
||
|
|
var conditionalList = new List<ConditionalModel>();
|
||
|
|
if (objectValue.Length == 2)
|
||
|
|
{
|
||
|
|
var startValue = objectValue[0];
|
||
|
|
var endValue = objectValue[1];
|
||
|
|
if (queryOperator == QueryOperator.DateRange)
|
||
|
|
{
|
||
|
|
if (startValue.IndexOf(":", StringComparison.Ordinal) == -1)
|
||
|
|
{
|
||
|
|
startValue = startValue + " 00:00:00";
|
||
|
|
}
|
||
|
|
if (endValue.IndexOf(":", StringComparison.Ordinal) == -1)
|
||
|
|
{
|
||
|
|
endValue = endValue + " 23:59:59";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrWhiteSpace(objectValue[0]))
|
||
|
|
{
|
||
|
|
conditionalList.Add(new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = condition.Key,
|
||
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
||
|
|
FieldValue = startValue
|
||
|
|
});
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrWhiteSpace(objectValue[1]))
|
||
|
|
{
|
||
|
|
conditionalList.Add(new ConditionalModel()
|
||
|
|
{
|
||
|
|
FieldName = condition.Key,
|
||
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
||
|
|
FieldValue = endValue
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return conditionalList;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 排序转换
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="orderBys">排序</param>
|
||
|
|
/// <returns>值</returns>
|
||
|
|
private string ParseOrderBy(List<OrderSortEntity> orderBys)
|
||
|
|
{
|
||
|
|
var conds = "";
|
||
|
|
foreach (var con in orderBys)
|
||
|
|
{
|
||
|
|
switch (con.Order)
|
||
|
|
{
|
||
|
|
case OrderSequence.Asc:
|
||
|
|
conds += $"{con.Sort} asc,";
|
||
|
|
break;
|
||
|
|
case OrderSequence.Desc:
|
||
|
|
conds += $"{con.Sort} desc,";
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
throw new ArgumentOutOfRangeException();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return conds.TrimEnd(',');
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
SqlSugarDb.Ado.Dispose();
|
||
|
|
SqlSugarDb.Dispose();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|