group_controller.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 GroupController struct {
  8. Base
  9. Service service.GroupService
  10. }
  11. func (c *GroupController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/", "GetList" )
  13. b.Handle("GET", "/{groupId:int64}", "GetById" )
  14. b.Handle("GET", "/view", "GetTreeView" )
  15. b.Handle("POST", "/add", "Create" )
  16. b.Handle("PUT", "/update", "Update" )
  17. b.Handle("DELETE", "/{groupId:int64}", "DeleteById" )
  18. }
  19. func (c *GroupController) GetList() *JsonResult {
  20. ocId := c.GetCurOcId()
  21. params := c.buildParams()
  22. params["ocId"] = ocId
  23. data, err := c.Service.GetList(params)
  24. if err != nil {
  25. return ResultErr(err.Error(), nil)
  26. } else {
  27. return ResultOk("获取成功", data)
  28. }
  29. }
  30. func (c *GroupController) GetTreeView() *JsonResult {
  31. ocId := c.GetCurOcId()
  32. data, err := c.Service.GetTreeView(ocId)
  33. if err != nil {
  34. return ResultErr(err.Error(), nil)
  35. } else {
  36. return ResultOk("获取成功", data)
  37. }
  38. }
  39. func (c *GroupController) GetById(groupId int64) *JsonResult {
  40. ocId := c.GetCurOcId()
  41. data, err := c.Service.GetById(ocId, groupId)
  42. if err != nil {
  43. return ResultErr(err.Error(), nil)
  44. } else {
  45. return ResultOk("获取成功", data)
  46. }
  47. }
  48. func (c *GroupController) Create() *JsonResult {
  49. ocId := c.GetCurOcId()
  50. userId := c.GetCurUserId()
  51. group := datamodel.Group{}
  52. if err := c.Ctx.ReadJSON(&group); err != nil {
  53. return ResultErr("读取数据失败", nil)
  54. }
  55. group.OcId = ocId
  56. group.CreatedBy = userId
  57. if err := c.Service.Create(&group); err != nil {
  58. return ResultErr(err.Error(), nil)
  59. } else {
  60. return ResultOk("新增成功", nil)
  61. }
  62. }
  63. func (c *GroupController) Update() *JsonResult {
  64. ocId := c.GetCurOcId()
  65. userId := c.GetCurUserId()
  66. group := datamodel.Group{}
  67. if err := c.Ctx.ReadJSON(&group); err != nil {
  68. return ResultErr("读取数据失败", nil)
  69. }
  70. group.OcId = ocId
  71. group.UpdatedBy = userId
  72. if err := c.Service.Update(&group); err != nil {
  73. return ResultErr(err.Error(), nil)
  74. } else {
  75. return ResultOk("更新成功", nil)
  76. }
  77. }
  78. func (c *GroupController) DeleteById(groupId int64) *JsonResult {
  79. ocId := c.GetCurOcId()
  80. userId := c.GetCurUserId()
  81. m := make(map[string]interface{})
  82. m["ocId"] = ocId
  83. m["groupId"] = groupId
  84. m["deletedBy"] = userId
  85. if err := c.Service.Delete(m); err != nil {
  86. return ResultErr(err.Error(), nil)
  87. } else {
  88. return ResultOk("删除成功", nil)
  89. }
  90. }