2024-08-03 17:07:10 +08:00
package storage
import (
2024-08-23 18:12:31 +08:00
"fmt"
"strconv"
"strings"
2024-08-03 17:07:10 +08:00
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
2024-08-23 18:12:31 +08:00
"github.com/samber/lo"
2024-08-03 17:07:10 +08:00
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type ProviderApi interface {
2024-08-23 18:12:31 +08:00
Get ( id uint ) ( provider v2 . Provider , err error )
List ( query PageListQuery ) ( providers [ ] v2 . Provider , total int64 , err error )
Create ( provider v2 . Provider ) error
Update ( provider v2 . Provider ) error
Delete ( id uint ) ( provider v2 . Provider , err error )
2024-08-26 17:09:29 +08:00
AutoMigrate ( ) error
2024-08-03 17:07:10 +08:00
}
2024-08-23 18:12:31 +08:00
func NewProviderApi ( db * gorm . DB ) ProviderApi {
return & providerApi { db : db }
2024-08-03 17:07:10 +08:00
}
2024-08-23 18:12:31 +08:00
type providerApi struct {
2024-08-03 17:07:10 +08:00
db * gorm . DB
}
2024-08-23 18:12:31 +08:00
type ListProviderQuery struct {
2024-08-03 17:07:10 +08:00
ID uint ` query:"id" `
ProviderId string ` query:"providerId" `
2024-08-23 18:12:31 +08:00
Keyword string ` query:"keyword" `
2024-08-03 17:07:10 +08:00
}
2024-08-23 18:12:31 +08:00
func ( g * ListProviderQuery ) Scope ( db * gorm . DB ) * gorm . DB {
2024-08-03 17:07:10 +08:00
if g . ID > 0 {
db = db . Where ( "id=?" , g . ID )
}
if g . ProviderId != "" {
db = db . Where ( "provider_id=?" , g . ProviderId )
}
2024-08-23 18:12:31 +08:00
if g . Keyword != "" {
db = db . Where ( "name LIKE ?" , "%" + g . Keyword + "%" )
2024-08-03 17:07:10 +08:00
}
return db
}
2024-08-23 18:12:31 +08:00
func ( p * providerApi ) Get ( id uint ) ( provider v2 . Provider , err error ) {
err = p . db . Where ( "id = ?" , id ) . Preload ( "CalculateProcess" ) . First ( & provider ) . Error
return
}
func ( p * providerApi ) List ( query PageListQuery ) ( providers [ ] v2 . Provider , total int64 , err error ) {
2024-08-26 17:51:14 +08:00
err = p . db . Scopes ( query . Scoper . Scope ) . Model ( & v2 . Provider { } ) . Count ( & total ) . Error
2024-08-23 18:12:31 +08:00
if err != nil {
return
}
err = p . db . Scopes ( query . Scope ) . Find ( & providers ) . Error
2024-08-03 17:07:10 +08:00
return
}
2024-08-23 18:12:31 +08:00
func ( p * providerApi ) Create ( provider v2 . Provider ) error {
if provider . ProviderId == "" {
return fmt . Errorf ( "标识为空" )
}
if provider . Name == "" {
return fmt . Errorf ( "名称为空" )
}
return p . db . Create ( & provider ) . Error
}
func ( p * providerApi ) Update ( provider v2 . Provider ) error {
if provider . ProviderId == "" {
return fmt . Errorf ( "标识为空" )
}
return p . db . Transaction ( func ( tx * gorm . DB ) error {
ids := lo . Map ( lo . Filter ( provider . CalculateProcess , func ( item v2 . CalculateProcess , index int ) bool { return item . ID > 0 } ) , func ( item v2 . CalculateProcess , index int ) string {
return strconv . Itoa ( int ( item . ID ) )
} )
// 删除已经没有的CalculateProcess
if err := tx . Where ( fmt . Sprintf ( "id not in (%s) AND owner_id = ? AND kind = ?" , strings . Join ( ids , "," ) ) , provider . ID , "provider" ) . Delete ( & v2 . CalculateProcess { } ) . Error ; err != nil {
return err
}
// 更新方案的name和描述
return tx . Session ( & gorm . Session { FullSaveAssociations : true } ) . Select ( "updated_at" , "name" , "config" , "mark" , "CalculateProcess" ) .
Where ( "id = ?" , provider . ID ) . Updates ( & provider ) . Error
} )
}
func ( p * providerApi ) Delete ( id uint ) ( provider v2 . Provider , err error ) {
err = p . db . Transaction ( func ( tx * gorm . DB ) error {
if err = tx . Where ( "owner_id = ? AND kind = ?" , id , "provider" ) . Delete ( & v2 . CalculateProcess { } ) . Error ; err != nil {
}
return p . db . Clauses ( clause . Returning { } ) . Where ( "id = ?" , id ) . Delete ( & provider ) . Error
} )
return
2024-08-03 17:07:10 +08:00
}
2024-08-26 17:09:29 +08:00
func ( p * providerApi ) AutoMigrate ( ) error {
return p . db . AutoMigrate ( & v2 . Provider { } , & v2 . CalculateProcess { } )
}