position_controller.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/datamodels"
  5. "xps/service/system"
  6. )
  7. type PositionController struct {
  8. Base
  9. Service system.PositionService
  10. }
  11. func (c *PositionController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/", "GetByList")
  13. b.Handle("GET", "/page", "GetByPage")
  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) GetByList() *JsonResult {
  20. params := c.buildParams()
  21. data, err := c.Service.GetByList(params)
  22. if err != nil {
  23. return ResultErr(err.Error(), nil)
  24. }
  25. return ResultOk("获取成功", data)
  26. }
  27. func (c *PositionController) GetByPage() *JsonResult {
  28. params := c.buildParams()
  29. data, err := c.Service.GetByPage(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. err := c.Ctx.ReadJSON(&position)
  52. if err != nil {
  53. return ResultErr("读取数据失败", nil)
  54. }
  55. position.OcId = ocId
  56. err = c.Service.Create(&position)
  57. if err != nil {
  58. return ResultErr(err.Error(), nil)
  59. } else {
  60. return ResultOk("新增成功", nil)
  61. }
  62. }
  63. func (c *PositionController) Update() *JsonResult {
  64. ocId := c.GetCurOcId()
  65. userId := c.GetCurUserId()
  66. position := datamodels.Position{}
  67. if err := c.Ctx.ReadJSON(&position); err != nil {
  68. return ResultErr("读取数据失败", nil)
  69. }
  70. position.OcId = ocId
  71. position.UpdatedBy = userId
  72. err := c.Service.Update(&position)
  73. if err != nil {
  74. return ResultErr(err.Error(), nil)
  75. } else {
  76. return ResultOk("更新成功", nil)
  77. }
  78. }
  79. func (c *PositionController) DeleteByID(positionId int64) *JsonResult {
  80. ocId := c.GetCurOcId()
  81. userId := c.GetCurUserId()
  82. var m = make(map[string]interface{})
  83. m["ocId"] = ocId
  84. m["positionId"]= positionId
  85. m["deletedBy"] = userId
  86. err := c.Service.Delete(m)
  87. if err != nil {
  88. return ResultErr(err.Error(), nil)
  89. } else {
  90. return ResultOk("删除成功", nil)
  91. }
  92. }