visitor.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 map[string]interface{}) error {
  32. err := db.Table(p.TableName()).Where(where).First(p).Error
  33. if err != nil {
  34. fields, _ := util.MarshalToString(where)
  35. logger.Error("mysql",
  36. zap.String("sql", "select from "+p.TableName()),
  37. zap.String("fields", fields),
  38. zap.String("error", err.Error()))
  39. }
  40. return err
  41. }
  42. func (p *TGateVisitor) Last(db *gorm.DB) error {
  43. err := db.Table(p.TableName()).Last(p).Error
  44. if err != nil {
  45. logger.Error("mysql",
  46. zap.String("sql", "select last from "+p.TableName()),
  47. zap.String("fields", ""),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. // Insert 插入一条记录
  53. func (p *TGateVisitor) Insert(db *gorm.DB) error {
  54. err := db.Create(p).Error
  55. if err != nil {
  56. fields, _ := util.MarshalToString(*p)
  57. logger.Error("mysql",
  58. zap.String("sql", "insert into "+p.TableName()),
  59. zap.String("fields", fields),
  60. zap.String("error", err.Error()))
  61. }
  62. return err
  63. }
  64. // Insert 插入多条记录
  65. func (p *TGateVisitor) InsertMulti(db *gorm.DB, values interface{}) error {
  66. err := db.Table(p.TableName()).Create(values).Error
  67. if err != nil {
  68. fields, _ := util.MarshalToString(*p)
  69. logger.Error("mysql",
  70. zap.String("sql", "insert into "+p.TableName()),
  71. zap.String("fields", fields),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. func (p *TGateVisitor) Delete(db *gorm.DB, filter map[string]interface{}) error {
  77. err := db.Where(filter).Delete(p).Error
  78. if err != nil {
  79. fields, _ := util.MarshalToString(filter)
  80. logger.Error("mysql",
  81. zap.String("sql", "delete from "+p.TableName()),
  82. zap.String("where", fields),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. func (p *TGateVisitor) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  88. cond, val, err := whereBuild(where)
  89. if err != nil {
  90. if err != nil {
  91. fields, _ := util.MarshalToString(values)
  92. logger.Error("mysql",
  93. zap.String("sql", "update "+p.TableName()),
  94. zap.String("fields", fields),
  95. zap.String("error", err.Error()))
  96. }
  97. return err
  98. }
  99. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  100. }
  101. func (p *TGateVisitor) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  102. cond, val, err := whereBuildAndOr(where, or)
  103. if err != nil {
  104. return 0, err
  105. }
  106. ret := int64(0)
  107. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  108. if err != nil {
  109. fields, _ := util.MarshalToString(where)
  110. logger.Error("mysql",
  111. zap.String("sql", "select count "+p.TableName()),
  112. zap.String("fields", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return ret, err
  116. }
  117. func (p *TGateVisitor) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGateVisitor, err error) {
  118. cond, val, err := whereBuildAndOr(where, or)
  119. if err != nil {
  120. return list, err
  121. }
  122. if pageSize < 0 {
  123. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  124. if result.Error != nil {
  125. wherefields, _ := util.MarshalToString(where)
  126. logger.Error("mysql",
  127. zap.String("sql", "select * from "+p.TableName()),
  128. zap.String("where", wherefields),
  129. zap.String("error", result.Error.Error()))
  130. }
  131. return list, result.Error
  132. }
  133. offset := (page - 1) * pageSize
  134. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  135. if result.Error != nil {
  136. wherefields, _ := util.MarshalToString(where)
  137. logger.Error("mysql",
  138. zap.String("sql", "select * from "+p.TableName()),
  139. zap.String("where", wherefields),
  140. zap.String("error", result.Error.Error()))
  141. }
  142. return list, result.Error
  143. }