/ZXFramework_2/source/Tool/HF.User.Data/UserData.cs

# · C# · 445 lines · 353 code · 20 blank · 72 comment · 26 complexity · 6425b8f785e81af8c1af6145c1b2d46f MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using HF.Framework.DataClientDBAgent;
  6. using HF.WorkFlow.DataContract;
  7. using HF.Framework.SqlDataContract;
  8. using HF.Framework;
  9. namespace HF.User.Data
  10. {
  11. /// <summary>
  12. /// 数据访问类UserData。
  13. /// </summary>
  14. public class UserData
  15. {
  16. #region 私有字段
  17. private SqlDataItem sqlDataItem = new SqlDataItem();
  18. private string[] fieldList = { "UserId", "UserName", "UserSex", "UserPassword", "UserDepart", "UserEmail", "UserTelephone", "UserFax", "UserMsgAcept", "UserChgPW", "UserStop", "UserDes", "UserQQ", "UserMsn" };
  19. private string tableName = "HF_User";
  20. private string keyField = "UserId";
  21. private string sqlString = "";
  22. #endregion
  23. #region 私有方法
  24. private void setParameter(UserEntity entity)
  25. {
  26. sqlDataItem.ParameterList.Clear();
  27. sqlDataItem.AppendParameter("@UserId", entity.UserId, typeof(string));
  28. sqlDataItem.AppendParameter("@UserName", entity.UserName, typeof(string));
  29. sqlDataItem.AppendParameter("@UserSex", entity.UserSex, typeof(string));
  30. sqlDataItem.AppendParameter("@UserPassword", entity.UserPassword, typeof(string));
  31. sqlDataItem.AppendParameter("@UserDepart", entity.UserDepart, typeof(string));
  32. sqlDataItem.AppendParameter("@UserEmail", entity.UserEmail, typeof(string));
  33. sqlDataItem.AppendParameter("@UserTelephone", entity.UserTelephone, typeof(string));
  34. sqlDataItem.AppendParameter("@UserFax", entity.UserFax, typeof(string));
  35. sqlDataItem.AppendParameter("@UserMsgAcept", entity.UserMsgAcept, typeof(bool));
  36. sqlDataItem.AppendParameter("@UserChgPW", entity.UserChgPW, typeof(bool));
  37. sqlDataItem.AppendParameter("@UserStop", entity.UserStop, typeof(bool));
  38. sqlDataItem.AppendParameter("@UserDes", entity.UserDes, typeof(string));
  39. sqlDataItem.AppendParameter("@UserQQ", entity.UserQQ, typeof(string));
  40. sqlDataItem.AppendParameter("@UserMsn", entity.UserMsn, typeof(string));
  41. }
  42. private void setInsertSql()
  43. {
  44. string tmpValueList = "";
  45. string tmpFieldName = "";
  46. sqlString = "insert into " + tableName + "(";
  47. int tmpInt = this.fieldList.Length;
  48. for (int i = 0; i < tmpInt - 1; i++)
  49. {
  50. tmpFieldName = fieldList[i].ToString();
  51. sqlString = sqlString + tmpFieldName + ", ";
  52. tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
  53. }
  54. tmpFieldName = this.fieldList[tmpInt - 1].ToString();
  55. sqlString = sqlString + tmpFieldName;
  56. tmpValueList = tmpValueList + "@" + tmpFieldName;
  57. this.sqlString = sqlString + ")values(" + tmpValueList + ")";
  58. sqlDataItem.CommandText = sqlString;
  59. }
  60. private void setUpdateSql()
  61. {
  62. string tmpFieldName = "";
  63. int tmpInt = this.fieldList.Length;
  64. sqlString = "update " + tableName + " set ";
  65. for (int i = 0; i < tmpInt - 1; i++)
  66. {
  67. tmpFieldName = fieldList[i].ToString();
  68. sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
  69. }
  70. tmpFieldName = this.fieldList[tmpInt - 1].ToString();
  71. sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
  72. sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
  73. sqlDataItem.CommandText = sqlString;
  74. }
  75. public UserEntity GetUserEntity(DataRow dr)
  76. {
  77. UserEntity entity = new UserEntity();
  78. string value = "";
  79. entity.UserId = dr["UserId"].ToString();
  80. entity.UserName = dr["UserName"].ToString();
  81. entity.UserSex = dr["UserSex"].ToString();
  82. entity.UserPassword = dr["UserPassword"].ToString();
  83. entity.UserDepart = dr["UserDepart"].ToString();
  84. entity.UserEmail = dr["UserEmail"].ToString();
  85. entity.UserTelephone = dr["UserTelephone"].ToString();
  86. entity.UserFax = dr["UserFax"].ToString();
  87. value = dr["UserMsgAcept"].ToString();
  88. if (string.IsNullOrEmpty(value) == false)
  89. {
  90. if ((value == "1") || (value.ToLower() == "true"))
  91. {
  92. entity.UserMsgAcept = true;
  93. }
  94. else
  95. {
  96. entity.UserMsgAcept = false;
  97. }
  98. }
  99. value = dr["UserChgPW"].ToString();
  100. if (string.IsNullOrEmpty(value) == false)
  101. {
  102. if ((value == "1") || (value.ToLower() == "true"))
  103. {
  104. entity.UserChgPW = true;
  105. }
  106. else
  107. {
  108. entity.UserChgPW = false;
  109. }
  110. }
  111. value = dr["UserStop"].ToString();
  112. if (string.IsNullOrEmpty(value) == false)
  113. {
  114. if ((value == "1") || (value.ToLower() == "true"))
  115. {
  116. entity.UserStop = true;
  117. }
  118. else
  119. {
  120. entity.UserStop = false;
  121. }
  122. }
  123. entity.UserDes = dr["UserDes"].ToString();
  124. entity.UserQQ = dr["UserQQ"].ToString();
  125. entity.UserMsn = dr["UserMsn"].ToString();
  126. return entity;
  127. }
  128. #endregion
  129. #region 构造函数
  130. public UserData()
  131. { }
  132. public static UserData GetInstance()
  133. {
  134. return new UserData();
  135. }
  136. #endregion
  137. #region 公有方法
  138. #region 基本方法
  139. /// <summary>
  140. /// 增加
  141. /// </summary>
  142. /// <param name="entity">数据实体</param>
  143. /// <returns>影响的记录条数</returns>
  144. public int Insert(UserEntity entity)
  145. {
  146. try
  147. {
  148. setInsertSql();//设定insert语句
  149. setParameter(entity);//设定参数
  150. ClientDBAgent agent = new ClientDBAgent();
  151. return agent.ExecuteNonQuery(sqlDataItem);
  152. }
  153. catch (Exception ex)
  154. {
  155. throw ex;
  156. }
  157. }
  158. /// <summary>
  159. /// 修改
  160. /// </summary>
  161. /// <param name="entity">数据实体</param>
  162. /// <returns>影响的记录条数</returns>
  163. public int Update(UserEntity entity)
  164. {
  165. try
  166. {
  167. setUpdateSql();//设定insert语句
  168. setParameter(entity);//设定参数
  169. ClientDBAgent agent = new ClientDBAgent();
  170. return agent.ExecuteNonQuery(sqlDataItem);
  171. }
  172. catch (Exception ex)
  173. {
  174. throw ex;
  175. }
  176. }
  177. /// <summary>
  178. /// 是否存在该记录
  179. /// </summary>
  180. public bool Exists(string userId)
  181. {
  182. string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
  183. SqlDataItem sqlItem = new SqlDataItem();
  184. sqlItem.CommandText = tmpSql;
  185. sqlItem.AppendParameter("@" + keyField, userId);
  186. ClientDBAgent agent = new ClientDBAgent();
  187. return agent.RecordExists(sqlItem);
  188. }
  189. /// <summary>
  190. /// 删除
  191. /// </summary>
  192. /// <param name="userId">主键Id</param>
  193. /// <returns>影响的记录条数</returns>
  194. public void Delete(string userId)
  195. {
  196. try
  197. {
  198. string tmpSql = "delete from " + tableName + " where " + keyField + "=@" + keyField;
  199. SqlDataItem sqlItem = new SqlDataItem();
  200. sqlItem.CommandText = tmpSql;
  201. sqlItem.AppendParameter("@" + keyField, userId);
  202. ClientDBAgent agent = new ClientDBAgent();
  203. agent.ExecuteNonQuery(sqlItem);
  204. }
  205. catch (Exception ex)
  206. {
  207. throw ex;
  208. }
  209. }
  210. /// <summary>
  211. /// 获取一条数据记录
  212. /// </summary>
  213. /// <param name="UserId">主键Id</param>
  214. /// <returns>DataTable</returns>
  215. public DataTable GetUserTable(string userId)
  216. {
  217. string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
  218. try
  219. {
  220. SqlDataItem sqlItem = new SqlDataItem();
  221. sqlItem.CommandText = tmpSql;
  222. sqlItem.AppendParameter("@" + keyField, userId);
  223. ClientDBAgent agent = new ClientDBAgent();
  224. return agent.ExecuteDataTable(sqlItem);
  225. }
  226. catch (Exception ex)
  227. {
  228. throw ex;
  229. }
  230. }
  231. /// <summary>
  232. /// 获取一个数据实体
  233. /// </summary>
  234. /// <param name="userId">主键Id</param>
  235. /// <returns>DataTable</returns>
  236. public UserEntity GetUserEntity(string userId)
  237. {
  238. DataTable dt = GetUserTable(userId);
  239. if (dt != null && dt.Rows.Count > 0)
  240. {
  241. return GetUserEntity(dt.Rows[0]);
  242. }
  243. return null;
  244. }
  245. #endregion
  246. #region 扩展方法
  247. //此处添加扩展方法
  248. /// <summary>
  249. /// 读取所有用户
  250. /// </summary>
  251. /// <returns>DataTable</returns>
  252. public DataTable GetAllUsers()
  253. {
  254. string tmpSql = "select * from HF_User order by userId";
  255. SqlDataItem sqlItem = new SqlDataItem();
  256. sqlItem.CommandText = tmpSql;
  257. ClientDBAgent agent = new ClientDBAgent();
  258. return agent.ExecuteDataTable(sqlItem);
  259. }
  260. /// <summary>
  261. /// 读取所有用户
  262. /// </summary>
  263. /// <returns>DataTable</returns>
  264. public List<UserEntity> GetAllUserEntitys()
  265. {
  266. List<UserEntity> result = new List<UserEntity>();
  267. DataTable dt = GetAllUsers();
  268. foreach (DataRow dr in dt.Rows)
  269. {
  270. UserEntity entity = GetUserEntity(dr);
  271. result.Add(entity);
  272. }
  273. return result;
  274. }
  275. /// <summary>
  276. /// 设置密码
  277. /// </summary>
  278. /// <param name="userId"></param>
  279. /// <param name="password"></param>
  280. /// <returns>是否成功</returns>
  281. public bool SetPassword(string userId, string password)
  282. {
  283. try
  284. {
  285. string tmpMd5 = SysBlack.MD5Encrypt(password);
  286. string tmpSql = "update HF_User set UserPassword=@UserPassword where userid=@userid";
  287. SqlDataItem sqlItem = new SqlDataItem();
  288. sqlItem.CommandText = tmpSql;
  289. sqlItem.AppendParameter("@userId", userId);
  290. sqlItem.AppendParameter("@UserPassword", tmpMd5);
  291. ClientDBAgent agent = new ClientDBAgent();
  292. int i= agent.ExecuteNonQuery(sqlItem);
  293. return i == 1;
  294. }
  295. catch (Exception ex)
  296. {
  297. throw ex;
  298. }
  299. }
  300. public string GetUserNameById(string userId)
  301. {
  302. string result="";
  303. UserEntity entity = GetUserEntity(userId);
  304. if (entity != null)
  305. {
  306. result = entity.UserName;
  307. }
  308. return result;
  309. }
  310. /// <summary>
  311. /// 用户验证
  312. /// </summary>
  313. /// <param name="userId">账号</param>
  314. /// <param name="password">密码</param>
  315. /// <returns>是否通过</returns>
  316. public bool LoginCheck(string userId, string password)
  317. {
  318. try
  319. {
  320. string tmpMd5 = SysBlack.MD5Encrypt(password);
  321. string tmpSql = "select * from HF_User where UserId=@userid and UserPassword=@password";
  322. SqlDataItem sqlItem = new SqlDataItem();
  323. sqlItem.CommandText = tmpSql;
  324. sqlItem.AppendParameter("@userId", userId);
  325. sqlItem.AppendParameter("@password", tmpMd5);
  326. ClientDBAgent agent = new ClientDBAgent();
  327. return agent.RecordExists(sqlItem);
  328. }
  329. catch (Exception ex)
  330. {
  331. throw ex;
  332. }
  333. }
  334. /// <summary>
  335. /// 取得用户列表,模糊查询
  336. /// </summary>
  337. /// <param name="userId">用户帐号</param>
  338. /// <returns>用户表</returns>
  339. public DataTable GetUserTableLikeId(string userId)
  340. {
  341. try
  342. {
  343. string tmpSql = "select * from HF_User where UserId like @userid";
  344. SqlDataItem sqlItem = new SqlDataItem();
  345. sqlItem.AppendParameter("@userid", "%" + userId + "%");
  346. sqlItem.CommandText = tmpSql;
  347. ClientDBAgent agent = new ClientDBAgent();
  348. return agent.ExecuteDataTable(sqlItem);
  349. }
  350. catch (Exception ex)
  351. {
  352. throw ex;
  353. }
  354. }
  355. /// <summary>
  356. /// 取得用户列表,模糊查询
  357. /// </summary>
  358. /// <param name="userId">用户帐号</param>
  359. /// <returns>Entitys</returns>
  360. public List<UserEntity> GetUserEntiysLikeId(string userId)
  361. {
  362. try
  363. {
  364. List<UserEntity> result = new List<UserEntity>();
  365. DataTable dt = GetUserTableLikeId(userId);
  366. foreach (DataRow dr in dt.Rows)
  367. {
  368. UserEntity entity = GetUserEntity(dr);
  369. result.Add(entity);
  370. }
  371. return result;
  372. }
  373. catch (Exception ex)
  374. {
  375. throw ex;
  376. }
  377. }
  378. /// <summary>
  379. /// 取得用户列表,模糊查询
  380. /// </summary>
  381. /// <param name="userName">用户名</param>
  382. /// <returns>用户表</returns>
  383. public DataTable GetUserTableLikeName(string userName)
  384. {
  385. try
  386. {
  387. string tmpSql = "select * from HF_User where UserName like @UserName";
  388. SqlDataItem sqlItem = new SqlDataItem();
  389. sqlItem.AppendParameter("@userName", "%" + userName + "%");
  390. sqlItem.CommandText = tmpSql;
  391. ClientDBAgent agent = new ClientDBAgent();
  392. return agent.ExecuteDataTable(sqlItem);
  393. }
  394. catch (Exception ex)
  395. {
  396. throw ex;
  397. }
  398. }
  399. /// <summary>
  400. /// 取得用户信息,模糊查询
  401. /// </summary>
  402. /// <param name="userName">用户帐号</param>
  403. /// <returns>Entitys</returns>
  404. public List<UserEntity> GetUserEntiysLikeName(string userName)
  405. {
  406. try
  407. {
  408. List<UserEntity> result = new List<UserEntity>();
  409. DataTable dt = GetUserTableLikeName(userName);
  410. foreach (DataRow dr in dt.Rows)
  411. {
  412. UserEntity entity = GetUserEntity(dr);
  413. result.Add(entity);
  414. }
  415. return result;
  416. }
  417. catch (Exception ex)
  418. {
  419. throw ex;
  420. }
  421. }
  422. #endregion
  423. #endregion
  424. }
  425. }