天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

214 lines
8.5 KiB

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
{
/// <summary>
/// 用户信息管理
/// 创建者:郭佳伟
/// 创建日期:2013.7.1
/// </summary>
public class UserOrgaizationManageDAL
{
#region 获取用户与组织机构信息列表
/// <summary>
/// 获取用户与组织机构信息列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(User condition, DataPage page)
{
try
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
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<User>(sqlBuilder.ToString(), parameters.ToArray(), page);
List<User> users = (List<User>)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<User> tmpusers = session.GetList<User>(sqlorg.ToString(), parameters.ToArray());
//取得一个用户所有的组织机构
foreach (User tmpuser in tmpusers)
{
//将取得的组织机构合并在一起放到页面上
u.OrgaDESC += tmpuser.OrgaDESC + "、";
}
}
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取用户与组织机构信息
/// <summary>
/// 获取用户与组织机构信息
/// </summary>
/// <param name="user">条件</param>
/// <returns>用户信息信息</returns>
public User Get(User user)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
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<User>(sqlBuilder.ToString(), parameters.ToArray());
}
return tmpuser;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新用户与组织机构信息
/// <summary>
/// 更新用户与组织机构信息
/// </summary>
/// <param name="user">用户信息</param>
/// <returns>更新个数</returns>
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<UserOrgaization>(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
}
}