package yb_community_question

import (
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/tables/yb_community_question_audio"
	"time"
)

func (item *YbCommunityQuestion) Create() (err error) {
	err = global.DEFAULT_MYSQL.Create(item).Error
	return
}

func (item *YbCommunityQuestion) Update(updateCols []string) (err error) {
	err = global.DEFAULT_MYSQL.Model(item).Select(updateCols).Updates(*item).Error
	return
}

// GetPageListByCondition 获取问答列表-分页
func GetPageListByCondition(condition string, pars []interface{}, pageIndex, pageSize int) (list []*YbCommunityQuestion, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).Where(condition, pars...).Offset(offset).Limit(pageSize).Order("create_time DESC").Scan(&list).Error
	return
}

// GetItemById 主键获取问答信息
func GetItemById(questionId int) (item *YbCommunityQuestion, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
		Where("community_question_id = ? AND is_deleted = 0", questionId).
		First(&item).Error
	return
}

// UpdateQuestionAndAudioList 更新问答及音频
func UpdateQuestionAndAudioList(item *YbCommunityQuestion, updateCols []string, audioList []*yb_community_question_audio.YbCommunityQuestionAudio) (err error) {
	tx := global.DEFAULT_MYSQL.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	if err = tx.Model(item).Select(updateCols).Updates(*item).Error; err != nil {
		return
	}
	if err = tx.Where("community_question_id = ?", item.CommunityQuestionID).
		Delete(yb_community_question_audio.YbCommunityQuestionAudio{}).Error; err != nil {
		return
	}
	if err = tx.Model(yb_community_question_audio.YbCommunityQuestionAudio{}).
		CreateInBatches(audioList, len(audioList)).Error; err != nil {
	}
	return
}

type QuestionListCount struct {
	ReplierUserId uint64 `json:"replier_user_id"`
	UserId        uint64 `json:"user_id"`
	ReplyStatus   int    `json:"reply_status"`
	Total         int    `json:"total"`
}

// GetQuestionListCount 获取问答数量统计
func GetQuestionListCount(condition string, pars []interface{}) (list []*QuestionListCount, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
		Select("reply_status, replier_user_id, user_id, COUNT(1) total").
		Where(condition, pars...).
		Group("reply_status, replier_user_id, user_id").
		Scan(&list).Error
	return
}

// GetUnreadNum 获取未读数
func GetUnreadNum(condition string, pars []interface{}) (num int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).Where(condition, pars...).Count(&num).Error
	return
}

// UpdateReplierRead 更新回复人已读
func UpdateReplierRead(replierUserId int, questionIds []int) (err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
		Where("replier_user_id = ? AND community_question_id IN (?) AND replier_is_read=0", replierUserId, questionIds).
		Updates(YbCommunityQuestion{
			ReplierIsRead: 1,
			ModifyTime:    time.Now().Local(),
		}).Error
	return
}

// UpdateUserRead 更新用户已读
func UpdateUserRead(userId int, questionIds []int) (err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
		Where("user_id = ? AND community_question_id IN (?) AND is_read=0", userId, questionIds).
		Updates(YbCommunityQuestion{
			IsRead:     1,
			ModifyTime: time.Now().Local(),
		}).Error
	return
}
