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
|
}
|