service-database-category.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package api
  2. import (
  3. "mesh/db/repo"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "infish.cn/comm"
  8. )
  9. // 创建数据库分类api
  10. func CreateDatabaseCategoryRouter(router *GinRouter) {
  11. //创建分类定义
  12. router.POSTJWT("/user/category/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  13. scope := c.Param("scope")
  14. if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
  15. return nil, NewError("Id参数非法!")
  16. }
  17. body := &comm.DbCategory{}
  18. c.ShouldBindJSON(body)
  19. body.UserId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
  20. body.Scope = scope
  21. body.CreateTime = time.Now()
  22. if body.Id.IsZero() {
  23. body.CreateTime = time.Now()
  24. } else {
  25. body.UpdateTime = time.Now()
  26. }
  27. curr := &comm.DbCategory{}
  28. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  29. CollectName: repo.CollectionCategories,
  30. Project: []string{"_id"},
  31. Query: repo.Map{"userId": body.UserId, "scope": body.Scope},
  32. }, curr)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if !ok { //没有查询到新增
  37. id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, body)
  38. if err != nil {
  39. return nil, err
  40. }
  41. body.Id, _ = primitive.ObjectIDFromHex(id)
  42. return body, nil
  43. }
  44. body.Id = primitive.NilObjectID
  45. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, curr.Id.Hex(), body)
  46. })
  47. // //更新
  48. // router.POSTJWT("/dbcategory/update/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  49. // id := c.Param("id")
  50. // if len(id) < 1 {
  51. // return nil, NewError("数据库Id不能为空")
  52. // }
  53. // body := &model.DbCategory{}
  54. // c.ShouldBindJSON(body)
  55. // if len(body.Id) < 1 {
  56. // return nil, NewError("id不能为空")
  57. // }
  58. // optSet := repo.Map{}
  59. // if len(body.Name) > 0 {
  60. // optSet["categories.$.name"] = body.Name
  61. // }
  62. // if len(body.Value) > 0 {
  63. // optSet["categories.$.value"] = body.Value
  64. // }
  65. // option := &repo.ArrayOneUpdateOption{
  66. // CollectName: repo.CollectionDatabase,
  67. // Id: id,
  68. // Query: repo.Map{"categories.id": body.Id},
  69. // Set: optSet,
  70. // }
  71. // return repo.RepoDocArrayOneUpdate(
  72. // apictx.CreateRepoCtx(),
  73. // option,
  74. // )
  75. // })
  76. //获取用户的所有分类定义和 资产分类定义
  77. router.GETJWT("/user/category/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  78. scope := c.Param("scope")
  79. if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
  80. return nil, NewError("Id参数非法!")
  81. }
  82. uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  83. ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  84. CollectName: repo.CollectionCategories,
  85. Query: repo.Map{"userId": uidObj, "scope": scope},
  86. })
  87. if !ok {
  88. return comm.DbCategory{}, nil
  89. }
  90. return ret, nil
  91. })
  92. //创建资产分类
  93. router.POSTJWT("/user/assetcategory/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  94. scope := c.Param("scope")
  95. if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
  96. return nil, NewError("Id参数非法!")
  97. }
  98. body := &comm.DbAssetUserCategory{}
  99. c.ShouldBindJSON(body)
  100. body.UserId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
  101. body.Scope = scope
  102. body.CreateTime = time.Now()
  103. curr := &comm.DbAssetUserCategory{}
  104. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  105. CollectName: repo.CollectionDbAssetCategory,
  106. Project: []string{"_id"},
  107. Query: repo.Map{"userId": body.UserId, "scope": body.Scope},
  108. }, curr)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if !ok { //没有查询到新增
  113. body.CreateTime = time.Now()
  114. id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDbAssetCategory, body)
  115. if err != nil {
  116. return nil, err
  117. }
  118. body.Id, _ = primitive.ObjectIDFromHex(id)
  119. return body, nil
  120. }
  121. body.Id = primitive.NilObjectID
  122. body.UpdateTime = time.Now()
  123. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionDbAssetCategory, curr.Id.Hex(), body)
  124. })
  125. router.GETJWT("/user/assetcategory/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  126. scope := c.Param("scope")
  127. if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
  128. return nil, NewError("Id参数非法!")
  129. }
  130. uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  131. ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  132. CollectName: repo.CollectionDbAssetCategory,
  133. Query: repo.Map{"userId": uidObj, "scope": scope},
  134. })
  135. if !ok {
  136. return comm.DbAssetUserCategory{}, nil
  137. }
  138. return ret, nil
  139. })
  140. router.GETJWT("/user/categoryconf/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  141. scope := c.Param("scope")
  142. if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
  143. return nil, NewError("Id参数非法!")
  144. }
  145. uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  146. _, ret1 := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  147. CollectName: repo.CollectionCategories,
  148. Query: repo.Map{"userId": uidObj, "scope": scope},
  149. })
  150. _, ret2 := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  151. CollectName: repo.CollectionDbAssetCategory,
  152. Query: repo.Map{"userId": uidObj, "scope": scope},
  153. })
  154. return map[string]interface{}{
  155. "categories": ret1,
  156. "assetCategories": ret2,
  157. }, nil
  158. })
  159. router.GET("/public/category/:cateId/:assetCateConfId", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  160. cateId := c.Param("cateId")
  161. assetCateConfId := c.Param("assetCateConfId")
  162. if !primitive.IsValidObjectID(cateId) || !primitive.IsValidObjectID(assetCateConfId) {
  163. return nil, NewError("Id参数非法!")
  164. }
  165. cateIdObj, _ := primitive.ObjectIDFromHex(cateId)
  166. ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  167. CollectName: repo.CollectionCategories,
  168. Query: repo.Map{"_id": cateIdObj},
  169. })
  170. if !ok {
  171. return nil, NewError("no find!")
  172. }
  173. assetCateIdObj, _ := primitive.ObjectIDFromHex(assetCateConfId)
  174. _, assetConf := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  175. CollectName: repo.CollectionDbAssetCategory,
  176. Query: repo.Map{"_id": assetCateIdObj},
  177. })
  178. return map[string]interface{}{
  179. "categories": ret,
  180. "assetCategories": assetConf,
  181. }, nil
  182. })
  183. }