gatewayDao.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. // Gateway 网关设备
  8. type Gateway struct {
  9. ID int `gorm:"primary_key" json:"id"` //编号
  10. GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"` //设备名称
  11. GatewaySn string `gorm:"type:varchar(60)" json:"gatewaySn"` //设备序列号
  12. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆
  13. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //所属灯杆SN
  14. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  15. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  16. LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"` //纬度
  17. LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"` //经度
  18. BrandId int `gorm:"type:int" json:"brandId"` //设备品牌
  19. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  20. GatewayInstallTime common.Time `gorm:"type:date" json:"gatewayInstallTime"` //安装时间
  21. RatedPower float32 `gorm:"type:double(8,2);default:0.00" json:"ratedPower"` //功率
  22. IpAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  23. DiNum int `gorm:"type:int;default 0" json:"diNum"` //DI口数量
  24. DoNum int `gorm:"type:int;default 0" json:"doNum"` //DO口数量
  25. Num485 int `gorm:"type:int;default 0" json:"num485"` //485口数量
  26. NetworkNum int `gorm:"type:int;default 0" json:"networkNum"` //网口数量
  27. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  28. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  29. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  30. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  31. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  32. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  33. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  34. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  35. }
  36. func (Gateway) TableName() string {
  37. return "device_wisdom_gateway"
  38. }
  39. func (c *Gateway) Delete() error {
  40. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  41. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  42. }
  43. func (c Gateway) IsExistedBySN() bool {
  44. var count int64
  45. _ = Db.Model(&c).Where(" gateway_sn = ? and is_deleted = ?",
  46. c.GatewaySn, c.IsDeleted).Count(&count).Error
  47. return count > 0
  48. }
  49. func (c Gateway) IsExistedByNameAndCode() bool {
  50. var devices []Gateway
  51. err := Db.Model(&c).Where("gateway_sn = ? and is_deleted = ?",
  52. c.TenantId, c.IsDeleted).Find(&devices).Error
  53. if gorm.ErrRecordNotFound == err {
  54. return false
  55. }
  56. for _, d := range devices {
  57. if d.ID != c.ID {
  58. return true
  59. }
  60. }
  61. return false
  62. }
  63. func (c *Gateway) Create() error {
  64. return Db.Model(&c).Save(&c).Error
  65. }
  66. func (c *Gateway) Update() error {
  67. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  68. }
  69. func (c *Gateway) GetDevice() error {
  70. err := Db.Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  71. return err
  72. }
  73. func (c Gateway) GetDevices(offset, limit int) ([]Gateway, int64, error) {
  74. var devices []Gateway
  75. var count int64
  76. db := Db.Model(&c)
  77. if c.GatewaySn != "" {
  78. db = db.Where("gateway_name like ? or gateway_sn like ?", "%"+c.GatewaySn+"%", "%"+c.GatewaySn+"%")
  79. }
  80. tx := db.Where("is_deleted = 0")
  81. tx.Count(&count)
  82. err := tx.Offset(offset).Limit(limit).Order("gateway_name asc").Find(&devices).Error
  83. return devices, count, err
  84. }
  85. func (c Gateway) GetAllDevices() ([]*Gateway, error) {
  86. var devices []*Gateway
  87. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  88. return devices, err
  89. }
  90. func (c Gateway) GetDevicesByLampPole() []Gateway {
  91. var devices []Gateway
  92. Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  93. c.LampPoleId).Find(&devices)
  94. return devices
  95. }
  96. func (c Gateway) DeviceCount1() (int64, int64) {
  97. var all, fault int64
  98. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  99. if err != nil {
  100. all = -1
  101. }
  102. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  103. if err != nil {
  104. fault = -1
  105. }
  106. return all, fault
  107. }
  108. func (c Gateway) ListDevices1() []Device {
  109. var devices []Gateway
  110. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  111. var rsp = make([]Device, 0, len(devices))
  112. for _, v := range devices {
  113. var d = Device{
  114. Lng: v.LampLng,
  115. Lat: v.LampLat,
  116. Status: v.Status,
  117. Name: v.GatewayName,
  118. Code: v.GatewaySn,
  119. LampPoleName: v.LampPoleName,
  120. Model: "模型",
  121. LinkNum: getLinkNum(v.LampPoleSn),
  122. }
  123. rsp = append(rsp, d)
  124. }
  125. return rsp
  126. }