qixiaoning
2025-07-18 24f44f6ecefb5e83295bab670533529c6bc81810
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
package models
 
type CameraRuleBase struct {
    CameraId             string             `gorm:"column:camera_id" json:"camera_id"`           //摄像机id
    SdkId                string             `gorm:"column:sdk_id" json:"sdk_id"`                    //算法id
    PolygonId            string             `gorm:"column:polygon_id" json:"polygon_id"`         //多边形id
    SdkSet                 string             `gorm:"column:sdk_set" json:"sdk_set"`                    //算法id
    RuleWithPre          string             `gorm:"column:rule_with_pre" json:"rule_with_pre"`   //与上一条记录的逻辑运算规则(&&,||)
    IsSaveAnyhow         bool             `gorm:"column:is_save_anyhow;default:0;" json:"is_save_anyhow"` //rule_with_pre是触发时,是否无论被触发的条件满不满足,都保留数据
    GroupId              string             `gorm:"column:group_id" json:"group_id"`             //分组id
    Sort                 int                `gorm:"column:sort" json:"sort"`                   //排序
}
 
type CameraRuleGroupArg struct {
    Id                   string             `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"`
    CameraRuleBase
}
 
func (CameraRuleGroupArg) TableName() string {
    return "camera_rule_group_arg"
}
 
func (rga *CameraRuleGroupArg) InsertByGroup() bool {
    tx := db.Table(rga.TableName()).Begin()
 
    if tx.Error != nil {
        return false
    }
 
    if err := tx.Create(&rga).Error; err != nil {
        tx.Rollback()
        return false
    }
    if tx.Commit().Error == nil {
        return true
    }
    return false
}
 
func (rga *CameraRuleGroupArg) FindAll() (args []CameraRuleGroupArg) {
    if err := db.Table(rga.TableName()).Scan(&args).Error; err != nil {
        return nil
    }
    return args
}
 
type DbGroupId struct {
    GroupId string `json:"group_id"`
}
 
 
func (rga *CameraRuleGroupArg) FindGroupIdByCameraId(cameraId string) (groupIds []DbGroupId, err error) {
    if err := db.Raw("select distinct group_id from "+rga.TableName()+" where camera_id=?", cameraId).Scan(&groupIds).Error; err != nil {
        return nil, err
    }
    return groupIds, nil
}
 
//DeleteByGroup 根据group_id删除规则组设置
func (rga CameraRuleGroupArg) DeleteByGroup(groupId string) bool {
    var crg CameraRuleGroup
    var err error
    tx := db.Begin()
    defer func() {
        if err != nil && tx != nil {
            tx.Rollback()
        }
    }()
    if err = tx.Table(rga.TableName()).Where("group_id=?", groupId).Delete(CameraRuleGroupArg{}).Error; err != nil {
        return false
    }
    if err = tx.Table(crg.TableName()).Where("group_id=?", groupId).Delete(crg).Error; err != nil {
        return false
    }
    if err = tx.Commit().Error; err != nil {
        return false
    } else {
        return true
    }
}
 
func (rga CameraRuleGroupArg) FindByGroupId(groupId string) (rags []CameraRuleGroupArg) {
    if err := db.Table(rga.TableName()).Where("group_id=?", groupId).Find(&rags).Error; err != nil {
        return nil
    }
    return rags
}
 
//判断多边形是否在规则设置中正在使用
func (rga CameraRuleGroupArg) IsPolygonInUse(polygonId string) bool {
    var rules []CameraRuleGroupArg
    if err := db.Table(rga.TableName()).Where("polygon_id=?", polygonId).Find(&rules).Error; err != nil {
        return true
    }
    if len(rules) == 0 {
        return false
    }
    return true
}
 
func (rga CameraRuleGroupArg) ExistRunningTask(cameraId string) bool {
    var arr []CameraRuleGroupArg
    if err = db.Raw("select a.* from "+CameraRuleGroupArg{}.TableName()+" a join "+CameraRuleGroup{}.TableName()+" g on a.group_id=g.id where a.camera_id=? and g.enable=1",cameraId).Scan(&arr).Error;err !=nil{
        return false
    }
    return len(arr) >0
 
}