household_user.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type THouseholdUser struct {
  13. ID int64 `gorm:"column:id" json:"id"`
  14. Phone string `gorm:"column:phone" json:"phone"`
  15. NickName string `gorm:"column:nick_name" json:"nick_name"`
  16. RealName string `gorm:"column:real_name" json:"real_name"`
  17. IdNumber string `gorm:"column:id_number" json:"id_number"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  19. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  20. OpenId string `gorm:"column:open_id" json:"open_id"`
  21. IdType int32 `gorm:"column:id_type" json:"id_type"`
  22. UnionId string `gorm:"column:union_id" json:"union_id"`
  23. Avatar string `gorm:"column:avatar" json:"avatar"`
  24. PublicOpenId string `gorm:"column:public_open_id" json:"public_open_id"`
  25. table string
  26. }
  27. func (p *THouseholdUser) TableName() string {
  28. return p.table
  29. }
  30. func NewHouseholdUser(database string) *THouseholdUser {
  31. return &THouseholdUser{table: fmt.Sprintf("%s.%s", database, "t_household_user")}
  32. }
  33. func (p *THouseholdUser) SetTable(database string) {
  34. p.table = fmt.Sprintf("%s.%s", database, "t_household_user")
  35. }
  36. func (p *THouseholdUser) CreateTable(db *gorm.DB) error {
  37. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  38. "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
  39. "`phone` varchar(16) COLLATE utf8mb4_bin NOT NULL," +
  40. "`nick_name` varchar(255) COLLATE utf8mb4_bin NOT NULL," +
  41. "`real_name` varchar(64) COLLATE utf8mb4_bin NOT NULL," +
  42. "`id_number` varchar(64) COLLATE utf8mb4_bin NOT NULL," +
  43. "`created_at` datetime NOT NULL," +
  44. "`updated_at` datetime NOT NULL," +
  45. "`open_id` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
  46. "`id_type` tinyint(1) NOT NULL COMMENT '1 身份证 2 港澳台居住证 3 港澳台同胞来往通行证 4 护照 5 军官证'," +
  47. "`union_id` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
  48. "`avatar` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '微信头像'," +
  49. "`public_open_id` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '公众号openid'," +
  50. "PRIMARY KEY (`id`)," +
  51. " UNIQUE KEY `phone` (`phone`,`open_id`) USING BTREE," +
  52. "UNIQUE KEY `open_id` (`open_id`) USING BTREE" +
  53. ") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  54. err := db.Exec(sql).Error
  55. if err != nil {
  56. logger.Error("mysql",
  57. zap.String("sql", "create table "+p.TableName()),
  58. zap.String("fields", ""),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *THouseholdUser) Find(db *gorm.DB, where map[string]interface{}) error {
  64. err := db.Table(p.TableName()).Where(where).Find(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(where)
  67. logger.Error("mysql",
  68. zap.String("sql", "select from "+p.TableName()),
  69. zap.String("fields", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *THouseholdUser) Last(db *gorm.DB) error {
  75. err := db.Table(p.TableName()).Last(p).Error
  76. if err != nil {
  77. logger.Error("mysql",
  78. zap.String("sql", "select last from "+p.TableName()),
  79. zap.String("fields", ""),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. // Insert 插入一条记录
  85. func (p *THouseholdUser) Insert(db *gorm.DB) error {
  86. err := db.Table(p.TableName()).Create(p).Error
  87. if err != nil {
  88. fields, _ := util.MarshalToString(*p)
  89. logger.Error("mysql",
  90. zap.String("sql", "insert into "+p.TableName()),
  91. zap.String("fields", fields),
  92. zap.String("error", err.Error()))
  93. }
  94. return err
  95. }
  96. // Insert 插入多条记录
  97. func (p *THouseholdUser) InsertMulti(db *gorm.DB, values interface{}) error {
  98. err := db.Table(p.TableName()).Create(values).Error
  99. if err != nil {
  100. fields, _ := util.MarshalToString(*p)
  101. logger.Error("mysql",
  102. zap.String("sql", "insert into "+p.TableName()),
  103. zap.String("fields", fields),
  104. zap.String("error", err.Error()))
  105. }
  106. return err
  107. }
  108. func (p *THouseholdUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  109. cond, val, err := whereBuild(filter)
  110. if err != nil {
  111. return err
  112. }
  113. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  114. }
  115. func (p *THouseholdUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  116. cond, val, err := whereBuild(where)
  117. if err != nil {
  118. if err != nil {
  119. fields, _ := util.MarshalToString(values)
  120. logger.Error("mysql",
  121. zap.String("sql", "update "+p.TableName()),
  122. zap.String("fields", fields),
  123. zap.String("error", err.Error()))
  124. }
  125. return err
  126. }
  127. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  128. }
  129. func (p *THouseholdUser) UpdateByModel(db *gorm.DB) error {
  130. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  131. if err != nil {
  132. fields, _ := util.MarshalToString(*p)
  133. logger.Error("mysql",
  134. zap.String("sql", "update "+p.TableName()),
  135. zap.String("fields", fields),
  136. zap.String("error", err.Error()))
  137. }
  138. return err
  139. }
  140. func (p *THouseholdUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  141. cond, val, err := whereBuildAndOr(where, or)
  142. if err != nil {
  143. return 0, err
  144. }
  145. ret := int64(0)
  146. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  147. if err != nil {
  148. fields, _ := util.MarshalToString(where)
  149. logger.Error("mysql",
  150. zap.String("sql", "select count "+p.TableName()),
  151. zap.String("fields", fields),
  152. zap.String("error", err.Error()))
  153. }
  154. return ret, err
  155. }
  156. func (p *THouseholdUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseholdUser, err error) {
  157. cond, val, err := whereBuildAndOr(where, or)
  158. if err != nil {
  159. return list, err
  160. }
  161. if pageSize < 0 {
  162. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  163. if result.Error != nil {
  164. wherefields, _ := util.MarshalToString(where)
  165. logger.Error("mysql",
  166. zap.String("sql", "select * from "+p.TableName()),
  167. zap.String("where", wherefields),
  168. zap.String("error", result.Error.Error()))
  169. }
  170. return list, result.Error
  171. }
  172. offset := (page - 1) * pageSize
  173. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  174. if result.Error != nil {
  175. wherefields, _ := util.MarshalToString(where)
  176. logger.Error("mysql",
  177. zap.String("sql", "select * from "+p.TableName()),
  178. zap.String("where", wherefields),
  179. zap.String("error", result.Error.Error()))
  180. }
  181. return list, result.Error
  182. }