123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package dao
- import "server/global"
- type Device struct {
- global.GVA_MODEL
- Sn string `json:"sn" gorm:"comment:设备sn"`
- Name string `json:"name" gorm:"comment:名称"`
- Address string `json:"address" gorm:"comment:设备地址"`
- TaskTime uint `json:"taskTime" gorm:"comment:采集时间"`
- WaitTime uint `json:"waitTime" gorm:"comment:延迟时间"`
- RadarId int8 `json:"radarId" gorm:"comment:雷达id"`
- SerialId int8 `json:"serialId" gorm:"comment:串口id"`
- Genre int `json:"genre" gorm:"index;comment:类型id"`
- DeviceGenre DeviceGenre `json:"deviceGenre" gorm:"foreignKey:Genre;"`
- TunnelId int `json:"tunnelId" gorm:"column:tunnel_id"`
- Tunnel Tunnel `json:"tunnel" gorm:"foreignKey:TunnelId"`
- }
- func (Device) TableName() string {
- return "device"
- }
- func QueryAllDevices() (devices []Device, err error) {
- err = global.GVA_DB.Model(Device{}).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
- return
- }
- func QueryDeviceList(sn, name string, genre, limit, offset int) (devices []Device, total int64, err error) {
- db := global.GVA_DB.Model(Device{})
- if sn != "" {
- db = db.Where("sn LIKE ?", "%"+sn+"%")
- }
- if name != "" {
- db = db.Where("name LIKE ?", "%"+name+"%")
- }
- if genre != 0 {
- db = db.Where("genre = ?", genre)
- }
- err = db.Count(&total).Error
- if err != nil {
- return
- }
- err = db.Limit(limit).Offset(offset).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
- return
- }
- func QueryDeviceByTunnelId(id int) (devices []Device, err error) {
- err = global.GVA_DB.Where("tunnel_id =?", id).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
- return
- }
- func (d Device) CreateDevice() error {
- return global.GVA_DB.Create(&d).Error
- }
- func (d Device) UpdateDevice() error {
- return global.GVA_DB.Where("id = ?", d.ID).Updates(&d).Error
- }
- func DeleteDevice(id int) error {
- return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Device{}).Error
- }
|