StaffGradeController.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using wispro.sp.entity;
  6. using wispro.sp.share;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Authentication.JwtBearer;
  9. namespace wispro.sp.api.Controllers
  10. {
  11. [Authorize]
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. public class StaffGradeController : ControllerBase
  15. {
  16. spDbContext Context;
  17. public StaffGradeController(spDbContext context)
  18. {
  19. Context = context;
  20. }
  21. public List<StaffGrade> GetAll()
  22. {
  23. var result= Context.StaffGrades.Where<StaffGrade>(x=>x.Id >0);
  24. return result.ToList<StaffGrade>();
  25. }
  26. [HttpPost]
  27. public ApiSaveResponse Save(StaffGrade staffGrade)
  28. {
  29. ApiSaveResponse ret = new ApiSaveResponse();
  30. try
  31. {
  32. if (staffGrade.Id == 0)
  33. {
  34. List<StaffGrade> grades = Context.StaffGrades.Where<StaffGrade>(x => x.Grade == staffGrade.Grade).ToList();
  35. if (grades.Count > 0)
  36. {
  37. ret.Success = false;
  38. ret.ErrorMessage = "该等级系数已设定!";
  39. }
  40. else
  41. {
  42. Context.StaffGrades.Add(staffGrade);
  43. }
  44. }
  45. else
  46. {
  47. List<StaffGrade> grades = Context.StaffGrades.Where<StaffGrade>(x => x.Id == staffGrade.Id).ToList();
  48. grades[0].Grade = staffGrade.Grade;
  49. grades[0].Coefficient = staffGrade.Coefficient;
  50. }
  51. Context.SaveChanges();
  52. ret.Success = true;
  53. }
  54. catch(Exception ex)
  55. {
  56. ret.Success = false;
  57. ret.ErrorMessage = ex.Message;
  58. }
  59. return ret;
  60. }
  61. }
  62. }