yintai2/app/common/dao/category.go

149 lines
3.4 KiB
Go

package dao
import (
"errors"
"fmt"
"yintai-api/app/common/model"
"gorm.io/gorm"
)
type Category struct {
db *gorm.DB
}
func NewCategory(db ...*gorm.DB) *Category {
return &Category{db: getDB(db...)}
}
func (r *Category) TableName() string {
return "yt_category"
}
func (r *Category) Create(m *model.Category) error {
return r.db.Table(r.TableName()).Create(m).Error
}
func (r *Category) Update(m *model.Category) error {
return r.db.Table(r.TableName()).Save(m).Error
}
func (r *Category) Delete(path string) error {
return r.db.Table(r.TableName()).Where("path LIKE ?", fmt.Sprintf("%s%%", path)).Delete(&model.Category{}).Error
}
func (r *Category) Get(id int64) (*model.Category, error) {
var res model.Category
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 *Category) GetWithType(id int64, t string) (*model.Category, error) {
var res model.Category
if err := r.db.Table(r.TableName()).Where("id = ? AND type = ?", id, t).First(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &res, nil
}
func (r *Category) 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 *Category) GetByPath(path string) ([]*model.Category, error) {
var res []*model.Category
if err := r.db.Table(r.TableName()).Where("path LIKE ?", fmt.Sprintf("%s%%", path)).Find(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return res, nil
}
func (r *Category) List(pid int64, name, nameEn, types string, page, size int, sort string) ([]*model.Category, int64, error) {
var req []*model.Category
var count int64
tx := r.db.Table(r.TableName()).Where("type = ?", types)
if pid != 0 {
tx = tx.Where("pid = ?", pid)
}
if name != "" {
tx = tx.Where("name LIKE ?", fmt.Sprintf("%%%s%%", name))
}
if nameEn != "" {
tx = tx.Where("name_en LIKE ?", fmt.Sprintf("%%%s%%", nameEn))
}
if sort != "" {
tx = tx.Order(sort)
} else {
tx = tx.Order("create_time desc")
}
if err := tx.Count(&count).Error; err != nil {
return nil, 0, err
}
if err := tx.Offset((page - 1) * size).Limit(size).Find(&req).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, 0, nil
}
return nil, 0, err
}
return req, count, nil
}
func (c *Category) GetByBiz(BizID int64, BizType string) (*model.Category, error) {
var res model.Category
tx := c.db.Table(c.TableName() + " c ").Joins(fmt.Sprintf(" LEFT JON %s r ON c.id = r.cateory_id", NewCategoryRelation().TableName()))
tx = tx.Where("r.biz_id = ? AND c.type = ?", BizID, BizType)
if err := tx.First(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &res, nil
}
func (c *Category) GetByIds(ids ...int64) (map[int64]*model.Category, error) {
resMap := make(map[int64]*model.Category)
var res []*model.Category
if len(ids) > 0 {
tx := c.db.Table(c.TableName()).Where("id IN (?)", ids).Find(&res)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return resMap, nil
}
return resMap, tx.Error
}
}
for _, re := range res {
resMap[re.ID] = re
}
return resMap, nil
}