service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package api
  2. import (
  3. "box-cost/db/repo"
  4. "encoding/hex"
  5. "fmt"
  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. out, err := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), option.Collection, objId, m, &repo.RecordLogReq{
  72. Path: c.Request.URL.Path,
  73. UserId: apictx.User.ID,
  74. TargetId: objId,
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. if out.MatchedCount != 1 {
  80. return nil, NewError("文件不存在!")
  81. }
  82. return out, nil
  83. })
  84. }
  85. //分页查询
  86. SearchPath := fmt.Sprintf("%s/list", prefix)
  87. pageSearchFn := func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. page, size, query := UtilQueryPageSize(c)
  89. if option.SearchFilter != nil {
  90. filter := option.SearchFilter(c, apictx, query)
  91. for k, v := range filter {
  92. query[k] = v
  93. }
  94. }
  95. pageOption := &repo.PageSearchOptions{
  96. CollectName: option.Collection,
  97. Page: page,
  98. Size: size,
  99. Query: query,
  100. Project: option.SearchProject,
  101. }
  102. if option.SearchSort != nil {
  103. pageOption.Sort = option.SearchSort
  104. }
  105. pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), pageOption)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if option.SearchPostProcess != nil {
  110. return option.SearchPostProcess(pageResult, c, apictx, query)
  111. }
  112. return pageResult, nil
  113. }
  114. if option.JWT {
  115. router.GETJWT(SearchPath, pageSearchFn)
  116. } else {
  117. router.GET(SearchPath, pageSearchFn)
  118. }
  119. //删除
  120. removePath := fmt.Sprintf("%s/delete/:id", prefix)
  121. router.POSTJWT(removePath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  122. id := c.Param("id")
  123. if len(id) < 1 {
  124. return nil, NewError("参数不能为空")
  125. }
  126. if option.Remove != nil {
  127. return option.Remove(c, apictx, id)
  128. }
  129. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), option.Collection, id)
  130. })
  131. //详情
  132. if len(option.DetailProject) > 0 {
  133. detailPath := fmt.Sprintf("%s/detail/:id", prefix)
  134. router.GET(detailPath, func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  135. id := c.Param("id")
  136. if len(id) < 1 {
  137. return nil, NewError("参数不能为空")
  138. }
  139. uid, _ := primitive.ObjectIDFromHex(id)
  140. ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  141. CollectName: option.Collection,
  142. Query: repo.Map{"_id": uid},
  143. Project: option.DetailProject,
  144. })
  145. if !ok {
  146. return nil, NewError("没有查询到数据")
  147. }
  148. return ret, nil
  149. })
  150. }
  151. }