using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using TCAEntity.Entities;

namespace TCABLL
{
    public class UserPowerBLL : IDisposable
    {
        #region  Loacl Declaration

        private ISession dataBaseSession = null;

        #endregion

        #region Methods

        /// <summary>
        /// 公共构造函数
        /// </summary>
        public UserPowerBLL()
        {

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model">信息</param>
        /// <returns>true 添加成功 false 添加失败</returns>
        public bool AddUserPower(TCA_UserPower model)
        {
            this.Dispose();
            if (dataBaseSession == null)
            {
                dataBaseSession = DataBaseSession;
            }
            ITransaction tran = dataBaseSession.BeginTransaction();
            try
            {
                dataBaseSession.Save(model);
                dataBaseSession.Flush();
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model">信息</param>
        /// <returns>true 更新成功 false 更新失败</returns>
        public bool UpdateUserPower(TCA_UserPower model)
        {
            this.Dispose();
            if (dataBaseSession == null)
            {
                dataBaseSession = DataBaseSession;
            }
            ITransaction tran = dataBaseSession.BeginTransaction();
            try
            {
                dataBaseSession.Update(model);
                dataBaseSession.Flush();
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="model">信息</param>
        /// <returns>true 更新成功 false 更新失败</returns>
        public bool DeleteUserPower(TCA_UserPower model)
        {
            this.Dispose();
            if (dataBaseSession == null)
            {
                dataBaseSession = DataBaseSession;
            }
            ITransaction tran = dataBaseSession.BeginTransaction();
            try
            {
                dataBaseSession.Delete(model);
                dataBaseSession.Flush();
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }

/// <summary> /// 通过用户编号移除用户所有角色 /// </summary> /// <param name="userCode">用户编号</param> /// <returns></returns> public bool UpdateUserRole(string userCode) { if (dataBaseSession == null) { dataBaseSession = DataBaseSession; } ITransaction tran = dataBaseSession.BeginTransaction(); try { IQuery query = dataBaseSession.CreateSQLQuery("Update TCA_UserPower Set RoleCode='' where UserCode=:UserCode"); query.SetParameter("UserCode", userCode); query.ExecuteUpdate(); tran.Commit(); return true; } catch { tran.Rollback(); return false; } } /// <summary> /// 获取指定的用户 /// </summary> /// <param name="Code">编号</param> /// <returns>返回指定的信息</returns> public TCA_UserPower GetUserPowerByCode(string Code) { this.Dispose(); if (dataBaseSession == null) { dataBaseSession = DataBaseSession; } return dataBaseSession.Get<TCA_UserPower>(Code); } /// <summary> /// 通过用户编号获取用户 /// </summary> /// <param name="userCode">用户编号</param> /// <returns></returns> public IList<TCA_UserPower> GetUserPowerByUserCode(string userCode) { this.Dispose(); if (dataBaseSession == null) { dataBaseSession = DataBaseSession; } ICriteria criteria = dataBaseSession.CreateCriteria(typeof(TCA_UserPower)); criteria.Add(Restrictions.Eq("UserCode", userCode)); return criteria.List<TCA_UserPower>(); } #endregion #region 释放资源 public void Dispose() { if (dataBaseSession != null) { try { dataBaseSession.Clear(); dataBaseSession.Close(); dataBaseSession.Dispose(); dataBaseSession = null; } catch { dataBaseSession = null; } } } #endregion #region Properties public ISession DataBaseSession { get { if (dataBaseSession == null) { dataBaseSession = new Configuration().AddAssembly("TCAEntity").BuildSessionFactory().OpenSession(); } return dataBaseSession; } } #endregion } }

 

posted on 2013-05-29 21:53  莫等闲也  阅读(592)  评论(0编辑  收藏  举报