package yb_community_question_comment

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

// GetByCommunityQuestionCommentId 根据留言ID,查询留言
func GetByCommunityQuestionCommentId(communityQuestionCommentId uint64) (item *YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Where("community_question_comment_id =  ? ", communityQuestionCommentId).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetListByUserIdCommunityQuestionID 获取用户的留言列表
func GetListByUserIdCommunityQuestionID(userId uint64, communityQuestionID, source int) (list []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("user_id = ? AND community_question_id = ? AND enabled = 1 AND source = ?", userId, communityQuestionID, source).
		Order("create_time desc, community_question_comment_id desc").
		Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetListTotalByUserIdCommunityQuestionID 获取用户的留言总条数
func GetListTotalByUserIdCommunityQuestionID(userId uint64, communityQuestionID, source int) (total int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("user_id = ? AND community_question_id = ? AND enabled = 1 AND source = ?", userId, communityQuestionID, source).
		Count(&total).Error
	return
}

// GetHotListByCommunityQuestionID 获取报告的精选留言列表(2022-08-23修改为获取所有留言, 不再精选)
func GetHotListByCommunityQuestionID(communityQuestionID int, offset, limit, source int) (list []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("community_question_id = ? AND enabled = 1 AND type = 1 AND source = ?", communityQuestionID, source).
		//Order("is_top desc, hot_top_time desc, community_question_comment_id desc").
		Order("create_time DESC").
		Offset(offset).
		Limit(limit).
		Scan(&list).Error
	return
}

// GetHotListTotalByCommunityQuestionID 获取精选留言总条数(2022-08-23修改为获取所有留言, 不再精选)
func GetHotListTotalByCommunityQuestionID(communityQuestionID, source int) (total int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("community_question_id = ? AND enabled = 1 AND type = 1 AND source = ?", communityQuestionID, source).
		Count(&total).Error
	return
}

// GetReplyListByReplyCommentId 获取报告的精选留言的回复列表
func GetReplyListByReplyCommentId(replyCommentIds []uint64) (list []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("enabled = 1 and type = 2 and reply_comment_id in ?", replyCommentIds).
		Order("community_question_comment_id asc").
		Scan(&list).Error
	return
}

type SimpleYbCommunityQuestionComment struct {
	CommunityQuestionCommentID uint64 `gorm:"primaryKey;column:community_question_comment_id;type:bigint(20) unsigned;not null" json:"-"`
	UserID                     uint64 `gorm:"column:user_id;type:bigint(20) unsigned;not null;default:0" json:"userId"` // 用户id
	Type                       int8   `gorm:"column:type;type:tinyint(1);not null;default:1" json:"type"`               // 留言类型 1-评论 2-回复
	Enabled                    int8   `gorm:"column:enabled;type:tinyint(1);not null;default:1" json:"enabled"`         // 是否有效, 0-无效留言 1-有效留言
}

// GetSimpleByCommentId 根据留言ID,查询留言
func GetSimpleByCommentId(commentId uint64) (item *SimpleYbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Select("community_question_comment_id, user_id, type, enabled").
		Where("community_question_comment_id = ?", commentId).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetMyLatestComment 获取用户最新的留言
func GetMyLatestComment(userId uint64) (item *YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Select("comment_id, user_id, type, enabled, is_show_name").
		Where("user_id = ? and type=1", userId).Order("community_question_comment_id desc").First(&item).Error
	return
}

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

// GetNumCommentByCommunityQuestionIds 获取用户留言的汇总数
func GetNumCommentByCommunityQuestionIds(communityQuestionIds []uint32, userId uint64) (item []*NumCommentByCommunityQuestion, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Select("community_question_id, count(1) total").
		Where("community_question_id in (?) and type=1 and enabled = 1 and (is_hot = 1 or user_id=?) ", communityQuestionIds, userId).Group("community_question_id").Scan(&item).Error
	return
}

// GetLastHotListByCommunityQuestionIds 根据问答id列表获取最近精选的留言
func GetLastHotListByCommunityQuestionIds(communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE community_question_id IN (?) AND enabled = 1 AND is_hot = 1 AND type = 1 GROUP BY community_question_id)", communityQuestionIds).
		Find(&items).Error
	return
}

// GetLastMyListByCommunityQuestionIds 根据问答id列表获取我最近的留言
func GetLastMyListByCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE user_id=? AND community_question_id IN (?) AND enabled = 1 AND type = 1 GROUP BY community_question_id)", userId, communityQuestionIds).
		Find(&items).Error
	return
}

type UserAvatarRandom struct {
	AvatarUrl string
}

func GetUserAvatarRandom() (item *UserAvatarRandom, err error) {
	sql := ` select * from user_avatar_random order by rand() limit 1 `
	err = global.DEFAULT_MYSQL.Raw(sql).Scan(&item).Error
	return
}

// GetHotListByCommunityQuestionIds 根据问答id列表获取精选留言-精选时间倒序(2022-0823不再精选,获取所有评论)
func GetHotListByCommunityQuestionIds(communityQuestionIds []uint32, source int) (items []*YbCommunityQuestionComment, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
		Where("community_question_id IN (?) AND enabled = 1 AND type = 1 AND source = ?", communityQuestionIds, source).
		Order("hot_time DESC").
		Find(&items).Error
	return
}