Files
cid/dao/application_dao.go

103 lines
3.0 KiB
Go
Raw Normal View History

2025-12-06 15:24:30 +08:00
package dao
import (
"context"
2025-12-09 16:10:45 +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 15:24:30 +08:00
)
// applicationDao 应用DAO
type applicationDao struct {
}
2025-12-06 15:24:30 +08:00
2025-12-30 10:55:35 +08:00
var Application = &applicationDao{}
2025-12-06 15:24:30 +08:00
// Create 创建应用
2025-12-10 15:41:52 +08:00
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
2025-12-30 10:55:35 +08:00
ids, err := mongo.DB().Insert(ctx, []interface{}{app}, "application")
2025-12-06 15:24:30 +08:00
if err != nil {
2025-12-10 15:41:52 +08:00
return "", err
}
if len(ids) > 0 {
return ids[0].(string), nil
2025-12-06 15:24:30 +08:00
}
2025-12-10 15:41:52 +08:00
return "", nil
2025-12-06 15:24:30 +08:00
}
// GetByID 根据ID获取应用
2025-12-10 15:41:52 +08:00
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
2025-12-06 15:24:30 +08:00
var app *entity.Application
2025-12-30 10:55:35 +08:00
err := mongo.DB().FindOne(ctx, bson.M{"_id": id}, &app, "application")
2025-12-06 15:24:30 +08:00
return app, err
}
// GetByTenantID 根据租户ID获取应用列表
2025-12-10 15:41:52 +08:00
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
2025-12-06 15:24:30 +08:00
var apps []*entity.Application
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application", page, nil)
2025-12-06 15:24:30 +08:00
return apps, err
}
// GetByAPIKey 根据API密钥获取应用
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
var app *entity.Application
2025-12-30 10:55:35 +08:00
err := mongo.DB().FindOne(ctx,
bson.M{"appKey": apiKey}, &app, "application")
2025-12-06 15:24:30 +08:00
return app, err
}
// Update 更新应用
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
2025-12-30 10:55:35 +08:00
_, err := mongo.DB().Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
2025-12-06 15:24:30 +08:00
return err
}
// Delete 删除应用
2025-12-10 15:41:52 +08:00
func (d *applicationDao) Delete(ctx context.Context, id string) error {
2025-12-30 10:55:35 +08:00
_, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "application")
2025-12-06 15:24:30 +08:00
return err
}
// List 应用列表
2025-12-10 15:41:52 +08:00
func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSize int) ([]*entity.Application, int, error) {
filter := bson.M{}
if tenantID != "" {
filter["tenantId"] = tenantID
2025-12-06 15:24:30 +08:00
}
var apps []*entity.Application
2025-12-30 10:55:35 +08:00
total, err := mongo.DB().Count(ctx, filter, "application")
2025-12-06 15:24:30 +08:00
if err != nil {
return nil, 0, err
}
// 使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(pageSize)}
total, err = mongo.DB().Find(ctx, filter, &apps, "application", pageBean, nil)
2025-12-06 15:24:30 +08:00
if err != nil {
return nil, 0, err
}
2025-12-10 15:41:52 +08:00
return apps, int(total), nil
2025-12-06 15:24:30 +08:00
}
// GetByName 根据名称获取应用
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
var app *entity.Application
2025-12-30 10:55:35 +08:00
err := mongo.DB().FindOne(ctx, bson.M{"name": name}, &app, "application")
2025-12-06 15:24:30 +08:00
return app, err
}
// UpdateFields 更新应用部分字段
2025-12-10 15:41:52 +08:00
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
2025-12-30 10:55:35 +08:00
_, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
2025-12-06 15:24:30 +08:00
return err
}