transformerDao.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dao
  2. import (
  3. "github.com/jinzhu/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 int `gorm:"type:int" json:"tenantId"` //租户id
  21. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  22. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  23. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  24. UpdateUser int64 `gorm:"type:bigint" 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. }
  28. func (Transformer) TableName() string {
  29. return "t_dev_transformer"
  30. }
  31. func (c Transformer) Delete() error {
  32. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  33. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  34. }
  35. func (c Transformer) IsExistedBySN() bool {
  36. var count = 0
  37. _ = Db.Debug().Model(&c).Where(" trans_sn = ? and is_deleted = ?",
  38. c.TransSN, c.IsDeleted).Count(&count).Error
  39. return count > 0
  40. }
  41. func (c Transformer) IsExistedByNameAndCode() bool {
  42. var devices []Transformer
  43. err := Db.Debug().Model(&c).Where(" trans_sn = ? and is_deleted = ?",
  44. c.TransSN, c.IsDeleted).Find(&devices).Error
  45. //如果查询不到,返回相应的错误
  46. if gorm.IsRecordNotFoundError(err) {
  47. return false
  48. }
  49. for _, d := range devices {
  50. if d.ID != c.ID {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. func (c *Transformer) Create() error {
  57. return Db.Debug().Model(&c).Save(&c).Error
  58. }
  59. func (c *Transformer) Update() error {
  60. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  61. }
  62. func (c *Transformer) GetDevice() error {
  63. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  64. return err
  65. }
  66. func (c Transformer) GetDevices(offset, limit int) ([]Transformer, error) {
  67. var devices []Transformer
  68. err := Db.Debug().Model(&c).Where(" trans_name like ? and is_deleted = 0", "%"+c.TransName+"%").Offset(offset).Limit(limit).Find(&devices).Error
  69. return devices, err
  70. }
  71. func (c Transformer) GetAllDevices() ([]*Transformer, error) {
  72. var devices []*Transformer
  73. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  74. return devices, err
  75. }