qixiaoning
2025-07-24 343a89f9fa20d2d142469b2a4531e16ce03d3525
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
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
}