gate_white.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TGateWhite struct {
  12. ID int64 `gorm:"column:id;Primary_key" json:"id"`
  13. DeviceId string `gorm:"column:device_id" json:"device_id"`
  14. CodeVal string `gorm:"column:code_val" json:"code_val"`
  15. CodeType int32 `gorm:"column:code_type" json:"code_type"`
  16. Status int32 `gorm:"column:status" json:"status"`
  17. CardOwner string `gorm:"column:card_owner" json:"card_owner"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  19. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  20. }
  21. func (p *TGateWhite) TableName() string {
  22. return "t_gate_white"
  23. }
  24. func (p *TGateWhite) Find(db *gorm.DB, where map[string]interface{}) error {
  25. err := db.Table(p.TableName()).Where(where).Find(p).Error
  26. if err != nil {
  27. fields, _ := util.MarshalToString(where)
  28. logger.Error("mysql",
  29. zap.String("sql", "select from "+p.TableName()),
  30. zap.String("fields", fields),
  31. zap.String("error", err.Error()))
  32. }
  33. return err
  34. }
  35. func (p *TGateWhite) Last(db *gorm.DB) error {
  36. err := db.Table(p.TableName()).Last(p).Error
  37. if err != nil {
  38. logger.Error("mysql",
  39. zap.String("sql", "select last from "+p.TableName()),
  40. zap.String("fields", ""),
  41. zap.String("error", err.Error()))
  42. }
  43. return err
  44. }
  45. // Insert 插入一条记录
  46. func (p *TGateWhite) Insert(db *gorm.DB) error {
  47. err := db.Create(p).Error
  48. if err != nil {
  49. fields, _ := util.MarshalToString(*p)
  50. logger.Error("mysql",
  51. zap.String("sql", "insert into "+p.TableName()),
  52. zap.String("fields", fields),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. func (p *TGateWhite) InsertMulti(db *gorm.DB, values interface{}) error {
  58. err := db.Table(p.TableName()).Create(values).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. func (p *TGateWhite) Delete(db *gorm.DB, filter map[string]interface{}) error {
  69. cond, val, err := whereBuild(filter)
  70. if err != nil {
  71. return err
  72. }
  73. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  74. }
  75. func (p *TGateWhite) DeleteOr(db *gorm.DB, filter map[string]interface{}, or map[string]interface{},) error {
  76. cond, val, err := whereBuildAndOr(filter, or)
  77. if err != nil {
  78. return err
  79. }
  80. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  81. }
  82. func (p *TGateWhite) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  83. cond, val, err := whereBuild(where)
  84. if err != nil {
  85. if err != nil {
  86. fields, _ := util.MarshalToString(values)
  87. logger.Error("mysql",
  88. zap.String("sql", "update "+p.TableName()),
  89. zap.String("fields", fields),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  95. }
  96. func (p *TGateWhite) UpdateOr(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, values map[string]interface{}) error {
  97. cond, val, err := whereBuildAndOr(where, or)
  98. if err != nil {
  99. if err != nil {
  100. fields, _ := util.MarshalToString(values)
  101. logger.Error("mysql",
  102. zap.String("sql", "update "+p.TableName()),
  103. zap.String("fields", fields),
  104. zap.String("error", err.Error()))
  105. }
  106. return err
  107. }
  108. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  109. }
  110. func (p *TGateWhite) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  111. cond, val, err := whereBuildAndOr(where, or)
  112. if err != nil {
  113. return 0, err
  114. }
  115. ret := int64(0)
  116. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  117. if err != nil {
  118. fields, _ := util.MarshalToString(where)
  119. logger.Error("mysql",
  120. zap.String("sql", "select count "+p.TableName()),
  121. zap.String("fields", fields),
  122. zap.String("error", err.Error()))
  123. }
  124. return ret, err
  125. }
  126. func (p *TGateWhite) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGateWhite, err error) {
  127. cond, val, err := whereBuildAndOr(where, or)
  128. if err != nil {
  129. return list, err
  130. }
  131. if pageSize < 0 {
  132. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  133. if result.Error != nil {
  134. wherefields, _ := util.MarshalToString(where)
  135. logger.Error("mysql",
  136. zap.String("sql", "select * from "+p.TableName()),
  137. zap.String("where", wherefields),
  138. zap.String("error", result.Error.Error()))
  139. }
  140. return list, result.Error
  141. }
  142. offset := (page - 1) * pageSize
  143. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  144. if result.Error != nil {
  145. wherefields, _ := util.MarshalToString(where)
  146. logger.Error("mysql",
  147. zap.String("sql", "select * from "+p.TableName()),
  148. zap.String("where", wherefields),
  149. zap.String("error", result.Error.Error()))
  150. }
  151. return list, result.Error
  152. }