package dao

import (
	"time"
)

type Library struct {
	ID              int       `gorm:"primary_key" json:"id"`                      //编号
	MaterialAddress string    `gorm:"type:varchar(10000)" json:"materialAddress"` //素材地址
	LibName         string    `gorm:"type:varchar(64)" json:"libName"`            //素材名称
	LibExplain      string    `gorm:"type:varchar(255)" json:"libExplain"`        //素材说明
	LibType         int       `gorm:"type:int" json:"libType"`                    //素材类型：1-视频,2-图片,3-文字,4-音频
	Resolution      string    `gorm:"type:varchar(64)" json:"resolution"`         //分辨率
	FileSize        int64     `gorm:"type:bigint" json:"fileSize"`                //文件大小
	LibDuration     int       `gorm:"type:int;default 0" json:"libDuration"`      //获取视频时长
	FileId          string    `gorm:"type:varchar(255)" json:"fileId"`            //文件ID，系统类型为广播系统时使用
	TalkSpeed       int       `gorm:"type:int;default 0" json:"talkSpeed"`        //语速(用于文字广播)：0-正常,1-快一些,2-很快
	SysType         int       `gorm:"type:int" json:"sysType"`                    //系统类型：0-信息发布系统，1-广播系统
	TenantId        string    `gorm:"type:varchar(12)" json:"tenantId"`           //租户ID
	CreateTime      time.Time `gorm:"type:datetime" json:"createTime"`            //新增时间
	CreateUser      int64     `gorm:"type:bigint" json:"createUser"`              //新增记录操作用户ID
	UpdateTime      time.Time `gorm:"type:datetime" json:"updateTime"`            //修改时间
	UpdateUser      int64     `gorm:"type:bigint" json:"updateUser"`              //修改用户
	IsDeleted       int       `gorm:"type:int;default 0" json:"isDeleted"`        //是否删除 0=未删除,1=删除
}

func (Library) TableName() string {
	return "media_library"
}

func (c *Library) Create() error {
	return Db.Debug().Model(&c).Save(c).Error
}

func (c *Library) GetLibraries(offset, limit int) ([]Library, int64, error) {
	var libraries []Library
	var count int64
	db := Db.Debug().Model(&c)
	if c.LibName != "" {
		db = db.Where("lib_name like ?", "%"+c.LibName+"%")
	}
	if c.SysType == 0 || c.SysType == 1 {
		db = db.Where("sys_type = ?", c.SysType)
	}
	if c.LibType >= 1 {
		db = db.Where("lib_type = ?", c.LibType)
	}

	db = db.Where("is_deleted = 0 and tenant_id = ?", c.TenantId)
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&libraries).Error
	return libraries, count, err
}

func (c *Library) Get() error {
	return Db.Debug().Model(&c).Where("is_deleted = 0 and id = ?", c.ID).Find(&c).Error
}

func (c *Library) Delete() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
}

func (c *Library) GetAll() ([]Library, error) {
	var libraries []Library
	db := Db.Debug().Model(&c).Where("is_deleted=0 and tenant_id = ?", c.TenantId)
	if c.LibName != "" {
		db = db.Where("lib_name like ?", "%"+c.LibName+"%")
	}
	if c.LibType > 0 {
		db = db.Where("lib_type = ?", c.LibType)
	}
	if c.SysType == 0 || c.SysType == 1 {
		db = db.Where("sys_type = ?", c.SysType)
	}
	err := db.Find(&libraries).Order("create_time").Error
	return libraries, err
}

func (c *Library) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *Library) GetLibrariesByIds(ids []int) ([]Library, error) {
	var libraries []Library
	//err := Db.Debug().Model(&c).Where("is_deleted = 0 and tenant_id = ? and id in (?)",
	err := Db.Debug().Model(&c).Where("tenant_id = ? and id in (?)",
		c.TenantId, ids).Find(&libraries).Error
	return libraries, err
}
