service.go 4.7 KB

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