2023-04-27 15:56:12 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"Lee-WineList/client"
|
|
|
|
"Lee-WineList/model/entity"
|
|
|
|
"Lee-WineList/model/param"
|
|
|
|
"git.echol.cn/loser/logger/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type wine struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wine ...
|
|
|
|
func Wine() *wine {
|
|
|
|
return &wine{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWineList 获取酒单列表
|
|
|
|
func (w *wine) GetWineList(p param.GetWineList) (wines []entity.Wine, err error) {
|
|
|
|
db := client.MySQL.Model(&wines).Preload("Materials").Scopes(page(p.Current, p.Size))
|
|
|
|
|
|
|
|
if p.UserId != 0 {
|
|
|
|
db = db.Where("user_id = ?", p.UserId)
|
|
|
|
}
|
|
|
|
if p.Category != "" {
|
|
|
|
db = db.Where("category = ?", p.Category)
|
|
|
|
}
|
|
|
|
err = db.Find(&wines).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("获取酒单列表失败:%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:36:07 +08:00
|
|
|
func (w *wine) Add(p entity.Wine) (err error) {
|
|
|
|
err = client.MySQL.Create(&p).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wine) Update(p entity.Wine) (err error) {
|
|
|
|
err = client.MySQL.Save(&p).Error
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 15:56:12 +08:00
|
|
|
|
2023-04-27 18:02:38 +08:00
|
|
|
func (w *wine) Delete(p *entity.Wine, ue *entity.User) (err error) {
|
|
|
|
err = client.MySQL.Table(p.TableName()).Where("wine_id = ? and user_id = ?", p.WineId, ue.Id).Delete(&p).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("删除酒单失败:%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 15:56:12 +08:00
|
|
|
return
|
|
|
|
}
|