123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package dao
- import (
- "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 string `gorm:"type:varchar(12)" json:"tenantId"`
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
- CreateUser int `gorm:"type:int" json:"createUser"`
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`
- UpdateUser int `gorm:"type:int" 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);default='80,30,30,5'" json:"condition"`
- }
- func (InfoBoard) TableName() string {
- return "device_info_board"
- }
- func (c InfoBoard) Delete() error {
- return Db.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.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.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.Model(&c).Save(&c).Error
- }
- func (c *InfoBoard) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *InfoBoard) GetDevice() (InfoBoard, error) {
- var device InfoBoard
- err := Db.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.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.Model(&c).Where(" tenant_id = ? and is_deleted = 0 ", c.TenantId).Scan(&devices).Error
- return devices, err
- }
- func (c InfoBoard) GetAllDevicesNotTenant() ([]*InfoBoard, error) {
- var devices []*InfoBoard
- db := Db.Model(&c)
- if c.ID > 0 {
- db.Where("id=?", c.ID)
- }
- err := db.Where("is_deleted = 0 ").Scan(&devices).Error
- return devices, err
- }
- func (c InfoBoard) GetDevicesByGateway() []InfoBoard {
- var devices []InfoBoard
- Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
- c.GatewayId).Find(&devices)
- return devices
- }
- func (c InfoBoard) GetDevicesByLampPole() []InfoBoard {
- var devices []InfoBoard
- Db.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.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.Model(&c).Where("id in ("+ids+")"+" and tenant_id = ? and is_deleted = 0",
- c.TenantId).Scan(&devices)
- return devices
- }
- func (c InfoBoard) DeviceCount1() (int64, int64) {
- var all, fault int64
- err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
- if err != nil {
- all = 0
- }
- err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
- if err != nil {
- fault = 0
- }
- return all, fault
- }
- func (c InfoBoard) ListDevices1() []Device {
- var devices []InfoBoard
- Db.Model(&c).Where("is_deleted = 0").Find(&devices)
- var rsp = make([]Device, 0, len(devices))
- for _, v := range devices {
- var d = Device{
- Lng: v.PoleLng,
- Lat: v.PoleLat,
- Status: -1,
- Name: v.InfoName,
- Code: v.Sn,
- LampPoleName: v.LampPoleName,
- Model: "模型",
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|