gate_pic.go 5.6 KB

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