122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"yintai-api/app/common/model"
|
|
|
|
"github.com/spf13/cast"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Document struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDocument(db ...*gorm.DB) *Document {
|
|
return &Document{db: getDB(db...)}
|
|
}
|
|
|
|
func (r *Document) TableName() string {
|
|
return "yt_document"
|
|
}
|
|
|
|
func (r *Document) Create(m *model.Document) error {
|
|
return r.db.Table(r.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (r *Document) Update(m *model.Document) error {
|
|
return r.db.Table(r.TableName()).Save(m).Error
|
|
}
|
|
|
|
func (r *Document) Delete(id int64) error {
|
|
return r.db.Table(r.TableName()).Where("id = ?", id).Delete(&Document{}).Error
|
|
}
|
|
|
|
func (r *Document) Get(id int64) (*model.Document, error) {
|
|
var res model.Document
|
|
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 *Document) 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 *Document) List(lang string, categoryID int64, fileName, title, subTitle, desc string, page, size int, sort string) ([]*model.DocumentPage, int64, error) {
|
|
var list []*model.DocumentPage
|
|
var count int64
|
|
tx := r.db.Table(r.TableName() + " AS p ")
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS t ON p.id = t.document_id ", NewDocumentTranslations().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s l ON l.biz_id = p.id AND l.type = ?", NewCategoryRelation().TableName()), model.CateTypeFile)
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s c ON c.id = l.category_id", NewCategory().TableName()))
|
|
if lang != "" {
|
|
tx = tx.Where("t.lang = ?", lang)
|
|
}
|
|
|
|
if fileName != "" {
|
|
tx = tx.Where("t.name LIKE ?", "%"+fileName+"%")
|
|
}
|
|
|
|
if title != "" {
|
|
tx = tx.Where("t.title LIKE ?", "%"+title+"%")
|
|
}
|
|
|
|
if subTitle != "" {
|
|
tx = tx.Where("t.sub_title LIKE ?", "%"+subTitle+"%")
|
|
}
|
|
|
|
if desc != "" {
|
|
tx = tx.Where("t.description LIKE ?", "%"+desc+"%")
|
|
}
|
|
|
|
if cast.ToInt64(categoryID) != 0 {
|
|
tx = tx.Where("c.id = ?", cast.ToInt64(categoryID))
|
|
}
|
|
|
|
if err := tx.Count(&count).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if sort == "" {
|
|
tx = tx.Order("p.weight DESC")
|
|
} else {
|
|
tx = tx.Order(sort)
|
|
}
|
|
|
|
if err := tx.Offset((page - 1) * size).Limit(size).Select("p.*,t.*,c.name as category_name,c.name_en as category_en,c.id as category_id").Find(&list).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return list, count, nil
|
|
}
|
|
|
|
func (r *Document) GetLang(lang string, id int64) (*model.DocumentPage, error) {
|
|
var res *model.DocumentPage
|
|
tx := r.db.Table(r.TableName() + " AS p ")
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS t ON p.id = t.document_id ", NewDocumentTranslations().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s l ON l.biz_id = p.id AND l.type = ?", NewCategoryRelation().TableName()), model.CateTypeFile)
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s c ON c.id = l.category_id", NewCategory().TableName()))
|
|
tx = tx.Where("t.lang = ?", lang)
|
|
tx = tx.Where("p.id = ?", id)
|
|
field := "p.*,t.*,c.name as category_name,c.name_en as category_en,c.id as category_id"
|
|
|
|
if err := tx.Select(field).Find(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|