using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.Entity.Sys;
using System.Data;
namespace QMAPP.DAL.Sys
{
///
/// 用户信息管理
/// 创建者:郭佳伟
/// 创建日期:2013.7.1
///
public class UserOrgaizationManageDAL
{
#region 获取用户与组织机构信息列表
///
/// 获取用户与组织机构信息列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(User condition, DataPage page)
{
try
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
sqlBuilder.Append(" SELECT USERID, ");
sqlBuilder.Append(" LOGINUSERID, ");
sqlBuilder.Append(" USERNAME, ");
sqlBuilder.Append(" DELFLAG, ");
sqlBuilder.Append(" ISSTOP, ");
sqlBuilder.Append(" ORGAID, ");
sqlBuilder.Append(" (SELECT '【'+ORGADESC+'】' ");
sqlBuilder.Append(" FROM T_QM_ORGANIZATION ");
sqlBuilder.Append(" WHERE ORGAID = B.ORGAID) AS ORGADESC ");
sqlBuilder.Append(" FROM ( ");
sqlBuilder.Append(" SELECT USERID, ");
sqlBuilder.Append(" LOGINUSERID,USERNAME ");
sqlBuilder.Append(" ,DELFLAG,ISSTOP ");
sqlBuilder.Append(" ,(SELECT ORGAID FROM T_QM_USERORGAIZATION WHERE USERID = A.USERID AND ISMAINORGA='1') AS ORGAID ");
sqlBuilder.Append(" FROM T_QM_USER AS A WHERE DELFLAG ='0' AND ISSTOP='0' and LOGINUSERID !='admin' ");
//查询条件
if (string.IsNullOrEmpty(condition.UserName) == false)
{
whereBuilder.Append(" AND USERNAME = @USERNAME");
parameters.Add(new DataParameter { ParameterName = "USERNAME", DataType = DbType.String, Value = condition.UserName });
}
//查询条件,如果Orgas为空,证明是admin
if (condition.Orgas == null && string.IsNullOrEmpty(condition.OrgaID) == false)
{
whereBuilder.Append(" AND EXISTS (SELECT USERID FROM T_QM_USERORGAIZATION WHERE A.USERID = USERID AND ORGAID=@ORGAID)");
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = condition.OrgaID });
}
//查询条件,如果orga不为空
if (condition.Orgas != null)
{
whereBuilder.Append(" AND EXISTS (SELECT USERID FROM T_QM_USERORGAIZATION WHERE A.USERID = USERID AND (1!=1 ");
int tmpcount = 0;
foreach (var item in condition.Orgas)
{
whereBuilder.Append(" OR ORGAID=@ORGAID" + tmpcount );
parameters.Add(new DataParameter { ParameterName = "ORGAID" + tmpcount, DataType = DbType.String, Value = item.OrgaID });
tmpcount++;
}
whereBuilder.Append(")) ");
whereBuilder.Append(" OR (CREATEUSER = @CREATEUSER and DELFLAG = '0')");
parameters.Add(new DataParameter { ParameterName = "CREATEUSER", DataType = DbType.String, Value = condition.CreateUser });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(whereBuilder.ToString());
}
sqlBuilder.Append(") AS B");
//分页关键字段及排序
page.KeyName = "LOGINUSERID";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sqlBuilder.ToString(), parameters.ToArray(), page);
List users = (List)page.Result;
foreach (User u in users)
{
string sqlorg = "SELECT '【'+(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE " +
" ORGAID = T_QM_USERORGAIZATION.ORGAID AND DELFLAG='0')+'】' AS ORGADESC FROM " +
" T_QM_USERORGAIZATION WHERE USERID='"+u.UserID+"' AND ISMAINORGA='0' ";
IList tmpusers = session.GetList(sqlorg.ToString(), parameters.ToArray());
//取得一个用户所有的组织机构
foreach (User tmpuser in tmpusers)
{
//将取得的组织机构合并在一起放到页面上
u.OrgaDESC += tmpuser.OrgaDESC + "、";
}
}
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取用户与组织机构信息
///
/// 获取用户与组织机构信息
///
/// 条件
/// 用户信息信息
public User Get(User user)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
User tmpuser = new User();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取用户信息
sqlBuilder.Append(" SELECT USERID,LOGINUSERID,USERNAME,ORGAID, ");
sqlBuilder.Append(" (SELECT ORGADESC ");
sqlBuilder.Append(" FROM T_QM_ORGANIZATION ");
sqlBuilder.Append(" WHERE ORGAID = B.ORGAID) AS ORGADESC FROM ( ");
sqlBuilder.Append(" SELECT USERID, ");
sqlBuilder.Append(" LOGINUSERID,USERNAME,(SELECT ORGAID FROM T_QM_USERORGAIZATION WHERE USERID = A.USERID AND ISMAINORGA='1') AS ORGAID ");
sqlBuilder.Append(" FROM T_QM_USER AS A WHERE UserID = @UserID ) AS B");
parameters.Add(new DataParameter { ParameterName = "UserID", DataType = DbType.String, Value = user.UserID });
tmpuser = session.Get(sqlBuilder.ToString(), parameters.ToArray());
}
return tmpuser;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新用户与组织机构信息
///
/// 更新用户与组织机构信息
///
/// 用户信息
/// 更新个数
public int Update(UserOrgaization orga)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
if (orga.Orgas != null)
{
//更新用户与组织机构信息
string rolesql = "DELETE FROM T_QM_USERORGAIZATION WHERE USERID=@USERID AND ISMAINORGA='0' ";
session.ExecuteSql(rolesql, new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = orga.Orgas[0].UserID });
session.Insert(orga.Orgas);
}
else
{
//更新用户与组织机构信息
string rolesql = "DELETE FROM T_QM_USERORGAIZATION WHERE USERID=@USERID AND ISMAINORGA='0' ";
session.ExecuteSql(rolesql, new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = orga.UserID });
}
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}