天津投入产出系统后端
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.
 
 
 
 
 
 

412 lines
12 KiB

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