package dao

type ProgramRelation struct {
	ID        int    `gorm:"primary_key" json:"id"`               //编号
	ProgramId int    `gorm:"type:int" json:"programId"`           //节目单ID
	LibraryId int    `gorm:"type:int" json:"libraryId"`           //素材ID
	Duration  int    `gorm:"type:int" json:"duration"`            //时长
	OrderNo   int    `gorm:"type:int" json:"orderNo"`             //排序位置
	TenantId  string `gorm:"type:varchar(12)" json:"tenantId"`    //租户ID
	IsDeleted int    `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
}

func (ProgramRelation) TableName() string {
	return "media_program_relation"
}

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

func (c *ProgramRelation) Delete() error {
	return Db.Debug().Model(&c).Where("program_id = ?", c.ProgramId).Updates(map[string]interface{}{"is_deleted": c.
		IsDeleted}).Error
}

func (c *ProgramRelation) GetByProgram() ([]ProgramRelation, error) {
	var relations []ProgramRelation
	err := Db.Debug().Model(&c).Where("tenant_id = ? and is_deleted = 0 and program_id = ?", c.TenantId,
		c.ProgramId).Order("order_no").Find(&relations).Error
	return relations, err
}

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

func (c *ProgramRelation) Remove() error {
	return Db.Debug().Model(&c).Where("program_id = ?", c.ProgramId).Delete(c).Error
}
