t_user_card.go 5.7 KB

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