OrganizationController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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(int? deptId)
  138. {
  139. if (deptId == null)
  140. {
  141. return Context.Positions.ToList();
  142. }
  143. else
  144. {
  145. var retList = Context.DepartmentPositions
  146. .Where<DepartmentPosition>(p => p.departmentId == deptId)
  147. .Select(p => p.Position).Distinct<Position>().ToList();
  148. return retList;
  149. }
  150. }
  151. public Position GetPosition(int deptId,int staffId)
  152. {
  153. var data = Context.DepartmentPositions.Include(pi => pi.Position).FirstOrDefault<DepartmentPosition>(s => s.StaffId == staffId);
  154. if(data != null)
  155. {
  156. return data.Position;
  157. }
  158. else
  159. {
  160. return null;
  161. }
  162. }
  163. #endregion
  164. public List<Customer> GetCustomers()
  165. {
  166. return Context.Customers.ToList();
  167. }
  168. public void InitUserDepartment()
  169. {
  170. Position ptPosition = Context.Positions.FirstOrDefault(s => s.Name == "普通员工");
  171. if(ptPosition == null)
  172. {
  173. ptPosition = new Position() { Name = "普通员工" };
  174. Context.Positions.Add(ptPosition);
  175. Context.SaveChanges();
  176. }
  177. var lstStaff = Context.Staffs.ToList();
  178. foreach(var sf in lstStaff)
  179. {
  180. if (!string.IsNullOrEmpty(sf.Department))
  181. {
  182. string[] deptStrings = sf.Department.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  183. if (deptStrings.Length > 0)
  184. {
  185. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("\\((.+?)\\)");
  186. System.Text.RegularExpressions.Match m = r.Match(deptStrings[0]);
  187. Department pDept;
  188. if (m.Success)
  189. {
  190. string firstName = deptStrings[0].Substring(0, m.Index).Trim();
  191. string secondName = m.Groups[1].Value.Trim();
  192. pDept = Context.Departments.First<Department>(s=>s.Name == firstName);
  193. if(pDept == null)
  194. {
  195. pDept = new Department() { Name = firstName };
  196. Context.Departments.Add(pDept);
  197. Context.SaveChanges();
  198. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  199. if (secondDept == null)
  200. {
  201. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  202. Context.Departments.Add(secondDept);
  203. Context.SaveChanges();
  204. }
  205. pDept = secondDept;
  206. }
  207. else
  208. {
  209. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  210. if (secondDept == null)
  211. {
  212. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  213. Context.Departments.Add(secondDept);
  214. Context.SaveChanges();
  215. }
  216. pDept = secondDept;
  217. }
  218. }
  219. else
  220. {
  221. pDept = Context.Departments.FirstOrDefault<Department>(s => s.Name == deptStrings[0].Trim());
  222. if (pDept == null)
  223. {
  224. pDept = new Department() { Name = deptStrings[0].Trim() };
  225. Context.Departments.Add(pDept);
  226. Context.SaveChanges();
  227. }
  228. }
  229. for(int i = 1; i < deptStrings.Length; i++)
  230. {
  231. Department curDept = Context.Departments.FirstOrDefault<Department>(d=>d.parentId == pDept.Id && d.Name == deptStrings[i].Trim());
  232. if(curDept == null)
  233. {
  234. curDept = new Department()
  235. {
  236. Name = deptStrings[i].Trim(),
  237. parentId = pDept.Id,
  238. ancestors = (pDept.parentId == null) ? pDept.parentId.ToString() : $"{pDept.ancestors},{pDept.Id}"
  239. };
  240. Context.Departments.Add(curDept);
  241. Context.SaveChanges();
  242. }
  243. pDept = curDept;
  244. }
  245. if(pDept != null)
  246. {
  247. DepartmentPosition dp = Context.DepartmentPositions.FirstOrDefault(p => p.StaffId == sf.Id && p.departmentId == pDept.Id && p.PositionId == ptPosition.Id);
  248. if (dp == null)
  249. {
  250. dp = new DepartmentPosition();
  251. dp.StaffId = sf.Id;
  252. dp.departmentId = pDept.Id;
  253. dp.PositionId = ptPosition.Id;
  254. Context.DepartmentPositions.Add(dp);
  255. Context.SaveChanges();
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }