ArticleDao.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dao
  2. import (
  3. "demo/configs"
  4. "demo/data/domain"
  5. "demo/data/domain/vo"
  6. "time"
  7. )
  8. type ArticleCollectionVo struct {
  9. Topic domain.ArticleTopic `json:"topic"`
  10. ArticleList []ArticleTitleVo `json:"articleList"`
  11. Total int64 `json:"total"`
  12. }
  13. type ArticleTitleVo struct {
  14. Id int64 `json:"id"` // 11 0 注释:id
  15. Image string `xorm:"image" json:"image"` // 255 0 注释:文章名称
  16. ArticleTitle string `xorm:"article_title" json:"articleTitle"` // 255 0 注释:文章名称
  17. ArticleDesc string `xorm:"article_desc" json:"articleDesc"` // 255 0 注释:文章简介
  18. State string `xorm:"state" json:"state"` // 255 0 注释:状态
  19. ArticleTagIds string `xorm:"article_tag_ids" json:"articleTagIds"` // 255 0 注释:文章标签
  20. PublishTime time.Time `xorm:"publish_time" json:"publishTime"` // 0 0 注释:发表时间
  21. EyeFill int64 `xorm:"eye_fill" json:"eyeFill"` // 11 0 注释:浏览量
  22. LikeCount int64 `xorm:"like_count" json:"likeCount"` // 255 0 注释:点赞量
  23. CreateTime time.Time `xorm:"create_time" json:"createTime"`
  24. }
  25. func GetArticleList(pageNum, pageSize int) (vo.BaseListVo, error) {
  26. var arr []domain.ArticleTopic
  27. var vo vo.BaseListVo
  28. c, err := configs.Engine.Table("article_topic").
  29. Limit(pageSize, (pageNum-1)*pageSize).Desc("id").FindAndCount(&arr)
  30. if err != nil {
  31. return vo, err
  32. }
  33. m := make(map[int64]ArticleCollectionVo)
  34. for i := range arr {
  35. var list = make([]ArticleTitleVo, 0)
  36. count, _ := configs.Engine.Table("article").
  37. Select("id, article_title, article_tag_ids, state, publish_time, eye_fill, like_count, create_time, article_tag_ids").
  38. Where("article_topic_id = ? and state = '1'", arr[i].Id).
  39. Limit(5).Desc("create_time").FindAndCount(&list)
  40. m[arr[i].Id] = ArticleCollectionVo{
  41. Topic: arr[i],
  42. ArticleList: list,
  43. Total: count,
  44. }
  45. }
  46. var VoList = make([]interface{}, 0)
  47. for _, v := range m {
  48. VoList = append(VoList, v)
  49. }
  50. vo.Total = int(c)
  51. vo.PageNum = pageNum
  52. vo.PageSize = pageSize
  53. vo.List = VoList
  54. return vo, nil
  55. }
  56. func GetArticleListByTopicId(topicId int64, pageNum, pageSize int) (domain.ArticleTopic, vo.BaseListVo, error) {
  57. var topic = domain.ArticleTopic{}
  58. var vo vo.BaseListVo
  59. _, err := configs.Engine.Table("article_topic").
  60. Where("id = ?", topicId).Desc("id").Get(&topic)
  61. if err != nil {
  62. return topic, vo, err
  63. }
  64. var list = make([]ArticleTitleVo, 0)
  65. count, _ := configs.Engine.Table("article").
  66. Select("id, image, article_title, article_desc, article_desc, article_tag_ids, state, publish_time, eye_fill, like_count, create_time, article_tag_ids").
  67. Where("article_topic_id = ? and state = '1'", topic.Id).
  68. Limit(pageSize, (pageNum-1)*pageSize).Desc("create_time").FindAndCount(&list)
  69. var VoList = make([]interface{}, 0)
  70. for _, v := range list {
  71. VoList = append(VoList, v)
  72. }
  73. vo.Total = int(count)
  74. vo.PageNum = pageNum
  75. vo.PageSize = pageSize
  76. vo.List = VoList
  77. return topic, vo, nil
  78. }