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
{
///
/// 数据库连接信息
///
private static readonly string m_connString = ConfigurationManager.AppSettings["mysqlstring"];
///
/// DB实例化
///
///
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;
}
///
/// 获取分页数据
///
///
///
///
///
///
///
public static Tuple, int> GetPage(Pagination pagination, Expression> whereExpression,
Expression> orderExpression, OrderByType orderType)
{
using (var db = GetInstance())
{
var totalCount = 0;
List lstResult = null;
if (orderExpression == null)
{
lstResult = db.Queryable().Where(whereExpression).ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
}
else
{
lstResult = db.Queryable().Where(whereExpression).OrderBy(orderExpression, orderType).
ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
}
return new Tuple, int>(lstResult, totalCount);
}
}
public static List GetList(int topNum, Expression> whereExpression, Expression> orderExpression, OrderByType orderType)
{
using (var db = GetInstance())
{
var _list = new List();
if (orderExpression == null)
{
if (topNum == -1)
{
_list = db.Queryable().Where(whereExpression).ToList();
}
else
{
_list = db.Queryable().Where(whereExpression).Take(topNum).ToList();
}
}
else
{
if (topNum == -1)
{
_list = db.Queryable().Where(whereExpression).OrderBy(orderExpression, orderType).ToList();
}
else
{
_list = db.Queryable().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 @" ///
/// {PropertyDescription}
/// ";
})
.SettingPropertyTemplate(old =>
{
return old;
})
//.SettingConstructorTemplate(old =>
// {
// return old;//修改构造函数
// })
.CreateClassFile(@"D:\Model\bak", "FlowManage.Model.Model");
}
}
}
}