StaffController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. //DataTable dt = wispro.sp.utility.NPOIExcel.ExcelToDataTable(@"D:\users\luowen\Documents\工作文档\深圳威世博人力绩效\210730-威世博人员清单v1r01-xm.xls", true);
  23. //CacheList = new List<Staff>();
  24. //if (dt != null)
  25. //{
  26. // foreach (DataRow row in dt.Rows)
  27. // {
  28. // CacheList.Add(new Staff()
  29. // {
  30. // Name = row["姓名"].ToString(),
  31. // Status = row["岗位状态"].ToString(),
  32. // IsCalPerformsnce = (row["是否核算绩效"].ToString() == "是"),
  33. // StaffGrade = new StaffGrade() { Grade = row["工程师等级"].ToString(), Coefficient = double.Parse(row["等级系数"].ToString()) },
  34. // Department = row["部门"].ToString(),
  35. // WorkPlace = row["工作地"].ToString(),
  36. // EntyDate = DateTime.Parse(row["入职时间"].ToString()),
  37. // Memo = row["备注"].ToString()
  38. // });
  39. // }
  40. //}
  41. }
  42. public List<Staff> GetAll()
  43. {
  44. return Context.Staffs.ToList<Staff>();
  45. }
  46. public ListApiResponse<Staff> Query(int pageIndex,int pageSize)
  47. {
  48. ListApiResponse<Staff> ret = new ListApiResponse<Staff>();
  49. ret.TotalCount = Context.Staffs.Count<Staff>();
  50. List<Staff> retList = Context.Staffs.Skip<Staff>((pageIndex - 1) * pageSize).Take(pageSize).ToList<Staff>();
  51. //for(int i = (pageIndex-1) * pageSize; i < pageIndex * pageSize; i++)
  52. //{
  53. // if (i < CacheList.Count)
  54. // {
  55. // retList.Add(CacheList[i]);
  56. // }
  57. //}
  58. ret.Results = retList;
  59. return ret;
  60. }
  61. [HttpPost]
  62. public ApiSaveResponse Save(Staff staff)
  63. {
  64. ApiSaveResponse ret = new ApiSaveResponse();
  65. try
  66. {
  67. if (staff.Id == 0)
  68. {
  69. List<Staff> grades = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).ToList();
  70. if (grades.Count > 0)
  71. {
  72. ret.Success = false;
  73. ret.ErrorMessage = $"用户【{staff.Name}】已存在!";
  74. }
  75. else
  76. {
  77. Context.Staffs.Add(staff);
  78. }
  79. }
  80. else
  81. {
  82. Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();
  83. if (editObject != null)
  84. {
  85. editObject.Name = staff.Name;
  86. editObject.StaffGradeId = staff.StaffGradeId;
  87. editObject.Account = staff.Account;
  88. editObject.Department = staff.Department;
  89. editObject.EntyDate = staff.EntyDate;
  90. editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
  91. editObject.Memo = staff.Memo;
  92. editObject.Password = editObject.Password;
  93. editObject.Status = staff.Status;
  94. editObject.WorkPlace = staff.WorkPlace;
  95. }
  96. else
  97. {
  98. ret.Success = false;
  99. ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在!";
  100. }
  101. }
  102. Context.SaveChanges();
  103. ret.Success = true;
  104. }
  105. catch (Exception ex)
  106. {
  107. ret.Success = false;
  108. ret.ErrorMessage = ex.Message;
  109. }
  110. return ret;
  111. }
  112. }
  113. }