package voice_broadcast

import "hongze/hongze_yb/global"

func GetBroadcastByCondition(pageIndex, pageSize, sectionId int) (list []*VoiceBroadcast, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).Where("section_id=?", sectionId).Offset(offset).Limit(pageSize).Order("create_time DESC").Scan(&list).Error
	return
}

func GetBroadcastByIdAndPage(pageIndex, pageSize, broadcastId int) (list []*VoiceBroadcast, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).Where("broadcast_id=?", broadcastId).Offset(offset).Limit(pageSize).Order("create_time DESC").Scan(&list).Error
	return
}

func GetBroadcast(pageIndex, pageSize int) (list []*VoiceBroadcast, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).Offset(offset).Limit(pageSize).Order("create_time DESC").Scan(&list).Error
	return
}

func GetBroadcastById(broadcastId int) (item *VoiceBroadcast, err error) {
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).Where("broadcast_id=?", broadcastId).First(&item).Error
	return
}

func GetBroadcastList() (list []*VoiceBroadcast, err error) {
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).Scan(&list).Error
	return
}

func GetPageListByCondition(condition string, pars []interface{}, pageIndex, pageSize int) (list []*VoiceBroadcast, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).
		Where(condition, pars...).
		Offset(offset).Limit(pageSize).
		Order("create_time DESC").
		Scan(&list).Error
	return
}

type VoiceBroadcastListStatusCount struct {
	Num   int `json:"num" description:"计数"`
	State int `json:"state" description:"状态：0-未发布 1-已发布"`
}

// GetVoiceBroadcastListStatusCount 获取语音播报列表计数-根据状态分组
func GetVoiceBroadcastListStatusCount(condition string, pars []interface{}) (list []*VoiceBroadcastListStatusCount, err error) {
	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).
		Where(condition, pars...).
		Select("COUNT(1) AS num, publish_state AS state").
		Group("state").
		Scan(&list).Error
	return
}
