RoadFlow2.1 临时演示

Users.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace RoadFlow.Platform
  5. {
  6. public class Users
  7. {
  8. /// <summary>
  9. /// 前缀
  10. /// </summary>
  11. public const string PREFIX = "u_";
  12. private RoadFlow.Data.Interface.IUsers dataUsers;
  13. public Users()
  14. {
  15. this.dataUsers = Data.Factory.Factory.GetUsers();
  16. }
  17. /// <summary>
  18. /// 新增
  19. /// </summary>
  20. public int Add(RoadFlow.Data.Model.Users model)
  21. {
  22. return dataUsers.Add(model);
  23. }
  24. /// <summary>
  25. /// 更新
  26. /// </summary>
  27. public int Update(RoadFlow.Data.Model.Users model)
  28. {
  29. return dataUsers.Update(model);
  30. }
  31. /// <summary>
  32. /// 查询所有记录
  33. /// </summary>
  34. public List<RoadFlow.Data.Model.Users> GetAll()
  35. {
  36. return dataUsers.GetAll();
  37. }
  38. /// <summary>
  39. /// 查询单条记录
  40. /// </summary>
  41. public RoadFlow.Data.Model.Users Get(Guid id)
  42. {
  43. return dataUsers.Get(id);
  44. }
  45. /// <summary>
  46. /// 删除
  47. /// </summary>
  48. public int Delete(Guid id)
  49. {
  50. return dataUsers.Delete(id);
  51. }
  52. /// <summary>
  53. /// 查询记录条数
  54. /// </summary>
  55. public long GetCount()
  56. {
  57. return dataUsers.GetCount();
  58. }
  59. /// <summary>
  60. /// 根据帐号查询一条记录
  61. /// </summary>
  62. public RoadFlow.Data.Model.Users GetByAccount(string account)
  63. {
  64. return account.IsNullOrEmpty() ? null : dataUsers.GetByAccount(account);
  65. }
  66. /// <summary>
  67. /// 得到系统初始密码
  68. /// </summary>
  69. /// <returns></returns>
  70. public string GetInitPassword()
  71. {
  72. return RoadFlow.Utility.Config.SystemInitPassword;
  73. }
  74. /// <summary>
  75. /// 得到加密后的密码字符串
  76. /// </summary>
  77. /// <param name="password"></param>
  78. /// <returns></returns>
  79. public string EncryptionPassword(string password)
  80. {
  81. if (password.IsNullOrEmpty())
  82. {
  83. return "";
  84. }
  85. RoadFlow.Utility.HashEncrypt hash = new RoadFlow.Utility.HashEncrypt();
  86. return hash.MD5System(hash.MD5System(password)); //hash.SHA512Encrypt(hash.SHA512Encrypt(password.Trim()));
  87. }
  88. /// <summary>
  89. /// 得到一个用户加密后的密码
  90. /// </summary>
  91. /// <param name="userID"></param>
  92. /// <param name="password"></param>
  93. /// <returns></returns>
  94. public string GetUserEncryptionPassword(string userID, string password)
  95. {
  96. return password.IsNullOrEmpty() || userID.IsNullOrEmpty() ? "" : EncryptionPassword(userID.Trim().ToLower() + password.Trim());
  97. }
  98. /// <summary>
  99. /// 初始化一个用户密码
  100. /// </summary>
  101. /// <param name="userID"></param>
  102. /// <returns></returns>
  103. public bool InitPassword(Guid userID)
  104. {
  105. return dataUsers.UpdatePassword(GetUserEncryptionPassword(userID.ToString(), GetInitPassword()), userID);
  106. }
  107. /// <summary>
  108. /// 查询一个岗位下所有人员
  109. /// </summary>
  110. /// <param name="organizeID"></param>
  111. /// <returns></returns>
  112. public List<RoadFlow.Data.Model.Users> GetAllByOrganizeID(Guid organizeID)
  113. {
  114. return dataUsers.GetAllByOrganizeID(organizeID);
  115. }
  116. /// <summary>
  117. /// 得到一个用户的所有岗位
  118. /// </summary>
  119. /// <param name="userID"></param>
  120. /// <returns>Dictionary<Guid, bool> Guid 岗位ID bool 是否主要岗位</returns>
  121. public Dictionary<Guid, bool> GetAllStation(Guid userID)
  122. {
  123. UsersRelation ur = new UsersRelation();
  124. var urs = ur.GetAllByUserID(userID);
  125. Dictionary<Guid, bool> dict = new Dictionary<Guid, bool>();
  126. foreach (var u in urs)
  127. {
  128. if (!dict.ContainsKey(u.OrganizeID))
  129. {
  130. dict.Add(u.OrganizeID, u.IsMain == 1);
  131. }
  132. }
  133. return dict;
  134. }
  135. /// <summary>
  136. /// 得到一个用户的主要岗位
  137. /// </summary>
  138. /// <param name="userID"></param>
  139. /// <returns></returns>
  140. public Guid GetMainStation(Guid userID)
  141. {
  142. var ur = new UsersRelation().GetMainByUserID(userID);
  143. return ur == null ? Guid.Empty : ur.OrganizeID;
  144. }
  145. /// <summary>
  146. /// 查询一组岗位下所有人员
  147. /// </summary>
  148. /// <param name="organizeID"></param>
  149. /// <returns></returns>
  150. public List<RoadFlow.Data.Model.Users> GetAllByOrganizeIDArray(Guid[] organizeIDArray)
  151. {
  152. return dataUsers.GetAllByOrganizeIDArray(organizeIDArray);
  153. }
  154. /// <summary>
  155. /// 得到一个用户所在部门
  156. /// </summary>
  157. /// <param name="userID"></param>
  158. /// <returns></returns>
  159. public RoadFlow.Data.Model.Organize GetDeptByUserID(Guid userID)
  160. {
  161. Guid stationID=GetMainStation(userID);
  162. if(stationID==Guid.Empty)
  163. {
  164. return null;
  165. }
  166. var parents = new RoadFlow.Platform.Organize().GetAllParent(stationID);
  167. parents.Reverse();
  168. foreach (var parent in parents)
  169. {
  170. if (parent.Type == 2 || parent.Type==1)
  171. {
  172. return parent;
  173. }
  174. }
  175. return null;
  176. }
  177. /// <summary>
  178. /// 当前用户部门ID
  179. /// </summary>
  180. public static Guid CurrentDeptID
  181. {
  182. get
  183. {
  184. var dept = new Users().GetDeptByUserID(CurrentUserID);
  185. return dept == null ? Guid.Empty : dept.ID;
  186. }
  187. }
  188. /// <summary>
  189. /// 当前用户部门名称
  190. /// </summary>
  191. public static string CurrentDeptName
  192. {
  193. get
  194. {
  195. var dept = new Users().GetDeptByUserID(CurrentUserID);
  196. return dept == null ? "" : dept.Name;
  197. }
  198. }
  199. /// <summary>
  200. /// 检查帐号是否重复
  201. /// </summary>
  202. /// <param name="account">帐号</param>
  203. /// <param name="userID">人员ID(此人员除外)</param>
  204. /// <returns></returns>
  205. public bool HasAccount(string account, string userID = "")
  206. {
  207. return account.IsNullOrEmpty() ? false : dataUsers.HasAccount(account.Trim(), userID);
  208. }
  209. /// <summary>
  210. /// 修改用户密码
  211. /// </summary>
  212. /// <param name="password">明文的密码</param>
  213. /// <param name="userID"></param>
  214. /// <returns></returns>
  215. public bool UpdatePassword(string password, Guid userID)
  216. {
  217. return password.IsNullOrEmpty() ? false : dataUsers.UpdatePassword(GetUserEncryptionPassword(userID.ToString(), password.Trim()), userID);
  218. }
  219. /// <summary>
  220. /// 得到当前登录用户ID
  221. /// </summary>
  222. public static Guid CurrentUserID
  223. {
  224. get
  225. {
  226. object session = System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.UserID.ToString()];
  227. return session == null ? Guid.Empty : session.ToString().ToGuid();
  228. }
  229. }
  230. /// <summary>
  231. /// 得到当前登录用户
  232. /// </summary>
  233. public static RoadFlow.Data.Model.Users CurrentUser
  234. {
  235. get
  236. {
  237. object obj = System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.User.ToString()];
  238. if (obj == null)
  239. {
  240. Guid userID = CurrentUserID;
  241. if (userID == Guid.Empty)
  242. {
  243. return null;
  244. }
  245. else
  246. {
  247. var user = new Users().Get(userID);
  248. if (user != null)
  249. {
  250. System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.User.ToString()] = user;
  251. }
  252. return user;
  253. }
  254. }
  255. else
  256. {
  257. return obj as RoadFlow.Data.Model.Users;
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 当前用户名称
  263. /// </summary>
  264. [Obsolete("由于该方法拼写错误,请使用CurrentUserName属性")]
  265. public static string CurrentUsreName
  266. {
  267. get
  268. {
  269. return CurrentUser == null ? "" : CurrentUser.Name;
  270. }
  271. }
  272. /// <summary>
  273. /// 当前用户名称
  274. /// </summary>
  275. public static string CurrentUserName
  276. {
  277. get
  278. {
  279. return CurrentUser == null ? "" : CurrentUser.Name;
  280. }
  281. }
  282. /// <summary>
  283. /// 当前用户的所有角色
  284. /// </summary>
  285. public static List<Guid> CurrentUserRoles
  286. {
  287. get
  288. {
  289. Guid userID = CurrentUserID;
  290. if (userID.IsEmptyGuid())
  291. {
  292. return new List<Guid>();
  293. }
  294. List<Guid> list = new List<Guid>();
  295. var userRoles = new UsersRole().GetByUserIDFromCache(userID);
  296. foreach (var userRole in userRoles)
  297. {
  298. list.Add(userRole.RoleID);
  299. }
  300. return list;
  301. }
  302. }
  303. /// <summary>
  304. /// 得到一个不重复的帐号
  305. /// </summary>
  306. /// <param name="account"></param>
  307. /// <returns></returns>
  308. public string GetAccount(string account)
  309. {
  310. if (account.IsNullOrEmpty())
  311. {
  312. return "";
  313. }
  314. string newAccount = account.Trim();
  315. int i = 0;
  316. while (HasAccount(newAccount))
  317. {
  318. newAccount += (++i).ToString();
  319. }
  320. return newAccount;
  321. }
  322. /// <summary>
  323. /// 更新排序
  324. /// </summary>
  325. public int UpdateSort(Guid userID, int sort)
  326. {
  327. return dataUsers.UpdateSort(userID, sort);
  328. }
  329. /// <summary>
  330. /// 根据ID得到名称
  331. /// </summary>
  332. /// <param name="id"></param>
  333. /// <returns></returns>
  334. public string GetName(Guid id)
  335. {
  336. var user = Get(id);
  337. return user == null ? "" : user.Name;
  338. }
  339. /// <summary>
  340. /// 去除ID前缀
  341. /// </summary>
  342. /// <param name="id"></param>
  343. /// <returns></returns>
  344. public static string RemovePrefix(string id)
  345. {
  346. return id.IsNullOrEmpty() ? "" : id.Replace(PREFIX, "");
  347. }
  348. /// <summary>
  349. /// 去除ID前缀
  350. /// </summary>
  351. /// <param name="id"></param>
  352. /// <returns></returns>
  353. public string RemovePrefix1(string id)
  354. {
  355. return id.IsNullOrEmpty() ? "" : id.Replace(PREFIX, "");
  356. }
  357. /// <summary>
  358. /// 得到一个人员的主管
  359. /// </summary>
  360. /// <param name="userID"></param>
  361. /// <returns></returns>
  362. public string GetLeader(Guid userID)
  363. {
  364. var mainStation = GetMainStation(userID);
  365. if (mainStation == null)
  366. {
  367. return "";
  368. }
  369. RoadFlow.Platform.Organize borg = new Organize();
  370. var station = borg.Get(mainStation);
  371. if (station == null)
  372. {
  373. return "";
  374. }
  375. if (!station.Leader.IsNullOrEmpty())
  376. {
  377. return station.Leader;
  378. }
  379. var parents = borg.GetAllParent(station.Number);
  380. foreach (var parent in parents)
  381. {
  382. if (!parent.Leader.IsNullOrEmpty())
  383. {
  384. return parent.Leader;
  385. }
  386. }
  387. return "";
  388. }
  389. /// <summary>
  390. /// 得到一个人员的分管领导
  391. /// </summary>
  392. /// <param name="userID"></param>
  393. /// <returns></returns>
  394. public string GetChargeLeader(Guid userID)
  395. {
  396. var mainStation = GetMainStation(userID);
  397. if (mainStation == null)
  398. {
  399. return "";
  400. }
  401. RoadFlow.Platform.Organize borg = new Organize();
  402. var station = borg.Get(mainStation);
  403. if (station == null)
  404. {
  405. return "";
  406. }
  407. if (!station.ChargeLeader.IsNullOrEmpty())
  408. {
  409. return station.ChargeLeader;
  410. }
  411. var parents = borg.GetAllParent(station.Number);
  412. foreach (var parent in parents)
  413. {
  414. if (!parent.ChargeLeader.IsNullOrEmpty())
  415. {
  416. return parent.ChargeLeader;
  417. }
  418. }
  419. return "";
  420. }
  421. /// <summary>
  422. /// 判断一个人员是否是部门主管
  423. /// </summary>
  424. /// <param name="userID"></param>
  425. /// <returns></returns>
  426. public bool IsLeader(Guid userID)
  427. {
  428. string leader = GetLeader(userID);
  429. return leader.Contains(userID.ToString(), StringComparison.CurrentCultureIgnoreCase);
  430. }
  431. /// <summary>
  432. /// 判断一个人员是否是部门分管领导
  433. /// </summary>
  434. /// <param name="userID"></param>
  435. /// <returns></returns>
  436. public bool IsChargeLeader(Guid userID)
  437. {
  438. string leader = GetChargeLeader(userID);
  439. return leader.Contains(userID.ToString(), StringComparison.CurrentCultureIgnoreCase);
  440. }
  441. /// <summary>
  442. /// 判断一个人员是否在一个组织机构字符串里
  443. /// </summary>
  444. /// <param name="userID"></param>
  445. /// <param name="memberString"></param>
  446. /// <returns></returns>
  447. public bool IsContains(Guid userID, string memberString)
  448. {
  449. if (memberString.IsNullOrEmpty())
  450. {
  451. return false;
  452. }
  453. var user = new Organize().GetAllUsers(memberString).Find(p => p.ID == userID);
  454. return user != null;
  455. }
  456. }
  457. public class UsersEqualityComparer : IEqualityComparer<RoadFlow.Data.Model.Users>
  458. {
  459. public bool Equals(RoadFlow.Data.Model.Users user1, RoadFlow.Data.Model.Users user2)
  460. {
  461. return user1 == null || user2 == null || user1.ID == user2.ID;
  462. }
  463. public int GetHashCode(RoadFlow.Data.Model.Users user)
  464. {
  465. return user.ToString().GetHashCode();
  466. }
  467. }
  468. }