user_pic.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 TUserPic 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. PicUrl string `gorm:"column:pic_url" json:"pic_url"`
  16. HouseName string `gorm:"column:house_name" json:"house_name"`
  17. UserType int32 `gorm:"column:user_type" json:"user_type"`
  18. ApproveStatus int32 `gorm:"column:approve_status" json:"approve_status"`
  19. ApprovedAt int64 `gorm:"column:approved_at" json:"approved_at"`
  20. IdNumber string `gorm:"column:id_number" json:"id_number"`
  21. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  22. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  23. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  24. Feedback string `gorm:"column:feedback" json:"feedback"`
  25. DownStatus int32 `gorm:"column:down_status" json:"down_status"`
  26. Phone string `gorm:"column:phone" json:"phone"`
  27. }
  28. func (p *TUserPic) TableName() string {
  29. return "t_user_pic"
  30. }
  31. func (p *TUserPic) 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 *TUserPic) 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 *TUserPic) 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 *TUserPic) 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 *TUserPic) 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 *TUserPic) 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 *TUserPic) 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 *TUserPic) List(db *gorm.DB, where [][2]interface{}, or [][2]interface{}, page int, pageSize int) (list []TUserPic, 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. }
  166. func (p *TUserPic) SelectList(db *gorm.DB, where [][2]interface{}, selectStr string, ret interface{}) (err error) {
  167. cond, val, err := whereBuildAndOr(where, nil)
  168. if err != nil {
  169. return err
  170. }
  171. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
  172. if result.Error != nil {
  173. wherefields, _ := util.MarshalToString(where)
  174. logger.Error("mysql",
  175. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  176. zap.String("where", wherefields),
  177. zap.String("error", result.Error.Error()))
  178. }
  179. return result.Error
  180. }