OrganizationController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using wispro.sp.entity;
  8. namespace wispro.sp.api.Controllers
  9. {
  10. [Route("api/[controller]/[action]")]
  11. [ApiController]
  12. public class OrganizationController : ControllerBase
  13. {
  14. spDbContext Context;
  15. public OrganizationController(spDbContext context)
  16. {
  17. Context = context;
  18. }
  19. #region 部门API
  20. [HttpPost]
  21. public Department SaveDept(Department dept)
  22. {
  23. if(dept.Id > 0)
  24. {
  25. var inDbObj = Context.Departments.FirstOrDefault<Department>(d=>d.Id == dept.Id);
  26. inDbObj.Name = dept.Name;
  27. inDbObj.Memo = dept.Memo;
  28. if(dept.order_num != inDbObj.order_num)
  29. {
  30. inDbObj.order_num = dept.order_num;
  31. }
  32. if(dept.parentId != inDbObj.parentId)
  33. {
  34. inDbObj.parentId = dept.parentId;
  35. Department parentDept = null;
  36. if (dept.parentId != null)
  37. {
  38. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  39. }
  40. if (parentDept != null || !string.IsNullOrEmpty(parentDept.ancestors))
  41. {
  42. inDbObj.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  43. }
  44. else
  45. {
  46. inDbObj.ancestors = parentDept.Id.ToString();
  47. }
  48. if (dept.order_num == null)
  49. {
  50. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.parentId).Max(d => d.order_num);
  51. if (temOrderNum == null)
  52. {
  53. inDbObj.order_num = temOrderNum + 1;
  54. }
  55. else
  56. {
  57. inDbObj.order_num = 1;
  58. }
  59. }
  60. else
  61. {
  62. inDbObj.order_num = dept.order_num;
  63. }
  64. }
  65. Context.SaveChanges();
  66. return inDbObj;
  67. }
  68. else
  69. {
  70. Department parentDept = null;
  71. if (dept.parentId != null)
  72. {
  73. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  74. }
  75. if (dept.order_num == null)
  76. {
  77. if(parentDept == null)
  78. {
  79. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == null).Max(d => d.order_num);
  80. if (temOrderNum != null)
  81. {
  82. dept.order_num = temOrderNum + 1;
  83. }
  84. else
  85. {
  86. dept.order_num = 1;
  87. }
  88. }
  89. else
  90. {
  91. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.Id ).Max(d => d.order_num);
  92. if (temOrderNum != null)
  93. {
  94. dept.order_num = temOrderNum + 1;
  95. }
  96. else
  97. {
  98. dept.order_num = 1;
  99. }
  100. }
  101. }
  102. if(parentDept != null && !string.IsNullOrEmpty(parentDept.ancestors ))
  103. {
  104. dept.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  105. }
  106. else
  107. {
  108. if (parentDept != null)
  109. {
  110. dept.ancestors = parentDept.Id.ToString();
  111. }
  112. }
  113. Context.Departments.Add(dept);
  114. Context.SaveChanges();
  115. return dept;
  116. }
  117. }
  118. public List<Department> GetDepartments()
  119. {
  120. return Context.Departments.ToList();
  121. }
  122. public bool DeleteDept(int Id)
  123. {
  124. try
  125. {
  126. var delObj = Context.Departments.FirstOrDefault<Department>(d => d.Id == Id);
  127. Context.Departments.Remove(delObj);
  128. Context.SaveChanges();
  129. return true;
  130. }
  131. catch(Exception ex)
  132. {
  133. return false;
  134. }
  135. }
  136. #endregion
  137. }
  138. }