qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
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
package models
 
import (
    "basic.com/valib/logger.git"
    "strconv"
    "time"
)
 
type Area struct {
    Id                     string             `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"` /*auto_increment:false*/
    Parentid               string             `gorm:"column:parentid" json:"parentid"`
    Parentids              string             `gorm:"column:parentids" json:"parentids"`
    Name                   string             `gorm:"column:name" json:"name"`
    Alias                    string             `gorm:"column:alias" json:"alias"` //目录树的别名
    Createby               int               `gorm:"column:createby" json:"createby"`
    Createtime             time.Time         `gorm:"column:createtime" json:"createtime"`
    Reserved               string            `gorm:"column:reserved" json:"reserved"`
    Type                    int             `gorm:"column:type;default:0" json:"type"`//默认是0(本地树),1:是国标的树
}
 
const (
    TYPE_LOCAL_TREE = 0 //本地摄像机树
    TYPE_GB28181_TREE = 1 //国标摄像机树
)
 
func (Area) TableName() string {
    return "area"
}
 
func (area *Area) Find(parentId string, areaName string, conditions []string,treeType int) (areas []Area, err error) {
    sql :="select id,case ifnull(alias,'') when '' then name else alias end as name,parentid,parentids from area where type="+strconv.Itoa(treeType)+" and parentids LIKE '%,"+parentId+",%'"
    logger.Debug("area.Find sql:",sql)
    if areaName !=""{
        sql +=" and name like '%"+areaName+"%'"
    }
    sql += " order by id asc"
    if err:= db.Raw(sql).Scan(&areas).Error;err!=nil{
        return nil, err
    }
    //if err := db.Table("area").Where("type=? and parentids LIKE ?", treeType, parentids).Select(conditions).Find(&areas).Error; err != nil {
    //    return nil, err
    //}
    return areas, nil
}
 
func (area *Area) FindAll() (areas []Area, err error){
    if err := db.Raw("select id,case ifnull(alias,'') when '' then name else alias end as name,parentid,parentids,createby,createtime,reserved,type from area").Scan(&areas).Error;err!=nil{
        return nil,err
    }
    return areas,nil
}
 
func (area *Area) SelectbyId(id string) (rows int64, err error) {
    dbselect := db.Table("area").Where("id=?",id).First(&area)
    if dbselect.Error != nil || dbselect.RowsAffected == 0 {
        return 0, dbselect.Error
    }
    return dbselect.RowsAffected, nil
}
 
func (area *Area) Insert() (err error) {
    tx := db.Table("area").Begin()
 
    if tx.Error != nil {
        return err
    }
    logger.Debug(area)
 
    if err := tx.Create(&area).Error; err != nil {
        tx.Rollback()
        return err
    }
 
    return tx.Commit().Error
}
 
func (area *Area) Update(id string, name string, parentId string, alias string) error {
    if err := db.Table("area").Where("id=?",id).First(&area).Error; err != nil {
        return err
    }
 
    if err := db.Exec("update area set name=?,parentid=?,alias=? where id=?",name,parentId,alias,id).Error; err != nil {
        return err
    }
    return nil
}
 
func (area *Area) Delete(id string) (err error) {
    if err := db.Table("area").Where("id=?",id).Delete(&area).Error; err != nil {
        return err
    }
    return nil
}
 
func (area *Area) IsHasCnode(currentid string) (msg string, exist bool) {
    var areaCount int
    err := db.Raw("select count(1) from area where parentid='"+currentid+"'").Count(&areaCount).Error
    if err == nil {
        if areaCount >0 {
            return "存在子节点", true
        } else {
            var cCount int
            err = db.Raw("select count(1) from camera_area where areaId='"+currentid+"'").Count(&cCount).Error
            if err == nil {
                if cCount >0 {
                    return "存在摄像机", true
                } else {
                    return "",false
                }
            }
        }
    }
    return "db error",true
}