fusenapi/model/gmodel/fs_map_library_logic.go

54 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-06-16 06:52:45 +00:00
package gmodel
2023-06-21 10:06:39 +00:00
import (
"context"
"gorm.io/gorm"
)
2023-06-16 06:52:45 +00:00
2023-06-20 11:56:18 +00:00
func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsMapLibrary, err error) {
2023-06-20 06:59:13 +00:00
db := ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`status` = ?", 1)
2023-06-20 11:56:18 +00:00
if len(fields) != 0 {
db = db.Select(fields[0])
2023-06-20 03:52:26 +00:00
}
err = db.Find(&resp).Error
2023-06-16 06:52:45 +00:00
if err != nil {
return nil, err
}
return
}
2023-06-20 03:52:26 +00:00
func (ml *FsMapLibraryModel) Create(ctx context.Context, data *FsMapLibrary) error {
2023-06-20 06:59:13 +00:00
return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Create(data).Error
2023-06-20 03:52:26 +00:00
}
func (ml *FsMapLibraryModel) Update(ctx context.Context, id int64, data *FsMapLibrary) error {
2023-06-20 06:59:13 +00:00
return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`id` = ? ", id).Updates(data).Error
2023-06-20 03:52:26 +00:00
}
func (ml *FsMapLibraryModel) ChangeStatusByIds(ctx context.Context, ids []int64, status int64) error {
if len(ids) == 0 {
return nil
}
2023-06-20 06:59:13 +00:00
return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`id` in (?) ", ids).Update("status", 0).Error
2023-06-20 03:52:26 +00:00
}
2023-06-21 10:06:39 +00:00
func (ml *FsMapLibraryModel) SaveMapLibraryWithTransaction(ctx context.Context, createList []FsMapLibrary, updateList []FsMapLibrary) error {
return ml.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
//创建
for _, v := range createList {
if err := tx.Model(&FsMapLibrary{}).Create(&v).Error; err != nil {
return err
}
}
if len(updateList) == 0 {
return nil
}
//更新
notInIds := make([]int64, 0, len(updateList))
for _, v := range updateList {
notInIds = append(notInIds, v.Id)
if err := tx.Model(&FsMapLibrary{}).Where("`id` = ?", v.Id).Updates(&v).Error; err != nil {
return err
}
}
//删除
return tx.Model(&FsMapLibrary{}).Where("`id` not in (?) ", notInIds).Update("status", 0).Error
})
}