package media

import (
	"errors"
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/models"
	"eta/eta_mini_ht_api/models/report"
	"gorm.io/gorm"
	"time"
)

const (
	CommonColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,cover_src"
	DetailColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,author_id,author_name,cover_src"
)

type MediaType string

const (
	Video MediaType = "video"
	Audio MediaType = "audio"
)

type Media struct {
	Id                    int               `gorm:"primary_key;auto_increment;column:id"`
	AuthorID              int               `gorm:"column:author_id"`
	AuthorName            string            `gorm:"column:author_name"`
	MediaType             MediaType         `gorm:"column:media_type"`
	Src                   string            `gorm:"column:src"`
	MediaName             string            `gorm:"column:media_name"`
	SourceType            string            `gorm:"column:source_type"`
	CoverSrc              string            `gorm:"column:cover_src"`
	MediaPlayMilliseconds int               `gorm:"column:media_play_milliseconds"`
	PermissionIDs         string            `gorm:"column:permission_ids"`
	PublishedTime         time.Time         `gorm:"column:published_time"`
	SendStatus            report.SendStatus `gorm:"column:send_status"`
	Deleted               int               `gorm:"column:deleted"`
	CreatedTime           time.Time         `gorm:"column:created_time"`
	UpdatedTime           time.Time         `gorm:"column:updated_time"`
}

func (m *Media) BeforeCreate(_ *gorm.DB) (err error) {
	m.PublishedTime = time.Now()
	m.CreatedTime = time.Now()
	return
}
func GetCountByMediaType(mediaType string) (count int64, latestId int64) {
	db := models.Main()
	var ids []int
	err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ?", mediaType, false).Order("id desc").Scan(&ids).Error
	if err != nil {
		return 0, 0
	}
	count = int64(len(ids))
	latestId = int64(ids[0])
	return
}

func GetCountByMediaTypeByAnalystId(mediaType string, analystId int) (count int64, latestId int64) {
	db := models.Main()
	var ids []int
	err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ? and author_id = ?", mediaType, false, analystId).Order("id desc").Scan(&ids).Error
	if err != nil {
		return 0, 0
	}
	count = int64(len(ids))
	latestId = int64(ids[0])
	return
}
func GetMediaPage(latestId int64, limit int, offset int, mediaType string) (mediaList []Media, err error) {
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where("media_type= ? and id<= ?", mediaType, latestId).Order("created_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
	return
}
func GetMediaPageByIds(latestId int64, limit int, offset int, mediaType string, mediasIds []int) (mediaList []Media, err error) {
	if len(mediasIds) == 0 {
		return GetMediaPage(latestId, limit, offset, mediaType)
	}
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where(" id<= ? and media_type= ? and id in ?", latestId, mediaType, mediasIds).Order("created_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
	return
}
func GetMediaPageByAnalystId(latestId int64, limit int, offset int, mediaType string, analystId int) (mediaList []Media, err error) {
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where(" id<= ? and media_type= ? and author_id = ?", latestId, mediaType, analystId).Order("published_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
	return
}
func GetMediaById(mediaType string, mediaId int) (media Media, err error) {
	db := models.Main()
	err = db.Select(DetailColumns).Where("id =? and media_type=?", mediaId, mediaType).First(&media).Error
	return
}

// 测试造数据
func GetlIST() (media []Media) {
	db := models.Main()
	_ = db.Model(&Media{}).Where("deleted = ?", false).Find(&media).Error
	return
}
