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
| package models
|
| type CameraArea struct {
| Cameraid string `json:"cameraId"`
| Areaid string `json:"areaId"`
| }
|
| //摄像机所属区域新增
| 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 {
| return false
| }
| if err = tx.Exec("insert into camera_area(cameraId,areaId) values (?,?)",cameraId, areaId).Error;err != nil {
| return false
| }
| tx.Commit()
| return true
| }
|
|