user_record.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package models
  2. import (
  3. "eta/eta_mini_api/utils"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/beego/beego/v2/client/orm"
  8. )
  9. // UserRecord 用户表
  10. type UserRecord struct {
  11. UserRecordId int `orm:"column(user_record_id);pk"`
  12. UserId int `description:"用户ID"`
  13. OpenId string `description:"open_id"`
  14. UnionId string `description:"用户统一标识"`
  15. Subscribe int `description:"公众号关注状态:0-未关注;1-已关注"`
  16. SubscribeTime time.Time `description:"公众号关注/取消关注时间"`
  17. NickName string `description:"用户昵称"`
  18. RealName string `description:"用户姓名"`
  19. Gender int `description:"性别"`
  20. Avatar string `description:"用户头像"`
  21. Province string `description:"普通用户个人资料填写的省份"`
  22. City string `description:"普通用户个人资料填写的城市"`
  23. Country string `description:"国家,如中国CN"`
  24. SessionKey string `description:"微信小程序会话密钥"`
  25. CreateTime time.Time `description:"创建时间"`
  26. ModifyTime time.Time `description:"更新时间"`
  27. }
  28. func (m *UserRecord) TableName() string {
  29. return "user_record"
  30. }
  31. type UserRecordCols struct {
  32. PrimaryId string
  33. UserId string
  34. OpenId string
  35. UnionId string
  36. Subscribe string
  37. SubscribeTime string
  38. NickName string
  39. RealName string
  40. Gender string
  41. Avatar string
  42. Province string
  43. City string
  44. Country string
  45. SessionKey string
  46. CreateTime string
  47. ModifyTime string
  48. }
  49. func (m *UserRecord) Cols() UserRecordCols {
  50. return UserRecordCols{
  51. PrimaryId: "user_record_id",
  52. UserId: "user_id",
  53. OpenId: "open_id",
  54. UnionId: "union_id",
  55. Subscribe: "subscribe",
  56. SubscribeTime: "subscribe_time",
  57. NickName: "nick_name",
  58. RealName: "real_name",
  59. Gender: "gender",
  60. Avatar: "avatar",
  61. Province: "province",
  62. City: "city",
  63. Country: "country",
  64. SessionKey: "session_key",
  65. CreateTime: "create_time",
  66. ModifyTime: "modify_time",
  67. }
  68. }
  69. func (m *UserRecord) Create() (err error) {
  70. o := orm.NewOrm()
  71. id, err := o.Insert(m)
  72. if err != nil {
  73. return
  74. }
  75. m.UserRecordId = int(id)
  76. return
  77. }
  78. func (m *UserRecord) CreateMulti(items []*UserRecord) (err error) {
  79. if len(items) == 0 {
  80. return
  81. }
  82. o := orm.NewOrm()
  83. _, err = o.InsertMulti(len(items), items)
  84. return
  85. }
  86. func (m *UserRecord) Update(cols []string) (err error) {
  87. o := orm.NewOrm()
  88. _, err = o.Update(m, cols...)
  89. return
  90. }
  91. func (m *UserRecord) Remove() (err error) {
  92. o := orm.NewOrm()
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  94. _, err = o.Raw(sql, m.UserRecordId).Exec()
  95. return
  96. }
  97. func (m *UserRecord) MultiRemove(ids []int) (err error) {
  98. if len(ids) == 0 {
  99. return
  100. }
  101. o := orm.NewOrm()
  102. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  103. _, err = o.Raw(sql, ids).Exec()
  104. return
  105. }
  106. func (m *UserRecord) RemoveByCondition(condition string, pars []interface{}) (err error) {
  107. if condition == "" {
  108. return
  109. }
  110. o := orm.NewOrm()
  111. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  112. _, err = o.Raw(sql, pars).Exec()
  113. return
  114. }
  115. func (m *UserRecord) GetItemById(id int) (item *UserRecord, err error) {
  116. o := orm.NewOrm()
  117. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  118. err = o.Raw(sql, id).QueryRow(&item)
  119. return
  120. }
  121. func (m *UserRecord) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *UserRecord, err error) {
  122. o := orm.NewOrm()
  123. order := ``
  124. if orderRule != "" {
  125. order = ` ORDER BY ` + orderRule
  126. }
  127. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  128. err = o.Raw(sql, pars).QueryRow(&item)
  129. return
  130. }
  131. func (m *UserRecord) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  132. o := orm.NewOrm()
  133. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  134. err = o.Raw(sql, pars).QueryRow(&count)
  135. return
  136. }
  137. func (m *UserRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserRecord, err error) {
  138. o := orm.NewOrm()
  139. fields := strings.Join(fieldArr, ",")
  140. if len(fieldArr) == 0 {
  141. fields = `*`
  142. }
  143. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  144. if orderRule != "" {
  145. order = ` ORDER BY ` + orderRule
  146. }
  147. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  148. _, err = o.Raw(sql, pars).QueryRows(&items)
  149. return
  150. }
  151. func (m *UserRecord) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*UserRecord, err error) {
  152. o := orm.NewOrm()
  153. fields := strings.Join(fieldArr, ",")
  154. if len(fieldArr) == 0 {
  155. fields = `*`
  156. }
  157. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  158. if orderRule != "" {
  159. order = ` ORDER BY ` + orderRule
  160. }
  161. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  162. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  163. return
  164. }