tower_device.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package model
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. type TowerDeviceInfo struct {
  7. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"ID"`
  8. Code string `gorm:"column:Code" json:"code"`
  9. Name string `gorm:"column:Name" json:"name"`
  10. DeviceId string `gorm:"column:DeviceId" json:"deviceid"`
  11. DeviceModel string `gorm:"column:DeviceModel" json:"devicemodel"`
  12. DeviceName string `gorm:"column:DeviceName" json:"devicename"`
  13. Manufacturer string `gorm:"column:Manufacturer" json:"manufacturer"`
  14. Batch string `gorm:"column:Batch" json:"batch"`
  15. SN string `gorm:"column:SN" json:"sn"`
  16. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  17. Unit string `gorm:"column:Unit" json:"unit"`
  18. Person string `gorm:"column:Person" json:"person"`
  19. Phone string `gorm:"column:phone" json:"phone"`
  20. Remark string `gorm:"column:remark" json:"remark"`
  21. Longitude string `gorm:"column:Longitude" json:"longitude"`
  22. Latitude string `gorm:"column:Latitude" json:"latitude"`
  23. VerifyTime time.Time `gorm:"column:VerifyTime"`
  24. VerifyStatus int `gorm:"column:VerifyStatus"`
  25. Key string `gorm:"column:Key"`
  26. ProjectId int64 `gorm:"column:ProjectId"`
  27. ProviderId int64 `gorm:"column:ProviderId"`
  28. CreatedAt time.Time `gorm:"column:CreatedAt"`
  29. ChannelId int64 `gorm:"column:ChannelId"`
  30. }
  31. func (TowerDeviceInfo) TableName() string {
  32. return "db_smart_v2.TowerDeviceInfo"
  33. }
  34. func (p *TowerDeviceInfo) Insert(db *gorm.DB) error {
  35. return db.Table(p.TableName()).Create(p).Error
  36. }
  37. func (p *TowerDeviceInfo) Del(db *gorm.DB, where map[string]interface{}) error {
  38. cond, val, err := whereBuild(where)
  39. if err != nil {
  40. return err
  41. }
  42. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  43. }
  44. func (p *TowerDeviceInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  45. cond, val, err := whereBuild(where)
  46. if err != nil {
  47. return err
  48. }
  49. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  50. }
  51. func (p *TowerDeviceInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  52. cond, val, err := whereBuild(where)
  53. if err != nil {
  54. return err
  55. }
  56. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  57. }
  58. func (p *TowerDeviceInfo) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  59. cond, val, err := whereBuild(where)
  60. if err != nil {
  61. return err
  62. }
  63. ps := []TowerDeviceInfo{}
  64. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  65. if err != nil {
  66. return err
  67. }
  68. if len(ps) > 0 {
  69. *p = ps[0]
  70. }
  71. return nil
  72. }
  73. func (p *TowerDeviceInfo) Save(db *gorm.DB) error {
  74. return db.Save(p).Error
  75. }
  76. func (p *TowerDeviceInfo) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  77. if len(where) > 0 {
  78. cond, val, err := whereBuild(where)
  79. if err != nil {
  80. return 0, err
  81. }
  82. ret := int64(0)
  83. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  84. return ret, err
  85. }
  86. ret := int64(0)
  87. err := db.Table(p.TableName()).Count(&ret).Error
  88. return ret, err
  89. }
  90. func (p *TowerDeviceInfo) All(db *gorm.DB) (list []TowerDeviceInfo, err error) {
  91. result := db.Table(p.TableName()).Find(&list)
  92. return list, result.Error
  93. }