device.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "smart-auth/consts"
  6. "smart-auth/errors"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jinzhu/gorm"
  10. jsoniter "github.com/json-iterator/go"
  11. "go.uber.org/zap"
  12. )
  13. // 替换encoding/json包
  14. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  15. type TDevice struct {
  16. Id uint64 `gorm:"primary_key"`
  17. ProjectId int64 `gorm:"column:project_id"`
  18. ProviderId int64 `gorm:"column:provider_id"`
  19. Name string `gorm:"column:name"`
  20. Sn string `gorm:"column:sn"`
  21. Addr string `gorm:"column:addr"`
  22. DeviceCode int32 `gorm:"column:device_code"`
  23. Lon float32 `gorm:"column:lon"`
  24. Lat float32 `gorm:"column:lat"`
  25. XCoord float32 `gorm:"column:x_coord"`
  26. YCoord float32 `gorm:"column:y_coord"`
  27. Status int `gorm:"column:status"`
  28. VerifyStatus int `gorm:"column:verify_status"`
  29. Key string `gorm:"column:key"`
  30. CreatedAt string `gorm:"column:created_at"`
  31. UpdatedAt string `gorm:"column:updated_at"`
  32. }
  33. func (TDevice) TableName() string {
  34. return "t_device"
  35. }
  36. func (p *TDevice) Insert(db *gorm.DB) error {
  37. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  38. p.CreatedAt = timeNow
  39. p.UpdatedAt = timeNow
  40. err := db.Create(p).Error
  41. if err != nil {
  42. fields, _ := json.MarshalToString(*p)
  43. logger.Error("mysql",
  44. zap.String("sql", "insert into t_device"),
  45. zap.String("fields", fields),
  46. zap.String("error", err.Error()))
  47. }
  48. return err
  49. }
  50. func (p *TDevice) Delete(db *gorm.DB, filter map[string]interface{}) error {
  51. err := db.Where(filter).Delete(p).Error
  52. if err != nil {
  53. fields, _ := json.MarshalToString(filter)
  54. logger.Error("mysql",
  55. zap.String("sql", "delete from t_device"),
  56. zap.String("fields", fields),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  62. func (p *TDevice) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  63. if filed == nil {
  64. return errors.ParamsError
  65. }
  66. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  67. filed["updated_at"] = timeNow
  68. err := db.Model(p).Updates(filed).Error
  69. if err != nil {
  70. fields, _ := json.MarshalToString(filed)
  71. logger.Error("mysql",
  72. zap.String("sql", "update t_device"),
  73. zap.String("fields", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. func (p *TDevice) Query(db *gorm.DB, filter map[string]interface{}) error {
  79. err := db.Where(filter).Find(p).Error
  80. if err != nil {
  81. fields, _ := json.MarshalToString(filter)
  82. logger.Error("mysql",
  83. zap.String("sql", "select from t_device"),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. func (p *TDevice) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TDevice, err error) {
  90. err = db.Where(filter).Find(&list).Error
  91. if err != nil {
  92. fields, _ := json.MarshalToString(filter)
  93. logger.Error("mysql",
  94. zap.String("sql", "select from t_device"),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return list, err
  99. }