109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"github.com/jinzhu/gorm"
|
||
|
)
|
||
|
|
||
|
type ArticleCate struct {
|
||
|
Id uint `json:"id"`
|
||
|
CatName string `json:"cat_name"`
|
||
|
UserId string `json:"user_id"`
|
||
|
EntId string `json:"ent_id"`
|
||
|
IsTop uint `json:"is_top"`
|
||
|
}
|
||
|
type Article struct {
|
||
|
Id uint `json:"id"`
|
||
|
Title string `json:"title"`
|
||
|
Content string `json:"content"`
|
||
|
CatId uint `json:"cat_id"`
|
||
|
UserId string `json:"user_id"`
|
||
|
EntId string `json:"ent_id"`
|
||
|
ApiUrl string `json:"api_url"`
|
||
|
SearchType uint `json:"search_type"`
|
||
|
Score uint `json:"score"`
|
||
|
}
|
||
|
|
||
|
func FindTopArticles(query interface{}, args ...interface{}) []Article {
|
||
|
cates := FindArticleCates(query, args...)
|
||
|
|
||
|
result := make([]Article, 0)
|
||
|
if len(cates) != 0 {
|
||
|
for _, cate := range cates {
|
||
|
articles := FindArticleList(1, 10, "", "cat_id = ? ", cate.Id)
|
||
|
for _, article := range articles {
|
||
|
result = append(result, article)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
func FindArticleCates(query interface{}, args ...interface{}) []ArticleCate {
|
||
|
var list []ArticleCate
|
||
|
DB.Table("article_cate").Where(query, args...).Order("id desc").Find(&list)
|
||
|
return list
|
||
|
}
|
||
|
func CountArticleList(query interface{}, args ...interface{}) uint {
|
||
|
var v uint
|
||
|
DB.Table("article").Where(query, args...).Count(&v)
|
||
|
return v
|
||
|
}
|
||
|
func FindArticleList(page uint, pagesize uint, orderBy string, query interface{}, args ...interface{}) []Article {
|
||
|
offset := (page - 1) * pagesize
|
||
|
if offset < 0 {
|
||
|
offset = 0
|
||
|
}
|
||
|
var list []Article
|
||
|
if orderBy == "" {
|
||
|
orderBy = "id desc"
|
||
|
}
|
||
|
DB.Table("article").Where(query, args...).Offset(offset).Limit(pagesize).Order(orderBy).Find(&list)
|
||
|
return list
|
||
|
}
|
||
|
func FindArticleCatesByEnt(userId interface{}) []ArticleCate {
|
||
|
var list []ArticleCate
|
||
|
DB.Table("article_cate").Where("ent_id = ?", userId).Order("id desc").Find(&list)
|
||
|
return list
|
||
|
}
|
||
|
|
||
|
func FindArticleRow(query interface{}, args ...interface{}) Article {
|
||
|
var res Article
|
||
|
DB.Table("article").Where(query, args...).Order("id desc").Find(&res)
|
||
|
return res
|
||
|
}
|
||
|
func FindArticleRows(fields string, query interface{}, args ...interface{}) []Article {
|
||
|
var res []Article
|
||
|
DB.Table("article").Select(fields).Where(query, args...).Order("id desc").Find(&res)
|
||
|
return res
|
||
|
}
|
||
|
func DelArticles(query interface{}, args ...interface{}) {
|
||
|
DB.Where(query, args...).Delete(&Article{})
|
||
|
}
|
||
|
func DelArticleCate(query interface{}, args ...interface{}) {
|
||
|
DB.Where(query, args...).Delete(&ArticleCate{})
|
||
|
}
|
||
|
func (a *ArticleCate) SaveArticleCate(query interface{}, args ...interface{}) {
|
||
|
DB.Model(&ArticleCate{}).Where(query, args...).Update(a)
|
||
|
}
|
||
|
func (a *Article) SaveArticle(query interface{}, args ...interface{}) {
|
||
|
DB.Model(&Article{}).Where(query, args...).Update(a)
|
||
|
}
|
||
|
|
||
|
func (a *Article) AddArticle() uint {
|
||
|
DB.Create(a)
|
||
|
return a.Id
|
||
|
}
|
||
|
func (a *ArticleCate) AddArticleCate() uint {
|
||
|
DB.Create(a)
|
||
|
return a.Id
|
||
|
}
|
||
|
|
||
|
//查询构造
|
||
|
func (a *Article) buildQuery() *gorm.DB {
|
||
|
myDB := DB
|
||
|
myDB.Model(a)
|
||
|
if a.Id != 0 {
|
||
|
myDB = myDB.Where("id = ?", a.Id)
|
||
|
}
|
||
|
return myDB
|
||
|
}
|