/ZXFramework_2/source/Tool/HF.User.Data/UserData.cs
# · C# · 445 lines · 353 code · 20 blank · 72 comment · 26 complexity · 6425b8f785e81af8c1af6145c1b2d46f MD5 · raw file
- using System;
- using System.Data;
- using System.Text;
- using System.Collections.Generic;
- using HF.Framework.DataClientDBAgent;
- using HF.WorkFlow.DataContract;
- using HF.Framework.SqlDataContract;
- using HF.Framework;
- namespace HF.User.Data
- {
- /// <summary>
- /// 数据访问类UserData。
- /// </summary>
- public class UserData
- {
- #region 私有字段
- private SqlDataItem sqlDataItem = new SqlDataItem();
- private string[] fieldList = { "UserId", "UserName", "UserSex", "UserPassword", "UserDepart", "UserEmail", "UserTelephone", "UserFax", "UserMsgAcept", "UserChgPW", "UserStop", "UserDes", "UserQQ", "UserMsn" };
- private string tableName = "HF_User";
- private string keyField = "UserId";
- private string sqlString = "";
-
- #endregion
-
- #region 私有方法
- private void setParameter(UserEntity entity)
- {
- sqlDataItem.ParameterList.Clear();
- sqlDataItem.AppendParameter("@UserId", entity.UserId, typeof(string));
- sqlDataItem.AppendParameter("@UserName", entity.UserName, typeof(string));
- sqlDataItem.AppendParameter("@UserSex", entity.UserSex, typeof(string));
- sqlDataItem.AppendParameter("@UserPassword", entity.UserPassword, typeof(string));
- sqlDataItem.AppendParameter("@UserDepart", entity.UserDepart, typeof(string));
- sqlDataItem.AppendParameter("@UserEmail", entity.UserEmail, typeof(string));
- sqlDataItem.AppendParameter("@UserTelephone", entity.UserTelephone, typeof(string));
- sqlDataItem.AppendParameter("@UserFax", entity.UserFax, typeof(string));
- sqlDataItem.AppendParameter("@UserMsgAcept", entity.UserMsgAcept, typeof(bool));
- sqlDataItem.AppendParameter("@UserChgPW", entity.UserChgPW, typeof(bool));
- sqlDataItem.AppendParameter("@UserStop", entity.UserStop, typeof(bool));
- sqlDataItem.AppendParameter("@UserDes", entity.UserDes, typeof(string));
- sqlDataItem.AppendParameter("@UserQQ", entity.UserQQ, typeof(string));
- sqlDataItem.AppendParameter("@UserMsn", entity.UserMsn, typeof(string));
- }
-
- private void setInsertSql()
- {
- string tmpValueList = "";
- string tmpFieldName = "";
- sqlString = "insert into " + tableName + "(";
- int tmpInt = this.fieldList.Length;
- for (int i = 0; i < tmpInt - 1; i++)
- {
- tmpFieldName = fieldList[i].ToString();
- sqlString = sqlString + tmpFieldName + ", ";
- tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
- }
- tmpFieldName = this.fieldList[tmpInt - 1].ToString();
- sqlString = sqlString + tmpFieldName;
- tmpValueList = tmpValueList + "@" + tmpFieldName;
- this.sqlString = sqlString + ")values(" + tmpValueList + ")";
- sqlDataItem.CommandText = sqlString;
- }
-
- private void setUpdateSql()
- {
- string tmpFieldName = "";
- int tmpInt = this.fieldList.Length;
- sqlString = "update " + tableName + " set ";
- for (int i = 0; i < tmpInt - 1; i++)
- {
- tmpFieldName = fieldList[i].ToString();
- sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
- }
- tmpFieldName = this.fieldList[tmpInt - 1].ToString();
- sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
- sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
- sqlDataItem.CommandText = sqlString;
- }
-
- public UserEntity GetUserEntity(DataRow dr)
- {
- UserEntity entity = new UserEntity();
- string value = "";
- entity.UserId = dr["UserId"].ToString();
- entity.UserName = dr["UserName"].ToString();
- entity.UserSex = dr["UserSex"].ToString();
- entity.UserPassword = dr["UserPassword"].ToString();
- entity.UserDepart = dr["UserDepart"].ToString();
- entity.UserEmail = dr["UserEmail"].ToString();
- entity.UserTelephone = dr["UserTelephone"].ToString();
- entity.UserFax = dr["UserFax"].ToString();
- value = dr["UserMsgAcept"].ToString();
- if (string.IsNullOrEmpty(value) == false)
- {
- if ((value == "1") || (value.ToLower() == "true"))
- {
- entity.UserMsgAcept = true;
- }
- else
- {
- entity.UserMsgAcept = false;
- }
- }
- value = dr["UserChgPW"].ToString();
- if (string.IsNullOrEmpty(value) == false)
- {
- if ((value == "1") || (value.ToLower() == "true"))
- {
- entity.UserChgPW = true;
- }
- else
- {
- entity.UserChgPW = false;
- }
- }
- value = dr["UserStop"].ToString();
- if (string.IsNullOrEmpty(value) == false)
- {
- if ((value == "1") || (value.ToLower() == "true"))
- {
- entity.UserStop = true;
- }
- else
- {
- entity.UserStop = false;
- }
- }
- entity.UserDes = dr["UserDes"].ToString();
- entity.UserQQ = dr["UserQQ"].ToString();
- entity.UserMsn = dr["UserMsn"].ToString();
- return entity;
- }
-
- #endregion
-
- #region 构造函数
- public UserData()
- { }
- public static UserData GetInstance()
- {
- return new UserData();
- }
- #endregion
-
- #region 公有方法
- #region 基本方法
- /// <summary>
- /// 增加
- /// </summary>
- /// <param name="entity">数据实体</param>
- /// <returns>影响的记录条数</returns>
- public int Insert(UserEntity entity)
- {
- try
- {
- setInsertSql();//设定insert语句
- setParameter(entity);//设定参数
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteNonQuery(sqlDataItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="entity">数据实体</param>
- /// <returns>影响的记录条数</returns>
- public int Update(UserEntity entity)
- {
- try
- {
- setUpdateSql();//设定insert语句
- setParameter(entity);//设定参数
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteNonQuery(sqlDataItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- /// <summary>
- /// 是否存在该记录
- /// </summary>
- public bool Exists(string userId)
- {
- string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- sqlItem.AppendParameter("@" + keyField, userId);
- ClientDBAgent agent = new ClientDBAgent();
- return agent.RecordExists(sqlItem);
- }
-
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="userId">主键Id</param>
- /// <returns>影响的记录条数</returns>
- public void Delete(string userId)
- {
- try
- {
- string tmpSql = "delete from " + tableName + " where " + keyField + "=@" + keyField;
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- sqlItem.AppendParameter("@" + keyField, userId);
- ClientDBAgent agent = new ClientDBAgent();
- agent.ExecuteNonQuery(sqlItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- /// <summary>
- /// 获取一条数据记录
- /// </summary>
- /// <param name="UserId">主键Id</param>
- /// <returns>DataTable</returns>
- public DataTable GetUserTable(string userId)
- {
- string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
- try
- {
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- sqlItem.AppendParameter("@" + keyField, userId);
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteDataTable(sqlItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- /// <summary>
- /// 获取一个数据实体
- /// </summary>
- /// <param name="userId">主键Id</param>
- /// <returns>DataTable</returns>
- public UserEntity GetUserEntity(string userId)
- {
- DataTable dt = GetUserTable(userId);
- if (dt != null && dt.Rows.Count > 0)
- {
- return GetUserEntity(dt.Rows[0]);
- }
- return null;
- }
-
- #endregion
-
- #region 扩展方法
- //此处添加扩展方法
- /// <summary>
- /// 读取所有用户
- /// </summary>
- /// <returns>DataTable</returns>
- public DataTable GetAllUsers()
- {
- string tmpSql = "select * from HF_User order by userId";
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteDataTable(sqlItem);
- }
-
- /// <summary>
- /// 读取所有用户
- /// </summary>
- /// <returns>DataTable</returns>
- public List<UserEntity> GetAllUserEntitys()
- {
- List<UserEntity> result = new List<UserEntity>();
- DataTable dt = GetAllUsers();
- foreach (DataRow dr in dt.Rows)
- {
- UserEntity entity = GetUserEntity(dr);
- result.Add(entity);
- }
- return result;
- }
- /// <summary>
- /// 设置密码
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="password"></param>
- /// <returns>是否成功</returns>
- public bool SetPassword(string userId, string password)
- {
- try
- {
- string tmpMd5 = SysBlack.MD5Encrypt(password);
- string tmpSql = "update HF_User set UserPassword=@UserPassword where userid=@userid";
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- sqlItem.AppendParameter("@userId", userId);
- sqlItem.AppendParameter("@UserPassword", tmpMd5);
- ClientDBAgent agent = new ClientDBAgent();
- int i= agent.ExecuteNonQuery(sqlItem);
- return i == 1;
-
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public string GetUserNameById(string userId)
- {
- string result="";
- UserEntity entity = GetUserEntity(userId);
- if (entity != null)
- {
- result = entity.UserName;
- }
- return result;
-
- }
- /// <summary>
- /// 用户验证
- /// </summary>
- /// <param name="userId">账号</param>
- /// <param name="password">密码</param>
- /// <returns>是否通过</returns>
- public bool LoginCheck(string userId, string password)
- {
- try
- {
- string tmpMd5 = SysBlack.MD5Encrypt(password);
- string tmpSql = "select * from HF_User where UserId=@userid and UserPassword=@password";
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.CommandText = tmpSql;
- sqlItem.AppendParameter("@userId", userId);
- sqlItem.AppendParameter("@password", tmpMd5);
- ClientDBAgent agent = new ClientDBAgent();
- return agent.RecordExists(sqlItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
-
- }
- /// <summary>
- /// 取得用户列表,模糊查询
- /// </summary>
- /// <param name="userId">用户帐号</param>
- /// <returns>用户表</returns>
- public DataTable GetUserTableLikeId(string userId)
- {
- try
- {
- string tmpSql = "select * from HF_User where UserId like @userid";
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.AppendParameter("@userid", "%" + userId + "%");
- sqlItem.CommandText = tmpSql;
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteDataTable(sqlItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 取得用户列表,模糊查询
- /// </summary>
- /// <param name="userId">用户帐号</param>
- /// <returns>Entitys</returns>
- public List<UserEntity> GetUserEntiysLikeId(string userId)
- {
- try
- {
- List<UserEntity> result = new List<UserEntity>();
- DataTable dt = GetUserTableLikeId(userId);
- foreach (DataRow dr in dt.Rows)
- {
- UserEntity entity = GetUserEntity(dr);
- result.Add(entity);
- }
- return result;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 取得用户列表,模糊查询
- /// </summary>
- /// <param name="userName">用户名</param>
- /// <returns>用户表</returns>
- public DataTable GetUserTableLikeName(string userName)
- {
- try
- {
- string tmpSql = "select * from HF_User where UserName like @UserName";
- SqlDataItem sqlItem = new SqlDataItem();
- sqlItem.AppendParameter("@userName", "%" + userName + "%");
- sqlItem.CommandText = tmpSql;
- ClientDBAgent agent = new ClientDBAgent();
- return agent.ExecuteDataTable(sqlItem);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 取得用户信息,模糊查询
- /// </summary>
- /// <param name="userName">用户帐号</param>
- /// <returns>Entitys</returns>
- public List<UserEntity> GetUserEntiysLikeName(string userName)
- {
- try
- {
- List<UserEntity> result = new List<UserEntity>();
- DataTable dt = GetUserTableLikeName(userName);
- foreach (DataRow dr in dt.Rows)
- {
- UserEntity entity = GetUserEntity(dr);
- result.Add(entity);
- }
- return result;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #endregion
- }
- }