using System;
using System.Collections.Generic;
using System.Data;
using Mesnac.Codd;
using Mesnac.Codd.Session;
namespace Mesnac.Basic
{
///
/// 登录用户信息类
/// 创建人:初风鸣
/// 创建时间:2013-7-23
/// 说明:采用单例模式
///
public class UserInfo
{
#region 事件定义
///
/// 重新登录事件
///
public event EventHandler OnReLogin;
#endregion
#region 字段定义
private string _userGUId;
private int _seqIndex;
private string _userName;
private string _realName;
private string _roleGUID;//未登录时为-1,预留超级用户为-99
private int _classID; //所属班组
private DateTime _loginTime;
public const bool MustLogin=false;
private List _purviewList = null; //当前用户的权限项列表
private List _allFormPurview = null; //所有权限项
#endregion
#region 单例模式
private static UserInfo user=null;
public static UserInfo Instance
{
get
{
//lock(String.Empty)
{
if (user == null)
{
user = new UserInfo();
}
return user;
}
}
}
#endregion
#region 构造方法
public UserInfo()
: this( "-1" , 0, "--" , "--" , "-1", 1)
{
}
public UserInfo( string userGUId , string userName, string realName , string roleGUID )
{
this._userGUId = userGUId;
this._userName = userName;
this._realName = realName;
this._roleGUID = roleGUID;
this._loginTime = DateTime.Now;
}
public UserInfo(string userGUId, int seqIndex, string userName, string realName, string roleGUID)
{
this._userGUId = userGUId;
this._seqIndex = seqIndex;
this._userName = userName;
this._realName = realName;
this._roleGUID = roleGUID;
this._loginTime = DateTime.Now;
}
public UserInfo(string userGUId, int seqIndex, string userName, string realName, string roleGUID, int classID)
{
this._userGUId = userGUId;
this._seqIndex = seqIndex;
this._userName = userName;
this._realName = realName;
this._roleGUID = roleGUID;
this._classID = classID;
this._loginTime = DateTime.Now;
}
#endregion
#region 属性定义
public string UserGUID
{
get
{
return _userGUId;
}
set
{
_userGUId = value;
}
}
public int SeqIndex
{
get { return _seqIndex; }
set { _seqIndex = value; }
}
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
public string RealName
{
get
{
return _realName;
}
set
{
_realName = value;
}
}
public string RoleGUID
{
get
{
return _roleGUID;
}
set
{
_roleGUID = value;
if ( _roleGUID.Equals( "-99" ) )
{
_userGUId = "mesnac";
_userName = "mesnac";
_realName = "超级用户";
}
}
}
public int ClassID
{
get { return _classID; }
set { _classID = value; }
}
public DateTime LoginTime
{
get
{
return _loginTime;
}
set
{
_loginTime = DateTime.Now;
}
}
///
/// 当前登录窗体对象
///
public System.Windows.Forms.Form FrmSysLogin { get; set; }
///
/// 获取权限项列表
///
public List AllFormPurview
{
get
{
this.RefreshAllFormPurview();
return this._allFormPurview;
}
}
///
/// 获取当前用户的权限项列表
///
public List PurviewList
{
get
{
this.RefreshPurview();
return this._purviewList;
}
}
#endregion
#region 方法定义
public DataTable GetPurview()
{
DataTable dt = new DataTable();
DbHelper dbhelper = DataSourceFactory.Instance.GetDbHelper( DataSourceFactory.MCDbType.Local);
if ( dbhelper != null )
{
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = "SELECT DISTINCT TA.PermissionItemGUID,TB.FormName,TB.FormText,TB.FormType FROM BasRolePermission TA INNER JOIN BasForm TB ON TA.PermissionItemGUID=TB.GUID AND TA.PermissionType=TB.FormType WHERE '" + _roleGUID + "' IN('-99',TA.RoleGUID) AND TB.DeleteFlag='0'";
dt = dbhelper.ToDataTable();
}
return dt;
}
///
/// 刷新权限项列表
///
public void RefreshAllFormPurview()
{
if (this._allFormPurview == null)
{
this._allFormPurview = new List();
}
this._allFormPurview.Clear();
DataTable dt = new DataTable();
DbHelper dbhelper = DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (dbhelper != null)
{
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = "SELECT [GUID],[FormName],[FormText],[FormType],[DeleteFlag] FROM [BasForm] where DeleteFlag = 0";
dt = dbhelper.ToDataTable();
}
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
this._allFormPurview.Add(row["FormName"] as string);
}
}
}
///
/// 刷新当前用户权限项
///
public void RefreshPurview()
{
if (this._purviewList == null)
{
this._purviewList = new List();
}
this._purviewList.Clear();
DataTable table = this.GetPurview();
if (table != null)
{
foreach (DataRow row in table.Rows)
{
this._purviewList.Add(row["FormName"] as string);
}
}
}
///
/// 触发重新登录事件
///
public void TriggerReLoginEvent()
{
if (this.OnReLogin != null)
{
this.OnReLogin(this, System.EventArgs.Empty);
}
}
#endregion
}
}