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
{
///
/// 用户管理数据层对象
/// 创建者:李炳海
/// 创建时间:2012.11.20
///
public class UserManageDAL1
{
public UserManageDAL1()
{
}
#region 插入用户信息
///
/// 插入用户信息(单表)
///
/// 用户信息
/// 插入行数
public int Insert(UserInfo1 user)
{
int count=0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count=session.Insert(user);
}
return count;
}
catch (Exception ex)
{
throw;
}
}
///
/// 插入用户信息(主、子表)
///
/// 用户信息
/// 插入行数
public int InsertWithDetails(UserInfo1 user)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//打开事务
session.OpenTs();
//插入主表信息
count = session.Insert(user);
//插入明细表信息
session.Insert(user.RoleList);
//提交事务
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 更新用户信息
///
/// 更新用户信息
///
/// 用户信息
/// 更新行数
public int Update(UserInfo1 user)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = session.Update(user);
}
return count;
}
catch (Exception ex)
{
throw;
}
}
///
/// 更新用户信息(主、子表)
///
/// 用户信息
/// 更新行数
public int UpdateWithDetails(UserInfo1 user)
{
int count = 0;
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//打开事务
session.OpenTs();
//更新主表信息
count = session.Update(user);
//更新明细信息
sql = "DELETE FROM USERANDROLE WHERE USERID=@USERID";
session.ExecuteSql(sql, new DataParameter { ParameterName = "USERID", Value = user.UserID });
session.Insert(user.RoleList);
//提交事务
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 删除用户信息
///
/// 删除用户信息
///
/// 用户信息
/// 删除行数
public int Delete(UserInfo1 user)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = session.Delete(user);
}
return count;
}
catch (Exception ex)
{
throw;
}
}
///
/// 删除用户信息(主、子表)
///
/// 用户信息
/// 删除行数
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(user);
//提交事务
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取单个用户信息
///
/// 获取单个用户信息
///
/// 查询条件
/// 用户信息
public UserInfo1 Get(UserInfo1 user)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
user = session.Get(user);
}
return user;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取用户列表
///
/// 获取用户列表
///
/// 查询条件
/// 用户列表
public List GetList(UserInfo1 user)
{
string sql = null;
string whereSql = null;
List parameters = null;
List list = null;
DataQueryHelper queryHelper = new DataQueryHelper();
try
{
sql = "SELECT "+queryHelper.GetSelectColumns()+" FROM UserInfo";
queryHelper.GetWhere(user, out whereSql, out parameters);
if (string.IsNullOrEmpty(whereSql) == false)
{
sql += " where " + whereSql;
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
///
/// 获取用户列表,反射查询(数据库端分页)
///
/// 查询条件
/// 数据页
public DataPage GetList(UserInfo1 user, DataPage page)
{
try
{
page.KeyName = "USERID";
page.SortExpression = "UPDATETIME DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(user, page);
}
return page;
}
catch (Exception ex)
{
throw;
}
}
///
/// 获取用户列表,自定义查询(数据库端分页)(oracle)
///
/// 查询条件
/// 数据页
public DataPage GetMyList(UserInfo1 user, DataPage page)
{
string sql = null;
List parameters = new List();
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(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw;
}
}
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(UserInfo1 user)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
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 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(UserInfo1 user, ref List 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
}
}