tree-define-add-matgroup.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package bus
  2. import (
  3. "assetcenter/conf"
  4. "assetcenter/db"
  5. "assetcenter/db/model"
  6. "assetcenter/db/repo"
  7. "context"
  8. "fmt"
  9. "math/rand"
  10. "time"
  11. "github.com/nats-io/nats.go"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. "infish.cn/comm"
  15. )
  16. func init() {
  17. // 保证每次生成的随机数不一样
  18. rand.Seed(time.Now().UnixNano())
  19. }
  20. func addTreeDefineMatgroup() *comm.NatsMsgReplyer {
  21. return &comm.NatsMsgReplyer{
  22. Subject: "tree.define.add.matgroup",
  23. Entity: func() interface{} { return &TreeAddDefReq{} },
  24. Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) {
  25. req, ok := entity.(*TreeAddDefReq)
  26. if !ok || len(req.Name) < 1 {
  27. return nil, fmt.Errorf("参数错误")
  28. }
  29. var createRandomColl = func() string {
  30. return fmt.Sprintf("matgrp-%s-%s", time.Now().Format("20060102"), RandName(6))
  31. }
  32. collName := createRandomColl()
  33. if len(req.DbName) < 1 {
  34. return nil, fmt.Errorf("数据库名字不能为空")
  35. }
  36. body := &comm.DbAsset{
  37. Collection: collName,
  38. Label: req.Name,
  39. Type: model.AssetTypeMaterialGroup,
  40. CategoryIds: []string{},
  41. CreateTime: time.Now(),
  42. UserId: req.UserId,
  43. }
  44. body.Id = primitive.NewObjectID().Hex()
  45. database := &comm.Database{}
  46. var CreateRepoCtx = func() *repo.RepoSession {
  47. return &repo.RepoSession{
  48. Ctx: context.Background(),
  49. Client: db.MongoClient,
  50. }
  51. }
  52. ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": req.DbName}, Project: []string{"_id"}}, database)
  53. if err != nil {
  54. return nil, fmt.Errorf("数据库不存在")
  55. }
  56. if !ok {
  57. return nil, err
  58. }
  59. ok, _ = repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"assets.collection": body.Collection, "_id": database.Id}, Project: []string{"_id"}}, database)
  60. if ok {
  61. return nil, fmt.Errorf("材料库编号重复")
  62. }
  63. ret, err := repo.RepoDocArrayAppend(
  64. CreateRepoCtx(),
  65. repo.CollectionDatabase,
  66. database.Id.Hex(),
  67. "assets",
  68. body,
  69. )
  70. if err != nil {
  71. return nil, err
  72. }
  73. if ret.MatchedCount < 1 {
  74. return nil, fmt.Errorf("添加失败")
  75. }
  76. return &TreeAddDefineResp{
  77. DbId: database.Id.Hex(),
  78. DefineId: body.Id,
  79. Collection: body.Collection,
  80. Host: conf.AppConfig.TreeDNS,
  81. }, nil
  82. },
  83. }
  84. }
  85. func removeTreeDefineMatgroup() *comm.NatsMsgReplyer {
  86. return &comm.NatsMsgReplyer{
  87. Subject: "tree.define.remove.matgroup",
  88. Entity: func() interface{} { return &TreeRemoveDefReq{} },
  89. Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) {
  90. req, ok := entity.(*TreeRemoveDefReq)
  91. if !ok || len(req.DbId) < 1 || len(req.DefineId) < 1 {
  92. return nil, fmt.Errorf("参数错误")
  93. }
  94. var CreateRepoCtx = func() *repo.RepoSession {
  95. return &repo.RepoSession{
  96. Ctx: context.Background(),
  97. Client: db.MongoClient,
  98. }
  99. }
  100. filter := repo.Map{"assets.id": req.DefineId}
  101. enity := &model.DatabaseAssetV0{}
  102. err := repo.RepoDocArraySearch(CreateRepoCtx(), &repo.ArrayOneSearchOption{
  103. CollectName: repo.CollectionDatabase,
  104. ArrayQuery: filter,
  105. Id: req.DbId,
  106. Field: "assets",
  107. }, enity)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if enity.Assets == nil {
  112. return true, nil
  113. }
  114. //删除数据表
  115. coll := enity.Assets.Collection
  116. fmt.Println("todo fixed me need delete collection ", coll)
  117. ret, err := repo.RepoDocArrayOneRemove(
  118. CreateRepoCtx(),
  119. &repo.ArrayOneRemoveOption{
  120. CollectName: repo.CollectionDatabase,
  121. Id: req.DbId,
  122. ArrayQuery: repo.Map{"assets": bson.M{"id": req.DefineId}},
  123. },
  124. )
  125. if err != nil {
  126. return nil, err
  127. }
  128. if ret.MatchedCount < 1 {
  129. return nil, fmt.Errorf("删除失败")
  130. }
  131. return nil, nil
  132. },
  133. }
  134. }