PostDepartmentManage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Central.Control.Domain;
  2. using MES.Production.Service.IService;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace MES.Production.Service.ServiceImp
  8. {
  9. /// <summary>
  10. /// 岗位部门关系业务实现类
  11. /// add 作者: 季健国 QQ:181589805 by 2016-09-10
  12. /// </summary>
  13. public class PostDepartmentManage : RepositoryBase<SYS_POST_DEPARTMENT>, IService.IPostDepartmentManage
  14. {
  15. /// <summary>
  16. /// 岗位人员
  17. /// </summary>
  18. IPostUserManage PostUserManage { get; set; }
  19. /// <summary>
  20. /// 根据部门ID获取岗位集合
  21. /// </summary>
  22. public List<SYS_POST> GetPostIDByDptId(string dptId)
  23. {
  24. return this.LoadAll(p => dptId == p.FK_DEPARTMENT_ID).Select(p => p.SYS_POST).ToList();
  25. }
  26. /// <summary>
  27. /// 根据岗位ID获取所应用部门的集合
  28. /// </summary>
  29. public List<SYS_DISTRIBUTORS> GetDptIDByPostID(string postId)
  30. {
  31. return this.LoadAll(p => postId == p.FK_POST_ID).Select(p => p.SYS_DISTRIBUTORS).ToList();
  32. }
  33. /// <summary>
  34. /// 根据部门获取部门岗位,供人员选择岗位使用
  35. /// </summary>
  36. public dynamic GetPostDptInfoByDptId(string dptId)
  37. {
  38. string sql = @"select d.name+'-'+ p.postname as postname,
  39. C.NAMETEXT AS posttype,s.id
  40. from sys_post p inner join Sys_Post_Department s
  41. on p.id=s.fk_post_id inner join SYS_DISTRIBUTORS d
  42. on s.fk_department_id=d.id
  43. left join sys_code c on p.posttype=c.codevalue and c.codetype='POSTTYPE'
  44. where s.fk_department_id in(" + dptId + ")";
  45. return this.ExecuteSqlQuery(sql);
  46. }
  47. /// <summary>
  48. /// 设置部门岗位
  49. /// </summary>
  50. public bool SetPost(string dptId, string postId,out string msg)
  51. {
  52. msg = string.Empty;
  53. var newpost = postId.Split(',').ToList();
  54. try
  55. {
  56. //----验证当前部门的岗位是否分配了人员
  57. //原有岗位是否存在
  58. if (this.IsExist(p => p.FK_DEPARTMENT_ID == dptId))
  59. {
  60. //原有岗位部门数据
  61. var originalPost = this.LoadAll(p => p.FK_DEPARTMENT_ID == dptId);
  62. var oldpost = originalPost.Select(p => p.FK_POST_ID).OrderBy(p => p).ToList();
  63. #region 对比当前选择的岗位与原有岗位是否一致
  64. //规则:1、判断数量 2、排序对比内项
  65. if (originalPost.Count() == newpost.Count && oldpost.All(newpost.Contains)) return true;
  66. #endregion
  67. #region 不一致处理
  68. //查找不一致的岗位记录,是否存在人员分配
  69. var differentPost = originalPost.Where(p => !postId.Contains(p.FK_POST_ID)).ToList();
  70. var differentPostDptId =
  71. differentPost.Aggregate(string.Empty, (current, t) => current + t.ID + ",").TrimEnd(',');
  72. //有人员分配返回false 并提示那个岗位分配了人员
  73. foreach (var item in differentPost.Select(c=>c.ID).ToList())
  74. {
  75. var postUser = this.PostUserManage.LoadAll(p => p.FK_POST_DEPARTMENTID == item).Select(p => p.SYS_POST_DEPARTMENT.SYS_POST.POSTNAME).GroupBy(p => p).ToList();
  76. if (postUser.Any())
  77. {
  78. msg += postUser[0].Key + " 岗位中存在人员,请先将人员分配其他岗位<br/>";
  79. }
  80. }
  81. if (!string.IsNullOrEmpty(msg)) return false;
  82. //删除原有岗位部门废弃的记录
  83. this.Delete(p => differentPostDptId.Contains(p.ID.ToString()));
  84. //移除原有不变的岗位ID
  85. postId = postId.PadRight(postId.Length + 1, ',');
  86. foreach (var item in originalPost.Where(c => postId.Contains(c.FK_POST_ID)).Select(c => c.FK_POST_ID).ToList())
  87. {
  88. postId = postId.Replace(item, "");
  89. }
  90. postId = postId.TrimStart(',').TrimEnd(',');
  91. #endregion
  92. }
  93. //保存新的数据
  94. if (!string.IsNullOrEmpty(postId))
  95. {
  96. var list = postId.Split(',').Select(item => new SYS_POST_DEPARTMENT()
  97. {
  98. FK_DEPARTMENT_ID = dptId,
  99. FK_POST_ID = item
  100. }).ToList();
  101. return this.SaveList(list) > 0;
  102. }
  103. return true;
  104. }
  105. catch (Exception e) { throw e.InnerException; }
  106. }
  107. }
  108. }