using Learun.Cache.Base;
using Learun.Cache.Factory;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
namespace Learun.Application.Base.SystemModule
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:数据源
///
public class DataSourceBLL : DataSourceIBLL
{
private DataSourceService dataSourceService = new DataSourceService();
private DatabaseLinkIBLL databaseLinkIBLL = new DatabaseLinkBLL();
#region 缓存定义
private ICache cache = CacheFactory.CaChe();
private string cacheKey = "Learun_adms_datasource_";// +编号
#endregion
#region 获取数据
///
/// 获取分页数据
///
/// 分页参数
/// 关键字
///
public IEnumerable GetPageList(Pagination pagination, string keyword)
{
try
{
return dataSourceService.GetPageList(pagination, keyword);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取列表数据
///
///
public IEnumerable GetList()
{
try
{
return dataSourceService.GetList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取实体
///
/// 编号
///
public DataSourceEntity GetEntityByCode(string code)
{
try
{
DataSourceEntity entity = dataSourceService.GetEntityByCode(code);
return entity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除数据源
///
/// 主键
public void DeleteEntity(string keyValue)
{
try
{
DataSourceEntity entity = dataSourceService.GetEntity(keyValue);
cache.Remove(cacheKey + entity.F_Code, CacheId.dataSource);
dataSourceService.DeleteEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存(新增、修改)
///
/// 主键值
/// 数据源实体
///
public bool SaveEntity(string keyValue, DataSourceEntity dataSourceEntity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
cache.Remove(cacheKey + dataSourceEntity.F_Code, CacheId.dataSource);
}
return dataSourceService.SaveEntity(keyValue, dataSourceEntity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 扩展方法
///
/// 获取数据源的数据
///
/// 数据源编码
/// sql查询条件语句
/// 查询条件
///
public DataTable GetDataTable(string code, string strWhere, string queryJson = "{}")
{
try
{
DataSourceEntity entity = GetEntityByCode(code);
if (entity == null)
{
return new DataTable();
}
else
{
if (!string.IsNullOrEmpty(strWhere))
{
strWhere = " where " + strWhere;
}
else
{
strWhere = "";
}
var queryParam = queryJson.ToJObject();
string sql = string.Format(" select * From ({0})t {1} ", entity.F_Sql, strWhere);
return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取树形数据
///
/// 编码
/// 父级ID
/// ID
/// 显示ID
///
public List GetTree(string code, string parentId, string Id, string showId)
{
try
{
DataSourceEntity entity = GetEntityByCode(code);
if (entity == null)
{
return new List();
}
else
{
DataTable list = databaseLinkIBLL.FindTable(entity.F_DbId, entity.F_Sql, new { });
List treeList = new List();
foreach (DataRow item in list.Rows)
{
TreeModel node = new TreeModel
{
id = item[Id].ToString(),
text = item[showId].ToString(),
value = item[Id].ToString(),
showcheck = false,
checkstate = 0,
isexpand = true,
parentId = item[parentId].ToString()
};
treeList.Add(node);
}
return treeList.ToTree();
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取数据源的数据(分页)
///
/// 数据源编码
/// 分页参数
/// 查询条件
///
public DataTable GetDataTable(string code, Pagination pagination, string strWhere, string queryJson = "{}")
{
try
{
DataSourceEntity entity = GetEntityByCode(code);
if (entity == null)
{
return new DataTable();
}
else
{
if (!string.IsNullOrEmpty(strWhere))
{
strWhere = " where " + strWhere;
}
else
{
strWhere = "";
}
var queryParam = queryJson.ToJObject();
string sql = string.Format(" select t.* From ({0})t {1} ", entity.F_Sql, strWhere);
return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam, pagination);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取数据源列名
///
/// 数据源编码
///
public List GetDataColName(string code)
{
try
{
Pagination pagination = new Pagination()
{
rows = 200,
page = 0,
sord = "",
sidx = ""
};
DataTable dt = GetDataTable(code, "");
List res = new List();
foreach (DataColumn item in dt.Columns)
{
if (item.ColumnName != "rownum")
{
res.Add(item.ColumnName);
}
}
return res;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public string GetKeyByValue(string code, string key, string keyText, string value)
{
string strWhere = " " + key + " =@" + key;
string queryJson = "{" + key + ":\"" + keyText + "\"}";
DataTable sourceDt = GetDataTable(code, strWhere, queryJson);
if (sourceDt.Rows.Count > 0)
{
return sourceDt.Rows[0][value].ToString();
}
else
{
return "";
}
}
#endregion
}
}