using learun.iapplication; using learun.util; using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Threading.Tasks; namespace learun.application { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.09.19 /// 描 述:数据源 /// public class DataSourceService : ServiceBase { #region 属性 构造函数 private readonly string fieldSql; /// /// /// public DataSourceService() { fieldSql = @" t.F_Id, t.F_Code, t.F_Name, t.F_DbId, t.F_Description, t.F_CreateUserId, t.F_CreateUserName, t.F_CreateDate, t.F_ModifyUserId, t.F_ModifyUserName, t.F_ModifyDate "; } #endregion 属性 构造函数 #region 获取数据 /// /// 获取分页数据 /// /// 分页参数 /// 关键字 /// public Task> GetPageList(Pagination pagination, string keyword) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql); strSql.Append(" FROM LR_Base_DataSource t "); strSql.Append(" WHERE 1=1 "); if (!string.IsNullOrEmpty(keyword)) { strSql.Append(" AND ( t.F_Name like @keyword OR t.F_Code like @keyword ) "); keyword = "%" + keyword + "%"; } return this.BaseRepository().FindList(strSql.ToString(), new { keyword = keyword }, pagination); } /// /// 获取列表数据 /// /// public Task> GetList() { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql); strSql.Append(" FROM LR_Base_DataSource t "); return this.BaseRepository().FindList(strSql.ToString()); } /// /// 获取实体 /// /// 编码 /// public Task GetEntityByCode(string code) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql); strSql.Append(",t.F_Sql FROM LR_Base_DataSource t where F_Code =@code "); return this.BaseRepository().FindEntity(strSql.ToString(), new { code }); } /// /// 获取实体 /// /// 主键 /// public Task GetEntity(string keyValue) { return this.BaseRepository().FindEntity(keyValue); } #endregion 获取数据 #region 提交数据 /// /// 删除数据源 /// /// 主键 public async Task DeleteEntity(string keyValue) { DataSourceEntity entity = new DataSourceEntity() { F_Id = keyValue, }; await this.BaseRepository().Delete(entity); } /// /// 保存(新增、修改) /// /// 主键值 /// 数据源实体 /// public async Task SaveEntity(string keyValue, DataSourceEntity dataSourceEntity) { if (!string.IsNullOrEmpty(keyValue)) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql); strSql.Append(" FROM LR_Base_DataSource t where F_Code =@code AND F_Id != @keyValue "); DataSourceEntity entity = await this.BaseRepository().FindEntity(strSql.ToString(), new { code = dataSourceEntity.F_Code, keyValue }); if (entity != null) { return false; } dataSourceEntity.F_Id = keyValue; dataSourceEntity.F_ModifyDate = DateTime.Now; dataSourceEntity.F_ModifyUserId = this.GetUserId(); dataSourceEntity.F_ModifyUserName = this.GetUserName(); await this.BaseRepository().Update(dataSourceEntity); } else { DataSourceEntity entity = await GetEntityByCode(dataSourceEntity.F_Code); if (entity != null) { return false; } dataSourceEntity.F_Id = Guid.NewGuid().ToString(); dataSourceEntity.F_CreateDate = DateTime.Now; dataSourceEntity.F_CreateUserId = this.GetUserId(); dataSourceEntity.F_CreateUserName = this.GetUserName(); await this.BaseRepository().Insert(dataSourceEntity); } return true; } #endregion 提交数据 #region 扩展方法 /// /// 获取数据源的数据 /// /// 数据源编码 /// 用户信息 /// 查询条件 /// public async Task GetDataTable(string code, UserEntity userInfo, string queryJson = "{}") { DataSourceEntity entity = await GetEntityByCode(code); if (entity == null) { return new DataTable(); } else { if (string.IsNullOrEmpty(queryJson)) { queryJson = "{}"; } var queryParam = queryJson.ToJObject(); var strWhere = new StringBuilder(); foreach (var item in queryParam.Properties()) { if (!string.IsNullOrEmpty(item.Value.ToString())) { strWhere.Append(" AND " + item.Name + " = '" + item.Value + "'"); } } string sql = string.Format(" select * From ({0})t where 1=1 {1} ", entity.F_Sql, strWhere.ToString()); if (!string.IsNullOrEmpty(entity.F_Sql)) { // 流程当前执行人 sql = sql.Replace("{userId}", "'" + userInfo.F_UserId + "'"); sql = sql.Replace("{userAccount}", "'" + userInfo.F_Account + "'"); sql = sql.Replace("{companyId}", "'" + userInfo.F_CompanyId + "'"); sql = sql.Replace("{departmentId}", "'" + userInfo.F_DepartmentId + "'"); } return await this.BaseRepository(entity.F_DbId).FindTable(sql); } } /// /// 获取数据源的数据(分页) /// /// 数据源编码 /// 用户信息 /// 分页参数 /// 查询条件 /// public async Task GetDataTable(string code, Pagination pagination, UserEntity userInfo, string queryJson = "{}") { DataSourceEntity entity = await GetEntityByCode(code); if (entity == null) { return new DataTable(); } else { if (string.IsNullOrEmpty(queryJson)) { queryJson = "{}"; } var queryParam = queryJson.ToJObject(); var strWhere = new StringBuilder(); foreach (var item in queryParam.Properties()) { if (!string.IsNullOrEmpty(item.Value.ToString())) { strWhere.Append(" AND " + item.Name + " = '" + item.Value + "'"); } } string sql = string.Format(" select * From ({0})t where 1=1 {1} ", entity.F_Sql, strWhere.ToString()); if (!string.IsNullOrEmpty(entity.F_Sql)) { // 流程当前执行人 sql = sql.Replace("{userId}", "'" + userInfo.F_UserId + "'"); sql = sql.Replace("{userAccount}", "'" + userInfo.F_Account + "'"); sql = sql.Replace("{companyId}", "'" + userInfo.F_CompanyId + "'"); sql = sql.Replace("{departmentId}", "'" + userInfo.F_DepartmentId + "'"); } return await this.BaseRepository(entity.F_DbId).FindTable(sql, pagination); } } /// /// 获取树形数据 /// /// 编码 /// 父级ID /// ID /// 显示ID /// public async Task> GetTree(string code, string parentId, string Id, string showId) { DataSourceEntity entity = await GetEntityByCode(code); if (entity == null) { return new List(); } else { DataTable list = await this.BaseRepository(entity.F_DbId).FindTable(entity.F_Sql); 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(); } } /// /// 获取数据 /// /// 数据库连接编码 /// sql /// public Task GetDataTableBySql(string code, string strSql) { return this.BaseRepository(code).FindTable(strSql); } /// /// 获取sql的列 /// /// 编码 /// public async Task> GetDataColName(string code) { DataSourceEntity entity = await GetEntityByCode(code); if (entity == null || string.IsNullOrEmpty(entity.F_Sql)) { return new List(); } else { string sql = entity.F_Sql; sql = sql.Replace("={userId}", " is not null "); sql = sql.Replace("={userAccount}", " is not null"); sql = sql.Replace("={companyId}", " is not null"); sql = sql.Replace("={departmentId}", " is not null"); sql = sql.Replace("= {userId}", " is not null"); sql = sql.Replace("= {userAccount}", " is not null"); sql = sql.Replace("= {companyId}", " is not null"); sql = sql.Replace("= {departmentId}", " is not null"); return await this.BaseRepository(entity.F_DbId).GetSqlColName(sql); } } #endregion 扩展方法 } }