package models import ( "basic.com/valib/bhomeclient.git" "basic.com/valib/logger.git" "strconv" "strings" ) type FileStack struct { Id string `gorm:"column:id;primary_key" json:"id"` Name string `gorm:"column:name" json:"name"` Type int `gorm:"column:type" json:"type"` Enable bool `gorm:"column:enable" json:"enable"` IsAutoDelFile bool `gorm:"column:isAutoDelFile" json:"isAutoDelFile"` Status int `gorm:"column:status;default:1" json:"status"` CreateTime string `gorm:"column:createTime" json:"createTime"` UpdateTime string `gorm:"column:updateTime" json:"updateTime"` Sort int `gorm:"column:sort" json:"sort"` ResolutionWidth int `gorm:"column:resolution_width;default:0" json:"resolution_width"`//分辨率宽 ResolutionHeight int `gorm:"column:resolution_height;default:0" json:"resolution_height"`//分辨率高 } const ( Id_Stack_Pre = "stack_" Stack_Status_NoRule = 0 //未配规则 Stack_Status_Wait = 1 //等待处理 Stack_Status_Doing = 2 //处理中 Stack_Status_Done = 9 //处理完成 ) func (FileStack) TableName() string { return "file_stack" } func (fs *FileStack) Total(name string, typ int, filterCIds *string) int { var total int sql := "select count(1) from file_stack where 1=1" if name !="" { sql += " and name like '%"+name+"%'" } if typ > 0 { if typ == 1 { //1.查处理完成 sql += " and status="+strconv.Itoa(bhomeclient.Stack_Status_Done) } else if typ == 2 { //2.查处理中 sql += " and status="+strconv.Itoa(bhomeclient.Stack_Status_Doing) } else if typ == 3 { //3.查未配规则 if *filterCIds != "" { sql += " and id not in ("+*filterCIds+")" } } else if typ == 4 { //4.查未开启 sql += " and enable=0" } } err := db.Raw(sql).Count(&total).Error if err !=nil { total = 0 } return total } func (fs *FileStack) FindAllByPage(typ int,name string, page int, size int, filterCIds *string) (list []FileStack, err error) { sql := "select * from file_stack where 1=1" if name != "" { sql += " and name like '%"+name+"%'" } if typ > 0 { if typ == 1 { //1.查处理完成 sql += " and status="+strconv.Itoa(bhomeclient.Stack_Status_Done) } else if typ == 2 { //2.查处理中 sql += " and status="+strconv.Itoa(bhomeclient.Stack_Status_Doing) } else if typ == 3 { //3.查未配规则 if *filterCIds != "" { sql += " and id not in ("+*filterCIds+")" } } else if typ == 4 { //4.查未开启 sql += " and enable=0" } } offset := (page-1)*size err = db.Raw(sql).Offset(offset).Limit(size).Find(&list).Error return list,nil } func (fs *FileStack) FindAll() (list []FileStack, err error) { err = db.Raw("select * from file_stack").Find(&list).Error return } func (fs *FileStack) SelectById(id string) (rows int64, err error) { result := db.Table(fs.TableName()).Where("id = ?", id).First(&fs) if result.Error != nil || result.RowsAffected == 0 { return 0, err } return result.RowsAffected, nil } func (fs *FileStack) DeleteById(id string) bool { result := db.Exec("delete from file_stack where id='"+id+"'") if result.Error != nil { return false } if result.RowsAffected > 0 { db.Exec("delete from file_analysis where stack_id='"+id+"'") return true } return false } func (fs *FileStack) UpdateEnable(id string, enable bool) bool { logger.Debug("models.FileStack UpdateEnable id:",id,"enable:",enable) str := "0" if enable { str = "1" } sql := "update file_stack set enable ="+str+" where id='"+id+"'" result := db.Exec(sql) if result.Error != nil { return false } if result.RowsAffected > 0 { return true } else { return false } } func (fs *FileStack) Add() bool { result := db.Table(fs.TableName()).Create(&fs) if result.Error ==nil && result.RowsAffected >0 { return true } return false } func (fs *FileStack) Update() bool { result := db.Table(fs.TableName()).Where("id=?",fs.Id).Update(&fs) if result.Error ==nil && result.RowsAffected >0 { return true } return false } //ids为正在做的数据栈列表 func (fs *FileStack) UpdateStatus(ids []string, status int) bool { idStr := "" for _,id := range ids { idStr += "'"+id+"'," } idStr = strings.TrimRight(idStr, ",") if idStr == "" { return false } result := db.Exec("update file_stack set status=? where id in ("+idStr+")", status) if result.Error ==nil && result.RowsAffected > 0 { return true } return false } func (fs *FileStack) HavingWorkFiles(stackId string) bool { var count int err := db.Raw("select count(1) as count from file_analysis where stack_id=? and (status="+strconv.Itoa(bhomeclient.File_Status_Doing)+" or status="+strconv.Itoa(bhomeclient.File_Status_Wait)+")", stackId).Count(&count).Error if err !=nil { return false } return count > 0 }