switchBoxDao.go 4.5 KB

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