using DataEntity.Device;
|
using DataEntity.Page;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Data;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
using XCommon.SqlSugar;
|
using DbType = SqlSugar.DbType;
|
|
namespace DataRWDAL.Base
|
{
|
public class BaseDB
|
{
|
/// <summary>
|
/// 数据库连接信息
|
/// </summary>
|
private static readonly string m_connString = ConfigurationManager.AppSettings["mysqlstring"];
|
|
/// <summary>
|
/// DB实例化
|
/// </summary>
|
/// <returns></returns>
|
public static SqlSugarClient GetInstance()
|
{
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
{
|
ConnectionString = m_connString,
|
DbType = DbType.MySql,
|
IsAutoCloseConnection = true,
|
ConfigureExternalServices = new ConfigureExternalServices()
|
{
|
SqlFuncServices = ExtMethods.GetExpMethods,
|
}
|
});
|
|
db.Ado.IsEnableLogEvent = true;
|
return db;
|
}
|
|
/// <summary>
|
/// 获取分页数据
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="pagination"></param>
|
/// <param name="whereExpression"></param>
|
/// <param name="orderExpression"></param>
|
/// <param name="orderType"></param>
|
/// <returns></returns>
|
public static Tuple<List<T>, int> GetPage<T>(Pagination pagination, Expression<Func<T, bool>> whereExpression,
|
Expression<Func<T, object>> orderExpression, OrderByType orderType)
|
{
|
using (var db = GetInstance())
|
{
|
var totalCount = 0;
|
List<T> lstResult = null;
|
if (orderExpression == null)
|
{
|
lstResult = db.Queryable<T>().Where(whereExpression).ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
|
}
|
else
|
{
|
lstResult = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).
|
ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
|
}
|
|
return new Tuple<List<T>, int>(lstResult, totalCount);
|
}
|
}
|
|
public static List<T> GetList<T>(int topNum, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderType)
|
{
|
using (var db = GetInstance())
|
{
|
var _list = new List<T>();
|
if (orderExpression == null)
|
{
|
if (topNum == -1)
|
{
|
_list = db.Queryable<T>().Where(whereExpression).ToList();
|
}
|
else
|
{
|
_list = db.Queryable<T>().Where(whereExpression).Take(topNum).ToList();
|
}
|
}
|
else
|
{
|
if (topNum == -1)
|
{
|
_list = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).ToList();
|
}
|
else
|
{
|
_list = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).Take(topNum).ToList();
|
}
|
}
|
return _list;
|
}
|
}
|
|
public static void CreateClassFile()
|
{
|
using (var db = GetInstance())
|
{
|
//db.DbFirst.CreateClassFile(@"D:\Model\bak", "FlowManage.Model.Model");
|
|
db.DbFirst
|
.SettingClassTemplate(old =>
|
{
|
return "{using}\r\n\r\nnamespace {Namespace}\r\n{\r\n\t{ClassDescription}{SugarTable}\r\n\tpublic partial class {ClassName} : BaseModel\r\n\t {\r\n\t{PropertyName}\r\n\t }\r\n}\r\n\r\n";
|
})
|
.SettingNamespaceTemplate(old =>
|
{
|
//修改using命名空间
|
return "using SqlSugar;\r\nusing System;\r\nusing System.ComponentModel;\r\nusing System.Linq;\r\nusing System.Text;";
|
})
|
.SettingPropertyDescriptionTemplate(old =>
|
{
|
return @" /// <summary>
|
/// {PropertyDescription}
|
/// </summary>";
|
})
|
.SettingPropertyTemplate(old =>
|
{
|
return old;
|
})
|
//.SettingConstructorTemplate(old =>
|
// {
|
// return old;//修改构造函数
|
// })
|
.CreateClassFile(@"D:\Model\bak", "FlowManage.Model.Model");
|
}
|
}
|
}
|
}
|