switchBoxDao.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. // SwitchBox 供电箱
  8. type SwitchBox struct {
  9. ID int `gorm:"primary_key" json:"id"` //编号
  10. BoxName string `gorm:"type:varchar(64)" json:"boxName"` //供电箱名称
  11. BoxSN string `gorm:"type:varchar(60)" json:"boxSn"` //设备序列号
  12. BrandId int `gorm:"type:int" json:"brandId"` //品牌
  13. ModelId int `gorm:"type:int" json:"modelId"` //配电箱型号
  14. TransformerId int `gorm:"type:int" json:"transformerId"` //变压器ID
  15. TransformerNum string `gorm:"type:varchar(60)" json:"transformerNum"` //变压器出线口编号
  16. RatedPower float32 `gorm:"type:double(8, 2) default 0.00 " json:"ratedPower"` //功率
  17. ProvinceName string `gorm:" type:varchar(12)" json:"provinceName"` //省份
  18. CityName string `gorm:"type:varchar(12)" json:"cityName"` //市
  19. DistrictName string `gorm:"type:varchar(12)" json:"districtName"` //区
  20. BoxLocation string `gorm:"type:varchar(100)" json:"boxLocation"` //详细位置
  21. PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"` //经度
  22. PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"` //纬度
  23. MonitorAddress string `gorm:"type:varchar(255) " json:"monitorAddress"` //监控地址
  24. InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
  25. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  26. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  27. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  28. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  29. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  30. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  31. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  32. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  33. BoxPhotoUrl string `gorm:"type:varchar(255)" json:"boxPhotoUrl"` //供电箱照片地址
  34. CountLampPole int64 `gorm:"-" json:"countLampPole" comment:"关联灯杆数" default:0`
  35. }
  36. func (SwitchBox) TableName() string {
  37. return "device_switch_box"
  38. }
  39. func (c SwitchBox) Delete() error {
  40. return Db.Debug().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 SwitchBox) IsExistedBySN() bool {
  44. var count int64
  45. _ = Db.Debug().Model(&c).Where(" box_sn = ? and is_deleted = ?",
  46. c.BoxSN, c.IsDeleted).Count(&count).Error
  47. return count > 0
  48. }
  49. func (c SwitchBox) IsExistedByNameAndCode() bool {
  50. var devices []SwitchBox
  51. err := Db.Debug().Model(&c).Where(" box_sn = ? and is_deleted = ?",
  52. c.BoxSN, c.IsDeleted).Find(&devices).Error
  53. //如果查询不到,返回相应的错误
  54. if gorm.ErrRecordNotFound == err {
  55. return false
  56. }
  57. for _, d := range devices {
  58. if d.ID != c.ID {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. func (c *SwitchBox) Create() error {
  65. return Db.Debug().Model(&c).Save(&c).Error
  66. }
  67. func (c *SwitchBox) Update() error {
  68. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  69. }
  70. func (c *SwitchBox) GetDevice() error {
  71. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  72. return err
  73. }
  74. func (c SwitchBox) GetDevices(offset, limit int) ([]SwitchBox, int64, error) {
  75. var devices []SwitchBox
  76. var count int64
  77. db := Db.Debug().Model(&c).Where("(box_name like ?) and is_deleted = 0",
  78. "%"+c.BoxName+"%")
  79. db.Count(&count)
  80. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  81. return devices, count, err
  82. }
  83. //得到关联设备 2022-12-08 dsx
  84. func (c SwitchBox) GetDeviceCount() (int64, error) {
  85. var count int64
  86. err := Db.Debug().Model(&c).Where("(transformer_id = ?) and is_deleted = 0",
  87. c.TransformerId).Count(&count).Error
  88. return count, err
  89. }
  90. func (c SwitchBox) GetAllDevices() ([]*SwitchBox, error) {
  91. var devices []*SwitchBox
  92. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  93. return devices, err
  94. }