using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Learun.Application.Organization
{
///
/// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
/// Copyright (c) 2013-2018 Hexagon PPM
/// 创建人:研发部
/// 日 期:2017.04.17
/// 描 述:部门管理
///
public class DepartmentService
{
#region 仓储
Repository _departmentRepository => new Repository();
#endregion
#region 构造函数和属性
private string fieldSql;
public DepartmentService()
{
fieldSql = @"
t.F_DepartmentId,
t.F_CompanyId,
t.F_ParentId,
t.F_EnCode,
t.F_FullName,
t.F_ShortName,
t.F_Nature,
t.F_Manager,
t.F_OuterPhone,
t.F_InnerPhone,
t.F_Email,
t.F_Fax,
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 获取数据
///
/// 获取部门列表信息(根据公司Id)
///
/// 公司主键
///
public List GetList(string companyId)
{
try
{
//var strSql = new StringBuilder();
//strSql.Append("SELECT ");
//strSql.Append(fieldSql);
//strSql.Append(" FROM LR_Base_Department t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 AND F_CompanyId = @companyId ORDER BY t.F_ParentId,t.F_FullName ");
////return this.BaseRepository().FindList(strSql.ToString(), new { companyId = companyId });
var res= _departmentRepository.GetList().Where(X => X.F_EnabledMark == 1 && X.F_DeleteMark == 0 && X.F_CompanyId == companyId).
OrderBy(x => x.F_ParentId).OrderBy(x => x.F_FullName);
return res.ToList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取部门列表信息
///
///
public List GetAllList()
{
try
{
return _departmentRepository.GetList().Where(t => t.F_EnabledMark == 1 && t.F_DeleteMark == 0).ToList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取部门数据实体
///
/// 主键
///
public DepartmentEntity GetEntity(string keyValue)
{
try
{
return _departmentRepository.GetById(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 虚拟删除部门
///
/// 主键
public void VirtualDelete(string keyValue)
{
try
{
DepartmentEntity entity = new DepartmentEntity()
{
F_DepartmentId = keyValue,
F_DeleteMark = 1
};
_departmentRepository.Update(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存部门表单(新增、修改)
///
/// 主键值
/// 部门实体
///
public void SaveEntity(string keyValue, DepartmentEntity departmentEntity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
departmentEntity.Modify(keyValue);
if (keyValue == departmentEntity.F_ParentId)
{
departmentEntity.F_ParentId = "0";
}
_departmentRepository.Update(departmentEntity);
}
else
{
departmentEntity.Create();
_departmentRepository.Insert(departmentEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}