gate.go 6.7 KB

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