switchBoxDao.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  28. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  29. UpdateUser int `gorm:"type:int" 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.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.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.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.Model(&c).Save(&c).Error
  66. }
  67. func (c *SwitchBox) Update() error {
  68. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  69. }
  70. func (c *SwitchBox) GetDevice() error {
  71. err := Db.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.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.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.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  93. return devices, err
  94. }
  95. // CountFault SELECT COUNT(id) FROM `device_lamp_pole` WHERE `is_deleted` = 0 AND `status` = 0;
  96. func (c SwitchBox) CountFault() (int64, error) {
  97. var i int64
  98. err := Db.Model(&c).Where(map[string]interface{}{"is_deleted": "0", "status": "0"}).Count(&i).Error
  99. return i, err
  100. }
  101. func (c SwitchBox) ListDevices() ([]Device, int64, error) {
  102. var devices []SwitchBox
  103. result := Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  104. var rsp = make([]Device, 0, len(devices))
  105. for _, v := range devices {
  106. var d = Device{
  107. Lng: v.PoleLng,
  108. Lat: v.PoleLat,
  109. Status: v.Status,
  110. Name: v.BoxName,
  111. Code: v.BoxSN,
  112. Model: "型号",
  113. }
  114. rsp = append(rsp, d)
  115. }
  116. var i int64
  117. result.Count(&i)
  118. return rsp, i, result.Error
  119. }
  120. func (c SwitchBox) DeviceCount1() (int64, int64) {
  121. var all, fault int64
  122. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  123. if err != nil {
  124. all = -1
  125. }
  126. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  127. if err != nil {
  128. fault = -1
  129. }
  130. return all, fault
  131. }
  132. func (c SwitchBox) ListDevices1() []Device {
  133. var devices []SwitchBox
  134. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  135. var rsp = make([]Device, 0, len(devices))
  136. for _, v := range devices {
  137. var d = Device{
  138. Lng: v.PoleLng,
  139. Lat: v.PoleLat,
  140. Status: v.Status,
  141. Name: v.BoxName,
  142. Code: v.BoxSN,
  143. Model: "模型",
  144. }
  145. rsp = append(rsp, d)
  146. }
  147. return rsp
  148. }