fusenapi/model/gmodel/fs_product_template_basemap_logic.go

41 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-06-19 10:27:31 +00:00
package gmodel
2023-06-21 07:06:09 +00:00
2023-06-21 08:35:43 +00:00
import (
"context"
"gorm.io/gorm"
)
2023-06-21 07:06:09 +00:00
// TODO: 使用model的属性做你想做的
func (bm *FsProductTemplateBasemapModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsProductTemplateBasemap, err error) {
db := bm.db.WithContext(ctx).Model(&FsProductTemplateBasemap{}).Where("`status` = ? ", 1)
if len(fields) != 0 {
db = db.Select(fields[0])
}
err = db.Find(&resp).Error
return resp, err
}
2023-06-21 08:35:43 +00:00
2023-06-21 10:06:39 +00:00
func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap) error {
return bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
notInIds := make([]int64, 0, len(dataList))
2023-06-21 08:35:43 +00:00
//更新
for _, v := range dataList {
2023-06-21 10:06:39 +00:00
notInIds = append(notInIds, v.Id)
2023-06-21 08:35:43 +00:00
err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error
if err != nil {
return err
}
}
if len(notInIds) == 0 {
return nil
}
//删除不在ids里面的
2023-06-21 10:06:39 +00:00
return tx.Model(&FsProductTemplateBasemap{}).
Where("`status` = ? and `id` not in (?)", 1, notInIds).Update("status", 0).Error
2023-06-21 08:35:43 +00:00
})
}
2023-06-25 03:28:37 +00:00
func (bm *FsProductTemplateBasemapModel) Create(ctx context.Context, data *FsProductTemplateBasemap) error {
return bm.db.WithContext(ctx).Create(&data).Error
}