OrganizationController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 Customer GetCustomer(int id)
  172. {
  173. return Context.Customers.FirstOrDefault(c => c.Id == id);
  174. }
  175. public ApiSaveResponse SaveCustomer(Customer customer)
  176. {
  177. ApiSaveResponse ret = new ApiSaveResponse();
  178. try
  179. {
  180. if (customer.Id == 0)
  181. {
  182. Customer temCustomer = Context.Customers.Where<Customer>(x => x.Name== customer.Name).FirstOrDefault();
  183. if (temCustomer != null)
  184. {
  185. if(temCustomer.Name != customer.Name)
  186. {
  187. temCustomer.Name = customer.Name;
  188. }
  189. if(temCustomer.ResponseManId != customer.ResponseManId)
  190. {
  191. temCustomer.ResponseManId = customer.ResponseManId;
  192. }
  193. if (temCustomer.ContactMan != customer.ContactMan)
  194. {
  195. temCustomer.ContactMan = customer.ContactMan;
  196. }
  197. if (temCustomer.Address != customer.Address)
  198. {
  199. temCustomer.Address = customer.Address;
  200. }
  201. Context.SaveChanges();
  202. }
  203. else
  204. {
  205. Context.Customers.Add(customer);
  206. }
  207. }
  208. else
  209. {
  210. Customer temCustomer = Context.Customers.Where<Customer>(x => x.Id == customer.Id).FirstOrDefault();
  211. if (temCustomer != null)
  212. {
  213. if (temCustomer.Name != customer.Name)
  214. {
  215. temCustomer.Name = customer.Name;
  216. }
  217. if (temCustomer.ResponseManId != customer.ResponseManId)
  218. {
  219. temCustomer.ResponseManId = customer.ResponseManId;
  220. }
  221. if (temCustomer.ContactMan != customer.ContactMan)
  222. {
  223. temCustomer.ContactMan = customer.ContactMan;
  224. }
  225. if (temCustomer.Address != customer.Address)
  226. {
  227. temCustomer.Address = customer.Address;
  228. }
  229. if(temCustomer.ARate != customer.ARate)
  230. {
  231. temCustomer.ARate = customer.ARate;
  232. }
  233. if (temCustomer.SRate != customer.SRate)
  234. {
  235. temCustomer.SRate = customer.SRate;
  236. }
  237. Context.SaveChanges();
  238. //ret.Success = false;
  239. //ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  240. }
  241. else
  242. {
  243. ret.Success = false;
  244. ret.ErrorMessage = $"编号为【{customer.Id}】的客户不存在!";
  245. }
  246. }
  247. Context.SaveChanges();
  248. ret.Success = true;
  249. }
  250. catch (Exception ex)
  251. {
  252. ret.Success = false;
  253. ret.ErrorMessage = ex.Message;
  254. }
  255. return ret;
  256. }
  257. public void InitUserDepartment()
  258. {
  259. Position ptPosition = Context.Positions.FirstOrDefault(s => s.Name == "普通员工");
  260. if(ptPosition == null)
  261. {
  262. ptPosition = new Position() { Name = "普通员工" };
  263. Context.Positions.Add(ptPosition);
  264. Context.SaveChanges();
  265. }
  266. var lstStaff = Context.Staffs.ToList();
  267. foreach(var sf in lstStaff)
  268. {
  269. if (!string.IsNullOrEmpty(sf.Department))
  270. {
  271. string[] deptStrings = sf.Department.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  272. if (deptStrings.Length > 0)
  273. {
  274. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("\\((.+?)\\)");
  275. System.Text.RegularExpressions.Match m = r.Match(deptStrings[0]);
  276. Department pDept;
  277. if (m.Success)
  278. {
  279. string firstName = deptStrings[0].Substring(0, m.Index).Trim();
  280. string secondName = m.Groups[1].Value.Trim();
  281. pDept = Context.Departments.First<Department>(s=>s.Name == firstName);
  282. if(pDept == null)
  283. {
  284. pDept = new Department() { Name = firstName };
  285. Context.Departments.Add(pDept);
  286. Context.SaveChanges();
  287. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  288. if (secondDept == null)
  289. {
  290. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  291. Context.Departments.Add(secondDept);
  292. Context.SaveChanges();
  293. }
  294. pDept = secondDept;
  295. }
  296. else
  297. {
  298. Department secondDept = Context.Departments.FirstOrDefault<Department>(d => d.parentId == pDept.Id && d.Name == secondName);
  299. if (secondDept == null)
  300. {
  301. secondDept = new Department() { Name = secondName, parentId = pDept.Id, ancestors = pDept.Id.ToString() };
  302. Context.Departments.Add(secondDept);
  303. Context.SaveChanges();
  304. }
  305. pDept = secondDept;
  306. }
  307. }
  308. else
  309. {
  310. pDept = Context.Departments.FirstOrDefault<Department>(s => s.Name == deptStrings[0].Trim());
  311. if (pDept == null)
  312. {
  313. pDept = new Department() { Name = deptStrings[0].Trim() };
  314. Context.Departments.Add(pDept);
  315. Context.SaveChanges();
  316. }
  317. }
  318. for(int i = 1; i < deptStrings.Length; i++)
  319. {
  320. Department curDept = Context.Departments.FirstOrDefault<Department>(d=>d.parentId == pDept.Id && d.Name == deptStrings[i].Trim());
  321. if(curDept == null)
  322. {
  323. curDept = new Department()
  324. {
  325. Name = deptStrings[i].Trim(),
  326. parentId = pDept.Id,
  327. ancestors = (pDept.parentId == null) ? pDept.parentId.ToString() : $"{pDept.ancestors},{pDept.Id}"
  328. };
  329. Context.Departments.Add(curDept);
  330. Context.SaveChanges();
  331. }
  332. pDept = curDept;
  333. }
  334. if(pDept != null)
  335. {
  336. DepartmentPosition dp = Context.DepartmentPositions.FirstOrDefault(p => p.StaffId == sf.Id);
  337. if (dp == null)
  338. {
  339. dp = new DepartmentPosition();
  340. dp.StaffId = sf.Id;
  341. dp.departmentId = pDept.Id;
  342. dp.PositionId = ptPosition.Id;
  343. Context.DepartmentPositions.Add(dp);
  344. Context.SaveChanges();
  345. }
  346. else
  347. {
  348. if (pDept.Id != dp.departmentId)
  349. {
  350. dp.departmentId = pDept.Id;
  351. dp.PositionId = ptPosition.Id;
  352. Context.SaveChanges();
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }