yintai2/app/common/dao/category_relation.go

74 lines
1.9 KiB
Go

package dao
import (
"errors"
"yintai-api/app/common/model"
"gorm.io/gorm"
)
type CategoryRelation struct {
db *gorm.DB
}
func NewCategoryRelation(db ...*gorm.DB) *CategoryRelation {
return &CategoryRelation{db: getDB(db...)}
}
func (r *CategoryRelation) TableName() string {
return "yt_category_relation"
}
func (r *CategoryRelation) Create(m *model.CategoryRelation) error {
return r.db.Table(r.TableName()).Create(m).Error
}
func (r *CategoryRelation) Update(m *model.CategoryRelation) error {
return r.db.Table(r.TableName()).Save(m).Error
}
func (r *CategoryRelation) Delete(id int64) error {
return r.db.Table(r.TableName()).Where("id = ?", id).Delete(&CategoryRelation{}).Error
}
func (r *CategoryRelation) DeleteByCondition(bizID int64, bizType string) error {
return r.db.Table(r.TableName()).Where("biz_id = ? AND type = ?", bizID, bizType).Delete(&CategoryRelation{}).Error
}
func (r *CategoryRelation) Get(id int64) (*model.CategoryRelation, error) {
var res model.CategoryRelation
if err := r.db.Table(r.TableName()).Where("id = ?", id).First(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &res, nil
}
func (r *CategoryRelation) GetByIds(ids ...int64) ([]*model.CategoryRelation, error) {
var res []*model.CategoryRelation
if len(ids) == 0 {
return res, nil
}
if err := r.db.Table(r.TableName()).Where("id in (?)", ids).Find(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return res, nil
}
func (r *CategoryRelation) Exists(id int64) (bool, error) {
var count int64
if err := r.db.Table(r.TableName()).Where("id = ?", id).Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
func (r *CategoryRelation) List(...any) ([]*model.CategoryRelation, int64, error) {
panic("implement me")
}