manholeCoverDao.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. // ManholeCover 井盖基本信息
  8. type ManholeCover struct {
  9. ID int `gorm:"primary_key" json:"id"` //编号
  10. ManholeName string `gorm:"type:varchar(64)" json:"manholeName"` //设备名称
  11. ManholeSn string `gorm:"type:varchar(60)" json:"manholeSn"` //设备序列号
  12. DeviceIMei string `gorm:"type:varchar(64)" json:"deviceIMei"` //IMEI-唯一不允许重复
  13. BrandId int `gorm:"type:int" json:"brandId"` //设备名称
  14. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  15. KindType int `gorm:"type:int" json:"kindType"` //种类
  16. Radius int `gorm:"type:int" json:"radius"` //半径
  17. MaterialQuality int `gorm:"type:int" json:"materialQuality"` //材质
  18. InstallLocationType int `gorm:"type:int" json:"installLocationType"` //安装位置类型
  19. WayName string `gorm:"type:varchar(60)" json:"wayName"` //所属道路
  20. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  21. ProvinceName string `gorm:"varchar(12)" json:"provinceName"` //省份
  22. CityName string `gorm:"type:varchar(12)" json:"cityName"` //市
  23. DistrictName string `gorm:"type:varchar(12)" json:"districtName"` //区
  24. InstallLocation string `gorm:"type:varchar(100)" json:"installLocation"` //安装位置
  25. Latitude float64 `gorm:"type:double(17, 14) " json:"latitude"` //纬度
  26. Longitude float64 `gorm:"type:double(17, 14) " json:"longitude"` //经度
  27. PhotoUrl string `gorm:"type:varchar(255) " json:"photoUrl"` //照片地址
  28. Remark string `gorm:"type:varchar(1000) " json:"remark"` //备注
  29. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  30. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  31. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  32. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  33. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  34. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  35. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  36. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  37. Threshold string `gorm:"type: varchar(255)" json:"threshold"` //阈值
  38. CameraId int `gorm:"type:int" json:"cameraId"` //归属摄像机id
  39. CameraSn string `gorm:"type: varchar(60) " json:"cameraSn"` //归属摄像机sn
  40. LeanAngleAlarm int `json:"leanAngleAlarm"` //告警倾斜
  41. BatteryAlarm float64 `gorm:"type: varchar(60) " json:"batteryAlarm"` //告警电压
  42. MinTemp float64 `gorm:"type: varchar(60) " json:"minTemp"` //低温阈值
  43. MaxTemp float64 `gorm:"type: varchar(60) " json:"maxTemp"` //高温阈值
  44. LeanAngle int `json:"leanAngle"` //倾斜角度
  45. Temperature float64 `json:"temperature"` //温度
  46. BatteryLevel float64 `json:"batteryLevel"` //电压
  47. Csq int `json:"csq"` //信号
  48. LastPostTime common.Time `gorm:"type:datetime" json:"lastPostTime"` //最后上报时间
  49. }
  50. func (ManholeCover) TableName() string {
  51. return "device_manhole_cover"
  52. }
  53. func (c ManholeCover) Delete() error {
  54. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  55. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  56. }
  57. func (c ManholeCover) IsExistedBySN() bool {
  58. var count int64
  59. _ = Db.Model(&c).Where(" manhole_sn = ? and is_deleted = ?",
  60. c.ManholeSn, c.IsDeleted).Count(&count).Error
  61. return count > 0
  62. }
  63. func (c ManholeCover) IsExistedByNameAndCode() bool {
  64. var devices []ManholeCover
  65. err := Db.Model(&c).Where(" manhole_sn = ? and is_deleted = ?",
  66. c.ManholeSn, c.IsDeleted).Find(&devices).Error
  67. //如果查询不到,返回相应的错误
  68. if gorm.ErrRecordNotFound == err {
  69. return false
  70. }
  71. for _, d := range devices {
  72. if d.ID != c.ID {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. func (c *ManholeCover) Create() error {
  79. return Db.Model(&c).Save(&c).Error
  80. }
  81. func (c *ManholeCover) Update() error {
  82. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  83. }
  84. func (c *ManholeCover) GetDevice() error {
  85. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  86. return err
  87. }
  88. func (c ManholeCover) GetDevices(offset, limit int) ([]ManholeCover, int64, error) {
  89. var devices []ManholeCover
  90. var count int64
  91. db := Db.Model(&c).Where(" manhole_name like ? and is_deleted = 0", "%"+c.ManholeName+"%")
  92. db.Count(&count)
  93. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  94. return devices, count, err
  95. }
  96. func (c ManholeCover) GetAllDevices() ([]*ManholeCover, error) {
  97. var devices []*ManholeCover
  98. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  99. return devices, err
  100. }
  101. func (c ManholeCover) FindDeviceIdByImei(imei string) (int, error) {
  102. var device ManholeCover
  103. err := Db.Raw("SELECT * FROM `device_manhole_cover` WHERE `device_i_mei` = ? AND `is_deleted` = '0' limit 1", imei).First(&device).Error
  104. return device.ID, err
  105. }
  106. func (c ManholeCover) DeviceCount1() (int64, int64) {
  107. var all, fault int64
  108. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  109. if err != nil {
  110. all = -1
  111. }
  112. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  113. if err != nil {
  114. fault = -1
  115. }
  116. return all, fault
  117. }
  118. func (c ManholeCover) ListDevices1() []Device {
  119. var devices []ManholeCover
  120. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  121. var rsp = make([]Device, 0, len(devices))
  122. for _, v := range devices {
  123. var d = Device{
  124. Lng: v.Longitude,
  125. Lat: v.Latitude,
  126. Status: v.Status,
  127. Name: v.ManholeName,
  128. Code: v.ManholeSn,
  129. LampPoleName: v.WayName,
  130. }
  131. rsp = append(rsp, d)
  132. }
  133. return rsp
  134. }