visitor.go 5.4 KB

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