gate_unit.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 TGateUnit struct {
  12. ID int64 `gorm:"column:id;Primary_key" json:"id"`
  13. DeviceId int64 `gorm:"column:device_id" json:"device_id" form:"device_id"`
  14. GardenId int64 `gorm:"column:garden_id" json:"garden_id" form:"garden_id"`
  15. UnitId int64 `gorm:"column:unit_id" json:"unit_id" form:"unit_id"`
  16. Sn string `gorm:"column:sn" json:"sn" form:"sn"`
  17. Protocol int32 `gorm:"column:protocol" json:"protocol" form:"protocol"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  19. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  20. }
  21. func (p *TGateUnit) TableName() string {
  22. return "t_gate_unit"
  23. }
  24. func (p *TGateUnit) Find(db *gorm.DB, where [][2]interface{}) error {
  25. cond, val, err := whereBuild(where)
  26. if err != nil {
  27. if err != nil {
  28. fields, _ := util.MarshalToString(where)
  29. logger.Error("mysql",
  30. zap.String("sql", "find "+p.TableName()),
  31. zap.String("fields", fields),
  32. zap.String("error", err.Error()))
  33. }
  34. return err
  35. }
  36. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  37. if err != nil {
  38. fields, _ := util.MarshalToString(where)
  39. logger.Error("mysql",
  40. zap.String("sql", "select from "+p.TableName()),
  41. zap.String("fields", fields),
  42. zap.String("error", err.Error()))
  43. }
  44. return err
  45. }
  46. func (p *TGateUnit) Last(db *gorm.DB) error {
  47. err := db.Table(p.TableName()).Last(p).Error
  48. if err != nil {
  49. logger.Error("mysql",
  50. zap.String("sql", "select last from "+p.TableName()),
  51. zap.String("fields", ""),
  52. zap.String("error", err.Error()))
  53. }
  54. return err
  55. }
  56. // Insert 插入一条记录
  57. func (p *TGateUnit) Insert(db *gorm.DB) error {
  58. err := db.Create(p).Error
  59. if err != nil {
  60. fields, _ := util.MarshalToString(*p)
  61. logger.Error("mysql",
  62. zap.String("sql", "insert into "+p.TableName()),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. // Insert 插入多条记录
  69. func (p *TGateUnit) InsertMulti(db *gorm.DB, values interface{}) error {
  70. err := db.Table(p.TableName()).Create(values).Error
  71. if err != nil {
  72. fields, _ := util.MarshalToString(*p)
  73. logger.Error("mysql",
  74. zap.String("sql", "insert into "+p.TableName()),
  75. zap.String("fields", fields),
  76. zap.String("error", err.Error()))
  77. }
  78. return err
  79. }
  80. func (p *TGateUnit) Delete(db *gorm.DB, filter [][2]interface{}) error {
  81. cond, val, err := whereBuild(filter)
  82. if err != nil {
  83. if err != nil {
  84. fields, _ := util.MarshalToString(filter)
  85. logger.Error("mysql",
  86. zap.String("sql", "delete "+p.TableName()),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return err
  91. }
  92. err = db.Where(cond, val...).Delete(p).Error
  93. if err != nil {
  94. fields, _ := util.MarshalToString(filter)
  95. logger.Error("mysql",
  96. zap.String("sql", "delete from "+p.TableName()),
  97. zap.String("where", fields),
  98. zap.String("error", err.Error()))
  99. }
  100. return err
  101. }
  102. func (p *TGateUnit) Update(db *gorm.DB, where [][2]interface{}, values map[string]interface{}) error {
  103. cond, val, err := whereBuild(where)
  104. if err != nil {
  105. if err != nil {
  106. fields, _ := util.MarshalToString(values)
  107. logger.Error("mysql",
  108. zap.String("sql", "update "+p.TableName()),
  109. zap.String("fields", fields),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }
  114. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  115. }
  116. func (p *TGateUnit) Count(db *gorm.DB, where [][2]interface{}, or [][2]interface{}) (int64, error) {
  117. cond, val, err := whereBuildAndOr(where, or)
  118. if err != nil {
  119. return 0, err
  120. }
  121. ret := int64(0)
  122. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  123. if err != nil {
  124. fields, _ := util.MarshalToString(where)
  125. logger.Error("mysql",
  126. zap.String("sql", "select count "+p.TableName()),
  127. zap.String("fields", fields),
  128. zap.String("error", err.Error()))
  129. }
  130. return ret, err
  131. }
  132. func (p *TGateUnit) List(db *gorm.DB, where [][2]interface{}, or [][2]interface{}, page int, pageSize int) (list []TGateUnit, err error) {
  133. cond, val, err := whereBuildAndOr(where, or)
  134. if err != nil {
  135. return list, err
  136. }
  137. if pageSize < 0 {
  138. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  139. if result.Error != nil {
  140. wherefields, _ := util.MarshalToString(where)
  141. logger.Error("mysql",
  142. zap.String("sql", "select * from "+p.TableName()),
  143. zap.String("where", wherefields),
  144. zap.String("error", result.Error.Error()))
  145. }
  146. return list, result.Error
  147. }
  148. offset := (page - 1) * pageSize
  149. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  150. if result.Error != nil {
  151. wherefields, _ := util.MarshalToString(where)
  152. logger.Error("mysql",
  153. zap.String("sql", "select * from "+p.TableName()),
  154. zap.String("where", wherefields),
  155. zap.String("error", result.Error.Error()))
  156. }
  157. return list, result.Error
  158. }
  159. func (p *TGateUnit) SelectList(db *gorm.DB, where [][2]interface{}, selectStr string, ret interface{}) (err error) {
  160. cond, val, err := whereBuildAndOr(where, nil)
  161. if err != nil {
  162. return err
  163. }
  164. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
  165. if result.Error != nil {
  166. wherefields, _ := util.MarshalToString(where)
  167. logger.Error("mysql",
  168. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  169. zap.String("where", wherefields),
  170. zap.String("error", result.Error.Error()))
  171. }
  172. return result.Error
  173. }