package models
|
|
import (
|
"basic.com/valib/bhomeclient.git"
|
"strconv"
|
"strings"
|
)
|
|
//本地文件处理
|
type FileAnalysis struct {
|
Id string `gorm:"primary_key;column:id" json:"id"`
|
Name string `gorm:"column:name" json:"name"`
|
Path string `gorm:"column:path" json:"path"`
|
Type int `gorm:"column:type;default:1" json:"type"`//类型:0:本地图片,1:本地视频,2:本地语音
|
Sort int `gorm:"column:sort;default:1" json:"sort"`//排序
|
Alias string `gorm:"column:alias" json:"alias"` //摄像机的别名
|
SnapshotUrl string `gorm:"column:snapshot_url" json:"snapshot_url"`//快照地址
|
Duration string `gorm:"column:duration" json:"duration"`//时长
|
IsRunning bool `gorm:"column:is_running" json:"is_running"`//是否正在解码
|
Progress int `gorm:"column:progress;default:0" json:"progress"`//进度
|
RuleType int `gorm:"column:ruleType;default:0" json:"ruleType"`//规则类型(0:独有规则,1:公共规则)
|
Status int `gorm:"column:status;default:1" json:"status"`//状态(-1:已删除,0:暂停,1:等待 2:处理中 9:已完成)
|
CreateTime string `gorm:"column:createTime" json:"createTime"`
|
UpdateTime string `gorm:"column:updateTime" json:"updateTime"`
|
Identifier string `gorm:"column:identifier" json:"identifier"` //整个文件的唯一标识,md5
|
StackId string `gorm:"column:stack_id" json:"stack_id"` //所属数据栈的id
|
Size int64 `gorm:"column:size" json:"size"`
|
}
|
|
func (FileAnalysis) TableName() string {
|
return "file_analysis"
|
}
|
|
func IsStack(id string) bool {
|
if strings.HasPrefix(id, bhomeclient.Id_Stack_Pre) {
|
return true
|
}
|
return false
|
}
|
|
func (fa *FileAnalysis) ExistByPath(path string) (flag bool,file FileAnalysis) {
|
foundResult := db.Table(fa.TableName()).Where("path=?", path).First(&file)
|
if foundResult.Error !=nil {
|
return false,file
|
}
|
if foundResult.RecordNotFound() {
|
return false,file
|
} else {
|
return true,file
|
}
|
}
|
|
func (fa *FileAnalysis) SelectById(id string) (rows int64, err error) {
|
result := db.Table(fa.TableName()).Where("id = ?", id).First(&fa)
|
if result.Error != nil || result.RowsAffected == 0 {
|
return 0, err
|
}
|
return result.RowsAffected, nil
|
}
|
|
func (fa *FileAnalysis) FindByName(name string) (list []FileAnalysis, err error) {
|
|
return list,err
|
}
|
|
func (fa *FileAnalysis) FindBySort(sort int) (rows int64, err error) {
|
result := db.Table(fa.TableName()).Where("sort = ?", sort).First(&fa)
|
if result.Error != nil || result.RowsAffected == 0 {
|
return 0, err
|
}
|
return result.RowsAffected, nil
|
}
|
|
func (fa *FileAnalysis) GetMaxSortFile(stackId string) (rows int64, err error) {
|
result := db.Table(fa.TableName()).Where("stack_id=?", stackId).Order("sort desc").First(&fa)
|
if result.Error != nil || result.RowsAffected == 0 {
|
return 0, err
|
}
|
return result.RowsAffected, nil
|
}
|
|
func (fa *FileAnalysis) Add() bool {
|
|
result := db.Table(fa.TableName()).Create(&fa)
|
if result.Error ==nil && result.RowsAffected >0 {
|
return true
|
}
|
return false
|
}
|
|
func (fa *FileAnalysis) Total(fileName string, fType int, stackId string) int {
|
var total int
|
sql := "select count(1) from file_analysis f where f.status !=-1"
|
if stackId != "" {
|
sql += " and f.stack_id='"+stackId+"'"
|
}
|
if fileName !="" {
|
sql += " and f.name like '%"+fileName+"%'"
|
}
|
if fType == 0 { //全部视频
|
|
} else if fType == 1 { //等待处理
|
sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Wait)
|
} else if fType == 2 { //处理中
|
sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Doing)
|
} else if fType == 3 {
|
sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Complete)
|
} else if fType == 4 {
|
sql += " and f.status=-2"
|
}
|
|
err := db.Raw(sql).Count(&total).Error
|
if err !=nil {
|
total = 0
|
}
|
return total
|
}
|
|
func (fa *FileAnalysis) FindAllByPage(fileName string, fType int, page int, size int, stackId string) (arr []FileAnalysis, err error) {
|
sql := "select * from file_analysis f where f.status !=-1"
|
if stackId != "" {
|
sql += " and f.stack_id='"+stackId+"'"
|
}
|
if fileName !="" {
|
sql += " and f.name like '%"+fileName+"%'"
|
}
|
if fType == 0 { //全部视频
|
|
} else if fType == 1 { //处理完成
|
sql += " and f.status=9"
|
} else if fType == 2 { //处理中
|
sql += " and f.status=3"
|
} else if fType == 3 {
|
sql += " and f.status=-2"
|
}
|
offset := (page-1)*size
|
err = db.Raw(sql).Offset(offset).Limit(size).Order("sort asc").Find(&arr).Error
|
return arr,err
|
}
|
|
func (fa *FileAnalysis) FindAll(stackId string, fileName string) (arr []FileAnalysis, err error) {
|
sql := "select * from file_analysis where status !=-1"
|
if fileName !="" {
|
sql += " and name like '%"+fileName+"%'"
|
}
|
if stackId != "" {
|
sql += " and stack_id='"+stackId+"'"
|
}
|
|
err = db.Raw(sql).Order("sort asc").Find(&arr).Error
|
return arr,err
|
}
|
|
func (fa *FileAnalysis) FindByStackId(stackId string, fType int, name string, page int, size int) (arr []FileAnalysis, err error) {
|
sql := "select * from file_analysis f where f.stack_id='"+stackId+"' and f.status !=-1"
|
if name != "" {
|
sql += " and f.name like '%"+name+"%'"
|
}
|
if fType == 0 { //全部视频
|
|
} else if fType == 1 { //待处理
|
sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Wait)
|
} else if fType == 2 { //处理中
|
sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Doing)
|
} else if fType == 3 { //处理完成
|
sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Complete)
|
} else if fType == 4 {//处理失败
|
sql += " and f.status=-2"
|
}
|
offset := (page-1)*size
|
err = db.Raw(sql).Offset(offset).Limit(size).Order("sort asc").Find(&arr).Error
|
|
return arr,err
|
}
|
|
func (fa *FileAnalysis) FindAnalysisFiles() (arr []FileAnalysis, err error) {
|
sql := "select * from file_analysis where status =1"
|
err = db.Raw(sql).Order("sort asc").Find(&arr).Error
|
return arr,err
|
}
|
|
func (fa *FileAnalysis) UpdateStatus(id string,status int) bool {
|
result := db.Exec("update file_analysis set status=? where id=?",status,id)
|
if result.Error ==nil && result.RowsAffected >0 {
|
return true
|
}
|
return false
|
}
|
|
func (fa *FileAnalysis) UpdateIsRunning(id string, isRunning bool) bool {
|
isRunningStr := "0"
|
if isRunning {
|
isRunningStr = "1"
|
}
|
result := db.Exec("update file_analysis set is_running="+isRunningStr+" where id='"+id+"'")
|
if result.Error !=nil {
|
return false
|
}
|
return result.RowsAffected>0
|
}
|
|
func (fa *FileAnalysis) UpdateProgress(ids []string,progress int) bool {
|
idStr := ""
|
for _,id := range ids {
|
idStr += "'"+id+"',"
|
}
|
idStr = strings.Trim(idStr, ",")
|
if idStr != "" {
|
result := db.Exec("update file_analysis set status=? where id in ("+idStr+")",progress)
|
if result.Error ==nil && result.RowsAffected >0 {
|
return true
|
}
|
}
|
|
return false
|
}
|
|
func (fa *FileAnalysis) Rename(id string, name string) bool {
|
result := db.Exec("update file_analysis set name=? where id=?",name, id)
|
if result.Error ==nil && result.RowsAffected >0 {
|
return true
|
}
|
return false
|
}
|
|
|
func (fa *FileAnalysis) Move(id string, stackId string) bool {
|
result := db.Exec("update file_analysis set stack_id=? where id=?",stackId, id)
|
if result.Error ==nil && result.RowsAffected >0 {
|
return true
|
}
|
return false
|
}
|