using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;
namespace Learun.Application.Organization
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.04
/// 描 述:角色管理
///
public class RoleService : RepositoryFactory
{
#region 构造函数和属性
private string fieldSql;
public RoleService()
{
fieldSql = @"
t.F_RoleId,
t.F_Category,
t.F_EnCode,
t.F_FullName,
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
";
}
#endregion
#region 获取数据
///
/// 获取角色数据列表
///
///
public IEnumerable GetList()
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_Role t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ORDER BY t.F_FullName ");
return this.BaseRepository().FindList(strSql.ToString());
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetPageList(Pagination pagination, string keyword)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_Role t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ");
if (!string.IsNullOrEmpty(keyword))
{
keyword = "%" + keyword + "%";
strSql.Append(" AND( t.F_FullName like @keyword or t.F_EnCode like @keyword ) ");
}
return this.BaseRepository().FindList(strSql.ToString(), new { keyword }, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 根据角色名获取角色
///
///
///
public RoleEntity GetRoleByRoleName(string roleName)
{
try
{
return this.BaseRepository().FindEntity(a => a.F_FullName == roleName);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
internal string GetIdByRoleName(string v)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_FullName == v).F_RoleId;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取角色数据列表
///
/// 主键串
///
public IEnumerable GetListByRoleIds(string roleIds)
{
try
{
return this.BaseRepository().FindList(t => roleIds.Contains(t.F_RoleId));
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 虚拟删除角色
///
/// 主键
public void VirtualDelete(string keyValue)
{
var db = this.BaseRepository().BeginTrans();
try
{
RoleEntity entity = new RoleEntity()
{
F_RoleId = keyValue,
F_DeleteMark = 1
};
db.Update(entity);
db.ExecuteBySql(" Delete From LR_BASE_USERRELATION where F_OBJECTID = @keyValue ", new { keyValue = keyValue });
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存角色(新增、修改)
///
/// 主键值
/// 角色实体
///
public void SaveEntity(string keyValue, RoleEntity roleEntity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
roleEntity.Modify(keyValue);
this.BaseRepository().Update(roleEntity);
}
else
{
roleEntity.Create();
this.BaseRepository().Insert(roleEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}