123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package dao
- import (
- "fmt"
- "gorm.io/gorm"
- "iot_manager_service/util/common"
- "time"
- )
- type InfoBoard struct {
- ID int `gorm:"primary_key" json:"id"`
- InfoName string `gorm:"type:varchar(60)" json:"infoName"`
- Sn string `gorm:"type:varchar(60)" json:"sn"`
- LampPoleId int `gorm:"type:int" json:"lampPoleId"`
- LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"`
- LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"`
- LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"`
- PoleLng float64 `gorm:"type:double(17, 14) " json:"lampLng"`
- PoleLat float64 `gorm:"type:double(17, 14) " json:"lampLat"`
- GatewayId int `gorm:"type:int" json:"gatewayId"`
- GroupId int `gorm:"type:int" json:"groupId"`
- Resolution int `gorm:"type:int" json:"resolution"`
- BrandId int `gorm:"type:int" json:"brandId"`
- ModelId int `gorm:"type:int" json:"modelId"`
- InfoSize int `gorm:"type:int" json:"infoSize"`
- RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"`
- IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"`
- BootStartTime string `gorm:"type:varchar(64)" json:"bootStartTime"`
- BootEndTime string `gorm:"type:varchar(64)" json:"bootEndTime"`
- NeverCloseDown int `gorm:"type:int" json:"neverCloseDown"`
- InstallTime common.Time `gorm:"type:date" json:"installTime"`
- TenantId int `gorm:"type:int" json:"tenantId"`
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
- CreateUser int64 `gorm:"type:bigint" json:"createUser"`
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`
- UpdateUser int64 `gorm:"type:bigint" json:"updateUser"`
- IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"`
- Tag string `gorm:"type:varchar(255)" json:"tag"`
- ExteriorUid int `gorm:"type:bigint" json:"exteriorUid"`
- IsEnable int `gorm:"type:int; default 2 " json:"isEnable"`
- Condition string `gorm:"type:varchar(255);" json:"condition"`
- }
- func (InfoBoard) TableName() string {
- return "device_info_board"
- }
- func (c InfoBoard) Delete() error {
- return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
- "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
- }
- func (c InfoBoard) IsExistedBySN() bool {
- var count int64
- _ = Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
- c.Sn, c.IsDeleted).Count(&count).Error
- return count > 0
- }
- func (c InfoBoard) IsExistedByNameAndCode() bool {
- var devices []InfoBoard
- err := Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
- c.Sn, c.IsDeleted).Find(&devices).Error
-
- if gorm.ErrRecordNotFound == err {
- return false
- }
- for _, d := range devices {
- if d.ID != c.ID {
- return true
- }
- }
- return false
- }
- func (c *InfoBoard) Create() error {
- return Db.Debug().Model(&c).Save(&c).Error
- }
- func (c *InfoBoard) Update() error {
- return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *InfoBoard) GetDevice() (InfoBoard, error) {
- var device InfoBoard
- err := Db.Debug().Model(&c).Where(" is_deleted = 0 and id = ? ", c.ID).Scan(&device).Error
- return device, err
- }
- func (c InfoBoard) GetDevices(offset, limit int) ([]InfoBoard, int64, error) {
- var devices []InfoBoard
- var count int64
- db := Db.Debug().Model(&c).Where("is_deleted = 0 and (info_name like ? or sn like ?) ", "%"+c.InfoName+"%", "%"+c.InfoName+"%")
- db.Count(&count)
- err := db.Offset(offset).Limit(limit).Order("info_name asc").Find(&devices).Error
- return devices, count, err
- }
- func (c InfoBoard) GetAllDevices() ([]*InfoBoard, error) {
- var devices []*InfoBoard
- err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = 0 ", c.TenantId).Scan(&devices).Error
- return devices, err
- }
- func (c InfoBoard) GetDevicesByGateway() []InfoBoard {
- var devices []InfoBoard
- Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
- c.GatewayId).Find(&devices)
- return devices
- }
- func (c InfoBoard) GetDevicesByLampPole() []InfoBoard {
- var devices []InfoBoard
- Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
- c.LampPoleId).Find(&devices)
- return devices
- }
- func (c InfoBoard) GetDevicesByResolution() []InfoBoard {
- var devices []InfoBoard
- Db.Debug().Model(&c).Where("resolution = ? and tenant_id = ? and is_deleted = 0",
- c.Resolution, c.TenantId).Find(&devices)
- return devices
- }
- func (c InfoBoard) GetDevicesByIds(ids string) []InfoBoard {
- var devices []InfoBoard
- Db.Debug().Model(&c).Where("id in ("+ids+")"+" and tenant_id = ? and is_deleted = 0",
- c.TenantId).Scan(&devices)
- fmt.Printf("devices = %v", devices)
- return devices
- }
|