package dao

import (
	"demo/configs"
	"demo/data/domain"
	"demo/data/domain/vo"
	"time"
)

type ArticleCollectionVo struct {
	Topic       domain.ArticleTopic `json:"topic"`
	ArticleList []ArticleTitleVo    `json:"articleList"`
	Total       int64               `json:"total"`
}

type ArticleTitleVo struct {
	Id            int64     `json:"id"`                                    // 11 0 注释：id
	Image         string    `xorm:"image"  json:"image"`                   // 255 0 注释：文章名称
	ArticleTitle  string    `xorm:"article_title"  json:"articleTitle"`    // 255 0 注释：文章名称
	ArticleDesc   string    `xorm:"article_desc"  json:"articleDesc"`      // 255 0 注释：文章简介 1
	State         string    `xorm:"state"  json:"state"`                   // 255 0 注释：状态
	ArticleTagIds string    `xorm:"article_tag_ids"  json:"articleTagIds"` // 255 0 注释：文章标签
	PublishTime   time.Time `xorm:"publish_time"  json:"publishTime"`      // 0 0 注释：发表时间
	EyeFill       int64     `xorm:"eye_fill"  json:"eyeFill"`              // 11 0 注释：浏览量
	LikeCount     int64     `xorm:"like_count"  json:"likeCount"`          // 255 0 注释：点赞量
	CreateBy      string    `xorm:"create_by"  json:"createBy"`
	CreateTime    time.Time `xorm:"create_time"  json:"createTime"`
}

func GetArticleList(pageNum, pageSize int) (vo.BaseListVo, error) {
	var arr []domain.ArticleTopic
	var vo vo.BaseListVo
	c, err := configs.Engine.Table("article_topic").
		Limit(pageSize, (pageNum-1)*pageSize).Desc("id").FindAndCount(&arr)
	if err != nil {
		return vo, err
	}
	m := make(map[int64]ArticleCollectionVo)
	for i := range arr {
		var list = make([]ArticleTitleVo, 0)
		count, _ := configs.Engine.Table("article").
			Select("id, article_title, article_tag_ids, state, publish_time, eye_fill, like_count, create_time, article_tag_ids").
			Where("article_topic_id = ? and state = '1'", arr[i].Id).
			Limit(5).Desc("create_time").FindAndCount(&list)
		m[arr[i].Id] = ArticleCollectionVo{
			Topic:       arr[i],
			ArticleList: list,
			Total:       count,
		}
	}
	var VoList = make([]interface{}, 0)
	for _, v := range m {
		VoList = append(VoList, v)
	}
	vo.Total = int(c)
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = VoList
	return vo, nil
}
func GetArticleListByTopicId(topicId int64, pageNum, pageSize int) (domain.ArticleTopic, vo.BaseListVo, error) {
	var topic = domain.ArticleTopic{}
	var vo vo.BaseListVo
	_, err := configs.Engine.Table("article_topic").
		Where("id = ?", topicId).Desc("id").Get(&topic)
	if err != nil {
		return topic, vo, err
	}
	var list = make([]ArticleTitleVo, 0)
	count, _ := configs.Engine.Table("article").
		Select("id, image, article_title, article_desc, article_desc, article_tag_ids, state, publish_time, eye_fill, like_count, create_time,create_by, article_tag_ids").
		Where("article_topic_id = ? and state = '1'", topic.Id).
		Limit(pageSize, (pageNum-1)*pageSize).Desc("create_time").FindAndCount(&list)
	var VoList = make([]interface{}, 0)
	for _, v := range list {
		VoList = append(VoList, v)
	}
	vo.Total = int(count)
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = VoList
	return topic, vo, nil
}
