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
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
package models
 
import (
    "regexp"
 
    "basic.com/valib/logger.git"
)
 
type CameraPolygon struct {
    Id            string `gorm:"primary_key;column:id" json:"id"`
    CameraId      string `gorm:"column:camera_id" json:"camera_id"`                   //摄像机id
    Name          string `gorm:"column:name" json:"name"`                             //形状名称
    Polygon       string `gorm:"column:polygon" json:"polygon"`                       //形状结构定义
    DirectionLine string `gorm:"column:direction_line" json:"direction_line"`         //方向线
    Type          string `gorm:"column:type" json:"type"`                             //类型,["line","rect","polygon"]
    DefenceState  int    `gorm:"column:defence_state;default:0" json:"defence_state"` //布撤防状态
}
 
const (
    TYPE_LINE              = "line"    //线
    TYPE_RECT              = "rect"    //矩形
    TYPE_POLYGON           = "polygon" //面
    CAMERAPOLYGON_AREA_ALL = "全部区域"
)
 
//坐标定义
//type Point struct {
//    X float32 `json:"x"`
//    Y float32 `json:"y"`
//}
 
func (CameraPolygon) TableName() string {
    return "camera_polygon"
}
 
func (cp *CameraPolygon) FindAll() (polygons []CameraPolygon) {
    if err := db.Table("camera_polygon").Scan(&polygons).Error; err != nil {
        return nil
    }
    return polygons
}
 
func (cp *CameraPolygon) FindAllMap() map[string]CameraPolygon {
    m := make(map[string]CameraPolygon, 0)
    polygons := cp.FindAll()
    if polygons != nil {
        for _, p := range polygons {
            m[p.Id] = p
        }
    }
    return m
}
 
func (cp *CameraPolygon) SelectById(id string) (model CameraPolygon, flag bool) {
    exist := db.Table("camera_polygon").First(&model, "id=?", id).RecordNotFound()
    return model, !exist
}
 
func (cp *CameraPolygon) FindAllByCameraId(cameraId string) (polygons []CameraPolygon, err error) {
    if err := db.Table("camera_polygon").Where("camera_id = ?", cameraId).Scan(&polygons).Error; err != nil {
        return nil, err
    }
    return polygons, nil
}
 
// FindByCameraId 根据摄像机id查找在规则中使用的多边形
func (cp *CameraPolygon) FindRulePolygonsByCameraId(cameraId string) (polygons []CameraPolygon, err error) {
    if err := db.Table("camera_polygon").Where("camera_id = ? and type !=?", cameraId, TYPE_LINE).Scan(&polygons).Error; err != nil {
        return nil, err
    }
    return polygons, nil
}
 
// 保存摄像机区域
func (cp *CameraPolygon) Insert() (result bool, err error) {
    if err = db.Table("camera_polygon").Save(&cp).Error; err != nil {
        return false, err
    }
    return true, nil
}
 
// 更新摄像机区域
func (cp *CameraPolygon) Update() (bool, error) {
    result := db.Exec("update camera_polygon set camera_id=?,name=?,polygon=?,direction_line=?,type=?,defence_state=? where id=?", cp.CameraId, cp.Name, cp.Polygon, cp.DirectionLine, cp.Type, cp.DefenceState, cp.Id)
    if result.Error != nil {
        return false, result.Error
    }
    return result.RowsAffected > 0, nil
}
 
// 判断摄像机有没有重名的区域
func (cp CameraPolygon) Exist(cameraId string, name string) (model CameraPolygon, exist bool) {
    exist = db.Table("camera_polygon").First(&model, "camera_id=? and name=?", cameraId, name).RecordNotFound()
    return model, !exist
}
 
// UnInstall 删除摄像机区域
func (cp *CameraPolygon) Delete(id string, name string) bool {
    if err := db.Table("camera_polygon").Where("id=?", id).Delete(&CameraPolygon{}).Error; err != nil {
        return false
    }
    var crg CameraRuleGroup
    groups := crg.FindGroupByPolygonId(id)
    if groups != nil {
        for _, g := range groups {
            //将groupText中的已删除的多边形名称替换为NULL
            reg := regexp.MustCompile(`(` + name + `于)`)
            newGText := reg.ReplaceAllString(g.GroupText, `<span style="color:RGB(255,0,0);">NULL</span>于`)
            crg.UpdateText(g.Id, newGText)
        }
    }
    err := db.Exec("update camera_task_args set polygon_id='' where polygon_id=?", id).Error
    if err != nil {
        logger.Debug("rm polygon_id in camera_task_args err:", err)
    }
    return true
}
 
func (cp *CameraPolygon) UpdateDefenceStateByPolygonId(polygonId string, state int) bool {
    result := db.Exec("update camera_polygon set defence_state=? where id=?", state, polygonId)
    if result.Error != nil {
        return false
    }
    if result.RowsAffected > 0 {
        return true
    } else {
        return false
    }
}
 
func (cp *CameraPolygon) UpdateDefenceStateByCameraId(cameraId string, state int) bool {
    result := db.Exec("update camera_polygon set defence_state=? where camera_id=?", state, cameraId)
    if result.Error != nil {
        return false
    }
    if result.RowsAffected > 0 {
        return true
    } else {
        return false
    }
}