package yb

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// YbCommunityQuestionLikeTease 问答点赞/吐槽表
type YbCommunityQuestionLikeTease struct {
	CommunityQuestionLikeTeaseID uint64    `orm:"column(community_question_like_tease_id);pk"  description:"点赞/吐槽自增id"`
	UserId                       uint64    `orm:"column(user_id)" description:"用户id"`
	OpType                       int8      `orm:"column(op_type)" description:"类型. 1-点赞 2-吐槽"`
	Enabled                      int8      `orm:"column(enabled)" description:"状态. 0-无效数据（已取消点赞/吐槽） 1-有效数据（点赞/吐槽）"`
	CreateTime                   time.Time `orm:"column(create_time)"  description:"创建时间"`
	ModifyTime                   time.Time `orm:"column(modify_time)"  description:"修改时间"`
	CommunityQuestionID          uint32    `gorm:"column(community_question_id)" description:"问答ID"`
	SourceAgent                  int8      `orm:"column(source_agent)" description:"点赞/吐槽入口来源，1：小程序，2：小程序pc, 4：web pc"`
}

// StatCommunityQuestionLikeTeaseItem 问答点赞/吐槽数据
type StatCommunityQuestionLikeTeaseItem struct {
	CommunityQuestionId int    `description:"问答id"`
	QuestionContent     string `description:"问答内容"`
	Num                 int    `description:"点赞/吐槽数"`
	CreateTime          string `description:"提问时间"`
	ModifyTime          string `description:"最新点赞时间"`
	VarietyTagId        int    `description:"品种标签ID"`
	VarietyClassifyTag  string `description:"品种分类及标签"`
}

// GetYbCommunityQuestionLikeTeaseQuestion 根据条件查询点赞/吐槽相关的 问答id
func GetYbCommunityQuestionLikeTeaseQuestion(opType int8, orderStr string, startSize, pageSize int) (list []*StatCommunityQuestionLikeTeaseItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	count( 1 ) AS num,
	a.community_question_id,
	MAX(a.modify_time) as modify_time,b.question_content,b.create_time
FROM
	yb_community_question_like_tease a 
join yb_community_question b on a.community_question_id=b.community_question_id
WHERE
	a.enabled = 1 AND b.is_deleted = 0  AND a.op_type = ? AND a.source = 1 
GROUP BY a.community_question_id ORDER BY  `
	sql += ` ` + orderStr
	sql += ` ,modify_time desc`

	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, opType, startSize, pageSize).QueryRows(&list)
	return
}

// GetYbCommunityQuestionLikeTeaseTotal 根据条件查询点赞/吐槽 数据汇总数
func GetYbCommunityQuestionLikeTeaseTotal(opType int8) (total int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	count( 1 ) AS like_tease_num,
	a.community_question_id,
	MAX(a.modify_time) as modify_time,b.question_content,b.create_time
FROM
	yb_community_question_like_tease a 
join yb_community_question b on a.community_question_id=b.community_question_id
WHERE
	a.enabled = 1 AND b.is_deleted = 0 AND a.op_type = ? 
GROUP BY a.community_question_id `

	sql = `select count(1) total from (` + sql + `) tmp `
	err = o.Raw(sql, opType).QueryRow(&total)
	return
}

// LikeTeaseListItem 问答详情点赞/吐槽用户列表详情数据
type LikeTeaseListItem struct {
	CommunityQuestionLikeTeaseId uint64 `description:"点赞ID"`
	NickName                     string `description:"用户名称"`
	RealName                     string `description:"用户真实姓名"`
	CompanyId                    int    `description:"客户id"`
	CompanyName                  string `description:"客户名称"`
	CompanyFiccStatus            string `description:"客户ficc状态"`
	UserId                       uint64 `description:"用户id"`
	SourceAgent                  int8   `description:"留言入口来源，1：小程序，2：pc"`
	ModifyTime                   string `description:"修改时间"`
}

// GetYbCommunityQuestionLikeTeaseQuestionListById 根据问答ID，查询点赞/吐槽列表
func GetYbCommunityQuestionLikeTeaseQuestionListById(CommunityQuestionId, opType, source, startSize, pageSize int) (total int, list []*LikeTeaseListItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				community_question_like_tease_id,
				user_id,
				source_agent,
				modify_time
			FROM
				yb_community_question_like_tease
			WHERE
				community_question_id =? AND op_type =? AND enabled = 1 AND source = ? `

	totalSql := `SELECT count(1) total FROM (` + sql + `) z `
	if err = o.Raw(totalSql, CommunityQuestionId, opType, source).QueryRow(&total); err != nil {
		return
	}

	sql += ` ORDER BY modify_time DESC, community_question_like_tease_id DESC`
	sql += ` LIMIT ?,?`

	_, err = o.Raw(sql, CommunityQuestionId, opType, source, startSize, pageSize).QueryRows(&list)
	return
}

// GetYbCommunityVideoLikeTeaseTotal 根据条件查询视频点赞/吐槽 数据汇总数
func GetYbCommunityVideoLikeTeaseTotal(opType int8) (total int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				count(1) AS like_tease_num,
				a.community_question_id,
				MAX(a.modify_time) AS modify_time,
				b.title AS question_content,
				b.create_time
			FROM
				yb_community_question_like_tease a
			JOIN yb_community_video b ON a.community_question_id = b.community_video_id
			WHERE
				a.enabled = 1 AND b.is_deleted = 0 AND a.op_type = ? AND a.source = 2
			GROUP BY
				a.community_question_id `
	sql = `select count(1) total from (` + sql + `) tmp `
	err = o.Raw(sql, opType).QueryRow(&total)
	return
}

// GetYbCommunityVideoLikeTeaseList 根据条件查询点赞/吐槽相关的 视频信息
func GetYbCommunityVideoLikeTeaseList(opType int8, orderStr string, startSize, pageSize int) (list []*StatCommunityQuestionLikeTeaseItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				count(1) AS num,
				a.community_question_id,
				MAX(a.modify_time) AS modify_time,
				b.title AS question_content,
				b.create_time,
				b.variety_tag_id
			FROM
				yb_community_question_like_tease a
			JOIN yb_community_video b ON a.community_question_id = b.community_video_id
			WHERE
				a.enabled = 1 AND b.is_deleted = 0 AND a.op_type = ? AND a.source = 2 
			GROUP BY
				a.community_question_id ORDER BY `
	sql += ` ` + orderStr
	sql += ` ,modify_time desc`

	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, opType, startSize, pageSize).QueryRows(&list)
	return
}

// GetYbRoadVideoLikeTeaseTotal 根据条件查询线上路演点赞/吐槽 数据汇总数
func GetYbRoadVideoLikeTeaseTotal(opType int8) (total int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				count(1) AS like_tease_num,
				a.community_question_id,
				MAX(a.modify_time) AS modify_time,
				b.title AS question_content,
				b.create_time
			FROM
				yb_community_question_like_tease a
			JOIN yb_road_video b ON a.community_question_id = b.road_video_id
			WHERE
				a.enabled = 1 AND b.is_deleted = 0 AND a.op_type = ? AND a.source = 3
			GROUP BY
				a.community_question_id `
	sql = `select count(1) total from (` + sql + `) tmp `
	err = o.Raw(sql, opType).QueryRow(&total)
	return
}

// GetYbRoadVideoLikeTeaseList 根据条件查询点赞/吐槽相关的 线上路演信息
func GetYbRoadVideoLikeTeaseList(opType int8, orderStr string, startSize, pageSize int) (list []*StatCommunityQuestionLikeTeaseItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				count(1) AS num,
				a.community_question_id,
				MAX(a.modify_time) AS modify_time,
				b.title AS question_content,
				b.create_time,
				b.chart_permission_ids AS variety_classify_tag
			FROM
				yb_community_question_like_tease a
			JOIN yb_road_video b ON a.community_question_id = b.road_video_id
			WHERE
				a.enabled = 1 AND b.is_deleted = 0 AND a.op_type = ? AND a.source = 3 
			GROUP BY
				a.community_question_id ORDER BY `
	sql += ` ` + orderStr
	sql += ` ,modify_time desc`

	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, opType, startSize, pageSize).QueryRows(&list)
	return
}