device.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/errors"
  6. "time"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jinzhu/gorm"
  9. "go.uber.org/zap"
  10. json2 "encoding/json"
  11. )
  12. // 替换encoding/json包
  13. type JsonStruct struct {
  14. }
  15. func (p *JsonStruct)MarshalToString(v interface{}) (string, error) {
  16. bytes, err := json2.Marshal(v)
  17. return string(bytes), err
  18. }
  19. func (p *JsonStruct)Marshal(v interface{}) ([]byte, error) {
  20. bytes, err := json2.Marshal(v)
  21. return bytes, err
  22. }
  23. func (p *JsonStruct)Unmarshal(bytes []byte, v interface{}) (error) {
  24. err := json2.Unmarshal(bytes, v)
  25. return err
  26. }
  27. var json = &JsonStruct{}
  28. type DeviceAll struct {
  29. ID int64 `gorm:"column:ID" json:"id"`
  30. SN string `gorm:"column:SN" json:"sn"`
  31. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  32. Longitude string `gorm:"column:Longitude" json:"longitude"`
  33. Latitude string `gorm:"column:Latitude" json:"latitude"`
  34. DeviceCode uint32 `gorm:"column:DeviceCode"`
  35. Key string `gorm:"column:Key"`
  36. DeviceName string `gorm:"column:DeviceName"`
  37. SubId int64 `gorm:"column:SubId"`
  38. ProjectId int64 `gorm:"column:ProjectId"`
  39. ProviderId int64 `gorm:"column:ProviderId"`
  40. CreatedAt time.Time `gorm:"column:CreatedAt"`
  41. VerifyTime time.Time `gorm:"column:VerifyTime"`
  42. VerifyStatus int `gorm:"column:VerifyStatus"`
  43. ChannelId int64 `gorm:"column:ChannelId"`
  44. }
  45. func (DeviceAll) TableName() string {
  46. return "DeviceAll"
  47. }
  48. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  49. func (p *DeviceAll) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  50. if filed == nil {
  51. return errors.ParamsError
  52. }
  53. err := db.Model(p).Updates(filed).Error
  54. if err != nil {
  55. fields, _ := json.MarshalToString(filed)
  56. logger.Error("mysql",
  57. zap.String("sql", "update t_device"),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *DeviceAll) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  64. cond, val, err := whereBuild(where)
  65. if err != nil {
  66. return err
  67. }
  68. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  69. }
  70. func (p *DeviceAll) Query(db *gorm.DB, filter map[string]interface{}) error {
  71. err := db.Where(filter).Find(p).Error
  72. if err != nil {
  73. fields, _ := json.MarshalToString(filter)
  74. logger.Error("mysql",
  75. zap.String("sql", "select from t_device"),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *DeviceAll) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []DeviceAll, err error) {
  82. err = db.Where(filter).Find(&list).Error
  83. if err != nil {
  84. fields, _ := json.MarshalToString(filter)
  85. logger.Error("mysql",
  86. zap.String("sql", "select from t_device"),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return list, err
  91. }