package models
|
|
import "basic.com/valib/logger.git"
|
|
type CameraArea struct {
|
Cameraid string `json:"cameraId"`
|
Areaid string `json:"areaId"`
|
}
|
|
func (CameraArea) TableName() string {
|
return "camera_area"
|
}
|
|
//摄像机所属区域新增
|
func (ca *CameraArea) Insert() (result bool, err error) {
|
var row int
|
if err := db.Raw("select count(1) from camera_area where cameraid=? and areaid=?", ca.Cameraid, ca.Areaid).Scan(&row).Error; err != nil {
|
return false, err
|
}
|
if row == 0 {
|
result := db.Table("camera_area").Save(&ca)
|
if result.Error != nil {
|
return false, result.Error
|
}
|
return true, nil
|
}
|
return true, nil
|
}
|
|
//删除摄像机所属的某一区域
|
func (ca *CameraArea) Delete() (result bool, err error) {
|
exeResult := db.Exec("delete from camera_area where cameraid=? and areaid=?", ca.Cameraid, ca.Areaid)
|
if exeResult.Error != nil {
|
return false, exeResult.Error
|
}
|
if exeResult.RowsAffected > 0 {
|
return true, nil
|
}
|
return false, nil
|
}
|
|
func (ca *CameraArea) UpdateArea(cameraId string, areaId string) bool {
|
var err error
|
tx := GetDB().Begin()
|
defer func() {
|
if err != nil && tx != nil {
|
tx.Rollback()
|
}
|
}()
|
if err = tx.Exec("delete from camera_area where cameraId=?", cameraId).Error; err != nil {
|
logger.Debug("err:", err)
|
return false
|
}
|
if err = tx.Exec("insert into camera_area(cameraId,areaId) values (?,?)", cameraId, areaId).Error; err != nil {
|
logger.Debug("err:", err)
|
return false
|
}
|
tx.Commit()
|
return true
|
}
|
|
func (ca *CameraArea) FindAll() (cams []CameraArea, err error) {
|
dberr := db.Table(ca.TableName()).Find(&cams)
|
if dberr.Error != nil {
|
return nil, err
|
}
|
return cams, nil
|
}
|