68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"yintai-api/app/common/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DocumentTranslations struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDocumentTranslations(db ...*gorm.DB) *DocumentTranslations {
|
|
return &DocumentTranslations{db: getDB(db...)}
|
|
}
|
|
|
|
func (r *DocumentTranslations) TableName() string {
|
|
return "yt_document_translations"
|
|
}
|
|
|
|
func (r *DocumentTranslations) Create(m *model.DocumentTranslations) error {
|
|
return r.db.Table(r.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (r *DocumentTranslations) Update(m *model.DocumentTranslations) error {
|
|
return r.db.Table(r.TableName()).Where("document_id = ? AND lang = ?", m.DocumentID, m.Lang).Save(m).Error
|
|
}
|
|
|
|
func (r *DocumentTranslations) Delete(masterID int64) error {
|
|
return r.db.Table(r.TableName()).Where("document_id = ?", masterID).Delete(&DocumentTranslations{}).Error
|
|
}
|
|
|
|
func (r *DocumentTranslations) Get(id int64) (*model.DocumentTranslations, error) {
|
|
var res model.DocumentTranslations
|
|
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 *DocumentTranslations) 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 *DocumentTranslations) List(...any) ([]*model.DocumentTranslations, int64, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *DocumentTranslations) GetByMaster(masterID int64, lang string) (*model.DocumentTranslations, error) {
|
|
var res model.DocumentTranslations
|
|
tx := r.db.Table(r.TableName()).Where("document_id = ? AND lang = ?", masterID, lang).First(&res)
|
|
if tx.Error != nil {
|
|
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, tx.Error
|
|
}
|
|
return &res, nil
|
|
}
|