|
| 1 | +/* |
| 2 | +Copyright 2024 The KodeRover Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mongodb |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "go.mongodb.org/mongo-driver/bson" |
| 24 | + "go.mongodb.org/mongo-driver/mongo" |
| 25 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 26 | + |
| 27 | + "github.com/koderover/zadig/v2/pkg/microservice/aslan/config" |
| 28 | + "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models" |
| 29 | + mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo" |
| 30 | +) |
| 31 | + |
| 32 | +type LarkPluginWorkflowConfigColl struct { |
| 33 | + *mongo.Collection |
| 34 | + |
| 35 | + coll string |
| 36 | +} |
| 37 | + |
| 38 | +func NewLarkPluginWorkflowConfigColl() *LarkPluginWorkflowConfigColl { |
| 39 | + name := models.LarkPluginWorkflowConfig{}.TableName() |
| 40 | + return &LarkPluginWorkflowConfigColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name} |
| 41 | +} |
| 42 | + |
| 43 | +func (c *LarkPluginWorkflowConfigColl) GetCollectionName() string { |
| 44 | + return c.coll |
| 45 | +} |
| 46 | + |
| 47 | +func (c *LarkPluginWorkflowConfigColl) EnsureIndex(ctx context.Context) error { |
| 48 | + mod := []mongo.IndexModel{ |
| 49 | + { |
| 50 | + Keys: bson.D{ |
| 51 | + bson.E{Key: "workspace_id", Value: 1}, |
| 52 | + bson.E{Key: "work_item_type_key", Value: 1}, |
| 53 | + }, |
| 54 | + Options: options.Index().SetUnique(true), |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + _, err := c.Indexes().CreateMany(ctx, mod) |
| 59 | + |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +func (c *LarkPluginWorkflowConfigColl) Get(workspaceID string) ([]*models.LarkPluginWorkflowConfig, error) { |
| 64 | + resp := make([]*models.LarkPluginWorkflowConfig, 0) |
| 65 | + cursor, err := c.Collection.Find(context.TODO(), bson.M{"workspace_id": workspaceID}) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + err = cursor.All(context.TODO(), &resp) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + return resp, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (c *LarkPluginWorkflowConfigColl) GetWorkItemTypeConfig(workspaceID, workItemType string) (*models.LarkPluginWorkflowConfig, error) { |
| 79 | + resp := new(models.LarkPluginWorkflowConfig) |
| 80 | + |
| 81 | + query := bson.M{"workspace_id": workspaceID, "work_item_type_key": workItemType} |
| 82 | + err := c.Collection.FindOne(context.TODO(), query).Decode(resp) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + return resp, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (c *LarkPluginWorkflowConfigColl) Update(configs []*models.LarkPluginWorkflowConfig) error { |
| 91 | + if len(configs) == 0 { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + // 构建批量操作 |
| 96 | + operations := make([]mongo.WriteModel, 0, len(configs)) |
| 97 | + for _, config := range configs { |
| 98 | + config.UpdateTime = time.Now().Unix() |
| 99 | + // 使用 WorkspaceID 和 WorkItemType 作为唯一标识字段进行 upsert 操作 |
| 100 | + query := bson.M{"workspace_id": config.WorkspaceID, "work_item_type_key": config.WorkItemTypeKey} |
| 101 | + operation := mongo.NewReplaceOneModel(). |
| 102 | + SetFilter(query). |
| 103 | + SetReplacement(config). |
| 104 | + SetUpsert(true) |
| 105 | + operations = append(operations, operation) |
| 106 | + } |
| 107 | + |
| 108 | + // 执行批量操作 |
| 109 | + opts := options.BulkWrite().SetOrdered(false) |
| 110 | + _, err := c.BulkWrite(context.TODO(), operations, opts) |
| 111 | + |
| 112 | + return err |
| 113 | +} |
| 114 | + |
| 115 | +func (c *LarkPluginWorkflowConfigColl) Delete(workItemTypeKeys []string) error { |
| 116 | + if len(workItemTypeKeys) == 0 { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + query := bson.M{"work_item_type_key": bson.M{"$in": workItemTypeKeys}} |
| 121 | + _, err := c.DeleteMany(context.TODO(), query) |
| 122 | + return err |
| 123 | +} |
0 commit comments