1
This commit is contained in:
44
iMES.Bi.Data/Appsettings.cs
Normal file
44
iMES.Bi.Data/Appsettings.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Json;
|
||||
using System;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// appsettings.json操作类
|
||||
/// </summary>
|
||||
public class Appsettings
|
||||
{
|
||||
static IConfiguration Configuration { get; set; }
|
||||
static Appsettings()
|
||||
{
|
||||
//ReloadOnChange = true 当appsettings.json被修改时重新加载
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })//请注意要把当前appsetting.json 文件->右键->属性->复制到输出目录->始终复制
|
||||
.Build();
|
||||
}
|
||||
/// <summary>
|
||||
/// 封装要操作的字符
|
||||
/// </summary>
|
||||
/// <param name="sections"></param>
|
||||
/// <returns></returns>
|
||||
public static string app(params string[] sections)
|
||||
{
|
||||
try
|
||||
{
|
||||
var val = string.Empty;
|
||||
for (int i = 0; i < sections.Length; i++)
|
||||
{
|
||||
val += sections[i] + ":";
|
||||
}
|
||||
|
||||
return Configuration[val.TrimEnd(':')];
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
270
iMES.Bi.Data/BaseEFDao.cs
Normal file
270
iMES.Bi.Data/BaseEFDao.cs
Normal file
@@ -0,0 +1,270 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// 公共接口
|
||||
/// </summary>
|
||||
public class BaseEFDao<T> : IBaseDao<T> where T : class, new() //限制T为class
|
||||
{
|
||||
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
|
||||
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来处理T表的常用操作
|
||||
|
||||
public static string ConnectionString = Appsettings.app(new string[] { "Connection", "DbConnectionString" });//获取连接字符串
|
||||
public static string DBType = Appsettings.app(new string[] { "Connection", "DBType" });//获取连接字符串
|
||||
|
||||
|
||||
public BaseEFDao()
|
||||
{
|
||||
|
||||
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = ConnectionString,
|
||||
DbType = DBType == "MySql" ? SqlSugar.DbType.MySql : SqlSugar.DbType.SqlServer,
|
||||
InitKeyType = InitKeyType.SystemTable,//从特性读取主键和自增列信息
|
||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
|
||||
|
||||
});
|
||||
// 调式代码 用来打印SQL
|
||||
Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public void CModel()
|
||||
{
|
||||
Db.DbFirst.CreateClassFile("E:\\Demo", "iMES.Bi.Data");
|
||||
}
|
||||
public virtual IEnumerable<T> GetALLEntities()
|
||||
{
|
||||
|
||||
//AsNoTracking不记录数据变化状况
|
||||
return CurrentDb.GetList();
|
||||
|
||||
}
|
||||
public virtual IEnumerable<T> GetEntities(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
|
||||
//AsNoTracking不记录数据变化状况
|
||||
return CurrentDb.GetList(exp).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据条件查找
|
||||
/// </summary>
|
||||
/// <param name="exp">lambda查询条件where</param>
|
||||
/// <returns></returns>
|
||||
public virtual T GetEntity(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return CurrentDb.GetList(exp).SingleOrDefault();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有Entity(立即执行请使用ToList()
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public virtual IEnumerable<T> GetEntities(string CommandText)
|
||||
{
|
||||
return Db.SqlQueryable<T>("select * from " + typeof(T).Name + " where 1=1 and " + CommandText).ToList();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 插入Entity
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Insert(T entity)
|
||||
{
|
||||
entity= Db.Insertable<T>(entity).ExecuteReturnEntity();
|
||||
//int dataID = CurrentDb.InsertReturnIdentity(entity);
|
||||
//List<string> List = Db.DbMaintenance.GetIsIdentities(entity.GetType().Name);
|
||||
//if (List.Count > 0)
|
||||
//{
|
||||
// //如果有自增,赋值
|
||||
// entity.GetType().GetProperty(List[0].ToString()).SetValue(entity, dataID, null);
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同时插入多个实体。
|
||||
/// </summary>
|
||||
/// <param name="entities"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Insert(IEnumerable<T> entities)
|
||||
{
|
||||
int Return = 0;
|
||||
if (entities.Count() > 0)
|
||||
{
|
||||
Return = Db.Insertable(entities.ToArray()).ExecuteCommand();
|
||||
}
|
||||
return Return > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新Entity(注意这里使用的傻瓜式更新,可能性能略低)
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update(T entity)
|
||||
{
|
||||
return CurrentDb.Update(entity);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除Entity
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(T entity)
|
||||
{
|
||||
return CurrentDb.Delete(entity);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 批量删除Entity
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return CurrentDb.Delete(exp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查找
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public virtual DataTable GetDTByCommand(string CommandText)
|
||||
{
|
||||
return Db.Ado.GetDataTable(CovSQL(CommandText));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取链接字符串
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public string GetDBString()
|
||||
{
|
||||
return Db.CurrentConnectionConfig.ConnectionString;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//执行SQL语句
|
||||
public void ExsSql(string sql)
|
||||
{
|
||||
List<SugarParameter> parameters = null;
|
||||
Db.Ado.ExecuteCommand(CovSQL(sql), parameters);
|
||||
}
|
||||
|
||||
public object ExsSclarSql(string sql)
|
||||
{
|
||||
List<SugarParameter> parameters = null;
|
||||
return Db.Ado.GetString(CovSQL(sql), parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 替换
|
||||
/// </summary>
|
||||
/// <param name="strSQL"></param>
|
||||
/// <returns></returns>
|
||||
private string CovSQL(string strSQL)
|
||||
{
|
||||
if (Db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql)//MYSQL数据库
|
||||
{
|
||||
strSQL = strSQL.Replace("isnull", "ifnull").Replace("ISNULL", "IFNULL");
|
||||
}
|
||||
return strSQL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 数据分页
|
||||
/// </summary>
|
||||
/// <param name="viewName">表名</param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="pageSize">默认20</param>
|
||||
/// <param name="pageNo">页数</param>
|
||||
/// <param name="orderString">排序</param>
|
||||
/// <param name="whereString">可选</param>
|
||||
/// <param name="recordTotal">总数</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataPager(string viewName, string fieldName, int pageSize, int pageNo, string orderString, string whereString, ref int recordTotal)
|
||||
{
|
||||
string strSQL = "select " + fieldName + " from " + viewName + " where " + whereString;
|
||||
DataTable dt = Db.SqlQueryable<Object>(CovSQL(strSQL)).OrderBy(orderString).ToDataTablePage(pageNo, pageSize, ref recordTotal);
|
||||
return dt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 数据分页
|
||||
/// </summary>
|
||||
/// <param name="strSQL"></param>
|
||||
/// <param name="strOrder"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="pageNo"></param>
|
||||
/// <param name="recordTotal"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataPager(string strSQL,string strOrder, int pageSize, int pageNo, ref int recordTotal)
|
||||
{
|
||||
DataTable dt = Db.SqlQueryable<Object>(CovSQL(strSQL)).OrderBy(strOrder).ToDataTablePage(pageNo, pageSize, ref recordTotal);
|
||||
return dt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 行专列
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public string GetDTHZL(string ExtendModes, string pdid)
|
||||
{
|
||||
string strSQL = "";
|
||||
if (Db.CurrentConnectionConfig.DbType == 0)//MYSQL数据库
|
||||
{
|
||||
string strTemp = "";
|
||||
foreach (string filename in ExtendModes.Split(','))
|
||||
{
|
||||
strTemp = strTemp + " MAX(CASE ExFiledColumn WHEN '" + filename + "' THEN ExtendDataValue ELSE 0 END ) " + filename + ",";
|
||||
}
|
||||
strTemp = strTemp.TrimEnd(',');
|
||||
strSQL = " SELECT * FROM ( SELECT uuid() AS ID, DataID,Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate, " + strTemp + " FROM JH_Auth_ExtendData INNER JOIN Yan_WF_PI ON JH_Auth_ExtendData.DataID=Yan_WF_PI.ID AND JH_Auth_ExtendData.PDID='" + pdid + "' GROUP BY DataID,Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate ) T ";
|
||||
|
||||
}
|
||||
else //sqlServer数据库
|
||||
{
|
||||
strSQL = " SELECT NEWID() AS ID, * FROM ( SELECT Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,JH_Auth_ExtendData.DataID, Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate, JH_Auth_ExtendMode.TableFiledColumn, ExtendDataValue from JH_Auth_ExtendMode INNER JOIN JH_Auth_ExtendData ON JH_Auth_ExtendMode.PDID=JH_Auth_ExtendData.PDID and JH_Auth_ExtendMode.TableFiledColumn=JH_Auth_ExtendData.ExFiledColumn and JH_Auth_ExtendMode.PDID='" + pdid + "' INNER JOIN Yan_WF_PI ON JH_Auth_ExtendData.DataID=Yan_WF_PI.ID ) AS P PIVOT ( MAX(ExtendDataValue) FOR p.TableFiledColumn IN (" + ExtendModes + ") ) AS T ";
|
||||
|
||||
}
|
||||
return strSQL;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
237
iMES.Bi.Data/BaseEFDaoJW.cs
Normal file
237
iMES.Bi.Data/BaseEFDaoJW.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// 公共接口
|
||||
/// </summary>
|
||||
public class BaseEFDaoJW<T> : IBaseDao<T> where T : class, new() //限制T为class
|
||||
{
|
||||
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
|
||||
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来处理T表的常用操作
|
||||
|
||||
public static string ConnectionString = Appsettings.app(new string[] { "Connection", "DbConnectionString" });//获取连接字符串
|
||||
public static string DBType = Appsettings.app(new string[] { "Connection", "DBType" });//获取连接字符串
|
||||
|
||||
public BaseEFDaoJW()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = ConnectionString,
|
||||
DbType = DBType == "MySql" ? SqlSugar.DbType.MySql : SqlSugar.DbType.SqlServer,
|
||||
InitKeyType = InitKeyType.SystemTable,//从特性读取主键和自增列信息
|
||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
|
||||
|
||||
});
|
||||
// 调式代码 用来打印SQL
|
||||
Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
//WriteLOG(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
}
|
||||
public void CModel()
|
||||
{
|
||||
Db.DbFirst.CreateClassFile("E:\\Demo", "iMES.Bi.Data");
|
||||
}
|
||||
public virtual IEnumerable<T> GetALLEntities()
|
||||
{
|
||||
|
||||
//AsNoTracking不记录数据变化状况
|
||||
return CurrentDb.GetList();
|
||||
}
|
||||
public virtual IEnumerable<T> GetEntities(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
//AsNoTracking不记录数据变化状况
|
||||
return CurrentDb.GetList(exp).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据条件查找
|
||||
/// </summary>
|
||||
/// <param name="exp">lambda查询条件where</param>
|
||||
/// <returns></returns>
|
||||
public virtual T GetEntity(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return CurrentDb.GetList(exp).SingleOrDefault();
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取所有Entity(立即执行请使用ToList()
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public virtual IEnumerable<T> GetEntities(string CommandText)
|
||||
{
|
||||
return Db.SqlQueryable<T>("select * from " + typeof(T).Name + " where 1=1 and " + CommandText).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入Entity
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Insert(T entity)
|
||||
{
|
||||
entity= Db.Insertable<T>(entity).ExecuteReturnEntity();
|
||||
//int dataID = CurrentDb.InsertReturnIdentity(entity);
|
||||
//List<string> List = Db.DbMaintenance.GetIsIdentities(entity.GetType().Name);
|
||||
//if (List.Count > 0)
|
||||
//{
|
||||
// //如果有自增,赋值
|
||||
// entity.GetType().GetProperty(List[0].ToString()).SetValue(entity, dataID, null);
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同时插入多个实体。
|
||||
/// </summary>
|
||||
/// <param name="entities"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Insert(IEnumerable<T> entities)
|
||||
{
|
||||
int Return = 0;
|
||||
if (entities.Count() > 0)
|
||||
{
|
||||
Return = Db.Insertable(entities.ToArray()).ExecuteCommand();
|
||||
}
|
||||
return Return > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新Entity(注意这里使用的傻瓜式更新,可能性能略低)
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update(T entity)
|
||||
{
|
||||
return CurrentDb.Update(entity);
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除Entity
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(T entity)
|
||||
{
|
||||
return CurrentDb.Delete(entity);
|
||||
}
|
||||
/// <summary>
|
||||
/// 批量删除Entity
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return CurrentDb.Delete(exp);
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据条件查找
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public virtual DataTable GetDTByCommand(string CommandText)
|
||||
{
|
||||
return Db.Ado.GetDataTable(CovSQL(CommandText));
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取链接字符串
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public string GetDBString()
|
||||
{
|
||||
return Db.CurrentConnectionConfig.ConnectionString;
|
||||
}
|
||||
//执行SQL语句
|
||||
public void ExsSql(string sql)
|
||||
{
|
||||
List<SugarParameter> parameters = null;
|
||||
Db.Ado.ExecuteCommand(CovSQL(sql), parameters);
|
||||
}
|
||||
public object ExsSclarSql(string sql)
|
||||
{
|
||||
List<SugarParameter> parameters = null;
|
||||
return Db.Ado.GetString(CovSQL(sql), parameters);
|
||||
}
|
||||
/// <summary>
|
||||
/// 替换
|
||||
/// </summary>
|
||||
/// <param name="strSQL"></param>
|
||||
/// <returns></returns>
|
||||
private string CovSQL(string strSQL)
|
||||
{
|
||||
if (Db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql)//MYSQL数据库
|
||||
{
|
||||
strSQL = strSQL.Replace("isnull", "ifnull").Replace("ISNULL", "IFNULL");
|
||||
}
|
||||
return strSQL;
|
||||
}
|
||||
/// <summary>
|
||||
/// 数据分页
|
||||
/// </summary>
|
||||
/// <param name="viewName">表名</param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="pageSize">默认20</param>
|
||||
/// <param name="pageNo">页数</param>
|
||||
/// <param name="orderString">排序</param>
|
||||
/// <param name="whereString">可选</param>
|
||||
/// <param name="recordTotal">总数</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataPager(string viewName, string fieldName, int pageSize, int pageNo, string orderString, string whereString, ref int recordTotal)
|
||||
{
|
||||
string strSQL = "select " + fieldName + " from " + viewName + " where " + whereString;
|
||||
DataTable dt = Db.SqlQueryable<Object>(CovSQL(strSQL)).OrderBy(orderString).ToDataTablePage(pageNo, pageSize, ref recordTotal);
|
||||
return dt;
|
||||
}
|
||||
/// <summary>
|
||||
/// 数据分页
|
||||
/// </summary>
|
||||
/// <param name="strSQL"></param>
|
||||
/// <param name="strOrder"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="pageNo"></param>
|
||||
/// <param name="recordTotal"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataPager(string strSQL,string strOrder, int pageSize, int pageNo, ref int recordTotal)
|
||||
{
|
||||
DataTable dt = Db.SqlQueryable<Object>(CovSQL(strSQL)).OrderBy(strOrder).ToDataTablePage(pageNo, pageSize, ref recordTotal);
|
||||
return dt;
|
||||
}
|
||||
/// <summary>
|
||||
/// 行专列
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
public string GetDTHZL(string ExtendModes, string pdid)
|
||||
{
|
||||
string strSQL = "";
|
||||
if (Db.CurrentConnectionConfig.DbType == 0)//MYSQL数据库
|
||||
{
|
||||
string strTemp = "";
|
||||
foreach (string filename in ExtendModes.Split(','))
|
||||
{
|
||||
strTemp = strTemp + " MAX(CASE ExFiledColumn WHEN '" + filename + "' THEN ExtendDataValue ELSE 0 END ) " + filename + ",";
|
||||
}
|
||||
strTemp = strTemp.TrimEnd(',');
|
||||
strSQL = " SELECT * FROM ( SELECT uuid() AS ID, DataID,Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate, " + strTemp + " FROM JH_Auth_ExtendData INNER JOIN Yan_WF_PI ON JH_Auth_ExtendData.DataID=Yan_WF_PI.ID AND JH_Auth_ExtendData.PDID='" + pdid + "' GROUP BY DataID,Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate ) T ";
|
||||
|
||||
}
|
||||
else //sqlServer数据库
|
||||
{
|
||||
strSQL = " SELECT NEWID() AS ID, * FROM ( SELECT Yan_WF_PI.ISGD,Yan_WF_PI.BranchNo,Yan_WF_PI.BranchName,JH_Auth_ExtendData.DataID, Yan_WF_PI.CRUser,Yan_WF_PI.CRUserName, Yan_WF_PI.CRDate, JH_Auth_ExtendMode.TableFiledColumn, ExtendDataValue from JH_Auth_ExtendMode INNER JOIN JH_Auth_ExtendData ON JH_Auth_ExtendMode.PDID=JH_Auth_ExtendData.PDID and JH_Auth_ExtendMode.TableFiledColumn=JH_Auth_ExtendData.ExFiledColumn and JH_Auth_ExtendMode.PDID='" + pdid + "' INNER JOIN Yan_WF_PI ON JH_Auth_ExtendData.DataID=Yan_WF_PI.ID ) AS P PIVOT ( MAX(ExtendDataValue) FOR p.TableFiledColumn IN (" + ExtendModes + ") ) AS T ";
|
||||
|
||||
}
|
||||
return strSQL;
|
||||
}
|
||||
}
|
||||
}
|
||||
173
iMES.Bi.Data/DBFactory.cs
Normal file
173
iMES.Bi.Data/DBFactory.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public class DBFactory
|
||||
{
|
||||
private SqlSugarClient db;
|
||||
public DBFactory()
|
||||
{
|
||||
}
|
||||
public static string DBType = Appsettings.app(new string[] { "Connection", "DBType" });//获取连接字符串
|
||||
public DBFactory(string ConnectionString)
|
||||
{
|
||||
db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = ConnectionString, //必填, 数据库连接字符串
|
||||
DbType = DBType == "MySql" ? SqlSugar.DbType.MySql : SqlSugar.DbType.SqlServer, //必填, 数据库类型
|
||||
IsAutoCloseConnection = true, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
|
||||
InitKeyType = InitKeyType.SystemTable //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
|
||||
});
|
||||
db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件
|
||||
{
|
||||
string strSQL = sql;
|
||||
SugarParameter[] pr = pars;
|
||||
};
|
||||
}
|
||||
public DBFactory(string DBType, string DBIP, string Port, string DBName, string DBUser, string DBPwd)
|
||||
{
|
||||
string connectionString = "";
|
||||
var DbType = SqlSugar.DbType.MySql;
|
||||
if (DBType.ToLower() == "sqlserver")
|
||||
{
|
||||
if (string.IsNullOrEmpty(Port))
|
||||
{
|
||||
Port = "1433";
|
||||
}
|
||||
DbType = SqlSugar.DbType.SqlServer;
|
||||
connectionString = string.Format("Data Source ={0},{4};Initial Catalog ={1};User Id ={2};Password={3};", DBIP, DBName, DBUser, DBPwd, Port);
|
||||
}
|
||||
else if (DBType.ToLower() == "mysql")
|
||||
{
|
||||
DbType = SqlSugar.DbType.MySql;
|
||||
if (string.IsNullOrEmpty(Port))
|
||||
{
|
||||
Port = "3306";
|
||||
}
|
||||
connectionString = string.Format("server={0};userid={1};password={2};database={3};PORT={4};", DBIP, DBUser, DBPwd, DBName, Port);
|
||||
}
|
||||
db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = connectionString, //必填, 数据库连接字符串
|
||||
DbType = DbType, //必填, 数据库类型
|
||||
IsAutoCloseConnection = true, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
|
||||
InitKeyType = InitKeyType.SystemTable //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
|
||||
});
|
||||
|
||||
db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件
|
||||
{
|
||||
string strSQL = sql;
|
||||
SugarParameter[] pr = pars;
|
||||
};
|
||||
}
|
||||
//测试连接
|
||||
public bool TestConn()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (db.Ado.Connection.State != ConnectionState.Open)
|
||||
{
|
||||
db.Ado.Connection.Open();
|
||||
}
|
||||
if (db.Ado.Connection.State == ConnectionState.Open)
|
||||
{
|
||||
db.Ado.Connection.Close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return false;
|
||||
}
|
||||
public string GetDBType()
|
||||
{
|
||||
return db.DbMaintenance.Context.CurrentConnectionConfig.DbType.ToString();
|
||||
}
|
||||
public void CModel()
|
||||
{
|
||||
db.DbFirst.CreateClassFile("D:\\Demo\\1");
|
||||
}
|
||||
public DataTable GetTables()
|
||||
{
|
||||
DataTable data = db.Ado.GetDataTable("select name ,type_desc from sys.objects where type_desc in ('USER_TABLE','VIEW') ORDER BY name");
|
||||
return data;
|
||||
}
|
||||
public List<DbTableInfo> GetDBTables()
|
||||
{
|
||||
List<DbTableInfo> data = db.DbMaintenance.GetTableInfoList();
|
||||
return data;
|
||||
}
|
||||
public SqlSugarClient GetDBClient()
|
||||
{
|
||||
return db;
|
||||
}
|
||||
public DataTable GetSQL(string strSQL)
|
||||
{
|
||||
DataTable data = db.Ado.GetDataTable(strSQL);
|
||||
return data;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取SQL模型数据
|
||||
/// </summary>
|
||||
/// <param name="DS"></param>
|
||||
/// <param name="wd"></param>
|
||||
/// <param name="dl"></param>
|
||||
/// <param name="strTableFiled"></param>
|
||||
/// <param name="strDataCount"></param>
|
||||
/// <param name="strWhere"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetYBData(string DSSQL, string wd, string dl, string strDataCount, string strWhere, string Order, int pageNo, int pageSize, string wdgroup, string wdhaving, ref int recordTotal, ref string strReturnSQL)
|
||||
{
|
||||
DataTable dt = new DataTable();
|
||||
String strfids = (wd + "," + dl).Trim(',');
|
||||
var Q = db.SqlQueryable<Object>(DSSQL).WhereIF(!string.IsNullOrWhiteSpace(strWhere), strWhere).Select(strfids).OrderByIF(!string.IsNullOrWhiteSpace(Order), Order).Distinct();
|
||||
if (!string.IsNullOrWhiteSpace(wdgroup))
|
||||
{
|
||||
Q = Q.GroupBy(wdgroup);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(wdhaving))
|
||||
{
|
||||
Q = Q.Having(wdhaving);
|
||||
}
|
||||
if (strDataCount != "0")
|
||||
{
|
||||
Q = Q.Take(int.Parse(strDataCount));
|
||||
}
|
||||
if (pageSize == 0)
|
||||
{
|
||||
dt = Q.ToDataTable();
|
||||
}
|
||||
else
|
||||
{
|
||||
dt = Q.ToDataTablePage(pageNo, pageSize, ref recordTotal);
|
||||
}
|
||||
if (dt.Columns.Contains("RowIndex"))
|
||||
{
|
||||
///SQLSERVER多出来得一列
|
||||
dt.Columns.Remove("RowIndex");
|
||||
}
|
||||
strReturnSQL = Q.ToSql().Key;
|
||||
return dt;
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入数据
|
||||
/// </summary>
|
||||
/// <param name="DS"></param>
|
||||
/// <param name="strTable"></param>
|
||||
/// <returns></returns>
|
||||
public int InserData(Dictionary<string, object> DS, string strTable)
|
||||
{
|
||||
var t66 = db.Insertable(DS).AS(strTable).ExecuteReturnIdentity();
|
||||
return int.Parse(t66.ToString());
|
||||
}
|
||||
public int UpdateData(Dictionary<string, object> DS, string strTable)
|
||||
{
|
||||
var returndata = db.Updateable(DS).AS(strTable).ExecuteCommand();
|
||||
return int.Parse(returndata.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
62
iMES.Bi.Data/IBaseDao.cs
Normal file
62
iMES.Bi.Data/IBaseDao.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Web;
|
||||
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public interface IBaseDao<T> where T : class //限制class
|
||||
{
|
||||
#region 查询普通实现方案(基于Lambda表达式的Where查询)
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有Entity
|
||||
/// </summary>
|
||||
/// <param name="exp">Lambda条件的where</param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<T> GetEntities(Expression<Func<T, bool>> exp);
|
||||
/// <summary>
|
||||
/// 根据条件查找
|
||||
/// </summary>
|
||||
/// <param name="exp">lambda查询条件where</param>
|
||||
/// <returns></returns>
|
||||
T GetEntity(Expression<Func<T, bool>> exp);
|
||||
|
||||
#endregion 查询普通实现方案(基于Lambda表达式的Where查询)
|
||||
|
||||
#region 查询Sql语句外接接口的查询实现
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有Entity(立即执行请使用ToList()
|
||||
/// </summary>
|
||||
/// <param name="CommandText">Sql语句</param>
|
||||
/// <param name="objParams">可变参数</param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<T> GetEntities(string CommandText);
|
||||
|
||||
#endregion 查询Sql语句外接接口的查询实现
|
||||
|
||||
/// <summary>
|
||||
/// 插入Entity
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
bool Insert(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 更新Entity
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
bool Update(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 删除Entity
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
bool Delete(T entity);
|
||||
}
|
||||
}
|
||||
18
iMES.Bi.Data/Models/BI_DB_Dim.cs
Normal file
18
iMES.Bi.Data/Models/BI_DB_Dim.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_Dim
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public int? STID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string ColumnName { get; set; }
|
||||
public string ColumnType { get; set; }
|
||||
public string Dimension { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
public string ColumnSource { get; set; }
|
||||
}
|
||||
}
|
||||
19
iMES.Bi.Data/Models/BI_DB_Set.cs
Normal file
19
iMES.Bi.Data/Models/BI_DB_Set.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_Set
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public int? SID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string SName { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
public DateTime? UPDate { get; set; }
|
||||
public string Type { get; set; }
|
||||
public string Option { get; set; }
|
||||
public string DSQL { get; set; }
|
||||
}
|
||||
}
|
||||
21
iMES.Bi.Data/Models/BI_DB_Source.cs
Normal file
21
iMES.Bi.Data/Models/BI_DB_Source.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_Source
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string DBType { get; set; }
|
||||
public string DBIP { get; set; }
|
||||
public string Port { get; set; }
|
||||
public string DBName { get; set; }
|
||||
public string Schema { get; set; }
|
||||
public string DBUser { get; set; }
|
||||
public string DBPwd { get; set; }
|
||||
public string Attach { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
}
|
||||
}
|
||||
19
iMES.Bi.Data/Models/BI_DB_Table.cs
Normal file
19
iMES.Bi.Data/Models/BI_DB_Table.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_Table
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string TableName { get; set; }
|
||||
public string TableDesc { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
public int ModelID { get; set; }
|
||||
public string Remark { get; set; }
|
||||
public int DSID { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
29
iMES.Bi.Data/Models/BI_DB_Tablefiled.cs
Normal file
29
iMES.Bi.Data/Models/BI_DB_Tablefiled.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_Tablefiled
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public int? TableID { get; set; }
|
||||
public string TableName { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
public string ColumnDescription { get; set; }
|
||||
public string DataType { get; set; }
|
||||
public string IsPrimarykey { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
public string DbColumnName { get; set; }
|
||||
public string IsIdentity { get; set; }
|
||||
public string IsNullable { get; set; }
|
||||
public string isPkey { get; set; }
|
||||
public string DecimalDigits { get; set; }
|
||||
public int Length { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
public string Scale { get; set; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
18
iMES.Bi.Data/Models/BI_DB_YBP.cs
Normal file
18
iMES.Bi.Data/Models/BI_DB_YBP.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public partial class BI_DB_YBP
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public int? DimID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string YBContent { get; set; }
|
||||
public string YBType { get; set; }
|
||||
public string YBOption { get; set; }
|
||||
public string CRUser { get; set; }
|
||||
public DateTime? CRDate { get; set; }
|
||||
public string Remark { get; set; }
|
||||
}
|
||||
}
|
||||
108
iMES.Bi.Data/SqlHelp.cs
Normal file
108
iMES.Bi.Data/SqlHelp.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
namespace iMES.Bi.Data
|
||||
{
|
||||
public class SqlHelp
|
||||
{
|
||||
/// <summary>
|
||||
/// 将SQLserver的连接字符串语法转为Mysql的语法
|
||||
/// </summary>
|
||||
/// <param name="objs"></param>
|
||||
/// <returns></returns>
|
||||
public static string concat(string strConSQL)
|
||||
{
|
||||
string strReturn = strConSQL;
|
||||
string strDbType = Appsettings.app("DBType");
|
||||
if (strDbType == "0")
|
||||
{
|
||||
strReturn = "CONCAT(" + strReturn.Replace('+', ',') + ")";
|
||||
}
|
||||
return strReturn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析日期格式
|
||||
/// </summary>
|
||||
/// <param name="objs"></param>
|
||||
/// <returns></returns>
|
||||
public static string TADate(string strWDColumCode, string strDbType, ref string strGroup)
|
||||
{
|
||||
string strFiledName = strWDColumCode.Split('_')[0];
|
||||
string Format = strWDColumCode.Split('_')[1];
|
||||
|
||||
string strReturn = "";
|
||||
if (strDbType == "MYSQL")
|
||||
{
|
||||
switch (Format)
|
||||
{
|
||||
case "YYYY":
|
||||
strGroup = " DATE_FORMAT(" + strFiledName + ",'%Y') ";
|
||||
strReturn = " DATE_FORMAT(" + strFiledName + ",'%Y') " + strWDColumCode;
|
||||
break;
|
||||
case "YYYYMM":
|
||||
strGroup = " DATE_FORMAT(" + strFiledName + ",'%Y%m') ";
|
||||
strReturn = " DATE_FORMAT(" + strFiledName + ",'%Y%m') " + strWDColumCode;
|
||||
break;
|
||||
case "YYYYMMDD":
|
||||
strGroup = " DATE_FORMAT(" + strFiledName + ",'%Y%m%d') ";
|
||||
strReturn = " DATE_FORMAT(" + strFiledName + ",'%Y%m%d') " + strWDColumCode;
|
||||
break;
|
||||
case "YYYYMMDDHH":
|
||||
strGroup = " DATE_FORMAT(" + strFiledName + ",'%Y%m%d%h') ";
|
||||
strReturn = " DATE_FORMAT(" + strFiledName + ",'%Y%m%d%h') " + strWDColumCode;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (strDbType == "SQLSERVER")
|
||||
{
|
||||
switch (Format)
|
||||
{
|
||||
case "YYYY":
|
||||
strGroup = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),4) ";
|
||||
strReturn = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),4) AS '" + strWDColumCode + "'";
|
||||
break;
|
||||
case "YYYYMM":
|
||||
strGroup = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),6) ";
|
||||
strReturn = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),6) AS '" + strWDColumCode + "'";
|
||||
break;
|
||||
case "YYYYMMDD":
|
||||
strGroup = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),8) ";
|
||||
strReturn = " LEFT(CONVERT(varchar(100)," + strFiledName + ",112),8) AS '" + strWDColumCode + "'";
|
||||
break;
|
||||
case "YYYYMMDDHH":
|
||||
strGroup = " (LEFT(CONVERT(varchar(100)," + strFiledName + ",112),8)+RIGHT('00'+cast(DATEPART(hh," + strFiledName + ") as VARCHAR),2 ) ) ";
|
||||
strReturn = " (LEFT(CONVERT(varchar(100)," + strFiledName + ",112),8)+RIGHT('00'+cast(DATEPART(hh," + strFiledName + ") as VARCHAR),2 ) ) AS '" + strWDColumCode + "'";
|
||||
break;
|
||||
}
|
||||
}
|
||||
return strReturn;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理链接字符串废弃
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string concatold(params string[] objs)
|
||||
{
|
||||
string strReturn = "";
|
||||
string strDbType = Appsettings.app("DBType");
|
||||
if (strDbType == "0")
|
||||
{
|
||||
for (int i = 0; i < objs.Length; i++)
|
||||
{
|
||||
strReturn = strReturn + ",";
|
||||
}
|
||||
strReturn = " CONCAT(" + strReturn.TrimEnd(',') + " ) ";
|
||||
}
|
||||
if (strDbType == "1")
|
||||
{
|
||||
for (int i = 0; i < objs.Length; i++)
|
||||
{
|
||||
strReturn = objs[i].ToString() + "+";
|
||||
}
|
||||
strReturn.TrimEnd('+');
|
||||
}
|
||||
|
||||
return strReturn;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
iMES.Bi.Data/iMES.Bi.Data.csproj
Normal file
12
iMES.Bi.Data/iMES.Bi.Data.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.211" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user