OrganizationController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using wispro.sp.entity;
  10. namespace wispro.sp.api.Controllers
  11. {
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. [Authorize]
  15. public class OrganizationController : ControllerBase
  16. {
  17. spDbContext Context;
  18. public OrganizationController(spDbContext context)
  19. {
  20. Context = context;
  21. }
  22. #region 部门API
  23. [HttpPost]
  24. public Department SaveDept(Department dept)
  25. {
  26. if(dept.Id > 0)
  27. {
  28. var inDbObj = Context.Departments.FirstOrDefault<Department>(d=>d.Id == dept.Id);
  29. inDbObj.Name = dept.Name;
  30. inDbObj.Memo = dept.Memo;
  31. if(dept.order_num != inDbObj.order_num)
  32. {
  33. inDbObj.order_num = dept.order_num;
  34. }
  35. if(dept.parentId != inDbObj.parentId)
  36. {
  37. inDbObj.parentId = dept.parentId;
  38. Department parentDept = null;
  39. if (dept.parentId != null)
  40. {
  41. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  42. }
  43. if (parentDept != null || !string.IsNullOrEmpty(parentDept.ancestors))
  44. {
  45. inDbObj.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  46. }
  47. else
  48. {
  49. inDbObj.ancestors = parentDept.Id.ToString();
  50. }
  51. if (dept.order_num == null)
  52. {
  53. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.parentId).Max(d => d.order_num);
  54. if (temOrderNum == null)
  55. {
  56. inDbObj.order_num = temOrderNum + 1;
  57. }
  58. else
  59. {
  60. inDbObj.order_num = 1;
  61. }
  62. }
  63. else
  64. {
  65. inDbObj.order_num = dept.order_num;
  66. }
  67. }
  68. Context.SaveChanges();
  69. return inDbObj;
  70. }
  71. else
  72. {
  73. Department parentDept = null;
  74. if (dept.parentId != null)
  75. {
  76. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  77. }
  78. if (dept.order_num == null)
  79. {
  80. if(parentDept == null)
  81. {
  82. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == null).Max(d => d.order_num);
  83. if (temOrderNum != null)
  84. {
  85. dept.order_num = temOrderNum + 1;
  86. }
  87. else
  88. {
  89. dept.order_num = 1;
  90. }
  91. }
  92. else
  93. {
  94. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.Id ).Max(d => d.order_num);
  95. if (temOrderNum != null)
  96. {
  97. dept.order_num = temOrderNum + 1;
  98. }
  99. else
  100. {
  101. dept.order_num = 1;
  102. }
  103. }
  104. }
  105. if(parentDept != null && !string.IsNullOrEmpty(parentDept.ancestors ))
  106. {
  107. dept.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  108. }
  109. else
  110. {
  111. if (parentDept != null)
  112. {
  113. dept.ancestors = parentDept.Id.ToString();
  114. }
  115. }
  116. Context.Departments.Add(dept);
  117. Context.SaveChanges();
  118. return dept;
  119. }
  120. }
  121. public List<Department> GetDepartments()
  122. {
  123. return Context.Departments.ToList();
  124. }
  125. public bool DeleteDept(int Id)
  126. {
  127. try
  128. {
  129. var delObj = Context.Departments.FirstOrDefault<Department>(d => d.Id == Id);
  130. Context.Departments.Remove(delObj);
  131. Context.SaveChanges();
  132. return true;
  133. }
  134. catch(Exception ex)
  135. {
  136. return false;
  137. }
  138. }
  139. public List<Position> GetPositions(int? deptId)
  140. {
  141. if (deptId == null)
  142. {
  143. return Context.Positions.ToList();
  144. }
  145. else
  146. {
  147. var retList = Context.DepartmentPositions
  148. .Where<DepartmentPosition>(p => p.departmentId == deptId)
  149. .Select(p => p.Position).Distinct<Position>().ToList();
  150. return retList;
  151. }
  152. }
  153. public Position GetPosition(int deptId,int staffId)
  154. {
  155. var data = Context.DepartmentPositions.Include(pi => pi.Position).FirstOrDefault<DepartmentPosition>(s => s.StaffId == staffId);
  156. if(data != null)
  157. {
  158. return data.Position;
  159. }
  160. else
  161. {
  162. return null;
  163. }
  164. }
  165. #endregion
  166. public List<Customer> GetCustomers()
  167. {
  168. return Context.Customers.ToList();
  169. }
  170. public void InitUserDepartment()
  171. {
  172. Position ptPosition = Context.Positions.FirstOrDefault(s => s.Name == "普通员工");
  173. if(ptPosition == null)
  174. {
  175. ptPosition = new Position() { Name = "普通员工" };
  176. Context.Positions.Add(ptPosition);
  177. Context.SaveChanges();
  178. }
  179. var lstStaff = Context.Staffs.ToList();
  180. foreach(var sf in lstStaff)
  181. {
  182. if (!string.IsNullOrEmpty(sf.Department))
  183. {
  184. string[] deptStrings = sf.Department.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  185. if (deptStrings.Length > 0)
  186. {
  187. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("\\((.+?)\\)");
  188. System.Text.RegularExpressions.Match m = r.Match(deptStrings[0]);
  189. Department pDept;
  190. if (m.Success)
  191. {
  192. string firstName = deptStrings[0].Substring(0, m.Index).Trim();
  193. string secondName = m.Groups[1].Value.Trim();
  194. pDept = Context.Departments.First<Department>(s=>s.Name == firstName);
  195. if(pDept == null)
  196. {
  197. pDept = new Department() { Name = firstName };
  198. Context.Departments.Add(pDept);
  199. Context.SaveChanges();
  200. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  201. if (secondDept == null)
  202. {
  203. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  204. Context.Departments.Add(secondDept);
  205. Context.SaveChanges();
  206. }
  207. pDept = secondDept;
  208. }
  209. else
  210. {
  211. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  212. if (secondDept == null)
  213. {
  214. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  215. Context.Departments.Add(secondDept);
  216. Context.SaveChanges();
  217. }
  218. pDept = secondDept;
  219. }
  220. }
  221. else
  222. {
  223. pDept = Context.Departments.FirstOrDefault<Department>(s => s.Name == deptStrings[0].Trim());
  224. if (pDept == null)
  225. {
  226. pDept = new Department() { Name = deptStrings[0].Trim() };
  227. Context.Departments.Add(pDept);
  228. Context.SaveChanges();
  229. }
  230. }
  231. for(int i = 1; i < deptStrings.Length; i++)
  232. {
  233. Department curDept = Context.Departments.FirstOrDefault<Department>(d=>d.parentId == pDept.Id && d.Name == deptStrings[i].Trim());
  234. if(curDept == null)
  235. {
  236. curDept = new Department()
  237. {
  238. Name = deptStrings[i].Trim(),
  239. parentId = pDept.Id,
  240. ancestors = (pDept.parentId == null) ? pDept.parentId.ToString() : $"{pDept.ancestors},{pDept.Id}"
  241. };
  242. Context.Departments.Add(curDept);
  243. Context.SaveChanges();
  244. }
  245. pDept = curDept;
  246. }
  247. if(pDept != null)
  248. {
  249. DepartmentPosition dp = Context.DepartmentPositions.FirstOrDefault(p => p.StaffId == sf.Id && p.departmentId == pDept.Id && p.PositionId == ptPosition.Id);
  250. if (dp == null)
  251. {
  252. dp = new DepartmentPosition();
  253. dp.StaffId = sf.Id;
  254. dp.departmentId = pDept.Id;
  255. dp.PositionId = ptPosition.Id;
  256. Context.DepartmentPositions.Add(dp);
  257. Context.SaveChanges();
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }