package yb_community_video

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

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

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

// GetPageListByCondition 分页获取视频列表
func GetPageListByCondition(where map[string]interface{}, pageIndex, pageSize int) (list []*YbCommunityVideo, err error) {
	cond, vals, e := utils.WhereBuild(where)
	if e != nil {
		err = errors.New("系统异常，生成查询语句失败")
		return
	}
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(YbCommunityVideo{}).
		Where(cond, vals...).
		Where("is_deleted = 0 AND publish_state = 1").
		Offset(offset).Limit(pageSize).
		Order("publish_time DESC").Scan(&list).Error
	return
}

// GetItemById 主键获取已发布的视频
func GetItemById(videoId int) (item *YbCommunityVideo, err error) {
	err = global.DEFAULT_MYSQL.Model(YbCommunityVideo{}).
		Where("community_video_id = ? AND is_deleted = 0 AND publish_state = 1", videoId).
		First(&item).Error
	return
}

// GetListByVideoIds 根据视频IDs获取列表
func GetListByVideoIds(videoIds []int) (list []*YbCommunityVideo, err error) {
	var where string
	where = `publish_state = 1 AND is_deleted = 0`
	if len(videoIds) > 0 {
		where += ` AND community_video_id IN (?)`
	}

	err = global.DEFAULT_MYSQL.Model(YbCommunityVideo{}).
		Where(where, videoIds).
		Order("community_video_id asc").
		Scan(&list).Error
	return
}