qixiaoning
2025-08-21 e38654fe9eff4562da4f18f8f018aed7879d493c
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
package models
 
type CameraRuleGroup struct {
    Id         string `gorm:"primary_key;column:id" json:"id"`
    GroupText  string `gorm:"column:group_text" json:"group_text"`
    AlarmLevel int    `gorm:"column:alarm_level;default:1" json:"alarm_level"`
    SetType    string `gorm:"column:set_type" json:"set_type"`
 
    TemplateId   string `gorm:"column:template_id" json:"template_id"`     //场景模板id
    TemplateRule string `gorm:"column:template_rule" json:"template_rule"` //场景模板的配置
    TimeRuleId   string `gorm:"column:time_rule_id" json:"time_rule_id"`   //时间段
    SceneName    string `gorm:"column:scene_name" json:"scene_name"`       //场景名称,从模板来,修改即自定义
    Desc         string `gorm:"column:desc" json:"desc"`                   //场景描述
    Enable       bool   `gorm:"column:enable;default:1" json:"enable"`     //开关,默认开启
 
    VoiceId string `gorm:"column:voiceId" json:"voiceId"` //事件声音
}
 
const (
    Type_SetType_Batch = "batchTask"
    Type_SetType_Link  = "linkTask"
    RecordNotFound     = "record not found"
)
 
func (CameraRuleGroup) TableName() string {
    return "camera_rule_group"
}
 
func (crg *CameraRuleGroup) SelectById(id string) (rows int64, err error) {
    result := db.Table(crg.TableName()).Where("id = ?", id).First(&crg)
    if result.Error != nil && result.Error.Error() != RecordNotFound {
        return 0, result.Error
    }
    return result.RowsAffected, nil
}
 
func (crg *CameraRuleGroup) FindAll() (list []CameraRuleGroup) {
    if err := db.Table(crg.TableName()).Scan(&list).Error; err != nil {
        return nil
    }
    return list
}
func (crg *CameraRuleGroup) Insert() (bool, error) {
    if err := db.Table(crg.TableName()).Create(&crg).Error; err != nil {
        return false, err
    }
    return true, nil
}
 
func (crg *CameraRuleGroup) FindByCameraId(cameraId string, setType string) (arr []CameraRuleGroup, err error) {
    sql := "select * from " + crg.TableName() + " where id in (select distinct group_id from " + CameraRuleGroupArg{}.TableName() + " where camera_id='" + cameraId + "')"
    if setType != "" {
        sql += " and set_type='batchTask'"
    }
    if err := db.Raw(sql).Scan(&arr).Error; err != nil {
        return nil, err
    }
    return arr, nil
}
 
func (crg *CameraRuleGroup) FindGroupByPolygonId(polygonId string) (list []CameraRuleGroup) {
    var rga CameraRuleGroupArg
    err := db.Raw("select * from "+crg.TableName()+" where id in (select distinct group_id from "+rga.TableName()+" where polygon_id=?)", polygonId).Find(&list).Error
    if err != nil {
        return nil
    }
    return list
}
 
func (crg *CameraRuleGroup) UpdateText(id string, text string) bool {
    execResult := db.Exec("update "+crg.TableName()+" set group_text=? where id=?", text, id)
    if execResult.Error != nil {
        return false
    }
    return execResult.RowsAffected > 0
}
 
func (crg *CameraRuleGroup) UpdateAlarmLevel(groupId string, alarmLevel int) bool {
    execResult := db.Exec("update "+crg.TableName()+" set alarm_level=? where id=?", alarmLevel, groupId)
    if execResult.Error != nil {
        return false
    }
    return execResult.RowsAffected > 0
}
 
func (crg *CameraRuleGroup) FindByTimeRuleId(timeRuleId string) (list []CameraRuleGroup, e error) {
    if err := db.Table(crg.TableName()).Where("time_rule_id=?", timeRuleId).Scan(&list).Error; err != nil {
        return nil, err
    }
    return list, nil
}