user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package models
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type User struct {
  9. UserId int `orm:"pk" description:"用户id"`
  10. OpenId string `description:"openid"`
  11. UnionId string `description:"unionid"`
  12. RealName string `description:"姓名"`
  13. Phone string `description:"手机号"`
  14. AreaCode string `description:"区号"`
  15. Email string `description:"邮箱"`
  16. SellerId int `description:"销售id(SysUserId)"`
  17. Company string `description:"所属公司"`
  18. ValidStartTime time.Time `description:"有效期开始时间"`
  19. ValidEndTime time.Time `description:"有效期结束时间"`
  20. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  21. CreateTime time.Time `description:"系统中首次新增用户的时间"`
  22. ModifyTime time.Time `description:"系统中用户信息变更的时间"`
  23. RegisterTime time.Time `description:"用户首次登录小程序的时间"`
  24. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  25. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  26. }
  27. func (u *User) Insert() (insertId int64, err error) {
  28. o := orm.NewOrm()
  29. insertId, err = o.Insert(u)
  30. return
  31. }
  32. type UserItem struct {
  33. UserId int `description:"用户id"`
  34. OpenId string `description:"open_id"`
  35. UnionId string `description:"union_id"`
  36. NickName string `description:"用户昵称"`
  37. RealName string `description:"用户实际名称"`
  38. Phone string `description:"手机号码"`
  39. Componey string `description:"所属公司"`
  40. AreaCode string `description:"区号"`
  41. SellerId int `description:"销售id"`
  42. Email string `description:"邮箱"`
  43. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  44. ValidEndTime time.Time `description:"服务截至时间"`
  45. RegisterTime time.Time `description:"登录时间,用户首次登录小程序的时间"`
  46. CreateTime time.Time `description:"系统中新增用户的时间"`
  47. ModifyTime time.Time `description:"系统中用户信息更新的时间"`
  48. IsRegistered bool `description:"是否注册:1:已注册,0:未注册"`
  49. }
  50. // 变更联系人是否已注册状态
  51. func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime time.Time) (err error) {
  52. o := orm.NewOrm()
  53. sql := `UPDATE user SET is_registered=?, register_time=?, modify_time=? WHERE user_id = ? `
  54. _, err = o.Raw(sql, status, registerTime, modifyTime, userId).Exec()
  55. return
  56. }
  57. func GetUserBySubscribe() (user []*User, err error) {
  58. o := orm.NewOrm()
  59. sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 AND union_id IS NOT NULL `
  60. _, err = o.Raw(sql).QueryRows(&user)
  61. return
  62. }
  63. // GetUserBySubscribeAndFormatUser 查询已经注册和关注公众号的正式用户
  64. func GetUserBySubscribeAndFormatUser() (user []*User, err error) {
  65. o := orm.NewOrm()
  66. sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 AND union_id IS NOT NULL AND status=2`
  67. _, err = o.Raw(sql).QueryRows(&user)
  68. return
  69. }
  70. func GetUserById(userId int) (item *User, err error) {
  71. o := orm.NewOrm()
  72. sql := `SELECT * FROM user WHERE user_id=? `
  73. err = o.Raw(sql, userId).QueryRow(&item)
  74. return
  75. }
  76. func GetUserList(condition string, pars []interface{}) (items []*User, err error) {
  77. o := orm.NewOrm()
  78. sql := `SELECT * FROM user WHERE 1=1 `
  79. if condition != "" {
  80. sql += condition
  81. }
  82. _, err = o.Raw(sql, pars...).QueryRows(&items)
  83. return
  84. }
  85. func GetUserItemByPhone(phone string) (item *UserItem, err error) {
  86. o := orm.NewOrm()
  87. sql := `SELECT * FROM user WHERE phone=? `
  88. err = o.Raw(sql, phone).QueryRow(&item)
  89. return
  90. }
  91. func GetUserItemByEmail(email string) (item *UserItem, err error) {
  92. sql := `SELECT * FROM user WHERE email=? `
  93. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  94. return
  95. }
  96. func GetUserItemByUserId(userId int) (item *UserItem, err error) {
  97. sql := `SELECT * FROM user WHERE user_id=? `
  98. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  99. return
  100. }
  101. func GetUserUnionIdListByIds(userIds []int) (items []string, err error) {
  102. sql := `SELECT union_id FROM user WHERE user_id IN (%s) AND union_id IS NOT NULL`
  103. var idsStr string
  104. if len(userIds) > 0 {
  105. var userIdsStr []string
  106. for _, userId := range userIds {
  107. userIdsStr = append(userIdsStr, fmt.Sprint(userId))
  108. }
  109. idsStr = strings.Join(userIdsStr, ",")
  110. sql = fmt.Sprintf(sql, idsStr)
  111. }
  112. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  113. return
  114. }