123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package dao
- import (
- "github.com/jinzhu/gorm"
- "time"
- )
- //LightControl 灯控
- type LightControl struct {
- ID int `gorm:"primary_key" json:"id"` //编号
- Name string `gorm:"type:varchar(64)" json:"name"` //名称
- SN string `gorm:"type:varchar(60)" json:"sn"` //设备序列号
- ControlType int `gorm:"type:int" json:"controlType"` //控制器类型 type:0=485灯控,1=NB-iot箱
- GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组
- GatewayId int `gorm:"type:varchar(32)" json:"gatewayId"` //所属网关id
- LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆 灯杆ID
- LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
- LampPoleSN string `gorm:"type:varchar(64)" json:"lampPoleSn"` //灯杆SN
- LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
- LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"` //纬度
- LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"` //经度
- RatedPower float32 `gorm:"type:double(8,2)" json:"ratedPower"` //额定功率(LED灯)
- BrandId int `gorm:"type:int" json:"brandId"` //设备品牌
- ModelId int `gorm:"type:int" json:"modelId"` //设备型号
- InstallTime time.Time `gorm:"type:date" json:"installTime"` //安装时间
- TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
- CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
- UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
- IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
- Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
- Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
- ControlNO string `gorm:"type:varchar(10)" json:"controlNo"` //编号
- ChannelNum int `gorm:"type:int" json:"channelNum"` //通道号
- NetworkNum int `gorm:"type:int" json:"networkNum"` //网络号
- ZigbeeId int `gorm:"type:int" json:"zigbeeId"` //zigbeeID
- ZigbeeName string `gorm:"type:varchar(10)" json:"zigbeeName"` //zigbee名称
- ZigbeeSn string `gorm:"type:varchar(10)" json:"zigbeeSn"` //zigbee编码
- IsEnable int `gorm:"type:int;default 2" json:"isEnable"` //启用禁用:1启用2禁用
- IsOnDemand int `gorm:"type:int; default 0" json:"isOnDemand"` //灯控类型:0-普通灯控,1-灯随车走灯控
- }
- func (LightControl) TableName() string {
- return "t_dev_light_control"
- }
- func (c *LightControl) 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 LightControl) IsExistedBySN() bool {
- var count = 0
- _ = Db.Debug().Model(&c).Where("sn = ? and is_deleted = ?",
- c.SN, c.IsDeleted).Count(&count).Error
- return count > 0
- }
- func (c LightControl) IsExistedByNameAndCode() bool {
- var devices []LightControl
- err := Db.Debug().Model(&c).Where("pole_sn = ? and is_deleted = ?",
- c.TenantId, c.IsDeleted).Find(&devices).Error
- if gorm.IsRecordNotFoundError(err) {
- return false
- }
- for _, d := range devices {
- if d.ID != c.ID {
- return true
- }
- }
- return false
- }
- func (c *LightControl) Create() error {
- return Db.Debug().Model(&c).Save(&c).Error
- }
- func (c *LightControl) Update() error {
- return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
- }
- func (c *LightControl) UpdateEnable() error {
- return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
- }
- func (c *LightControl) GetDevice() error {
- err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
- return err
- }
- func (c *LightControl) GetDevicesByGroup() ([]LightControl, error) {
- var devices []LightControl
- err := Db.Debug().Model(&c).Where(" group_id = ? and is_deleted = 0", c.GroupId).Find(&devices).Error
- return devices, err
- }
- func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
- var devices []LightControl
- db := Db.Debug().Model(&c)
- if c.SN != "" {
- db = db.Where("name like ? or sn like ?", "%"+c.SN+"%", "%"+c.SN+"%")
- }
- if c.ControlType != 0 {
- db = db.Where("control_type = ?", c.ControlType)
- }
- if c.ZigbeeId != 0 {
- db = db.Where("zigbee_id = ?", c.ZigbeeId)
- }
- err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
- return devices, err
- }
- func (c LightControl) GetAllDevices() ([]*LightControl, error) {
- var devices []*LightControl
- err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
|