using learun.iapplication;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace learun.application
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.10
/// 描 述:功能模块
///
public class ModuleService : ServiceBase
{
#region 属性 构造函数
private readonly string fieldSql;
private readonly string btnfieldSql;
private readonly string colfieldSql;
private readonly string formfieldSql;
///
///
///
public ModuleService()
{
fieldSql = @"
t.F_ModuleId,
t.F_ParentId,
t.F_EnCode,
t.F_FullName,
t.F_Icon,
t.F_UrlAddress,
t.F_Target,
t.F_IsMenu,
t.F_AllowExpand,
t.F_IsPublic,
t.F_AllowEdit,
t.F_AllowDelete,
t.F_SortCode,
t.F_DeleteMark,
t.F_EnabledMark,
t.F_Description,
t.F_CreateDate,
t.F_CreateUserId,
t.F_CreateUserName,
t.F_ModifyDate,
t.F_ModifyUserId,
t.F_ModifyUserName
";
btnfieldSql = @"
t.F_ModuleButtonId,
t.F_ModuleId,
t.F_ParentId,
t.F_Icon,
t.F_EnCode,
t.F_FullName,
t.F_ActionAddress,
t.F_SortCode
";
colfieldSql = @"
t.F_ModuleColumnId,
t.F_ModuleId,
t.F_ParentId,
t.F_EnCode,
t.F_FullName,
t.F_SortCode,
t.F_Description
";
formfieldSql = @"
t.F_ModuleFormId,
t.F_ModuleId,
t.F_EnCode,
t.F_FullName,
t.F_SortCode
";
}
#endregion 属性 构造函数
#region 功能模块
///
/// 功能列表
///
///
public Task> GetList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + fieldSql + " FROM LR_Base_Module t WHERE t.F_DeleteMark = 0 Order By t.F_ParentId,t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString());
}
///
/// 功能列表(url)
///
///
public Task GetEntityByUrl(string url)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + fieldSql + " FROM LR_Base_Module t WHERE t.F_DeleteMark = 0 AND t.F_UrlAddress = @url ");
return BaseRepository().FindEntity(strSql.ToString(), new { url });
}
///
/// 功能列表(code)
///
/// 编码
///
public Task GetEntityByCode(string code)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + fieldSql + " FROM LR_Base_Module t WHERE t.F_DeleteMark = 0 AND t.F_EnCode = @code ");
return BaseRepository().FindEntity(strSql.ToString(), new { code });
}
///
/// 功能实体
///
/// 主键值
///
public Task GetEntity(string keyValue)
{
return BaseRepository().FindEntityByKey(keyValue);
}
#endregion 功能模块
#region 模块按钮
///
/// 获取按钮列表数据
///
/// 模块Id
///
public Task> GetButtonList(string moduleId)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + btnfieldSql + " FROM LR_Base_ModuleButton t WHERE t.F_ModuleId = @moduleId ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString(), new { moduleId = moduleId });
}
///
/// 获取全部按钮列表数据
///
///
public Task> GetButtonList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + btnfieldSql + " FROM LR_Base_ModuleButton t ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString());
}
#endregion 模块按钮
#region 模块视图列表
///
/// 获取视图列表数据
///
/// 模块Id
///
public Task> GetColumnList(string moduleId)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + colfieldSql + " FROM LR_Base_ModuleColumn t WHERE t.F_ModuleId = @moduleId ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString(), new { moduleId = moduleId });
}
///
/// 获取全部视图列表数据
///
///
public Task> GetColumnList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + colfieldSql + " FROM LR_Base_ModuleColumn t ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString());
}
#endregion 模块视图列表
#region 模块表单字段
///
/// 获取表单字段数据
///
/// 模块Id
///
public Task> GetFormList(string moduleId)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + formfieldSql + " FROM LR_Base_ModuleForm t WHERE t.F_ModuleId = @moduleId ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString(), new { moduleId = moduleId });
}
///
/// 获取全部表单字段数据
///
///
public Task> GetFormList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT " + formfieldSql + " FROM LR_Base_ModuleForm t ORDER BY t.F_SortCode ");
return BaseRepository().FindList(strSql.ToString());
}
#endregion 模块表单字段
#region 提交数据
///
/// 虚拟删除模块功能
///
/// 主键
public async Task Delete(string keyValue)
{
var db = BaseRepository().BeginTrans();
try
{
await db.DeleteAny(new { F_ModuleId = keyValue });
await db.DeleteAny(new { F_ModuleId = keyValue });
await db.DeleteAny(new { F_ModuleId = keyValue });
await db.DeleteAny(new { F_ModuleId = keyValue });
db.Commit();
}
catch (Exception)
{
db.Rollback();
throw;
}
}
///
/// 保存模块功能实体(新增、修改)
///
/// 主键值
/// 实体
/// 按钮列表
/// 视图列集合
/// 表单字段
public async Task SaveEntity(string keyValue, ModuleEntity moduleEntity, List moduleButtonEntitys, List moduleColumnEntitys, List moduleFormEntitys)
{
var db = BaseRepository().BeginTrans();
try
{
if (string.IsNullOrEmpty(moduleEntity.F_ParentId) || moduleEntity.F_ParentId == "-1")
{
moduleEntity.F_ParentId = "0";
}
if (string.IsNullOrEmpty(keyValue))
{
// 新增
moduleEntity.F_ModuleId = Guid.NewGuid().ToString();
moduleEntity.F_CreateDate = DateTime.Now;
moduleEntity.F_CreateUserId = this.GetUserId();
moduleEntity.F_CreateUserName = this.GetUserName();
moduleEntity.F_DeleteMark = 0;
await db.Insert(moduleEntity);
}
else
{
// 编辑
moduleEntity.F_ModuleId = keyValue;
moduleEntity.F_ModifyDate = DateTime.Now;
moduleEntity.F_ModifyUserId = this.GetUserId();
moduleEntity.F_ModifyUserName = this.GetUserName();
await db.Update(moduleEntity);
await db.DeleteAny(new { F_ModuleId = keyValue });
await db.DeleteAny(new { F_ModuleId = keyValue });
await db.DeleteAny(new { F_ModuleId = keyValue });
}
int num = 0;
if (moduleButtonEntitys != null)
{
foreach (var item in moduleButtonEntitys)
{
item.F_ModuleButtonId = Guid.NewGuid().ToString();
item.F_SortCode = num;
item.F_ModuleId = moduleEntity.F_ModuleId;
if (moduleButtonEntitys.Find(t => t.F_ModuleButtonId == item.F_ParentId) == null)
{
item.F_ParentId = "0";
}
await db.Insert(item);
num++;
}
}
if (moduleColumnEntitys != null)
{
num = 0;
foreach (var item in moduleColumnEntitys)
{
item.F_ModuleColumnId = Guid.NewGuid().ToString();
item.F_SortCode = num;
item.F_ModuleId = moduleEntity.F_ModuleId;
await db.Insert(item);
num++;
}
}
if (moduleFormEntitys != null)
{
num = 0;
foreach (var item in moduleFormEntitys)
{
item.F_ModuleFormId = Guid.NewGuid().ToString();
item.F_SortCode = num;
item.F_ModuleId = moduleEntity.F_ModuleId;
await db.Insert(item);
num++;
}
}
db.Commit();
}
catch (Exception)
{
db.Rollback();
throw;
}
}
#endregion 提交数据
}
}