service-array.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package api
  2. import (
  3. "fmt"
  4. "pay/db/repo"
  5. "github.com/gin-gonic/gin"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. type DetailHandler func(c *gin.Context, apictx *ApiSession, entity interface{}) (interface{}, error)
  10. type ArrayCRUDOption struct {
  11. Collection string
  12. ArrayFieldPath string
  13. NewModel CreateModel
  14. EmtyModel CreateModel
  15. DocModel CreateModel
  16. SearchProject []string
  17. Query repo.Map
  18. OnUpdate Update
  19. OnDetail DetailHandler
  20. NeedsUpdate bool
  21. NeedsRemove bool
  22. }
  23. func CreateArrayCRUD(router *GinRouter, prefix string, option *ArrayCRUDOption) {
  24. //创建
  25. creatpath := fmt.Sprintf("%s/create/:id", prefix)
  26. if option.NewModel != nil {
  27. router.POSTJWT(creatpath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. id := c.Param("id")
  29. if len(id) < 1 {
  30. return nil, NewError("参数id不能为空")
  31. }
  32. var entity interface{} = nil
  33. entity = option.NewModel(c)
  34. if entity == nil {
  35. return nil, NewError("数据已存在!")
  36. }
  37. ret, _ := repo.RepoDocArrayAppend(apictx.CreateRepoCtx(), option.Collection, id, option.ArrayFieldPath, entity)
  38. if ret.ModifiedCount == 1 {
  39. return entity, nil
  40. }
  41. return nil, NewError("创建失败!")
  42. })
  43. }
  44. //更新
  45. if option.NeedsUpdate {
  46. updatePath := fmt.Sprintf("%s/update/:id", prefix)
  47. router.POSTJWT(updatePath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  48. docId := c.Param("id")
  49. if len(docId) < 1 {
  50. return nil, NewError("id不能为空!")
  51. }
  52. m := option.EmtyModel(c)
  53. err := c.ShouldBindJSON(m)
  54. if err != nil {
  55. fmt.Println(err)
  56. return nil, NewError("参数解析错误")
  57. }
  58. if option.OnUpdate != nil {
  59. option.OnUpdate(c, apictx, m, docId)
  60. }
  61. //Query: repo.Map{"scenes.id": scene.Id},
  62. //Set: repo.Map{"scenes.$": scene},
  63. updateQuery := repo.Map{}
  64. for k, v := range option.Query {
  65. p := fmt.Sprintf("%s.%s", option.ArrayFieldPath, k)
  66. updateQuery[p] = v
  67. }
  68. updateSet := repo.Map{}
  69. spath := fmt.Sprintf("%s.$", option.ArrayFieldPath)
  70. updateSet[spath] = m
  71. updateOption := &repo.ArrayOneUpdateOption{CollectName: option.Collection, Id: docId, Query: updateQuery, Set: updateSet}
  72. return repo.RepoDocArrayOneUpdate(apictx.CreateRepoCtx(), updateOption)
  73. })
  74. }
  75. //数组所有数据
  76. SearchPath := fmt.Sprintf("%s/list/:id", prefix)
  77. pageSearchFn := func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  78. docId := c.Param("id")
  79. if len(docId) < 1 {
  80. return nil, NewError("id不能为空!")
  81. }
  82. query := UtilQueryMap(c)
  83. query["_id"], _ = primitive.ObjectIDFromHex(docId)
  84. ok, docItem := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  85. CollectName: option.Collection,
  86. Query: query,
  87. Project: option.SearchProject,
  88. })
  89. if !ok {
  90. return nil, NewError("获取列表失败!")
  91. }
  92. return docItem, nil
  93. }
  94. router.GETJWT(SearchPath, pageSearchFn)
  95. //删除
  96. if option.NeedsRemove {
  97. removePath := fmt.Sprintf("%s/delete/:id", prefix)
  98. router.POSTJWT(removePath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  99. id := c.Param("id")
  100. if len(id) < 1 {
  101. return nil, NewError("参数不能为空")
  102. }
  103. body := map[string]interface{}{}
  104. c.ShouldBindJSON(&body)
  105. ArrayQuery := repo.Map{}
  106. if len(body) < 1 {
  107. return nil, NewError("body参数不能为空")
  108. }
  109. query := bson.M{}
  110. for k, v := range body {
  111. query[k] = v
  112. }
  113. ArrayQuery[option.ArrayFieldPath] = query
  114. options := &repo.ArrayOneRemoveOption{
  115. CollectName: option.Collection,
  116. Id: id,
  117. ArrayQuery: ArrayQuery,
  118. }
  119. ret, err := repo.RepoDocArrayOneRemove(apictx.CreateRepoCtx(), options)
  120. if ret.ModifiedCount < 1 {
  121. return nil, NewError("删除失败!")
  122. }
  123. return ret, err
  124. })
  125. }
  126. //详情
  127. if option.OnDetail != nil && option.DocModel != nil {
  128. DetailPath := fmt.Sprintf("%s/detail/:id", prefix)
  129. router.GETJWT(DetailPath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  130. docId := c.Param("id")
  131. if len(docId) < 1 {
  132. return nil, NewError("id不能为空!")
  133. }
  134. query := UtilQueryMap(c)
  135. filter := repo.Map{}
  136. if len(query) > 0 {
  137. filter[option.ArrayFieldPath] = query
  138. }
  139. m := option.DocModel(c)
  140. ok, ret := repo.RepoDocArraySearch(apictx.CreateRepoCtx(), &repo.ArrayOneSearchOption{
  141. CollectName: option.Collection,
  142. ArrayQuery: filter,
  143. Id: docId,
  144. Field: option.ArrayFieldPath,
  145. })
  146. if !ok {
  147. return nil, NewError("数据不存在!")
  148. }
  149. detail, err := option.OnDetail(c, apictx, ret)
  150. if err != nil {
  151. return nil, err
  152. }
  153. if detail != nil {
  154. return detail, nil
  155. }
  156. return m, nil
  157. })
  158. }
  159. }