position_controller.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/datamodels"
  5. "xps/service"
  6. )
  7. type PositionController struct {
  8. Base
  9. Service service.PositionService
  10. }
  11. func (c *PositionController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/", "GetList" )
  13. b.Handle("GET", "/page", "GetPage" )
  14. b.Handle("GET", "/{positionId:int64}", "GetById" )
  15. b.Handle("POST", "/add", "Create" )
  16. b.Handle("PUT", "/update", "Update" )
  17. b.Handle("DELETE", "/{positionId:int64}", "DeleteByID" )
  18. }
  19. func (c *PositionController) GetList() *JsonResult {
  20. params := c.buildParams()
  21. data, err := c.Service.GetList(params)
  22. if err != nil {
  23. return ResultErr(err.Error(), nil)
  24. }
  25. return ResultOk("获取成功", data)
  26. }
  27. func (c *PositionController) GetPage() *JsonResult {
  28. params := c.buildParams()
  29. data, err := c.Service.GetPage(params)
  30. if err != nil {
  31. return ResultErr(err.Error(), nil)
  32. } else {
  33. return ResultOk("获取成功", data)
  34. }
  35. }
  36. func (c *PositionController) GetById(positionId int64) *JsonResult {
  37. ocId := c.GetCurOcId()
  38. data, err := c.Service.GetById(ocId, positionId)
  39. if err != nil {
  40. return ResultErr(err.Error(), nil)
  41. } else {
  42. return ResultOk("获取成功", data)
  43. }
  44. }
  45. func (c *PositionController) Create() *JsonResult {
  46. ocId := c.GetCurOcId()
  47. userId := c.GetCurUserId()
  48. position := datamodels.Position{}
  49. position.OcId = ocId
  50. position.CreatedBy = userId
  51. if err := c.Ctx.ReadJSON(&position); err != nil {
  52. return ResultErr("读取数据失败", nil)
  53. }
  54. position.OcId = ocId
  55. if err := c.Service.Create(&position); err != nil {
  56. return ResultErr(err.Error(), nil)
  57. } else {
  58. return ResultOk("新增成功", nil)
  59. }
  60. }
  61. func (c *PositionController) Update() *JsonResult {
  62. ocId := c.GetCurOcId()
  63. userId := c.GetCurUserId()
  64. position := datamodels.Position{}
  65. if err := c.Ctx.ReadJSON(&position); err != nil {
  66. return ResultErr("读取数据失败", nil)
  67. }
  68. position.OcId = ocId
  69. position.UpdatedBy = userId
  70. if err := c.Service.Update(&position); err != nil {
  71. return ResultErr(err.Error(), nil)
  72. } else {
  73. return ResultOk("更新成功", nil)
  74. }
  75. }
  76. func (c *PositionController) DeleteByID(positionId int64) *JsonResult {
  77. ocId := c.GetCurOcId()
  78. userId := c.GetCurUserId()
  79. m := make(map[string]interface{})
  80. m["ocId"] = ocId
  81. m["positionId"]= positionId
  82. m["deletedBy"] = userId
  83. if err := c.Service.Delete(m); err != nil {
  84. return ResultErr(err.Error(), nil)
  85. } else {
  86. return ResultOk("删除成功", nil)
  87. }
  88. }