position_controller.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package controller
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/pkg/system/datamodel"
  5. "xps/pkg/system/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 := datamodel.Position{}
  49. if err := c.Ctx.ReadJSON(&position); err != nil {
  50. return ResultErr("读取数据失败", nil)
  51. }
  52. position.OcId = ocId
  53. position.CreatedBy = userId
  54. if err := c.Service.Create(&position); err != nil {
  55. return ResultErr(err.Error(), nil)
  56. } else {
  57. return ResultOk("新增成功", nil)
  58. }
  59. }
  60. func (c *PositionController) Update() *JsonResult {
  61. ocId := c.GetCurOcId()
  62. userId := c.GetCurUserId()
  63. position := datamodel.Position{}
  64. if err := c.Ctx.ReadJSON(&position); err != nil {
  65. return ResultErr("读取数据失败", nil)
  66. }
  67. position.OcId = ocId
  68. position.UpdatedBy = userId
  69. if err := c.Service.Update(&position); err != nil {
  70. return ResultErr(err.Error(), nil)
  71. } else {
  72. return ResultOk("更新成功", nil)
  73. }
  74. }
  75. func (c *PositionController) DeleteByID(positionId int64) *JsonResult {
  76. ocId := c.GetCurOcId()
  77. userId := c.GetCurUserId()
  78. m := make(map[string]interface{})
  79. m["ocId"] = ocId
  80. m["positionId"] = positionId
  81. m["deletedBy"] = userId
  82. if err := c.Service.Delete(m); err != nil {
  83. return ResultErr(err.Error(), nil)
  84. } else {
  85. return ResultOk("删除成功", nil)
  86. }
  87. }