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
92
93
94
95
96
package models
 
//摄像机区域之间的对应关系,目前主要是给长春跟踪使用
type CameraPolygonRelation struct {
    Id                        string                 `gorm:"primary_key;column:id" json:"id"`
    GroupId                 string                 `gorm:"column:group_id" json:"group_id"`                      //分组id
    SourceCameraId           string                 `gorm:"column:source_camera_id" json:"source_camera_id"`      //摄像机id
    SourcePolygonId           string                 `gorm:"column:source_polygon_id" json:"source_polygon_id"`      //区域id
    TargetCameraId           string                 `gorm:"column:target_camera_id" json:"target_camera_id"`      //摄像机id
    TargetPolygonId           string                 `gorm:"column:target_polygon_id" json:"target_polygon_id"`    //区域id
}
 
func (CameraPolygonRelation) TableName() string {
    return "camera_polygon_relation"
}
 
func (cpr *CameraPolygonRelation) FindAll() (arr []CameraPolygonRelation) {
    if err := db.Table(cpr.TableName()).Scan(&arr).Error;err !=nil {
        return nil
    }
    return arr
}
 
func (cpr *CameraPolygonRelation) FindAllMap() map[string]CameraPolygonRelation {
    m := make(map[string]CameraPolygonRelation,0)
    arr := cpr.FindAll()
    if arr !=nil {
        for _,p :=range arr {
            m[p.Id] = p
        }
    }
    return m
}
 
func (cpr *CameraPolygonRelation) SelectById(id string) (model CameraPolygonRelation,flag bool) {
    exist := db.Table(cpr.TableName()).First(&model,"id=?",id).RecordNotFound()
    return model,!exist
}
 
func (cpr *CameraPolygonRelation) FindByGroupId(groupId string) (arr []CameraPolygonRelation, err error) {
    if err := db.Table(cpr.TableName()).Where("group_id=?", groupId).Scan(&arr).Error;err !=nil {
        return nil, err
    }
    return arr,nil
}
 
func (cpr *CameraPolygonRelation) FindAllByCameraId(cameraId string) (arr []CameraPolygonRelation, err error) {
    if err := db.Table(cpr.TableName()).Where("source_camera_id = ? or target_camera_id=?", cameraId).Scan(&arr).Error;err !=nil {
        return nil, err
    }
    return arr,nil
}
 
func (cpr *CameraPolygonRelation) FindRelations(sourceCameraId string, targetCameraId string) (arr []CameraPolygonRelation, err error) {
    if err := db.Table(cpr.TableName()).Where("(source_camera_id=? and target_camera_id=?) or (source_camera_id=? and target_camera_id=?)", sourceCameraId, targetCameraId, targetCameraId, sourceCameraId).Scan(&arr).Error;err !=nil {
        return nil, err
    }
    return arr,nil
}
 
func (cpr *CameraPolygonRelation) Insert() (result bool, err error) {
    if err = db.Table(cpr.TableName()).Save(&cpr).Error; err != nil {
        return false, err
    }
    return true, nil
}
 
func (cpr *CameraPolygonRelation) Update() (bool,error) {
    result := db.Table(cpr.TableName()).Where("id=?",cpr.Id).Save(&cpr)
    if result.Error != nil {
        return false, err
    }
    return result.RowsAffected > 0, nil
}
 
func (cpr CameraPolygonRelation) Exist(sourceCamId string, sourcePgnId string, targetCamId string, targetPgnId string) (model CameraPolygonRelation, exist bool) {
    exist = db.Table(cpr.TableName()).First(&model, "(source_camera_id=? and source_polygon_id=? and target_camera_id=? and target_polygon_id=?) or (source_camera_id=? and source_polygon_id=? and target_camera_id=? and target_polygon_id=?)", sourceCamId, sourcePgnId, targetCamId, targetPgnId,targetCamId, targetPgnId, sourceCamId, sourcePgnId).RecordNotFound()
    return model, !exist
}
 
//Delete 删除摄像机区域关联关系
func (cpr *CameraPolygonRelation) Delete(id string) bool {
    if err := db.Table(cpr.TableName()).Where("id=?",id).Delete(&CameraPolygonRelation{}).Error; err != nil {
        return false
    }
 
    return true
}
 
func (cpr *CameraPolygonRelation) DeleteByGroupId(groupId string) bool {
    if err := db.Table(cpr.TableName()).Where("group_id=?",groupId).Delete(&CameraPolygonRelation{}).Error; err != nil {
        return false
    }
 
    return true
}