user_record.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. //SessionKey string `description:"微信小程序会话密钥"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"更新时间"`
  24. }
  25. func (m *UserRecord) TableName() string {
  26. return "user_record"
  27. }
  28. type UserRecordCols struct {
  29. PrimaryId string
  30. UserId string
  31. OpenId string
  32. UnionId string
  33. Subscribe string
  34. SubscribeTime string
  35. NickName string
  36. RealName string
  37. Gender string
  38. Avatar string
  39. SessionKey string
  40. CreateTime string
  41. ModifyTime string
  42. }
  43. func (m *UserRecord) Cols() UserRecordCols {
  44. return UserRecordCols{
  45. PrimaryId: "user_record_id",
  46. UserId: "user_id",
  47. OpenId: "open_id",
  48. UnionId: "union_id",
  49. Subscribe: "subscribe",
  50. SubscribeTime: "subscribe_time",
  51. NickName: "nick_name",
  52. RealName: "real_name",
  53. Gender: "gender",
  54. Avatar: "avatar",
  55. SessionKey: "session_key",
  56. CreateTime: "create_time",
  57. ModifyTime: "modify_time",
  58. }
  59. }
  60. func (m *UserRecord) Create() (err error) {
  61. o := orm.NewOrm()
  62. id, err := o.Insert(m)
  63. if err != nil {
  64. return
  65. }
  66. m.UserRecordId = int(id)
  67. return
  68. }
  69. func (m *UserRecord) CreateMulti(items []*UserRecord) (err error) {
  70. if len(items) == 0 {
  71. return
  72. }
  73. o := orm.NewOrm()
  74. _, err = o.InsertMulti(len(items), items)
  75. return
  76. }
  77. func (m *UserRecord) Update(cols []string) (err error) {
  78. o := orm.NewOrm()
  79. _, err = o.Update(m, cols...)
  80. return
  81. }
  82. func (m *UserRecord) Remove() (err error) {
  83. o := orm.NewOrm()
  84. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  85. _, err = o.Raw(sql, m.UserRecordId).Exec()
  86. return
  87. }
  88. func (m *UserRecord) MultiRemove(ids []int) (err error) {
  89. if len(ids) == 0 {
  90. return
  91. }
  92. o := orm.NewOrm()
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  94. _, err = o.Raw(sql, ids).Exec()
  95. return
  96. }
  97. func (m *UserRecord) RemoveByCondition(condition string, pars []interface{}) (err error) {
  98. if condition == "" {
  99. return
  100. }
  101. o := orm.NewOrm()
  102. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  103. _, err = o.Raw(sql, pars).Exec()
  104. return
  105. }
  106. func (m *UserRecord) GetItemById(id int) (item *UserRecord, err error) {
  107. o := orm.NewOrm()
  108. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  109. err = o.Raw(sql, id).QueryRow(&item)
  110. return
  111. }
  112. func (m *UserRecord) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *UserRecord, err error) {
  113. o := orm.NewOrm()
  114. order := ``
  115. if orderRule != "" {
  116. order = ` ORDER BY ` + orderRule
  117. }
  118. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  119. err = o.Raw(sql, pars).QueryRow(&item)
  120. return
  121. }
  122. func (m *UserRecord) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  123. o := orm.NewOrm()
  124. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  125. err = o.Raw(sql, pars).QueryRow(&count)
  126. return
  127. }
  128. func (m *UserRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserRecord, err error) {
  129. o := orm.NewOrm()
  130. fields := strings.Join(fieldArr, ",")
  131. if len(fieldArr) == 0 {
  132. fields = `*`
  133. }
  134. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  135. if orderRule != "" {
  136. order = ` ORDER BY ` + orderRule
  137. }
  138. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  139. _, err = o.Raw(sql, pars).QueryRows(&items)
  140. return
  141. }
  142. func (m *UserRecord) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*UserRecord, err error) {
  143. o := orm.NewOrm()
  144. fields := strings.Join(fieldArr, ",")
  145. if len(fieldArr) == 0 {
  146. fields = `*`
  147. }
  148. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  149. if orderRule != "" {
  150. order = ` ORDER BY ` + orderRule
  151. }
  152. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  153. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  154. return
  155. }