StaffController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //for(int i = (pageIndex-1) * pageSize; i < pageIndex * pageSize; i++)
  33. //{
  34. // if (i < CacheList.Count)
  35. // {
  36. // retList.Add(CacheList[i]);
  37. // }
  38. //}
  39. ret.Results = retList;
  40. return ret;
  41. }
  42. [HttpPost]
  43. public ApiSaveResponse Save(Staff staff)
  44. {
  45. ApiSaveResponse ret = new ApiSaveResponse();
  46. try
  47. {
  48. if (staff.Id == 0)
  49. {
  50. Staff temStaff = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).FirstOrDefault();
  51. if (temStaff != null)
  52. {
  53. temStaff.Account = staff.Account;
  54. temStaff.Department = staff.Department;
  55. temStaff.EntyDate = staff.EntyDate;
  56. temStaff.IsCalPerformsnce = staff.IsCalPerformsnce;
  57. temStaff.IsOnJob = staff.IsOnJob;
  58. temStaff.Mail = staff.Mail;
  59. temStaff.Memo = staff.Memo;
  60. temStaff.Mobile = staff.Mobile;
  61. temStaff.Name = staff.Name;
  62. temStaff.Password = staff.Password;
  63. temStaff.Sex = staff.Sex;
  64. temStaff.StaffGradeId = staff.StaffGradeId;
  65. temStaff.Status = staff.Status;
  66. temStaff.Tel = staff.Tel;
  67. temStaff.WorkPlace = staff.WorkPlace;
  68. Context.SaveChanges();
  69. ret.Success = false;
  70. ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  71. }
  72. else
  73. {
  74. Context.Staffs.Add(staff);
  75. }
  76. }
  77. else
  78. {
  79. Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();
  80. if (editObject != null)
  81. {
  82. editObject.Account = staff.Account;
  83. editObject.Department = staff.Department;
  84. editObject.EntyDate = staff.EntyDate;
  85. editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
  86. editObject.IsOnJob = staff.IsOnJob;
  87. editObject.Mail = staff.Mail;
  88. editObject.Memo = staff.Memo;
  89. editObject.Mobile = staff.Mobile;
  90. editObject.Name = staff.Name;
  91. editObject.Password = staff.Password;
  92. editObject.Sex = staff.Sex;
  93. editObject.StaffGradeId = staff.StaffGradeId;
  94. editObject.Status = staff.Status;
  95. editObject.Tel = staff.Tel;
  96. editObject.WorkPlace = staff.WorkPlace;
  97. }
  98. else
  99. {
  100. ret.Success = false;
  101. ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在!";
  102. }
  103. }
  104. Context.SaveChanges();
  105. ret.Success = true;
  106. }
  107. catch (Exception ex)
  108. {
  109. ret.Success = false;
  110. ret.ErrorMessage = ex.Message;
  111. }
  112. return ret;
  113. }
  114. }
  115. }