2023-07-28 03:15:42 +00:00
|
|
|
package gmodel
|
2023-08-01 10:13:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-03 09:37:25 +00:00
|
|
|
"fusenapi/utils/handlers"
|
2023-08-01 10:13:30 +00:00
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: 使用model的属性做你想做的
|
|
|
|
|
2023-08-09 07:29:27 +00:00
|
|
|
func (p *FsResourceModel) FindOneById(ctx context.Context, resourceId string) (resp *FsResource, err error) {
|
|
|
|
err = p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", resourceId).Take(&resp).Error
|
|
|
|
return resp, err
|
2023-08-01 10:13:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 03:50:16 +00:00
|
|
|
func (p *FsResourceModel) Create(ctx context.Context, req *FsResource) (resp *FsResource, err error) {
|
|
|
|
err = p.db.Table(p.name).WithContext(ctx).Create(req).Error
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FsResourceModel) Update(ctx context.Context, req *FsResource) (resp *FsResource, err error) {
|
|
|
|
err = p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", req.ResourceId).Save(req).Error
|
2023-08-01 10:13:30 +00:00
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *FsResourceModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string) (*FsResource, error) {
|
|
|
|
var resp FsResource
|
|
|
|
|
|
|
|
if filterMap != nil {
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-08-01 10:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
} else {
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *FsResourceModel) RowSelectBuilder(selectData []string) *gorm.DB {
|
|
|
|
var rowBuilder = m.db.Table(m.name)
|
|
|
|
|
|
|
|
if selectData != nil {
|
|
|
|
rowBuilder = rowBuilder.Select(selectData)
|
|
|
|
} else {
|
|
|
|
rowBuilder = rowBuilder.Select("*")
|
|
|
|
}
|
|
|
|
return rowBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
// 事务
|
|
|
|
func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error {
|
2023-08-10 06:29:59 +00:00
|
|
|
|
2023-08-10 06:49:46 +00:00
|
|
|
return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
err := fn(ctx, tx)
|
2023-08-01 10:13:30 +00:00
|
|
|
return err
|
2023-08-10 06:49:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// tx := m.db.Table(m.name).WithContext(ctx)
|
|
|
|
// defer func() {
|
|
|
|
// if r := recover(); r != nil {
|
|
|
|
// tx.Rollback()
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
|
|
|
|
// if err := tx.Error; err != nil {
|
|
|
|
// tx.Rollback()
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if err := fn(ctx, tx); err != nil {
|
|
|
|
// tx.Rollback()
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err := tx.Commit().Error
|
|
|
|
// if err != nil {
|
|
|
|
// tx.Rollback()
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return err
|
2023-08-01 10:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *FsResourceModel) TableName() string {
|
|
|
|
return m.name
|
|
|
|
}
|