StaffController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using wispro.sp.entity;
  6. using wispro.sp.share;
  7. using System.Linq;
  8. using Microsoft.AspNetCore.Authentication.JwtBearer;
  9. using Microsoft.AspNetCore.Authorization;
  10. using wispro.sp.entity.workflowDefine;
  11. using Microsoft.EntityFrameworkCore;
  12. namespace wispro.sp.api.Controllers
  13. {
  14. [Authorize]
  15. [Route("api/[controller]/[action]")]
  16. [ApiController]
  17. public class StaffController : ControllerBase
  18. {
  19. private static List<Staff> CacheList = new List<Staff>();
  20. spDbContext Context;
  21. public StaffController(spDbContext context)
  22. {
  23. Context = context;
  24. }
  25. public List<Staff> GetAll()
  26. {
  27. return Context.Staffs.ToList<Staff>();
  28. }
  29. private List<Staff> GetStaffList(dynamic dynamic,UserField userField)
  30. {
  31. try
  32. {
  33. Staff temUser = null;
  34. switch (userField.UserConditionType)
  35. {
  36. case UserConditionType.Staff:
  37. //UserField user = new UserField();
  38. //user.UserConditionType = UserConditionType.UserDepartmentPosition;
  39. //user.Department = "120";
  40. //user.Positon = "6";
  41. //System.Diagnostics.Debug.WriteLine(System.Text.Json.JsonSerializer.Serialize(user));
  42. switch (userField.UserType)
  43. {
  44. case UserType.BindObjectProperty:
  45. if (dynamic == null)
  46. {
  47. return null;
  48. }
  49. else
  50. {
  51. var retObj = share.Utility.ObjectHelper.GetPropertyValue(userField.UserValue, dynamic);
  52. if (retObj is Staff)
  53. {
  54. return new List<Staff>() { (Staff)retObj };
  55. }
  56. else
  57. {
  58. if (retObj is List<Staff>)
  59. {
  60. return (List<Staff>)retObj;
  61. }
  62. }
  63. }
  64. break;
  65. case UserType.DoActionUser:
  66. break;
  67. case UserType.LoginUser:
  68. temUser = Context.Staffs.FirstOrDefault(s => s.Name == User.Identity.Name);
  69. return new List<Staff>() { temUser };
  70. break;
  71. case UserType.Staff:
  72. temUser = Context.Staffs.FirstOrDefault(s => s.Id == int.Parse(userField.UserValue.ToString()));
  73. if (temUser != null)
  74. {
  75. return new List<Staff>() { temUser };
  76. }
  77. break;
  78. }
  79. break;
  80. case UserConditionType.Department:
  81. var retList = Context.DepartmentPositions.Where(d => d.departmentId == int.Parse(userField.Department)).Select(p => p.Staff).ToList();
  82. return retList;
  83. break;
  84. case UserConditionType.UserDepartment:
  85. break;
  86. case UserConditionType.UserDepartmentPosition:
  87. break;
  88. case UserConditionType.DepartmentPosition:
  89. var retList1 = Context.DepartmentPositions.Where(d =>
  90. d.departmentId == int.Parse(userField.Department) &&
  91. d.PositionId == int.Parse(userField.Positon)
  92. )
  93. .Select(p => p.Staff).ToList();
  94. return retList1;
  95. break;
  96. }
  97. return new List<Staff>();
  98. }
  99. catch(Exception ex)
  100. {
  101. throw ex;
  102. }
  103. }
  104. public List<Staff> GetReviewers(int itemId, int appealTypeId)
  105. {
  106. try
  107. {
  108. var item = Context.PerformanceItems
  109. .Include(p=>p.Reviewer)
  110. .Include(p=>p.ItemStaffs).ThenInclude(p=>p.DoPerson)
  111. .Include(p=>p.PreOastaff)
  112. .Include(p=>p.Customer)
  113. .FirstOrDefault(p => p.Id == itemId);
  114. var apType = Context.AppealTypes.FirstOrDefault(p => p.Id == appealTypeId);
  115. UserField user = new UserField();
  116. if (String.IsNullOrEmpty(apType.ReviewerExpress))
  117. {
  118. return Context.Staffs.ToList();
  119. }
  120. else
  121. {
  122. UserField userField = new UserField();
  123. if (apType.ReviewerExpress.StartsWith("p."))
  124. {
  125. userField.UserConditionType = UserConditionType.Staff;
  126. userField.UserType = UserType.BindObjectProperty;
  127. userField.UserValue = apType.ReviewerExpress.Replace("p.", "").Trim();
  128. }
  129. else
  130. {
  131. userField = System.Text.Json.JsonSerializer.Deserialize<UserField>(apType.ReviewerExpress);
  132. }
  133. return GetStaffList(item, userField);
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. throw ex;
  139. }
  140. }
  141. public Staff GetUser(int Id)
  142. {
  143. return Context.Staffs.FirstOrDefault(p=>p.Id == Id);
  144. }
  145. public ListApiResponse<Staff> Query(int pageIndex,int pageSize)
  146. {
  147. ListApiResponse<Staff> ret = new ListApiResponse<Staff>();
  148. ret.TotalCount = Context.Staffs.Count<Staff>();
  149. List<Staff> retList = Context.Staffs.Skip<Staff>((pageIndex - 1) * pageSize).Take(pageSize).ToList<Staff>();
  150. ret.Results = retList;
  151. return ret;
  152. }
  153. public ListApiResponse<Staff> QueryInDepartment(int deptId,int pageIndex,int pageSize)
  154. {
  155. ListApiResponse<Staff> ret = new ListApiResponse<Staff>();
  156. var dept = Context.Departments.FirstOrDefault<Department>(d=>d.Id == deptId);
  157. string strancestors = $"{dept.ancestors},{deptId}";
  158. if (dept != null)
  159. {
  160. var retList = Context.Staffs.Where<Staff>(s =>
  161. s.Positions.Where<DepartmentPosition>(dp => dp.department.ancestors.StartsWith(strancestors) || dp.department.Id == deptId).Count() > 0 );
  162. ret.TotalCount = retList.Count();
  163. ret.Results = retList.Skip<Staff>((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  164. }
  165. else
  166. {
  167. ret.Results = new List<Staff>();
  168. }
  169. return ret;
  170. }
  171. public ApiSaveResponse SaveUser(SaveUserObject saveUser)
  172. {
  173. ApiSaveResponse ret = new ApiSaveResponse();
  174. using (var t = Context.Database.BeginTransaction())
  175. {
  176. try
  177. {
  178. Staff staff = saveUser.staff;
  179. if (staff.Id == 0)
  180. {
  181. Staff temStaff = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).FirstOrDefault();
  182. if (temStaff != null)
  183. {
  184. ret.Success = false;
  185. ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  186. }
  187. else
  188. {
  189. Context.Staffs.Add(staff);
  190. Context.SaveChanges();
  191. DepartmentPosition dp = new DepartmentPosition();
  192. dp.departmentId = saveUser.deptId;
  193. dp.PositionId = saveUser.deptId;
  194. dp.StaffId = temStaff.Id;
  195. Context.DepartmentPositions.Add(dp);
  196. }
  197. }
  198. else
  199. {
  200. Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();
  201. if (editObject != null)
  202. {
  203. editObject.Account = staff.Account;
  204. editObject.Department = staff.Department;
  205. editObject.EntyDate = staff.EntyDate;
  206. editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
  207. editObject.IsOnJob = staff.IsOnJob;
  208. editObject.Mail = staff.Mail;
  209. editObject.Memo = staff.Memo;
  210. editObject.Mobile = staff.Mobile;
  211. editObject.Name = staff.Name;
  212. editObject.Password = staff.Password;
  213. editObject.Sex = staff.Sex;
  214. editObject.StaffGradeId = staff.StaffGradeId;
  215. editObject.Status = staff.Status;
  216. editObject.Tel = staff.Tel;
  217. editObject.WorkPlace = staff.WorkPlace;
  218. Context.SaveChanges();
  219. var temDP = Context.DepartmentPositions
  220. .Where<DepartmentPosition>(d =>
  221. d.departmentId == saveUser.deptId &&
  222. d.StaffId == editObject.Id).ToList<DepartmentPosition>();
  223. foreach (var depPosition in temDP)
  224. {
  225. Context.DepartmentPositions.Remove(depPosition);
  226. }
  227. Context.SaveChanges();
  228. DepartmentPosition dp = new DepartmentPosition();
  229. dp.departmentId = saveUser.deptId;
  230. dp.PositionId = saveUser.positionId;
  231. dp.StaffId = editObject.Id;
  232. Context.DepartmentPositions.Add(dp);
  233. Context.SaveChanges();
  234. }
  235. else
  236. {
  237. ret.Success = false;
  238. ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在!";
  239. }
  240. }
  241. t.Commit();
  242. ret.Success = true;
  243. }
  244. catch (Exception ex)
  245. {
  246. t.Rollback();
  247. ret.Success = false;
  248. ret.ErrorMessage = ex.Message;
  249. }
  250. }
  251. return ret;
  252. }
  253. [HttpPost]
  254. public ApiSaveResponse Save(Staff staff)
  255. {
  256. ApiSaveResponse ret = new ApiSaveResponse();
  257. try
  258. {
  259. if (staff.Id == 0)
  260. {
  261. Staff temStaff = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).FirstOrDefault();
  262. if (temStaff != null)
  263. {
  264. temStaff.Account = staff.Account;
  265. temStaff.Department = staff.Department;
  266. temStaff.EntyDate = staff.EntyDate;
  267. temStaff.IsCalPerformsnce = staff.IsCalPerformsnce;
  268. temStaff.IsOnJob = staff.IsOnJob;
  269. temStaff.Mail = staff.Mail;
  270. temStaff.Memo = staff.Memo;
  271. temStaff.Mobile = staff.Mobile;
  272. temStaff.Name = staff.Name;
  273. temStaff.Password = staff.Password;
  274. temStaff.Sex = staff.Sex;
  275. temStaff.StaffGradeId = staff.StaffGradeId;
  276. temStaff.Status = staff.Status;
  277. temStaff.Tel = staff.Tel;
  278. temStaff.WorkPlace = staff.WorkPlace;
  279. Context.SaveChanges();
  280. ret.Success = false;
  281. ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  282. }
  283. else
  284. {
  285. Context.Staffs.Add(staff);
  286. }
  287. }
  288. else
  289. {
  290. Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();
  291. if (editObject != null)
  292. {
  293. editObject.Account = staff.Account;
  294. editObject.Department = staff.Department;
  295. editObject.EntyDate = staff.EntyDate;
  296. editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
  297. editObject.IsOnJob = staff.IsOnJob;
  298. editObject.Mail = staff.Mail;
  299. editObject.Memo = staff.Memo;
  300. editObject.Mobile = staff.Mobile;
  301. editObject.Name = staff.Name;
  302. editObject.Password = staff.Password;
  303. editObject.Sex = staff.Sex;
  304. editObject.StaffGradeId = staff.StaffGradeId;
  305. editObject.Status = staff.Status;
  306. editObject.Tel = staff.Tel;
  307. editObject.WorkPlace = staff.WorkPlace;
  308. }
  309. else
  310. {
  311. ret.Success = false;
  312. ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在!";
  313. }
  314. }
  315. Context.SaveChanges();
  316. ret.Success = true;
  317. }
  318. catch (Exception ex)
  319. {
  320. ret.Success = false;
  321. ret.ErrorMessage = ex.Message;
  322. }
  323. return ret;
  324. }
  325. }
  326. }