StaffController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. namespace wispro.sp.api.Controllers
  11. {
  12. //[Authorize]
  13. [Route("api/[controller]/[action]")]
  14. [ApiController]
  15. public class StaffController : ControllerBase
  16. {
  17. private static List<Staff> CacheList = new List<Staff>();
  18. spDbContext Context;
  19. public StaffController(spDbContext context)
  20. {
  21. Context = context;
  22. }
  23. public List<Staff> GetAll()
  24. {
  25. return Context.Staffs.ToList<Staff>();
  26. }
  27. public ListApiResponse<Staff> Query(int pageIndex,int pageSize)
  28. {
  29. ListApiResponse<Staff> ret = new ListApiResponse<Staff>();
  30. ret.TotalCount = Context.Staffs.Count<Staff>();
  31. List<Staff> retList = Context.Staffs.Skip<Staff>((pageIndex - 1) * pageSize).Take(pageSize).ToList<Staff>();
  32. ret.Results = retList;
  33. return ret;
  34. }
  35. [HttpPost]
  36. public ApiSaveResponse Save(Staff staff)
  37. {
  38. ApiSaveResponse ret = new ApiSaveResponse();
  39. try
  40. {
  41. if (staff.Id == 0)
  42. {
  43. Staff temStaff = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).FirstOrDefault();
  44. if (temStaff != null)
  45. {
  46. temStaff.Account = staff.Account;
  47. temStaff.Department = staff.Department;
  48. temStaff.EntyDate = staff.EntyDate;
  49. temStaff.IsCalPerformsnce = staff.IsCalPerformsnce;
  50. temStaff.IsOnJob = staff.IsOnJob;
  51. temStaff.Mail = staff.Mail;
  52. temStaff.Memo = staff.Memo;
  53. temStaff.Mobile = staff.Mobile;
  54. temStaff.Name = staff.Name;
  55. temStaff.Password = staff.Password;
  56. temStaff.Sex = staff.Sex;
  57. temStaff.StaffGradeId = staff.StaffGradeId;
  58. temStaff.Status = staff.Status;
  59. temStaff.Tel = staff.Tel;
  60. temStaff.WorkPlace = staff.WorkPlace;
  61. Context.SaveChanges();
  62. ret.Success = false;
  63. ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  64. }
  65. else
  66. {
  67. Context.Staffs.Add(staff);
  68. }
  69. }
  70. else
  71. {
  72. Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();
  73. if (editObject != null)
  74. {
  75. editObject.Account = staff.Account;
  76. editObject.Department = staff.Department;
  77. editObject.EntyDate = staff.EntyDate;
  78. editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
  79. editObject.IsOnJob = staff.IsOnJob;
  80. editObject.Mail = staff.Mail;
  81. editObject.Memo = staff.Memo;
  82. editObject.Mobile = staff.Mobile;
  83. editObject.Name = staff.Name;
  84. editObject.Password = staff.Password;
  85. editObject.Sex = staff.Sex;
  86. editObject.StaffGradeId = staff.StaffGradeId;
  87. editObject.Status = staff.Status;
  88. editObject.Tel = staff.Tel;
  89. editObject.WorkPlace = staff.WorkPlace;
  90. }
  91. else
  92. {
  93. ret.Success = false;
  94. ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在!";
  95. }
  96. }
  97. Context.SaveChanges();
  98. ret.Success = true;
  99. }
  100. catch (Exception ex)
  101. {
  102. ret.Success = false;
  103. ret.ErrorMessage = ex.Message;
  104. }
  105. return ret;
  106. }
  107. }
  108. }