OrganizationController.cs 5.6 KB

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