| | |
| | | package models |
| | | |
| | | import ( |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/valib/logger.git" |
| | | "encoding/json" |
| | | "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"] |
| | | 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" //面 |
| | | TYPE_LINE = "line" //线 |
| | | TYPE_RECT = "rect" //矩形 |
| | | TYPE_POLYGON = "polygon" //面 |
| | | CAMERAPOLYGON_AREA_ALL = "全部区域" |
| | | ) |
| | | |
| | |
| | | } |
| | | |
| | | func (cp *CameraPolygon) FindAll() (polygons []CameraPolygon) { |
| | | if err := db.Table("camera_polygon").Scan(&polygons).Error;err !=nil { |
| | | 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) |
| | | m := make(map[string]CameraPolygon, 0) |
| | | polygons := cp.FindAll() |
| | | if polygons !=nil { |
| | | for _,p :=range polygons { |
| | | 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) 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 { |
| | | if err := db.Table("camera_polygon").Where("camera_id = ?", cameraId).Scan(&polygons).Error; err != nil { |
| | | return nil, err |
| | | } |
| | | return polygons,nil |
| | | return polygons, nil |
| | | } |
| | | |
| | | //FindByCameraId 根据摄像机id查找在规则中使用的多边形 |
| | | // 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 { |
| | | 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 |
| | | 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) |
| | | // 更新摄像机区域 |
| | | 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 { |
| | | // 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 { |
| | | for _, g := range groups { |
| | | //将groupText中的已删除的多边形名称替换为NULL |
| | | reg := regexp.MustCompile(`(`+name+`于)`) |
| | | 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) |
| | | 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{ |
| | | 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{ |
| | | 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{ |
| | | 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{ |
| | | if result.RowsAffected > 0 { |
| | | return true |
| | | } else { |
| | | return false |
| | | } |
| | | } |
| | | } |