oc_controller.go 3.0 KB

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