wx_user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package wx_user
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type WxUser struct {
  7. UserId int64 `orm:"column(user_id);pk"`
  8. Mobile string
  9. Email string
  10. CompanyId int
  11. RealName string `description:"姓名"`
  12. CreatedTime time.Time
  13. MobileTwo string `description:"备用手机号"`
  14. BusinessCardUrl string `description:"名片"`
  15. IsMaker int `description:"是否决策人,1:是,0:否"`
  16. Position string `description:"职位"`
  17. Sex int `description:"普通用户性别,1为男性,2为女性"`
  18. DepartmentName string `description:"联系人部门"`
  19. RegisterTime time.Time
  20. RegisterPlatform int
  21. Enabled int8 `description:"用户状态"`
  22. FirstLogin int `description:"是否第一次登陆"`
  23. CountryCode string `description:"区号,86、852、886等"`
  24. OutboundMobile string `description:"外呼手机号"`
  25. OutboundCountryCode string `description:"外呼手机号区号,86、852、886等"`
  26. }
  27. type OpenIdList struct {
  28. OpenId string
  29. }
  30. // 添加日志记录
  31. func AddWxUser(item *WxUser) (lastId int64, err error) {
  32. o := orm.NewOrm()
  33. lastId, err = o.Insert(item)
  34. return
  35. }
  36. // 获取所有的用户openid列表
  37. func GetOpenIdList(openIdStr string) (items []*OpenIdList, err error) {
  38. sql := `SELECT DISTINCT ur.open_id FROM wx_user AS wu
  39. INNER JOIN company AS c ON c.company_id = wu.company_id
  40. INNER JOIN company_product AS d ON c.company_id=d.company_id
  41. INNER join user_record as ur on wu.user_id=ur.user_id
  42. WHERE ur.open_id != "" and ur.create_platform=1 AND d.status IN('正式','试用','永续') `
  43. if openIdStr != "" {
  44. sql += ` AND ur.open_id in (` + openIdStr + `) `
  45. }
  46. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  47. return
  48. }
  49. // 根据手机号获取用户的openid列表
  50. func GetOpenIdListByMobile(mobile, openIdStr string) (items []*OpenIdList, err error) {
  51. sql := `SELECT DISTINCT ur.open_id FROM wx_user AS wu
  52. INNER JOIN company AS c ON c.company_id = wu.company_id
  53. INNER join user_record as ur on wu.user_id=ur.user_id
  54. WHERE ur.open_id != "" and ur.create_platform=1 AND wu.mobile=? `
  55. if openIdStr != "" {
  56. sql += ` AND ur.open_id in (` + openIdStr + `) `
  57. }
  58. _, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
  59. return
  60. }
  61. type WxUserItem struct {
  62. UserId int
  63. RealName string
  64. Mobile string
  65. Email string
  66. CompanyName string
  67. }
  68. // GetWxUserByMobile 根据手机号获取用户信息
  69. func GetWxUserByMobile(mobile string) (item *WxUserItem, err error) {
  70. sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a
  71. INNER JOIN company AS b ON a.company_id = b.company_id
  72. WHERE a.mobile=? `
  73. err = orm.NewOrm().Raw(sql, mobile).QueryRow(&item)
  74. return
  75. }
  76. // GetWxUserByEmail 根据邮箱获取用户信息
  77. func GetWxUserByEmail(email string) (item *WxUserItem, err error) {
  78. sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a
  79. INNER JOIN company AS b ON a.company_id = b.company_id
  80. WHERE a.email=? `
  81. err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
  82. return
  83. }
  84. // GetWxUserByMobile 根据手机号获取用户信息
  85. func GetWxUserByMobileStr(mobile string) (item *WxUserItem, err error) {
  86. sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a
  87. INNER JOIN company AS b ON a.company_id = b.company_id
  88. WHERE a.mobile IN (` + mobile + `) LIMIT 1 `
  89. err = orm.NewOrm().Raw(sql).QueryRow(&item)
  90. return
  91. }
  92. // GetWxUserByMobileCountryCode 根据手机号和区号获取用户信息
  93. func GetWxUserByMobileCountryCode(mobile, countryCode string) (item *WxUser, err error) {
  94. o := orm.NewOrm()
  95. sql := `SELECT * FROM wx_user WHERE mobile = ? `
  96. sql += ` and country_code in ("","` + countryCode + `") `
  97. sql += ` LIMIT 1 `
  98. err = o.Raw(sql, mobile).QueryRow(&item)
  99. return
  100. }
  101. // GetByUserId 根据user_id获取用户信息
  102. func GetByUserId(userId int) (wxUser *WxUser, err error) {
  103. o := orm.NewOrm()
  104. sql := `SELECT * FROM wx_user WHERE user_id = ? LIMIT 1 `
  105. err = o.Raw(sql, userId).QueryRow(&wxUser)
  106. return
  107. }
  108. // GetByUserMobile 根据mobile获取用户信息
  109. func GetByUserMobile(mobile string) (wxUser *WxUser, err error) {
  110. o := orm.NewOrm()
  111. sql := `SELECT * FROM wx_user WHERE mobile = ? LIMIT 1 `
  112. err = o.Raw(sql, mobile).QueryRow(&wxUser)
  113. return
  114. }
  115. // GetWxUserCountByMobile 查询系统中是否存在该手机号
  116. func GetWxUserCountByMobile(mobile string) (count int, err error) {
  117. sql := `SELECT COUNT(1) AS count FROM wx_user WHERE mobile=? `
  118. o := orm.NewOrm()
  119. err = o.Raw(sql, mobile).QueryRow(&count)
  120. return
  121. }