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.
408 lines
12 KiB
408 lines
12 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Data;
|
||
|
using QMFrameWork.Data;
|
||
|
using QMAPP.FJC.Entity.Example;
|
||
|
|
||
|
namespace QMAPP.FJC.DAL.Example
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 用户管理数据层对象
|
||
|
/// 创建者:李炳海
|
||
|
/// 创建时间:2012.11.20
|
||
|
/// </summary>
|
||
|
public class UserManageDAL1
|
||
|
{
|
||
|
public UserManageDAL1()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
#region 插入用户信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 插入用户信息(单表)
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>插入行数</returns>
|
||
|
public int Insert(UserInfo1 user)
|
||
|
{
|
||
|
int count=0;
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
count=session.Insert<UserInfo1>(user);
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 插入用户信息(主、子表)
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>插入行数</returns>
|
||
|
public int InsertWithDetails(UserInfo1 user)
|
||
|
{
|
||
|
int count = 0;
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
//打开事务
|
||
|
session.OpenTs();
|
||
|
|
||
|
//插入主表信息
|
||
|
count = session.Insert<UserInfo1>(user);
|
||
|
|
||
|
//插入明细表信息
|
||
|
session.Insert<UserAndRole1>(user.RoleList);
|
||
|
|
||
|
//提交事务
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 更新用户信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新用户信息
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>更新行数</returns>
|
||
|
public int Update(UserInfo1 user)
|
||
|
{
|
||
|
int count = 0;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
count = session.Update<UserInfo1>(user);
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新用户信息(主、子表)
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>更新行数</returns>
|
||
|
public int UpdateWithDetails(UserInfo1 user)
|
||
|
{
|
||
|
int count = 0;
|
||
|
string sql = null;
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
//打开事务
|
||
|
session.OpenTs();
|
||
|
|
||
|
//更新主表信息
|
||
|
count = session.Update<UserInfo1>(user);
|
||
|
|
||
|
//更新明细信息
|
||
|
sql = "DELETE FROM USERANDROLE WHERE USERID=@USERID";
|
||
|
session.ExecuteSql(sql, new DataParameter { ParameterName = "USERID", Value = user.UserID });
|
||
|
session.Insert<UserAndRole1>(user.RoleList);
|
||
|
|
||
|
//提交事务
|
||
|
session.CommitTs();
|
||
|
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 删除用户信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 删除用户信息
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>删除行数</returns>
|
||
|
public int Delete(UserInfo1 user)
|
||
|
{
|
||
|
int count = 0;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
count = session.Delete<UserInfo1>(user);
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 删除用户信息(主、子表)
|
||
|
/// </summary>
|
||
|
/// <param name="user">用户信息</param>
|
||
|
/// <returns>删除行数</returns>
|
||
|
public int DeleteWithDetails(UserInfo1 user)
|
||
|
{
|
||
|
int count = 0;
|
||
|
string sql = null;
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
//打开事务
|
||
|
session.OpenTs();
|
||
|
|
||
|
//删除明细信息
|
||
|
sql = "DELETE FROM USERANDROLE WHERE USERID=@USERID";
|
||
|
session.ExecuteSql(sql, new DataParameter { ParameterName = "USERID", Value = user.UserID });
|
||
|
|
||
|
//删除主表信息
|
||
|
count = session.Delete<UserInfo1>(user);
|
||
|
|
||
|
//提交事务
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取单个用户信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取单个用户信息
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>用户信息</returns>
|
||
|
public UserInfo1 Get(UserInfo1 user)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
user = session.Get<UserInfo1>(user);
|
||
|
}
|
||
|
return user;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取用户列表
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取用户列表
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>用户列表</returns>
|
||
|
public List<UserInfo1> GetList(UserInfo1 user)
|
||
|
{
|
||
|
string sql = null;
|
||
|
string whereSql = null;
|
||
|
List<DataParameter> parameters = null;
|
||
|
List<UserInfo1> list = null;
|
||
|
DataQueryHelper queryHelper = new DataQueryHelper();
|
||
|
try
|
||
|
{
|
||
|
sql = "SELECT "+queryHelper.GetSelectColumns<UserInfo1>()+" FROM UserInfo";
|
||
|
|
||
|
queryHelper.GetWhere<UserInfo1>(user, out whereSql, out parameters);
|
||
|
|
||
|
if (string.IsNullOrEmpty(whereSql) == false)
|
||
|
{
|
||
|
sql += " where " + whereSql;
|
||
|
}
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
list = session.GetList<UserInfo1>(sql, parameters.ToArray()).ToList();
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取用户列表,反射查询(数据库端分页)
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>数据页</returns>
|
||
|
public DataPage GetList(UserInfo1 user, DataPage page)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
page.KeyName = "USERID";
|
||
|
page.SortExpression = "UPDATETIME DESC";
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
page = session.GetDataPage<UserInfo1, UserInfo1>(user, page);
|
||
|
}
|
||
|
|
||
|
return page;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取用户列表,自定义查询(数据库端分页)(oracle)
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>数据页</returns>
|
||
|
public DataPage GetMyList(UserInfo1 user, DataPage page)
|
||
|
{
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sql = this.GetQuerySql(user, ref parameters);
|
||
|
|
||
|
//分页关键字段及排序
|
||
|
page.KeyName = "USERID";
|
||
|
if(string.IsNullOrEmpty(page.SortExpression)==true)
|
||
|
page.SortExpression = "UPDATETIME DESC";
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
page = session.GetDataPage<UserInfo1>(sql, parameters.ToArray(), page);
|
||
|
}
|
||
|
|
||
|
return page;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取导出的数据
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>数据</returns>
|
||
|
public DataTable GetExportData(UserInfo1 user)
|
||
|
{
|
||
|
DataTable dt = null;
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sql = this.GetQuerySql(user, ref parameters);
|
||
|
sql += " ORDER BY UPDATETIME DESC";
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
dt = session.GetTable(sql, parameters.ToArray());
|
||
|
dt.TableName = "UserInfo1";
|
||
|
}
|
||
|
|
||
|
return dt;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取查询语句
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取查询语句
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <param name="parameters">参数</param>
|
||
|
/// <returns>查询语句</returns>
|
||
|
private string GetQuerySql(UserInfo1 user, ref List<DataParameter> parameters)
|
||
|
{
|
||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||
|
StringBuilder whereBuilder = new StringBuilder();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sqlBuilder.Append("SELECT USERID,USERNAME,USERDES,SEX,ORGANID,UPDATETIME,REMARK ");
|
||
|
sqlBuilder.Append("FROM T_EXAMPLE_UserInfo ");
|
||
|
//查询条件
|
||
|
if (string.IsNullOrEmpty(user.UserName) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND USERNAME like @USERNAME");
|
||
|
parameters.Add(new DataParameter { ParameterName = "USERNAME", DataType = DbType.String, Value = "%" + user.UserName + "%" });
|
||
|
}
|
||
|
|
||
|
if (string.IsNullOrEmpty(user.UserDes) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND USERDES like @USERDES");
|
||
|
parameters.Add(new DataParameter { ParameterName = "USERDES", DataType = DbType.String, Value = "%" + user.UserDes + "%" });
|
||
|
}
|
||
|
|
||
|
if (whereBuilder.Length > 0)
|
||
|
{
|
||
|
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
|
||
|
}
|
||
|
return sqlBuilder.ToString();
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|