1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package dao
- import (
- "gorm.io/gorm"
- "server/global"
- )
- type Tunnel struct {
- global.GVA_MODEL
- TunnelSn string `json:"tunnelSn" gorm:"comment:隧道序号"`
- Name string `json:"name" gorm:"comment:名称"`
- Tactics int `json:"tactics" gorm:"comment:策略"`
- RegionId int `json:"regionId" gorm:"column:region_id"`
- Region Region `json:"region" gorm:"foreignKey:RegionId"` // 关系
- TimeId int `json:"timeId" gorm:"comment:定时id"`
- TunnelTime TunnelTime `json:"tunnelTime" gorm:"foreignKey:TimeId"`
- Users []SysUser `json:"users" gorm:"many2many:user_tunnel"`
- Devices []Device `json:"devices" gorm:"foreignKey:TunnelId"`
- }
- func (Tunnel) TableName() string {
- return "tunnel"
- }
- func QueryAllTunnels() (tunnels []Tunnel, err error) {
- err = global.GVA_DB.Find(&tunnels).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Region").Error
- return tunnels, err
- }
- func QueryTunnelList(name string, regionId, limit, offset int) (tunnels []Tunnel, total int64, err error) {
- db := global.GVA_DB.Model(Tunnel{})
- if name != "" {
- db = db.Where("name LIKE ?", "%"+name+"%")
- }
- if regionId != 0 {
- db = db.Where("region_id = ?", regionId)
- }
- err = db.Count(&total).Error
- if err != nil {
- return
- }
- err = db.Limit(limit).Offset(offset).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Region").Find(&tunnels).Error
- return
- }
- func QueryNoRegionTunnels() (tunnels []Tunnel, err error) {
- err = global.GVA_DB.Where("region_id =?", 0).Find(&tunnels).Error
- return
- }
- func (t Tunnel) CreateTunnel() error {
- err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
- err := tx.Create(&t).Error
- if err != nil {
- return err
- }
- time := TunnelTime{
- TunnelSn: t.TunnelSn,
- StartTime: "00:00",
- EndTime: "00:00",
- }
- err = tx.Create(&time).Error
- if err != nil {
- return err
- }
- return nil
- })
- return err
- }
- func (t Tunnel) UpdateTunnel() error {
- return global.GVA_DB.Where("id = ?", t.ID).Updates(&t).Error
- }
- func UpdateTunnelsRegion(tunnelIds []int, regionId int) error {
- return global.GVA_DB.Model(&Tunnel{}).Where("id in (?)", tunnelIds).Update("region_id", regionId).Error
- }
- func DeleteTunnel(id int) error {
- return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Tunnel{}).Error
- }
|