package yb_road_video

import (
	"hongze/hongze_yb/global"
)

func (r *YbRoadVideo) Create() (err error) {
	err = global.DEFAULT_MYSQL.Create(r).Error
	return
}

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

// GetPageListByCondition 分页获取视频列表
func GetPageListByCondition(where string, par []interface{}, pageIndex, pageSize int) (list []*YbRoadVideo, total int64, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(YbRoadVideo{}).
		Where(where, par...).Count(&total).Error
	if err != nil {
		return
	}
	err = global.DEFAULT_MYSQL.Model(YbRoadVideo{}).
		Where(where, par...).
		Offset(offset).Limit(pageSize).
		Order("publish_time DESC").Scan(&list).Error

	return
}

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

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

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

// GetItemByReportId 报告ID获取已绑定的路演视频
func GetItemByReportId(reportId int) (item *YbRoadVideo, err error) {
	err = global.DEFAULT_MYSQL.Model(YbRoadVideo{}).
		Where("report_id = ? AND is_deleted = 0 AND publish_state = 1", reportId).
		First(&item).Error
	return
}
