初始化项目
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// applicationDao 应用DAO
|
||||
@@ -14,84 +14,85 @@ type applicationDao struct{}
|
||||
|
||||
var Application = &applicationDao{}
|
||||
|
||||
// Ctx 获取数据库上下文
|
||||
func (d *applicationDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return g.DB().Model("application").Ctx(ctx)
|
||||
}
|
||||
|
||||
// Create 创建应用
|
||||
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (int64, error) {
|
||||
result, err := d.Ctx(ctx).Insert(app)
|
||||
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{app}, "application")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return "", err
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
return id, nil
|
||||
if len(ids) > 0 {
|
||||
return ids[0].(string), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取应用
|
||||
func (d *applicationDao) GetByID(ctx context.Context, id int64) (*entity.Application, error) {
|
||||
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := d.Ctx(ctx).Where("id", id).Scan(&app)
|
||||
err := mongo.FindOne(ctx, bson.M{"_id": id}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// GetByTenantID 根据租户ID获取应用列表
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID int64) ([]*entity.Application, error) {
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
|
||||
var apps []*entity.Application
|
||||
err := d.Ctx(ctx).Where("tenant_id", tenantID).Scan(&apps)
|
||||
err := mongo.Find(ctx, bson.M{"tenantId": tenantID}, &apps, "application")
|
||||
return apps, err
|
||||
}
|
||||
|
||||
// GetByAPIKey 根据API密钥获取应用
|
||||
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := d.Ctx(ctx).Where("app_key", apiKey).Scan(&app)
|
||||
err := mongo.FindOne(ctx, bson.M{"appKey": apiKey}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// Update 更新应用
|
||||
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
|
||||
_, err := d.Ctx(ctx).Where("id", app.Id).Update(app)
|
||||
_, err := mongo.Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (d *applicationDao) Delete(ctx context.Context, id int64) error {
|
||||
_, err := d.Ctx(ctx).Where("id", id).Delete()
|
||||
func (d *applicationDao) Delete(ctx context.Context, id string) error {
|
||||
_, err := mongo.Delete(ctx, bson.M{"_id": id}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
// List 应用列表
|
||||
func (d *applicationDao) List(ctx context.Context, tenantID int64, page, pageSize int) ([]*entity.Application, int, error) {
|
||||
model := d.Ctx(ctx)
|
||||
if tenantID > 0 {
|
||||
model = model.Where("tenant_id", tenantID)
|
||||
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
|
||||
err := model.Page(page, pageSize).OrderDesc("created_at").Scan(&apps)
|
||||
total, err := mongo.Count(ctx, filter, "application")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
total, err := model.Count()
|
||||
offset := (page - 1) * pageSize
|
||||
err = mongo.Find(ctx, filter, &apps, "application",
|
||||
options.Find().SetSort(bson.M{"createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(pageSize)))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return apps, total, nil
|
||||
return apps, int(total), nil
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取应用
|
||||
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := d.Ctx(ctx).Where("name", name).Scan(&app)
|
||||
err := mongo.FindOne(ctx, bson.M{"name": name}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// UpdateFields 更新应用部分字段
|
||||
func (d *applicationDao) UpdateFields(ctx context.Context, id int64, data *entity.Application) error {
|
||||
_, err := d.Ctx(ctx).Where("id", id).Update(data)
|
||||
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
|
||||
_, err := mongo.Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user