package dao

import (
	"time"
)

type Program struct {
	ID          int       `gorm:"primary_key" json:"id"`                 //编号
	Name        string    `gorm:"type:varchar(64)" json:"name"`          //名称
	Resolution  int       `gorm:"type:int" json:"resolution"`            //分辨率 枚举编号
	Duration    int64     `gorm:"type:bigint" json:"duration"`           //统计总时长
	FileSize    int64     `gorm:"type:bigint" json:"fileSize"`           //统计文件总大小
	ImgDuration int       `gorm:"type:int;default 0" json:"imgDuration"` //图片播放时长(毫秒)
	Remarks     string    `gorm:"type:varchar(4000)" json:"remarks"`     //备注
	SysType     int       `gorm:"type:int" json:"sysTrecordype"`         //系统类型：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 (Program) TableName() string {
	return "media_program"
}

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

func (c *Program) Remove() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Delete(c).Error
}

func (c *Program) GetPrograms(offset, limit int) ([]Program, int64, error) {
	var Programs []Program
	var count int64
	db := Db.Debug().Model(&c)
	if c.Name != "" {
		db = db.Where("name like ?", "%"+c.Name+"%")
	}
	if c.SysType == 0 || c.SysType == 1 {
		db = db.Where("sys_type = ?", c.SysType)
	}

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

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

func (c *Program) 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 *Program) GetAll() ([]Program, error) {
	var Programs []Program
	db := Db.Debug().Model(&c).Where("tenant_id = ?", c.TenantId)
	if c.Name != "" {
		db = db.Where("name like ?", "%"+c.Name+"%")
	}

	err := db.Find(&Programs).Order("create_time").Error
	return Programs, err
}

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