2025-12-06 10:38:48 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2025-12-10 15:41:52 +08:00
|
|
|
|
"cid/model/entity"
|
2025-12-30 10:55:35 +08:00
|
|
|
|
|
2026-02-24 16:24:47 +08:00
|
|
|
|
"gitea.com/red-future/common/beans"
|
2026-02-24 17:17:10 +08:00
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
2025-12-10 15:41:52 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
2025-12-06 10:38:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
var Strategy = &strategyDao{}
|
2025-12-29 15:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
type strategyDao struct {
|
|
|
|
|
|
}
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// GetByName 根据名称获取策略
|
|
|
|
|
|
func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &strategy, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetByID 根据ID获取策略
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &strategy, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetByTenantLevel 根据租户级别获取策略
|
|
|
|
|
|
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
|
2026-01-08 11:07:58 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create 创建策略
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id string, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
ids, err := mongo.DB().Insert(ctx, []interface{}{strategy}, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
2025-12-10 15:41:52 +08:00
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
|
|
id = ids[0].(string)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update 更新策略
|
|
|
|
|
|
func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (affected int64, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
result, err := mongo.DB().Update(ctx, bson.M{"_id": strategy.Id}, bson.M{"$set": strategy}, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
2026-02-27 14:36:00 +08:00
|
|
|
|
return result, nil
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Delete 删除策略
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *strategyDao) Delete(ctx context.Context, id string) (affected int64, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
2025-12-10 15:41:52 +08:00
|
|
|
|
return count, nil
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetList 获取策略列表
|
|
|
|
|
|
func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel, status string) (list []*entity.Strategy, total int64, err error) {
|
2025-12-10 15:41:52 +08:00
|
|
|
|
filter := bson.M{}
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 筛选条件
|
|
|
|
|
|
if tenantLevel != "" {
|
2025-12-10 15:41:52 +08:00
|
|
|
|
filter["tenantLevel"] = tenantLevel
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
if status != "" {
|
2025-12-10 15:41:52 +08:00
|
|
|
|
filter["status"] = status
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取总数
|
2025-12-30 10:55:35 +08:00
|
|
|
|
total, err = mongo.DB().Count(ctx, filter, "strategies")
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 11:07:58 +08:00
|
|
|
|
// 分页查询,使用common/mongo的Find方法,自动处理分页、租户等
|
|
|
|
|
|
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
|
|
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &list, "strategies", pageBean, nil)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|