service.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package api
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "mesh/db/repo"
  6. "reflect"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. type CreateModel func(c *gin.Context, apictx *ApiSession) (interface{}, error)
  11. type EmptyModel func(c *gin.Context, apictx *ApiSession) interface{}
  12. type CreateModel2 func(c *gin.Context, apictx *ApiSession) (interface{}, error)
  13. type SearchFilterFn func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{}
  14. type Update func(c *gin.Context, apictx *ApiSession, entity interface{})
  15. type SearchPostFn func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error)
  16. type RemoveFn func(c *gin.Context, apictx *ApiSession, id string) (interface{}, error)
  17. type CRUDOption struct {
  18. Collection string
  19. NewModel CreateModel
  20. EmtyModel EmptyModel
  21. SearchFilter SearchFilterFn
  22. SearchPostProcess SearchPostFn
  23. SearchSort interface{}
  24. Remove RemoveFn
  25. JWT bool
  26. SearchProject []string
  27. DetailProject []string
  28. OnUpdate Update
  29. noUpdate bool
  30. }
  31. func CreateCRUD(router *GinRouter, prefix string, option *CRUDOption) {
  32. //创建
  33. if option.NewModel != nil {
  34. creatpath := fmt.Sprintf("%s/create", prefix)
  35. router.POSTJWT(creatpath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  36. entity, err := option.NewModel(c, apictx)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if entity == nil {
  41. return nil, NewError("数据已存在!")
  42. }
  43. return repo.RepoAddDoc(apictx.CreateRepoCtx(), option.Collection, entity)
  44. })
  45. }
  46. //更新
  47. if !option.noUpdate {
  48. updatePath := fmt.Sprintf("%s/update", prefix)
  49. router.POSTJWT(updatePath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  50. m := option.EmtyModel(c, apictx)
  51. err := c.ShouldBindJSON(m)
  52. if err != nil {
  53. fmt.Println(err)
  54. return nil, NewError("参数解析错误")
  55. }
  56. v := reflect.ValueOf(m)
  57. v = v.Elem()
  58. mid := v.FieldByName("Id")
  59. if !mid.IsValid() {
  60. return nil, NewError("ID不能为空")
  61. }
  62. slice := mid.Slice(0, mid.Len())
  63. objId := hex.EncodeToString(slice.Bytes())
  64. for i := 0; i < 12; i++ {
  65. mid.Index(i).SetUint(0)
  66. }
  67. if option.OnUpdate != nil {
  68. option.OnUpdate(c, apictx, m)
  69. }
  70. out, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), option.Collection, objId, m)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if out.MatchedCount != 1 {
  75. return nil, NewError("文件不存在!")
  76. }
  77. return out, nil
  78. })
  79. }
  80. //分页查询
  81. SearchPath := fmt.Sprintf("%s/list", prefix)
  82. pageSearchFn := func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  83. page, size, query := UtilQueryPageSize(c)
  84. if option.SearchFilter != nil {
  85. filter := option.SearchFilter(c, apictx, query)
  86. for k, v := range filter {
  87. query[k] = v
  88. }
  89. }
  90. pageOption := &repo.PageSearchOptions{
  91. CollectName: option.Collection,
  92. Page: page,
  93. Size: size,
  94. Query: query,
  95. Project: option.SearchProject,
  96. }
  97. if option.SearchSort != nil {
  98. pageOption.Sort = option.SearchSort
  99. }
  100. pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), pageOption)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if option.SearchPostProcess != nil {
  105. return option.SearchPostProcess(pageResult, c, apictx, query)
  106. }
  107. return pageResult, nil
  108. }
  109. if option.JWT {
  110. router.GETJWT(SearchPath, pageSearchFn)
  111. } else {
  112. router.GET(SearchPath, pageSearchFn)
  113. }
  114. //删除
  115. removePath := fmt.Sprintf("%s/delete/:id", prefix)
  116. router.POSTJWT(removePath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  117. id := c.Param("id")
  118. if len(id) < 1 {
  119. return nil, NewError("参数不能为空")
  120. }
  121. if option.Remove != nil {
  122. return option.Remove(c, apictx, id)
  123. }
  124. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), option.Collection, id)
  125. })
  126. //详情
  127. if len(option.DetailProject) > 0 {
  128. detailPath := fmt.Sprintf("%s/detail/:id", prefix)
  129. router.GET(detailPath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  130. id := c.Param("id")
  131. if len(id) < 1 {
  132. return nil, NewError("参数不能为空")
  133. }
  134. uid, _ := primitive.ObjectIDFromHex(id)
  135. ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  136. CollectName: option.Collection,
  137. Query: repo.Map{"_id": uid},
  138. Project: option.DetailProject,
  139. })
  140. if !ok {
  141. return nil, NewError("没有查询到数据")
  142. }
  143. return ret, nil
  144. })
  145. }
  146. }