OrganizationController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. using wispro.sp.share;
  11. namespace wispro.sp.api.Controllers
  12. {
  13. [Route("api/[controller]/[action]")]
  14. [ApiController]
  15. [Authorize]
  16. public class OrganizationController : ControllerBase
  17. {
  18. spDbContext Context;
  19. public OrganizationController(spDbContext context)
  20. {
  21. Context = context;
  22. }
  23. #region 部门API
  24. [HttpPost]
  25. public Department SaveDept(Department dept)
  26. {
  27. if(dept.Id > 0)
  28. {
  29. var inDbObj = Context.Departments.FirstOrDefault<Department>(d=>d.Id == dept.Id);
  30. inDbObj.Name = dept.Name;
  31. inDbObj.Memo = dept.Memo;
  32. if(dept.order_num != inDbObj.order_num)
  33. {
  34. inDbObj.order_num = dept.order_num;
  35. }
  36. if(dept.parentId != inDbObj.parentId)
  37. {
  38. inDbObj.parentId = dept.parentId;
  39. Department parentDept = null;
  40. if (dept.parentId != null)
  41. {
  42. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  43. }
  44. if (parentDept != null || !string.IsNullOrEmpty(parentDept.ancestors))
  45. {
  46. inDbObj.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  47. }
  48. else
  49. {
  50. inDbObj.ancestors = parentDept.Id.ToString();
  51. }
  52. if (dept.order_num == null)
  53. {
  54. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.parentId).Max(d => d.order_num);
  55. if (temOrderNum == null)
  56. {
  57. inDbObj.order_num = temOrderNum + 1;
  58. }
  59. else
  60. {
  61. inDbObj.order_num = 1;
  62. }
  63. }
  64. else
  65. {
  66. inDbObj.order_num = dept.order_num;
  67. }
  68. }
  69. Context.SaveChanges();
  70. return inDbObj;
  71. }
  72. else
  73. {
  74. Department parentDept = null;
  75. if (dept.parentId != null)
  76. {
  77. parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
  78. }
  79. if (dept.order_num == null)
  80. {
  81. if(parentDept == null)
  82. {
  83. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == null).Max(d => d.order_num);
  84. if (temOrderNum != null)
  85. {
  86. dept.order_num = temOrderNum + 1;
  87. }
  88. else
  89. {
  90. dept.order_num = 1;
  91. }
  92. }
  93. else
  94. {
  95. var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.Id ).Max(d => d.order_num);
  96. if (temOrderNum != null)
  97. {
  98. dept.order_num = temOrderNum + 1;
  99. }
  100. else
  101. {
  102. dept.order_num = 1;
  103. }
  104. }
  105. }
  106. if(parentDept != null && !string.IsNullOrEmpty(parentDept.ancestors ))
  107. {
  108. dept.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  109. }
  110. else
  111. {
  112. if (parentDept != null)
  113. {
  114. dept.ancestors = parentDept.Id.ToString();
  115. }
  116. }
  117. Context.Departments.Add(dept);
  118. Context.SaveChanges();
  119. return dept;
  120. }
  121. }
  122. public List<Department> GetDepartments()
  123. {
  124. return Context.Departments.ToList();
  125. }
  126. public bool DeleteDept(int Id)
  127. {
  128. try
  129. {
  130. var delObj = Context.Departments.FirstOrDefault<Department>(d => d.Id == Id);
  131. Context.Departments.Remove(delObj);
  132. Context.SaveChanges();
  133. return true;
  134. }
  135. catch(Exception ex)
  136. {
  137. return false;
  138. }
  139. }
  140. public List<Position> GetPositions(int? deptId)
  141. {
  142. if (deptId == null)
  143. {
  144. return Context.Positions.ToList();
  145. }
  146. else
  147. {
  148. var retList = Context.DepartmentPositions
  149. .Where<DepartmentPosition>(p => p.departmentId == deptId)
  150. .Select(p => p.Position).Distinct<Position>().ToList();
  151. return retList;
  152. }
  153. }
  154. public Position GetPosition(int deptId,int staffId)
  155. {
  156. var data = Context.DepartmentPositions.Include(pi => pi.Position).FirstOrDefault<DepartmentPosition>(s => s.StaffId == staffId);
  157. if(data != null)
  158. {
  159. return data.Position;
  160. }
  161. else
  162. {
  163. return null;
  164. }
  165. }
  166. #endregion
  167. public List<Customer> GetCustomers()
  168. {
  169. return Context.Customers.ToList();
  170. }
  171. public ApiSaveResponse SaveCustomer(Customer customer)
  172. {
  173. ApiSaveResponse ret = new ApiSaveResponse();
  174. try
  175. {
  176. if (customer.Id == 0)
  177. {
  178. Customer temCustomer = Context.Customers.Where<Customer>(x => x.Name== customer.Name).FirstOrDefault();
  179. if (temCustomer != null)
  180. {
  181. if(temCustomer.Name != customer.Name)
  182. {
  183. temCustomer.Name = customer.Name;
  184. }
  185. if(temCustomer.ResponseManId != customer.ResponseManId)
  186. {
  187. temCustomer.ResponseManId = customer.ResponseManId;
  188. }
  189. if (temCustomer.ContactMan != customer.ContactMan)
  190. {
  191. temCustomer.ContactMan = customer.ContactMan;
  192. }
  193. if (temCustomer.Address != customer.Address)
  194. {
  195. temCustomer.Address = customer.Address;
  196. }
  197. Context.SaveChanges();
  198. }
  199. else
  200. {
  201. Context.Customers.Add(customer);
  202. }
  203. }
  204. else
  205. {
  206. Customer temCustomer = Context.Customers.Where<Customer>(x => x.Id == customer.Id).FirstOrDefault();
  207. if (temCustomer != null)
  208. {
  209. if (temCustomer.Name != customer.Name)
  210. {
  211. temCustomer.Name = customer.Name;
  212. }
  213. if (temCustomer.ResponseManId != customer.ResponseManId)
  214. {
  215. temCustomer.ResponseManId = customer.ResponseManId;
  216. }
  217. if (temCustomer.ContactMan != customer.ContactMan)
  218. {
  219. temCustomer.ContactMan = customer.ContactMan;
  220. }
  221. if (temCustomer.Address != customer.Address)
  222. {
  223. temCustomer.Address = customer.Address;
  224. }
  225. Context.SaveChanges();
  226. //ret.Success = false;
  227. //ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  228. }
  229. else
  230. {
  231. ret.Success = false;
  232. ret.ErrorMessage = $"编号为【{customer.Id}】的客户不存在!";
  233. }
  234. }
  235. Context.SaveChanges();
  236. ret.Success = true;
  237. }
  238. catch (Exception ex)
  239. {
  240. ret.Success = false;
  241. ret.ErrorMessage = ex.Message;
  242. }
  243. return ret;
  244. }
  245. public void InitUserDepartment()
  246. {
  247. Position ptPosition = Context.Positions.FirstOrDefault(s => s.Name == "普通员工");
  248. if(ptPosition == null)
  249. {
  250. ptPosition = new Position() { Name = "普通员工" };
  251. Context.Positions.Add(ptPosition);
  252. Context.SaveChanges();
  253. }
  254. var lstStaff = Context.Staffs.ToList();
  255. foreach(var sf in lstStaff)
  256. {
  257. if (!string.IsNullOrEmpty(sf.Department))
  258. {
  259. string[] deptStrings = sf.Department.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  260. if (deptStrings.Length > 0)
  261. {
  262. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("\\((.+?)\\)");
  263. System.Text.RegularExpressions.Match m = r.Match(deptStrings[0]);
  264. Department pDept;
  265. if (m.Success)
  266. {
  267. string firstName = deptStrings[0].Substring(0, m.Index).Trim();
  268. string secondName = m.Groups[1].Value.Trim();
  269. pDept = Context.Departments.First<Department>(s=>s.Name == firstName);
  270. if(pDept == null)
  271. {
  272. pDept = new Department() { Name = firstName };
  273. Context.Departments.Add(pDept);
  274. Context.SaveChanges();
  275. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  276. if (secondDept == null)
  277. {
  278. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  279. Context.Departments.Add(secondDept);
  280. Context.SaveChanges();
  281. }
  282. pDept = secondDept;
  283. }
  284. else
  285. {
  286. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  287. if (secondDept == null)
  288. {
  289. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  290. Context.Departments.Add(secondDept);
  291. Context.SaveChanges();
  292. }
  293. pDept = secondDept;
  294. }
  295. }
  296. else
  297. {
  298. pDept = Context.Departments.FirstOrDefault<Department>(s => s.Name == deptStrings[0].Trim());
  299. if (pDept == null)
  300. {
  301. pDept = new Department() { Name = deptStrings[0].Trim() };
  302. Context.Departments.Add(pDept);
  303. Context.SaveChanges();
  304. }
  305. }
  306. for(int i = 1; i < deptStrings.Length; i++)
  307. {
  308. Department curDept = Context.Departments.FirstOrDefault<Department>(d=>d.parentId == pDept.Id && d.Name == deptStrings[i].Trim());
  309. if(curDept == null)
  310. {
  311. curDept = new Department()
  312. {
  313. Name = deptStrings[i].Trim(),
  314. parentId = pDept.Id,
  315. ancestors = (pDept.parentId == null) ? pDept.parentId.ToString() : $"{pDept.ancestors},{pDept.Id}"
  316. };
  317. Context.Departments.Add(curDept);
  318. Context.SaveChanges();
  319. }
  320. pDept = curDept;
  321. }
  322. if(pDept != null)
  323. {
  324. DepartmentPosition dp = Context.DepartmentPositions.FirstOrDefault(p => p.StaffId == sf.Id);
  325. if (dp == null)
  326. {
  327. dp = new DepartmentPosition();
  328. dp.StaffId = sf.Id;
  329. dp.departmentId = pDept.Id;
  330. dp.PositionId = ptPosition.Id;
  331. Context.DepartmentPositions.Add(dp);
  332. Context.SaveChanges();
  333. }
  334. else
  335. {
  336. if (pDept.Id != dp.departmentId)
  337. {
  338. dp.departmentId = pDept.Id;
  339. dp.PositionId = ptPosition.Id;
  340. Context.SaveChanges();
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }