package yb_community_question_like_tease

import (
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
)

// GetByUserIdAndCommunityQuestionId 根据用户和问答id获取点赞/吐槽记录
func GetByUserIdAndCommunityQuestionId(userId uint64, communityQuestionId uint32, source int8) (item *YbCommunityQuestionLikeTease, err error) {
	err = global.DEFAULT_MYSQL.
		Where("user_id = ? AND community_question_id = ? AND source = ?", userId, communityQuestionId, source).
		First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetLikeNumByCommunityQuestionId 获取某个问答的点赞数
func GetLikeNumByCommunityQuestionId(communityQuestionId uint32, source int) (num int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
		Where(" community_question_id = ? AND enabled=1 AND op_type = 1 AND source = ?", communityQuestionId, source).
		Count(&num).Error
	return
}

// GetTeaseNumByCommunityQuestionId 获取某个问答的吐槽数
func GetTeaseNumByCommunityQuestionId(communityQuestionId uint32, source int) (num int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
		Where(" community_question_id = ? AND enabled=1 AND op_type = 2 AND source = ?", communityQuestionId, source).
		Count(&num).Error
	return
}

// NumCommentByCommunityQuestionIds 评论统计结构体
type NumCommentByCommunityQuestionIds struct {
	CommunityQuestionID uint32 `json:"community_question_id"`
	Total               int    `json:"total"`
}

// GetLikeNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的点赞数
func GetLikeNumCommentByCommunityQuestionIds(communityQuestionIds []uint32, source int) (items []*NumCommentByCommunityQuestionIds, err error) {
	if len(communityQuestionIds) <= 0 {
		return
	}
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
		Select("community_question_id, count(1) total").
		Where("community_question_id in (?) AND enabled=1  AND op_type=1 AND source = ?", communityQuestionIds, source).
		Group("community_question_id").Scan(&items).Error
	return
}

// GetTeaseNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的吐槽数
func GetTeaseNumCommentByCommunityQuestionIds(communityQuestionIds []uint32, source int) (items []*NumCommentByCommunityQuestionIds, err error) {
	if len(communityQuestionIds) <= 0 {
		return
	}
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
		Select("community_question_id, count(1) total").
		Where("community_question_id in (?) AND enabled=1  AND op_type=2 AND source = ?", communityQuestionIds, source).
		Group("community_question_id").Scan(&items).Error
	return
}

// GetByUserIdAndCommunityQuestionIds 根据用户id和问答id列表获取成功点赞/吐槽记录
func GetByUserIdAndCommunityQuestionIds(userId uint64, communityQuestionIds []uint32, source int) (items []*YbCommunityQuestionLikeTease, err error) {
	if len(communityQuestionIds) <= 0 {
		return
	}
	err = global.DEFAULT_MYSQL.
		Where("user_id = ? AND enabled=1 AND source = ? AND community_question_id in (?)", userId, source, communityQuestionIds).Group("community_question_id").
		Find(&items).Error
	return
}
