transformerDao.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "time"
  5. )
  6. // Transformer 变压器
  7. type Transformer struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. TransName string `gorm:"type:varchar(60)" json:"transName"` //变压器名称
  10. TransSN string `gorm:"type:varchar(60)" json:"transSn"` //设备序列号
  11. RatedPower float32 `gorm:"type:double(8, 2) default 0.00 " json:"ratedPower"` //功率
  12. ProvinceName string `gorm:" varchar(12)" json:"provinceName"` //省份
  13. CityName string `gorm:"type:varchar(12)" json:"cityName"` //市
  14. DistrictName string `gorm:"type:varchar(12)" json:"districtName"` //区
  15. InstallLocation string `gorm:"type:varchar(100)" json:"installLocation"` //详细位置
  16. PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"` //经度
  17. PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"` //纬度
  18. TransPhotoUrl string `gorm:"type:varchar(255)" json:"transPhotoUrl"` //变压器照片地址
  19. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  20. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  21. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  22. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  23. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  24. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  25. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  26. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  27. CountNum int64 `gorm:"-" json:"countNum"` //关联供电箱数量
  28. }
  29. func (Transformer) TableName() string {
  30. return "device_transformer"
  31. }
  32. func (c Transformer) Delete() error {
  33. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  34. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  35. }
  36. func (c Transformer) IsExistedBySN() bool {
  37. var count int64
  38. _ = Db.Model(&c).Where(" trans_sn = ? and is_deleted = ?",
  39. c.TransSN, c.IsDeleted).Count(&count).Error
  40. return count > 0
  41. }
  42. func (c Transformer) IsExistedByNameAndCode() bool {
  43. var devices []Transformer
  44. err := Db.Model(&c).Where(" trans_sn = ? and is_deleted = ?",
  45. c.TransSN, c.IsDeleted).Find(&devices).Error
  46. //如果查询不到,返回相应的错误
  47. if gorm.ErrRecordNotFound == err {
  48. return false
  49. }
  50. for _, d := range devices {
  51. if d.ID != c.ID {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. func (c *Transformer) Create() error {
  58. return Db.Model(&c).Save(&c).Error
  59. }
  60. func (c *Transformer) Update() error {
  61. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  62. }
  63. func (c *Transformer) GetDevice() error {
  64. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  65. return err
  66. }
  67. func (c Transformer) GetDevices(offset, limit int) ([]Transformer, int64, error) {
  68. var count int64
  69. var devices []Transformer
  70. db := Db.Model(&c).Where(" trans_name like ? and is_deleted = 0", "%"+c.TransName+"%")
  71. db.Count(&count)
  72. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  73. return devices, count, err
  74. }
  75. func (c Transformer) GetAllDevices() ([]*Transformer, error) {
  76. var devices []*Transformer
  77. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  78. return devices, err
  79. }