using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QMFrameWork.Data;
using QMAPP.Entity.Sys;
using QMAPP.Entity.Sys.Entity;
namespace QMAPP.DAL.Sys
{
///
/// 组织机构管理
/// 创建者:郭佳伟
/// 创建日期:2013.1.16
///
public class OrgaizationManageDAL : BaseDAL
{
#region 获取组织机构列表
///
/// 获取组织机构列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Orgaization condition, DataPage page)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
string sql = null;
try
{
sqlBuilder.Append("SELECT A.ORGAID,A.ORGADESC,A.ORGAPARENT,A.ACTIVATEFLG,U1.USERNAME AS CREATEUSER,A.CREATEDATE,U2.USERNAME AS UPDATEUSER,A.UPDATEDATE,");
sqlBuilder.Append("A.FLGDEL,(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = A.ORGAPARENT) AS ORGAPARENTDESC ");
sqlBuilder.Append(" FROM T_QM_ORGANIZATION A,T_QM_USER U1,T_QM_USER U2 WHERE A.CREATEUSER = U1.USERID AND A.UPDATEUSER = U2.USERID ");
sqlBuilder.Append(" AND A.FLGDEL = '0' ");
//查询条件
if (string.IsNullOrEmpty(condition.OrgaDESC) == false)
{
whereBuilder.Append(" AND A.ORGADESC = @ORGADESC");
parameters.Add(new DataParameter { ParameterName = "ORGADESC", DataType = DbType.String, Value = condition.OrgaDESC });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(whereBuilder.ToString());
}
//分页关键字段及排序
page.KeyName = "ORGAID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "UPDATEDATE DESC";
}
if (page.SortExpression.IndexOf("CreateTime") > -1)
{
page.SortExpression = page.SortExpression.Replace("CreateTime", "CREATEDATE");
}
if (page.SortExpression.IndexOf("UpdateTime") > -1)
{
page.SortExpression = page.SortExpression.Replace("UpdateTime", "UPDATEDATE");
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取组织机构信息
///
/// 获取组织机构信息
///
/// 条件
/// 组织机构信息
public Orgaization Get(Orgaization orga)
{
string sql = null;
List parameters = new List();
Orgaization tmporga = new Orgaization();
try
{
//获取用户信息
sql = @"SELECT ORGAID,ORGADESC,ORGAPARENT,ACTIVATEFLG,CREATEUSER,CREATEDATE,UPDATEUSER,UPDATEDATE,FLGDEL,
(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = A.ORGAPARENT) AS ORGAPARENTDESC FROM T_QM_ORGANIZATION A WHERE ORGAID=@ORGAID";
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
tmporga = session.Get(sql, parameters.ToArray());
}
return tmporga;
}
catch (Exception ex)
{
throw;
}
}
///
/// 获取机构角色
///
/// 获取条件
/// 机构角色
public Orgaization GetOrganRoleList(Orgaization organ)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_QM_ORGANROLE WHERE ORGANID = @ORGANID";
parameters.Add(new DataParameter("ORGANID",organ.OrgaID));
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
organ.RoleList = session.GetList(sql, parameters.ToArray()).ToList();
}
return organ;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 组织机构编号是否重复
///
/// 判断组织机构编号是否重复
///
/// 组织机构信息
/// true:已存在;fasel:不存在。
public bool ExistsOrga(Orgaization orga)
{
string OrgaID = "";
int count = 0;
string sql = null;
List parameters = new List();
try
{
if (string.IsNullOrEmpty(orga.OrgaID) == false)
{
OrgaID = orga.OrgaID;
}
if (orga.IsUpdate == true)
{
sql = "SELECT COUNT(*) FROM T_QM_ORGANIZATION WHERE ORGAID <> @ORGAID AND ORGADESC=@ORGADESC";
parameters.Add(new DataParameter("ORGAID", orga.OrgaID));
parameters.Add(new DataParameter("ORGADESC", orga.OrgaDESC));
}
else
{
sql = "SELECT COUNT(*) FROM T_QM_ORGANIZATION WHERE ORGAID = @ORGAID OR ORGADESC=@ORGADESC";
parameters.Add(new DataParameter("ORGAID", orga.OrgaID));
parameters.Add(new DataParameter("ORGADESC", orga.OrgaDESC));
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
count = int.Parse(session.ExecuteSqlScalar(sql, parameters.ToArray()).ToString());
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取组织机构所有的权限
///
/// 获取组织机构所有的权限
///
/// 条件
/// 同级别菜单列表
public Orgaization GetOrgaAuthorityList(Orgaization orga)
{
Orgaization list = new Orgaization();
List parameters = new List();
string sql = null;
try
{
sql = "SELECT * FROM T_QM_ORGAIZATIONAUTHORITY WHERE ORGAID=@ORGAID";
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
list.OrgaAuthorityList = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取组织机构树菜单列表
///
/// 获取同一级别的所有组织机构树菜单列表
///
/// 条件
/// 同级别菜单列表
public List GetSubTreeList(Orgaization orga)
{
List parameters = new List();
List list = null;
string sql = null;
try
{
sql = "SELECT * FROM T_QM_ORGANIZATION WHERE DELFLAG='0' AND ORGAPARENT=@ORGAPARENT";
parameters.Add(new DataParameter { ParameterName = "ORGAPARENT", DataType = DbType.String, Value = orga.OrgaParent });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取组织机构树菜单列表
///
/// 获取所有组织机构树菜单列表
///
/// 条件
/// 同级别菜单列表
public List GetAllList()
{
List parameters = new List();
List list = null;
string sql = null;
try
{
sql = "SELECT * FROM T_QM_ORGANIZATION WHERE FLGDEL='0'";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 插入组织机构与人员信息
///
/// 插入组织机构与人员信息
///
/// 组织机构与人员信息
/// 插入数
public int InsertOrgaUser(UserOrgaization orga)
{
int count = 0;
UserOrgaization insertUserOrga = new UserOrgaization();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
string[] userlist = orga.UserID.Split(new Char[]{ ',' });
insertUserOrga = orga;
for(int i = 0 ;i(orga);
}
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 删除组织机构与人员信息
///
/// 删除组织机构信息
///
/// 组织机构信息
/// 删除个数
public int DeleteOrgaUser(UserOrgaization orga)
{
int count = 0;
string sql = null;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//先删除改人员所在组织机构的所有权限信息
sql = "DELETE T_QM_USERAUTHORITY WHERE ORGAID=@ORGAID AND USERID = @USERID";
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = orga.UserID });
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, parameters.ToArray());
//在删除组织机构下人员的信息
parameters.Clear();
sql = "DELETE T_QM_USERORGAIZATION WHERE ORGAID=@ORGAID AND USERID = @USERID";
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = orga.UserID });
parameters.Add(new DataParameter { ParameterName = "ISMAINORGA", DataType = DbType.String, Value = orga.IsMainOgra });
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, parameters.ToArray());
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 插入组织机构信息
///
/// 插入组织机构信息
///
/// 组织机构信息
/// 插入数
public int Insert(Orgaization orga)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
orga.DelFlag = "0";
if (String.IsNullOrEmpty(orga.OrgaParent))
{
orga.OrgaParent = "0";
}
//插入基本信息
session.Insert(orga);
//插入组织机构与权限信息
if (orga.OrgaAuthorityList!=null)
{
session.Insert(orga.OrgaAuthorityList);
}
//插入组织机构与角色信息
if (orga.RoleList != null)
{
session.Insert(orga.RoleList);
}
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 删除组织机构信息
///
/// 删除组织机构信息
///
/// 组织机构信息
/// 删除个数
public int Delete(Orgaization orga)
{
int count = 0;
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//校验是否为激活状态
string activateFlg = "";
sql = "SELECT ACTIVATEFLG FROM T_QM_ORGANIZATION WHERE ORGAID = @ORGAID";
sql = this.ChangeSqlByDB(sql, session);
activateFlg = (string)session.ExecuteSqlScalar(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
if (activateFlg == "1")
return 0;
session.OpenTs();
//删除组织机构与权限信息
sql = "DELETE T_QM_ORGAIZATIONAUTHORITY WHERE ORGAID = @ORGAID";
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
//删除组织机构与角色信息
sql = "DELETE T_QM_ORGANROLE WHERE ORGANID = @ORGAID";
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
//删除组织机构信息
sql = "UPDATE T_QM_ORGANIZATION SET FLGDEL = '1' WHERE ORGAID = @ORGAID";
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 更新组织机构
///
/// 更新组织机构
///
/// 组织机构
/// 更新个数
public int Update(Orgaization orga)
{
int count = 0;
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//删除组织机构与权限信息
sql = "DELETE T_QM_ORGAIZATIONAUTHORITY WHERE ORGAID=@ORGAID";
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
//插入组织机构与权限信息
if (orga.OrgaAuthorityList != null)
{
session.Insert(orga.OrgaAuthorityList);
}
//删除组织机构与角色信息
sql = "DELETE T_QM_ORGANROLE WHERE ORGANID=@ORGANID";
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGANID", DataType = DbType.String, Value = orga.OrgaID });
//插入组织机构与角色信息
if (orga.RoleList != null)
{
session.Insert(orga.RoleList);
}
//设置用户激活状态
if (orga.ACTIVATEFLG == "0")
{
sql = "UPDATE T_QM_USER SET ACTIVATEFLG = '0' WHERE ORGANID = @ORGAID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = orga.OrgaID });
}
//更新基本信息
count = session.Update(orga);
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(Orgaization condition)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
DataTable dt = null;
string sql = null;
List parameters = new List();
try
{
//构成查询语句
sqlBuilder.Append("SELECT A.ORGAID,A.ORGADESC,A.ORGAPARENT,A.ACTIVATEFLG,U1.USERNAME AS CREATEUSER,A.CREATEDATE,U2.USERNAME AS UPDATEUSER,A.UPDATEDATE,");
sqlBuilder.Append("A.FLGDEL,(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = A.ORGAPARENT) AS ORGAPARENTDESC ");
sqlBuilder.Append(" FROM T_QM_ORGANIZATION A,T_QM_USER U1,T_QM_USER U2 WHERE A.CREATEUSER = U1.USERID AND A.UPDATEUSER = U2.USERID ");
sqlBuilder.Append(" AND A.FLGDEL = '0' ");
//查询条件
if (string.IsNullOrEmpty(condition.OrgaDESC) == false)
{
whereBuilder.Append(" AND A.ORGADESC = @ORGADESC");
parameters.Add(new DataParameter { ParameterName = "ORGADESC", DataType = DbType.String, Value = condition.OrgaDESC });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(whereBuilder.ToString());
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
dt = session.GetTable(sql, parameters.ToArray());
dt.TableName = "T_QM_ORGANIZATION";
}
foreach (DataRow row in dt.Rows)
{
if (row["ACTIVATEFLG"].ToString() == "1")
{
row["ACTIVATEFLG"] = "是";
}
else
{
row["ACTIVATEFLG"] = "否";
}
}
return dt;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取组织机构
///
/// 获取组织机构信息
///
/// 条件
/// 组织机构信息
public Orgaization Get(string orgaDesc)
{
string sql = null;
List parameters = new List();
Orgaization tmporga = new Orgaization();
try
{
//获取用户信息
sql = @"SELECT ORGAID FROM T_QM_ORGANIZATION A WHERE ORGADESC=@ORGADESC";
parameters.Add(new DataParameter { ParameterName = "ORGADESC", DataType = DbType.String, Value = orgaDesc });
sql = this.ChangeSqlByDB(sql, this.BaseSession);
tmporga = this.BaseSession.Get(sql, parameters.ToArray());
return tmporga;
}
catch (Exception ex)
{
throw;
}
}
#endregion
}
}