103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
|
||
"cid/model/entity"
|
||
|
||
"gitea.com/red-future/common/beans"
|
||
"gitea.com/red-future/common/db/mongo"
|
||
"go.mongodb.org/mongo-driver/v2/bson"
|
||
)
|
||
|
||
// applicationDao 应用DAO
|
||
type applicationDao struct {
|
||
}
|
||
|
||
var Application = &applicationDao{}
|
||
|
||
// Create 创建应用
|
||
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
|
||
ids, err := mongo.DB().Insert(ctx, []interface{}{app}, "application")
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
if len(ids) > 0 {
|
||
return ids[0].(string), nil
|
||
}
|
||
return "", nil
|
||
}
|
||
|
||
// GetByID 根据ID获取应用
|
||
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
|
||
var app *entity.Application
|
||
err := mongo.DB().FindOne(ctx, bson.M{"_id": id}, &app, "application")
|
||
return app, err
|
||
}
|
||
|
||
// GetByTenantID 根据租户ID获取应用列表
|
||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
|
||
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)
|
||
return apps, err
|
||
}
|
||
|
||
// GetByAPIKey 根据API密钥获取应用
|
||
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
|
||
var app *entity.Application
|
||
err := mongo.DB().FindOne(ctx,
|
||
bson.M{"appKey": apiKey}, &app, "application")
|
||
return app, err
|
||
}
|
||
|
||
// Update 更新应用
|
||
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
|
||
_, err := mongo.DB().Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
|
||
return err
|
||
}
|
||
|
||
// Delete 删除应用
|
||
func (d *applicationDao) Delete(ctx context.Context, id string) error {
|
||
_, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "application")
|
||
return err
|
||
}
|
||
|
||
// List 应用列表
|
||
func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSize int) ([]*entity.Application, int, error) {
|
||
filter := bson.M{}
|
||
if tenantID != "" {
|
||
filter["tenantId"] = tenantID
|
||
}
|
||
|
||
var apps []*entity.Application
|
||
total, err := mongo.DB().Count(ctx, filter, "application")
|
||
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)
|
||
if err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
return apps, int(total), nil
|
||
}
|
||
|
||
// GetByName 根据名称获取应用
|
||
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
|
||
var app *entity.Application
|
||
err := mongo.DB().FindOne(ctx, bson.M{"name": name}, &app, "application")
|
||
return app, err
|
||
}
|
||
|
||
// UpdateFields 更新应用部分字段
|
||
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
|
||
_, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
|
||
return err
|
||
}
|