gate.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TGate struct {
  12. ID string `gorm:"column:id" json:"id"`
  13. DeviceName string `gorm:"column:device_name" json:"device_name"`
  14. Sn string `gorm:"column:sn" json:"sn"`
  15. Manufactor string `gorm:"column:manufactor" json:"manufactor"`
  16. AuthKey string `gorm:"column:auth_key" json:"auth_key"`
  17. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  18. OutUser string `gorm:"column:out_user" json:"out_user"`
  19. OutTime int64 `gorm:"column:out_time" json:"out_time"`
  20. Status int32 `gorm:"column:status" json:"status"`
  21. GateType int32 `gorm:"column:gate_type" json:"gate_type"`
  22. Enable int32 `gorm:"column:enable" json:"enable"`
  23. Location string `gorm:"column:location" json:"location"`
  24. Protocol string `gorm:"column:protocol" json:"protocol"`
  25. Direction int32 `gorm:"column:direction" json:"direction"`
  26. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  27. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  28. }
  29. func (p *TGate) TableName() string {
  30. return "t_gate"
  31. }
  32. func (p *TGate) Find(db *gorm.DB, where map[string]interface{}) error {
  33. err := db.Table(p.TableName()).Where(where).First(p).Error
  34. if err != nil {
  35. fields, _ := util.MarshalToString(where)
  36. logger.Error("mysql",
  37. zap.String("sql", "select from "+p.TableName()),
  38. zap.String("fields", fields),
  39. zap.String("error", err.Error()))
  40. }
  41. return err
  42. }
  43. func (p *TGate) Last(db *gorm.DB) error {
  44. err := db.Table(p.TableName()).Last(p).Error
  45. if err != nil {
  46. logger.Error("mysql",
  47. zap.String("sql", "select last from "+p.TableName()),
  48. zap.String("fields", ""),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. // Insert 插入一条记录
  54. func (p *TGate) Insert(db *gorm.DB) error {
  55. err := db.Create(p).Error
  56. if err != nil {
  57. fields, _ := util.MarshalToString(*p)
  58. logger.Error("mysql",
  59. zap.String("sql", "insert into "+p.TableName()),
  60. zap.String("fields", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. // Insert 插入多条记录
  66. func (p *TGate) InsertMulti(db *gorm.DB, values interface{}) error {
  67. err := db.Table(p.TableName()).Create(values).Error
  68. if err != nil {
  69. fields, _ := util.MarshalToString(*p)
  70. logger.Error("mysql",
  71. zap.String("sql", "insert into "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. func (p *TGate) Delete(db *gorm.DB, filter map[string]interface{}) error {
  78. err := db.Where(filter).Delete(p).Error
  79. if err != nil {
  80. fields, _ := util.MarshalToString(filter)
  81. logger.Error("mysql",
  82. zap.String("sql", "delete from "+p.TableName()),
  83. zap.String("where", fields),
  84. zap.String("error", err.Error()))
  85. }
  86. return err
  87. }
  88. func (p *TGate) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  89. cond, val, err := whereBuild(where)
  90. if err != nil {
  91. if err != nil {
  92. fields, _ := util.MarshalToString(values)
  93. logger.Error("mysql",
  94. zap.String("sql", "update "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  101. }
  102. func (p *TGate) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  103. cond, val, err := whereBuildAndOr(where, or)
  104. if err != nil {
  105. return 0, err
  106. }
  107. ret := int64(0)
  108. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  109. if err != nil {
  110. fields, _ := util.MarshalToString(where)
  111. logger.Error("mysql",
  112. zap.String("sql", "select count "+p.TableName()),
  113. zap.String("fields", fields),
  114. zap.String("error", err.Error()))
  115. }
  116. return ret, err
  117. }
  118. func (p *TGate) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGate, err error) {
  119. cond, val, err := whereBuildAndOr(where, or)
  120. if err != nil {
  121. return list, err
  122. }
  123. if pageSize < 0 {
  124. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  125. if result.Error != nil {
  126. wherefields, _ := util.MarshalToString(where)
  127. logger.Error("mysql",
  128. zap.String("sql", "select * from "+p.TableName()),
  129. zap.String("where", wherefields),
  130. zap.String("error", result.Error.Error()))
  131. }
  132. return list, result.Error
  133. }
  134. offset := (page - 1) * pageSize
  135. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  136. if result.Error != nil {
  137. wherefields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select * from "+p.TableName()),
  140. zap.String("where", wherefields),
  141. zap.String("error", result.Error.Error()))
  142. }
  143. return list, result.Error
  144. }