初始化项目
This commit is contained in:
97
dao/application_dao.go
Normal file
97
dao/application_dao.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cidservice/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// applicationDao 应用DAO
|
||||
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)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取应用
|
||||
func (d *applicationDao) GetByID(ctx context.Context, id int64) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := d.Ctx(ctx).Where("id", id).Scan(&app)
|
||||
return app, err
|
||||
}
|
||||
|
||||
// GetByTenantID 根据租户ID获取应用列表
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID int64) ([]*entity.Application, error) {
|
||||
var apps []*entity.Application
|
||||
err := d.Ctx(ctx).Where("tenant_id", tenantID).Scan(&apps)
|
||||
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)
|
||||
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)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (d *applicationDao) Delete(ctx context.Context, id int64) error {
|
||||
_, err := d.Ctx(ctx).Where("id", id).Delete()
|
||||
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)
|
||||
}
|
||||
|
||||
var apps []*entity.Application
|
||||
err := model.Page(page, pageSize).OrderDesc("created_at").Scan(&apps)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
total, err := model.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return apps, 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)
|
||||
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)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user