package model import ( "fmt" "github.com/codinl/go-logger" ) //go:generate goqueryset -in article.go // gen:qs type Article struct { Model Title string `json:"title"` SubTitle string `json:"sub_title"` IconUrl string `json:"icon_url"` Content string `json:"content"` SourceName string `json:"source_name"` SourceUrl string `json:"source_url"` ReadCount uint32 `json:"read_count"` ThumbsCount uint32 `json:"thumbs_count"` Status uint32 `json:"status"` ColumnType int `json:"column_type"` // 1-攻略 2-新闻 IsCollect bool `json:"is_collect" gorm:"-"` } // gen:qs type ArticleCollect struct { Model ArticleId uint32 `json:"article_id"` Uid uint32 `json:"uid"` ColumnType int `json:"column_type"` // 1-攻略 2-新闻 } func (m *Article) TableName() string { return "article" } type ArticleListReq struct { Title string `json:"title"` Status uint32 `json:"status"` ColumnType int `json:"column_type"` PageIdx int `json:"page_idx"` PageSize int `json:"page_size"` } func (m *ArticleListReq) GetArticleList(uid uint32) ([]Article, int, error) { page := m.PageIdx - 1 if page < 0 { page = 0 } if m.PageSize == 0 { m.PageSize = 10 } var articles []Article qs := NewArticleQuerySet(DB).StatusEq(1) if m.Title != "" { var articleList []Article sql := "SELECT * FROM article WHERE title LIKE '%" + m.Title + "%'" err := DB.Raw(sql).Scan(&articleList).Error if err != nil { logger.Error("err:", err) } articleId := make([]uint32, 0) for i, _ := range articleList { articleId = append(articleId, articleList[i].ID) } if len(articleId) == 0 { return articles, 0, err } qs = qs.IDIn(articleId...) } //if m.Status != 0 { // qs = qs.StatusEq(m.Status) //} if m.ColumnType != 0 { qs = qs.ColumnTypeEq(m.ColumnType) } count, err := qs.Count() if err != nil { logger.Error("err:", err) return articles, 0, err } totalPage := count/m.PageSize + 1 err = qs.OrderDescByID().Offset(page * m.PageSize).Limit(m.PageSize).All(&articles) if err != nil { logger.Error("err:", err) return articles, totalPage, err } articlesIds := make([]uint32, 0, len(articles)) for i, _ := range articles { articlesIds = append(articlesIds, articles[i].ID) } if len(articlesIds) == 0 { return articles, totalPage, err } var articlesCollect []ArticleCollect err = NewArticleCollectQuerySet(DB).UidEq(uid).ArticleIdIn(articlesIds...).All(&articlesCollect) if err != nil { logger.Error("err:", err) return articles, totalPage, err } articlesCollectMap := make(map[uint32]uint32, 0) for i, _ := range articlesCollect { articlesCollectMap[articlesCollect[i].ArticleId] = uid } for i, _ := range articles { _, ok := articlesCollectMap[articles[i].ID] if ok { articles[i].IsCollect = true } fmt.Println("articles:", articles[i].ID, articles[i].IsCollect) } return articles, totalPage, nil } type ArticleCollectListReq struct { PageIdx int `json:"page_idx"` PageSize int `json:"page_size"` } func (m *ArticleCollectListReq) GetArticleCollectList(uid uint32) ([]Article, int, error) { page := m.PageIdx - 1 if page < 0 { page = 0 } if m.PageSize == 0 { m.PageSize = 10 } var articlesCollects []ArticleCollect var articles []Article qs := NewArticleCollectQuerySet(DB).UidEq(uid) qs.OrderDescByID().Offset(page * m.PageSize).Limit(m.PageSize).All(&articlesCollects) count, err := qs.Count() if err != nil { logger.Error("err:", err) return articles, 0, err } totalPage := count/m.PageSize + 1 articleIds := make([]uint32, 0) for i, _ := range articlesCollects { articleIds = append(articleIds, articlesCollects[i].ArticleId) } if len(articleIds) == 0 { return articles, totalPage, err } err = NewArticleQuerySet(DB).IDIn(articleIds...).All(&articles) if err != nil { logger.Error("err:", err) return articles, totalPage, err } articleMap := make(map[uint32]Article, 0) for i, _ := range articles { articleMap[articles[i].ID] = articles[i] } list := make([]Article, 0, len(articles)) for i, _ := range articlesCollects { art, ok := articleMap[articlesCollects[i].ArticleId] if ok { list = append(list, art) } } return list, totalPage, nil } // gen:qs type ArticleTitlePanel struct { Model ArticlePanel string `json:"article_panel"` Status uint32 `json:"status"` Sort uint32 `json:"sort"` ColumnType uint32 `json:"column_type"` // 1-攻略 2-新闻 }