123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package dao
- import (
- "gorm.io/gorm"
- "iot_manager_service/util/common"
- "time"
- )
- type IpBroadcast struct {
- ID int `gorm:"primary_key" json:"id"` //编号
- CastName string `gorm:"type:varchar(64)" json:"castName"` //设备名称
- CastSn string `gorm:"type:varchar(60)" json:"castSn"` //设备序列号
- LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆id
- LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //所属灯杆sn
- GroupId int `gorm:"type:int" json:"groupId"` //灯杆分组ID
- GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
- GatewaySn string `gorm:"type:varchar(64)" json:"gatewaySn"` //所属网关sn
- BrandId int `gorm:"type:int" json:"brandId"` //设备名称
- ModelId int `gorm:"type:int" json:"modelId"` //设备型号
- RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
- IPAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
- PlayState string `gorm:"-" json:"playState"` //播放状态
- ServiceIpAddress string `gorm:"type:varchar(50)" json:"serviceIpAddress"` //服务IP地址
- ServerPort int `gorm:"type:int" json:"serverPort"` //服务端口
- SoundVolume int `gorm:"type:int;default=0" json:"soundVolume"` //音量0-100换算
- InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
- TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
- CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
- UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
- IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
- }
- func (IpBroadcast) TableName() string {
- return "device_ip_broadcast"
- }
- func (c IpBroadcast) 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 IpBroadcast) IsExistedBySN() bool {
- var count int64
- _ = Db.Model(&c).Where(" cast_sn = ? and is_deleted = ?",
- c.CastSn, c.IsDeleted).Count(&count).Error
- return count > 0
- }
- func (c IpBroadcast) IsExistedByNameAndCode() bool {
- var devices []IpBroadcast
- err := Db.Model(&c).Where(" cast_sn = ? and is_deleted = ?",
- c.CastSn, 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 *IpBroadcast) Create() error {
- return Db.Model(&c).Save(&c).Error
- }
- func (c *IpBroadcast) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *IpBroadcast) GetDevice() error {
- err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
- return err
- }
- func (c IpBroadcast) GetDevices(offset, limit int) ([]IpBroadcast, int64, error) {
- var devices []IpBroadcast
- var count int64
- db := Db.Model(&c).Where("is_deleted = 0 and cast_name like ? ", "%"+c.CastName+"%")
- db.Count(&count)
- err := db.Offset(offset).Limit(limit).Find(&devices).Error
- return devices, count, err
- }
- func (c IpBroadcast) GetAllDevices() ([]IpBroadcast, error) {
- var devices []IpBroadcast
- err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
- c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
- func (c IpBroadcast) GetDevicesByGateway() []IpBroadcast {
- var devices []IpBroadcast
- Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
- c.GatewayId).Find(&devices)
- return devices
- }
- func (c IpBroadcast) GetDevicesByLampPole() []IpBroadcast {
- var devices []IpBroadcast
- Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
- c.LampPoleId).Find(&devices)
- return devices
- }
- func (c IpBroadcast) GetAll() []IpBroadcast {
- var devices []IpBroadcast
- Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
- c.IsDeleted).Scan(&devices)
- return devices
- }
- func (c IpBroadcast) GetByIds(ids string) []IpBroadcast {
- var devices []IpBroadcast
- Db.Model(&c).Where(" tenant_id = ? and is_deleted = ?", c.TenantId,
- c.IsDeleted).Where("id in (" + ids + ")").Scan(&devices)
- return devices
- }
- func (c IpBroadcast) GetAllDevicesNotTenant() ([]*IpBroadcast, error) {
- var devices []*IpBroadcast
- 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 IpBroadcast) 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 IpBroadcast) ListDevices1() []Device {
- var devices []IpBroadcast
- Db.Model(&c).Where("is_deleted = 0").Find(&devices)
- var rsp = make([]Device, 0, len(devices))
- for _, v := range devices {
- var d = Device{
- Name: v.CastName,
- Code: v.CastSn,
- Model: "模型",
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|