package topic import ( "fmt" "github.com/golangkit/formatime" "gorm.io/gorm" "live/app/lib/db" ) type Topic struct { db.BaseModel Id uint `gorm:"column:id" json:"id"` Title string `gorm:"column:title" json:"title"` Substance uint `gorm:"column:substance" json:"substance"` Partake uint `gorm:"column:partake" json:"partake"` CreatedAt formatime.Second `gorm:"column:created_at" json:"createdAt"` UpdatedAt formatime.Second `gorm:"column:updated_at" json:"updatedAt"` IsDel int `gorm:"column:is_del" json:"isDel"` IsHot int `gorm:"column:is_hot" json:"isHot"` TopicImg string `gorm:"column:topic_img" json:"topicImg"` } // 插入 func (t *Topic) Create(topic *Topic) uint { t.GetDb().Create(topic) if topic.Id > 0 { return topic.Id } return 0 } // @Title 获取热门列表 func (t *Topic) GetHotlist(count int) (result []Topic) { t.GetDb().Model(t).Where("is_del = ? and is_hot = ?", 0, 1).Limit(count).Find(&result) return } // @Title 根据关键字检索话题列表 func (t *Topic) Getlist(keyword string, start, limit int) (result []Topic) { t.GetDb().Model(t).Where("is_del = ? and title like ?", 0, fmt.Sprintf("%%%v%%", keyword)). Limit(limit).Offset(start).Find(&result) return } // @Title 根据关键字检索话题列表数量 func (t *Topic) GetlistCount(keyword string) (result int64) { t.GetDb().Model(t).Where("is_del = ? and title like ?", 0, fmt.Sprintf("%%%v%%", keyword)). Count(&result) return } // @Title 根据ids获取列表 func (t *Topic) GetlistByIds(topicIds []uint) (result []Topic) { t.GetDb().Model(t).Where("is_del = ? and id in ?", 0, topicIds).Find(&result) return } // @Title 根据id获取单条数据 func (t *Topic) GetById(topicId uint) (result Topic) { if topicId <= 0 { return } result.Id = topicId t.GetDb().First(&result) return } // @Title 添加参与人 func (t *Topic) AddSubstance(topicId uint) int64 { if topicId <= 0 { return 0 } return t.GetDb().Model(t).Where(Topic{Id: topicId}).Update("substance", gorm.Expr("substance + ?", 1)).RowsAffected } // @Title 添加内容数 func (t *Topic) AddPartake(topicId uint) int64 { if topicId <= 0 { return 0 } return t.GetDb().Model(t).Where(Topic{Id: topicId}).Update("partake", gorm.Expr("partake + ?", 1)).RowsAffected } // @Title 获取话题 func (t *Topic) GetByTitle(title string) (result Topic) { if title == "" { return } t.GetDb().Model(t).Where("title = ?", title).First(&result) return }