package dao import ( "gorm.io/gorm" "time" ) type Video struct { gorm.Model CameraID int `gorm:"column:camera_id"` VideoPath string `gorm:"column:video_path"` VideoName string `gorm:"column:video_name"` AuthId int `gorm:"column:auth_id"` Human int `gorm:"column:human"` IsDelete int `gorm:"column:isdelete"` CreateTime string `gorm:"column:create_time"` EndTime string `gorm:"column:end_time"` DeleteTime string `gorm:"column:delete_time"` FileSize int `gorm:"column:file_size"` } func FindWillDelVideoList(id int) []Video { var videos []Video DB.Debug().Where("auth_id = ?", id).Where("delete_time<=now()").Where("isdelete=0").Find(&videos) return videos } func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete int, createTime, endTime, deleteTime string, fileSize int) uint { video := Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, DeleteTime: deleteTime, FileSize: fileSize} res := DB.Debug().Create(&video) if res.Error != nil { return 0 } if deleteTime == "" { DB.Debug().Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=?", video.ID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) } return video.ID } func DeleteVideoByID(id, user int) int { delete_time := time.Now().Format("2006-01-02 15:04:05") DB.Debug().Where("id = ? and auth_id = ?", id, user).Updates(&Video{DeleteTime: delete_time, IsDelete: 1}) DB.Debug().Where("id = ? and auth_id = ?", id, user).Delete(&Video{}) return id } func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human, isDelete int, createTime, endTime string, fileSize int) bool { res := DB.Debug().Model(&Video{}).Where("id = ? and auth_id = ?", videoID, authID).Updates(Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize}) if res.Error != nil { return false } res = DB.Debug().Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=? and auth_id=?", videoID, authID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) if res.Error != nil { return false } return true } func FindVideoByID(id, auth_id int) Video { var video Video DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).Where("isdelete = ?", 0).First(&video) return video } // 根据用户id查找视频列表,返回最新30条 func FindVideoListsByAuthID(auth_id int) []Video { var videos []Video DB.Debug().Where("auth_id = ? and isdelete =? ", auth_id, 0).Order("create_time DESC").Limit(30).Find(&videos) return videos } func FindVideoListByTime(auth_id int, startTime, endTime string) []Video { var videos []Video DB.Debug().Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos) return videos } // id 为视频id,auth_id为用户id,day为延长天数,返回修改的行数 func DelayVideo(id, auth_id, day int) int { res := DB.Debug().Exec("update videos set delete_time = date_add(delete_time, interval ? day) where id = ? and auth_id = ? and isdelete=0", day, id, auth_id) if res.Error != nil { return 0 } return int(res.RowsAffected) } // id 为用户id,day为延长天数,返回修改的行数 func DelayAllVideo(id, day int) int { res := DB.Debug().Exec("update videos set delete_time = date_add(delete_time, interval ? day) where auth_id = ? and isdelete = 0", day, id) if res.Error != nil { return 0 } return int(res.RowsAffected) }