123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package dao
- import (
- "gorm.io/gorm"
- "iot_manager_service/util/common"
- "time"
- )
- type Zigbee struct {
- ID int `gorm:"primary_key" json:"id"`
- Name string `gorm:"type:varchar(64)" json:"name"`
- Sn string `gorm:"type:varchar(60)" json:"sn"`
- GroupId int `gorm:"type:int" json:"groupId"`
- 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:"poleLng"`
- PoleLat float64 `gorm:"type:double(17, 14)" json:"poleLat"`
- BrandId int `gorm:"type:int" json:"brandId"`
- ModelId int `gorm:"type:int" json:"modelId"`
- GatewayId int `gorm:"type:int" json:"gatewayId"`
- GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"`
- GatewaySn string `gorm:"type:varchar(60)" json:"gatewaySn"`
- ChannelNum int `gorm:"type:int" json:"chanelNum"`
- NetworkNum int `gorm:"type:int" json:"networkNum"`
- 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"`
- Status int `gorm:"type:int" json:"status"`
- Tag string `gorm:"type:varchar(255)" json:"tag"`
- IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"`
- CountRel int `gorm:"-;default 0" json:"countRel"`
- }
- func (Zigbee) TableName() string {
- return "device_zigbee"
- }
- func (c Zigbee) 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 Zigbee) 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 Zigbee) IsExistedByNameAndCode() bool {
- var devices []Zigbee
- 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 *Zigbee) Create() error {
- return Db.Model(&c).Save(&c).Error
- }
- func (c *Zigbee) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *Zigbee) GetDevice() error {
- err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
- return err
- }
- func (c Zigbee) GetDevices(offset, limit int) ([]Zigbee, int64, error) {
- var devices []Zigbee
- var count int64
- db := Db.Model(&c).Where("is_deleted=0 and name like ? ", "%"+c.Name+"%")
- db.Count(&count)
- err := db.Offset(offset).Limit(limit).Find(&devices).Error
- return devices, count, err
- }
- func (c Zigbee) GetAllDevices() ([]Zigbee, error) {
- var devices []Zigbee
- err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
- func (c Zigbee) GetDevicesByGateway() []Zigbee {
- var devices []Zigbee
- Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
- c.GatewayId).Find(&devices)
- return devices
- }
- func (c Zigbee) GetDevicesByLampPole() []Zigbee {
- var devices []Zigbee
- Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
- c.LampPoleId).Find(&devices)
- return devices
- }
- func (c Zigbee) DeviceCount1() (int64, int64) {
- var all, fault int64
- err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
- if err != nil {
- all = -1
- }
- err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
- if err != nil {
- fault = -1
- }
- return all, fault
- }
- func (c Zigbee) ListDevices1() []Device {
- var devices []Zigbee
- 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: v.Status,
- Name: v.Name,
- Code: v.Sn,
- LampPoleName: v.LampPoleName,
- Model: "模型",
- LinkNum: getLinkNum(v.LampPoleSn),
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|