UserDepartmentManage.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Central.Control.Domain;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MES.Production.Service.ServiceImp
  7. {
  8. /// <summary>
  9. /// 用户部门关系业务实现类
  10. /// add 作者: 季健国 QQ:181589805 by 2016-09-08
  11. /// </summary>
  12. public class UserDepartmentManage:RepositoryBase<SYS_USER_DEPARTMENT>,IService.IUserDepartmentManage
  13. {
  14. /// <summary>
  15. /// 根据部门ID获取当前部门的所有用户ID集合
  16. /// </summary>
  17. public List<SYS_USER> GetUserListByDptId(List<string> dptId)
  18. {
  19. return this.LoadAll(p => dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.SYS_USER).ToList();
  20. }
  21. /// <summary>
  22. /// 根据用户ID获取所在的部门ID集合
  23. /// </summary>
  24. public List<SYS_DISTRIBUTORS> GetDptListByUserId(int userId)
  25. {
  26. return this.LoadAll(p => p.USER_ID == userId).Select(p=>p.SYS_DISTRIBUTORS).ToList();
  27. }
  28. /// <summary>
  29. /// 保存用户部门关系
  30. /// </summary>
  31. /// <param name="userId">用户ID</param>
  32. /// <param name="dptId">部门ID集合</param>
  33. public bool SaveUserDpt(int userId, string dptId)
  34. {
  35. try
  36. {
  37. //原始部门人员关系是否与当前设置一致,不一致重新构造
  38. if (this.IsExist(p => p.USER_ID == userId))
  39. {
  40. //存在之后再对比是否一致
  41. var oldCount = this.LoadAll(p => p.USER_ID == userId && dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.DEPARTMENT_ID).ToList();
  42. var newdptid = dptId.Split(',').OrderBy(c => c).ToList();
  43. if (oldCount.Count == newdptid.Count && oldCount.All(newdptid.Contains)) return true;
  44. //删除原有关系
  45. this.Delete(p => p.USER_ID == userId);
  46. }
  47. if (!string.IsNullOrEmpty(dptId))
  48. {
  49. //添加现有关系
  50. var list = dptId.Split(',').Select(item => new SYS_USER_DEPARTMENT()
  51. {
  52. DEPARTMENT_ID = item,
  53. USER_ID = userId
  54. }).ToList();
  55. return this.SaveList(list) > 0;
  56. }
  57. return true;
  58. }
  59. catch (Exception e) { throw e.InnerException; }
  60. }
  61. }
  62. }