2024-08-03 17:07:10 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-08-27 16:39:06 +08:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2024-08-03 17:07:10 +08:00
|
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
2024-08-27 16:39:06 +08:00
|
|
|
"github.com/samber/lo"
|
2024-08-03 17:07:10 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SellerApi interface {
|
2024-08-27 16:39:06 +08:00
|
|
|
Get(id uint) (seller v2.Seller, err error)
|
2024-08-30 13:39:16 +08:00
|
|
|
GetBySellerId(sellerId v2.SellerId) (seller v2.Seller, err error)
|
2024-08-27 16:39:06 +08:00
|
|
|
List(query PageListQuery) (sellers []v2.Seller, total int64, err error)
|
|
|
|
Create(seller v2.Seller) error
|
|
|
|
Update(seller v2.Seller) error
|
|
|
|
Delete(id uint) (seller v2.Seller, err error)
|
|
|
|
AutoMigrate() error
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
func NewSellerApi(db *gorm.DB) SellerApi {
|
|
|
|
return &sellerApi{db: db}
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
type sellerApi struct {
|
2024-08-03 17:07:10 +08:00
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
type ListSellerQuery struct {
|
2024-08-03 17:07:10 +08:00
|
|
|
ID uint `query:"id"`
|
|
|
|
SellerId string `query:"sellerId"`
|
2024-08-27 16:39:06 +08:00
|
|
|
Keyword string `query:"keyword"`
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
func (g *ListSellerQuery) 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.SellerId != "" {
|
2024-08-27 16:39:06 +08:00
|
|
|
db = db.Where("seller_id = ?", g.SellerId)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
2024-08-27 16:39:06 +08:00
|
|
|
if g.Keyword != "" {
|
|
|
|
db = db.Where("name LIKE ?", "%"+g.Keyword+"%")
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
func (p *sellerApi) Get(id uint) (seller v2.Seller, err error) {
|
|
|
|
err = p.db.Where("id = ?", id).Preload("CalculateProcess").First(&seller).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-30 13:39:16 +08:00
|
|
|
func (p *sellerApi) GetBySellerId(sellerId v2.SellerId) (seller v2.Seller, err error) {
|
|
|
|
err = p.db.Where("seller_id = ?", sellerId).Preload("CalculateProcess").First(&seller).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
func (p *sellerApi) List(query PageListQuery) (sellers []v2.Seller, total int64, err error) {
|
|
|
|
err = p.db.Scopes(query.Scoper.Scope).Model(&v2.Seller{}).Count(&total).Error
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = p.db.Scopes(query.Scope).Find(&sellers).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sellerApi) Create(seller v2.Seller) error {
|
|
|
|
if seller.SellerId == "" {
|
|
|
|
return fmt.Errorf("标识为空")
|
|
|
|
}
|
|
|
|
if seller.Name == "" {
|
|
|
|
return fmt.Errorf("名称为空")
|
|
|
|
}
|
|
|
|
return p.db.Create(&seller).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sellerApi) Update(seller v2.Seller) error {
|
|
|
|
if seller.SellerId == "" {
|
|
|
|
return fmt.Errorf("标识为空")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
ids := lo.Map(lo.Filter(seller.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.Not(ids).Where("owner_id = ? AND kind = ?", seller.ID, "seller").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 = ?", seller.ID).Updates(&seller).Error
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sellerApi) Delete(id uint) (seller v2.Seller, err error) {
|
|
|
|
err = p.db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
if err = tx.Where("owner_id = ? AND kind = ?", id, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
return p.db.Clauses(clause.Returning{}).Where("id = ?", id).Delete(&seller).Error
|
|
|
|
})
|
2024-08-03 17:07:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:39:06 +08:00
|
|
|
func (p *sellerApi) AutoMigrate() error {
|
|
|
|
return p.db.AutoMigrate(&v2.Seller{}, &v2.CalculateProcess{})
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|