You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.7 KiB
74 lines
1.7 KiB
package goods
|
|
|
|
import (
|
|
"errors"
|
|
"recook/internal/dbc"
|
|
goods2 "recook/internal/v2/model/gys/goods"
|
|
strict "recook/internal/v2/model/http/goods"
|
|
"recook/internal/v2/model/recook/goods"
|
|
)
|
|
|
|
var CountryLogic = &countryLogic{}
|
|
|
|
type countryLogic struct {
|
|
}
|
|
|
|
func (c countryLogic) Create(args *strict.ArgsCountry) error {
|
|
if err := dbc.DB.Create(goods.RecookAbroadCountryModel{
|
|
Name: args.Name,
|
|
ParentID: args.ParentID,
|
|
Icon: args.Icon,
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c countryLogic) Query() (result []strict.CountryResult, err error) {
|
|
var t goods.RecookAbroadCountryModel
|
|
if err = dbc.DB.Table(t.TableName()).Preload("Children").
|
|
Find(&result, "parent_id = 0").Error; err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c countryLogic) Delete(args *strict.ArgsCountryDelete) error {
|
|
var t goods.RecookAbroadCountryModel
|
|
if err := dbc.DB.Table(t.TableName()).First(&t, "id = ?", args.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
var all []uint
|
|
|
|
if t.ParentID != 0 {
|
|
dbc.DB.Table(t.TableName()).Where("parent_id = ?", t.ParentID).Pluck("id", &all)
|
|
} else {
|
|
all = append(all, args.ID)
|
|
}
|
|
|
|
var total uint
|
|
if err := dbc.DB.Table((&goods2.GysGoodsInfoModel{}).TableName()).
|
|
Where("country in (?)", all).Count(&total).Error; err != nil {
|
|
return err
|
|
}
|
|
if total != 0 {
|
|
return errors.New("正在使用,无法删除")
|
|
}
|
|
if err := dbc.DB.Delete(&goods.RecookAbroadCountryModel{}, "id = ?", args.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c countryLogic) Update(args *strict.ArgsCountryUpdate) error {
|
|
if err := dbc.DB.Updates(goods.RecookAbroadCountryModel{
|
|
ID: args.ID,
|
|
Name: args.Name,
|
|
ParentID: args.ParentID,
|
|
Icon: args.Icon,
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|