users.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "strings"
  5. "time"
  6. )
  7. type HongzeUsers struct {
  8. RealName string
  9. CompanyName string
  10. Mobile string
  11. IsRegister string
  12. UserType string
  13. }
  14. func GetHongzeUsers() (items []*HongzeUsers, err error) {
  15. sql := `select wu.real_name,c.company_name,left(wu.mobile, 11) as mobile ,if(wu.open_id='','否','是') as is_register,if(c.type in (1),'是','否') as user_type
  16. from wx_user wu
  17. inner join company c on c.company_id = wu.company_id
  18. where c.company_id <> 1 and c.company_id <> 16
  19. and c.type in (1)
  20. order by company_name desc `
  21. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  22. return
  23. }
  24. type WxUser struct {
  25. ExpiredIn int64
  26. UserId int64 `orm:"column(user_id);pk"`
  27. Mobile string
  28. Email string
  29. CompanyId int
  30. RealName string `description:"姓名"`
  31. NickName string `description:"昵称"`
  32. CreatedTime time.Time
  33. MobileTwo string `description:"备用手机号"`
  34. BusinessCardUrl string `description:"名片"`
  35. IsMaker int `description:"是否决策人,1:是,0:否"`
  36. Position string `description:"职位"`
  37. Sex int `description:"普通用户性别,1为男性,2为女性"`
  38. DepartmentName string `description:"联系人部门"`
  39. RegisterTime time.Time
  40. RegisterPlatform int
  41. Remark string `description:"备注"`
  42. CountryCode string `description:"区号,86、852、886等"`
  43. OutboundMobile string `description:"外呼手机号"`
  44. OutboundCountryCode string `description:"外呼手机号区号,86、852、886等"`
  45. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  46. IsDeal int `description:"是否标记处理 0-未处理 1-已处理"`
  47. OpenId string `orm:"column(open_id)" description:"微信openid"`
  48. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  49. }
  50. func GetWxUserByUserId(userId int) (item *WxUser, err error) {
  51. sql := `SELECT * FROM wx_user WHERE user_id=? `
  52. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  53. return
  54. }
  55. func GetWxUserByMobile(mobile string) (item *WxUser, err error) {
  56. sql := `SELECT * FROM wx_user WHERE mobile=? `
  57. err = orm.NewOrm().Raw(sql, mobile).QueryRow(&item)
  58. return
  59. }
  60. // GetWxUserByMobileCountryCode 根据手机号和区号获取用户信息
  61. func GetWxUserByMobileCountryCode(mobile, countryCode string) (item *WxUser, err error) {
  62. o := orm.NewOrm()
  63. sql := `SELECT * FROM wx_user WHERE mobile = ? `
  64. sql += ` and country_code in ("","` + countryCode + `") `
  65. sql += ` LIMIT 1 `
  66. err = o.Raw(sql, mobile).QueryRow(&item)
  67. return
  68. }
  69. func AddWxUser(companyId int, mobile, realName, email string) (err error) {
  70. sql := `INSERT INTO wx_user(company_id,real_name,mobile,first_login,enabled,is_note,from_type,apply_method,email) VALUES (?,?,?,1,1,1,'report',0,?);`
  71. orm.NewOrm().Raw(sql, companyId, realName, mobile, email).Exec()
  72. return
  73. }
  74. func GetPotentialUser() (items []*WxUser, err error) {
  75. sql := ` SELECT * FROM wx_user WHERE company_id=1 AND report_last_view_time <> '' `
  76. o := orm.NewOrm()
  77. _, err = o.Raw(sql).QueryRows(&items)
  78. return
  79. }
  80. func GetMaxReviewTime(uid int) (max_time string, err error) {
  81. o := orm.NewOrm()
  82. sql := `SELECT MAX(a.max_time) AS max_time FROM (
  83. SELECT MAX(created_time)AS max_time FROM user_view_history AS a WHERE user_id=?
  84. UNION
  85. SELECT MAX(create_time)AS max_time FROM hongze_rddp.report_view_record AS a WHERE user_id=?
  86. )AS a`
  87. err = o.Raw(sql, uid, uid).QueryRow(&max_time)
  88. return
  89. }
  90. func ModifyUserLastViewTime(uid int, lastViewTime string) (err error) {
  91. o := orm.NewOrm()
  92. sql := `UPDATE wx_user SET report_last_view_time=? WHERE user_id=? `
  93. _, err = o.Raw(sql, lastViewTime, uid).Exec()
  94. return
  95. }
  96. // GetUserOpenidListByUserId 根据用户id来获取他的openid列表集合
  97. func GetUserOpenidListByUserId(userId int) (list []*OpenIdList, err error) {
  98. sql := `SELECT open_id FROM user_record WHERE user_id = ? and create_platform = 1`
  99. _, err = orm.NewOrm().Raw(sql, userId).QueryRows(&list)
  100. return
  101. }
  102. type UserMobileList struct {
  103. UserId int
  104. Mobile string
  105. CountryCode string
  106. Email string
  107. }
  108. // GetUserMobileFormal
  109. func GetUserMobileFormal() (list []*UserMobileList, err error) {
  110. sql := `SELECT
  111. a.user_id,a.mobile,a.country_code,a.email
  112. FROM
  113. wx_user AS a
  114. INNER JOIN company_product AS p ON a.company_id = p.company_id
  115. WHERE
  116. p.status IN (
  117. '试用',
  118. '永续',
  119. '正式') AND p.product_id = 1 `
  120. _, err = orm.NewOrm().Raw(sql).QueryRows(&list)
  121. return
  122. }
  123. // GetUserOpenidListByUserIds 根据用户id字符串集合来获取他的openid列表集合
  124. func GetUserOpenidListByUserIds(userIdStr []string) (list []*OpenIdList, err error) {
  125. if len(userIdStr) <= 0 {
  126. return
  127. }
  128. sql := `SELECT open_id FROM user_record WHERE user_id in (` + strings.Join(userIdStr, ",") + `) and create_platform = 1`
  129. _, err = orm.NewOrm().Raw(sql).QueryRows(&list)
  130. return
  131. }
  132. // GetUserMobileNotFormal
  133. func GetUserMobileNotFormal() (list []*UserMobileList, err error) {
  134. sql := `SELECT
  135. a.user_id,a.mobile,a.country_code,a.email
  136. FROM
  137. wx_user AS a
  138. INNER JOIN company_product AS p ON a.company_id = p.company_id
  139. WHERE
  140. p.status NOT IN (
  141. '试用',
  142. '永续',
  143. '正式') AND p.product_id = 1 `
  144. _, err = orm.NewOrm().Raw(sql).QueryRows(&list)
  145. return
  146. }