115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"yintai-api/app/common/model"
|
|
|
|
"github.com/spf13/cast"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Process struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewProcess(db ...*gorm.DB) *Process {
|
|
return &Process{db: getDB(db...)}
|
|
}
|
|
|
|
func (r *Process) TableName() string {
|
|
return "yt_process"
|
|
}
|
|
|
|
func (r *Process) Create(m *model.Process) error {
|
|
return r.db.Table(r.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (r *Process) Update(m *model.Process) error {
|
|
return r.db.Table(r.TableName()).Save(m).Error
|
|
}
|
|
|
|
func (r *Process) Delete(id int64) error {
|
|
return r.db.Table(r.TableName()).Where("id = ?", id).Delete(&Process{}).Error
|
|
}
|
|
|
|
func (r *Process) Get(id int64) (*model.Process, error) {
|
|
var res model.Process
|
|
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 *Process) 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 *Process) List(lang, title, content, processTime string, categoryID int64, page, size int, sort string) ([]*model.ProcessPage, int64, error) {
|
|
var list []*model.ProcessPage
|
|
var count int64
|
|
tx := r.db.Table(r.TableName() + " AS p ").Joins(fmt.Sprintf("LEFT JOIN %s AS t ON p.id = t.process_id ", NewProcessTranslations().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s l ON l.biz_id = p.id AND l.type = ?", NewCategoryRelation().TableName()), model.CateTypeProcess)
|
|
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 title != "" {
|
|
tx = tx.Where("t.title LIKE ?", "%"+title+"%")
|
|
}
|
|
|
|
if content != "" {
|
|
tx = tx.Where("t.content LIKE ?", "%"+content+"%")
|
|
}
|
|
|
|
if processTime != "" {
|
|
tx = tx.Where("t.process_time = ?", processTime)
|
|
}
|
|
|
|
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(sort)
|
|
} 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 *Process) GetLang(lang string, id int64) (*model.ProcessPage, error) {
|
|
var res *model.ProcessPage
|
|
tx := r.db.Table(r.TableName() + " AS p ").Joins(fmt.Sprintf("LEFT JOIN %s AS t ON p.id = t.process_id ", NewProcessTranslations().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s l ON l.biz_id = p.id AND l.type = ?", NewCategoryRelation().TableName()), model.CateTypeProcess)
|
|
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).First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|