qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
}