user_record.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type UserRecord struct {
  7. UserRecordId int `orm:"column(user_record_id);pk"`
  8. OpenId string `description:"用户openid,最大长度:32"`
  9. UnionId string `description:"用户unionid,最大长度:64"`
  10. Subscribe int `description:"是否关注"`
  11. NickName string `descritpion:"用户昵称,最大长度:32"`
  12. RealName string `descritpion:"用户实际名称,最大长度:32"`
  13. BindAccount string `descritpion:"绑定时的账号,最大长度:128"`
  14. Sex int `descritpion:"普通用户性别,1为男性,2为女性"`
  15. Province string `description:"普通用户个人资料填写的省份,最大长度:30"`
  16. City string `description:"普通用户个人资料填写的城市,最大长度:30"`
  17. Country string `description:"国家,如中国为CN,最大长度:30"`
  18. Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"`
  19. CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"`
  20. CreatePlatform int `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1"`
  21. SessionKey string `description:"微信小程序会话密钥,最大长度:255"`
  22. UserId int `description:"用户id"`
  23. }
  24. // 根据openid获取用户关系
  25. func GetUserRecordByOpenId(openId string) (item *UserRecord, err error) {
  26. sql := `SELECT * FROM user_record WHERE open_id=? `
  27. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  28. return
  29. }
  30. // 根据用户id和平台id获取用户关系
  31. func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
  32. sql := `SELECT * FROM user_record WHERE user_id=? AND create_platform = ?`
  33. err = orm.NewOrm().Raw(sql, userId, platform).QueryRow(&item)
  34. return
  35. }
  36. // 添加用户关系
  37. func AddUserRecord(record *UserRecord) (recordId int64, err error) {
  38. o := orm.NewOrm()
  39. recordId, err = o.Insert(record)
  40. return
  41. }
  42. // 根据openid绑定用户关系
  43. func BindUserRecordByOpenid(userId int, openId, bindAccount string) (err error) {
  44. o := orm.NewOrm()
  45. msql := " UPDATE user_record SET user_id = ?,bind_account=? WHERE open_id = ? "
  46. _, err = o.Raw(msql, userId, bindAccount, openId).Exec()
  47. return
  48. }
  49. // 修改用户微信信息
  50. func ModifyUserRecordInfo(openId, nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
  51. o := orm.NewOrm()
  52. sql := `UPDATE user_record SET nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? and openid=? `
  53. _, err = o.Raw(sql, nickName, headimgUrl, sex, city, province, country, userId, openId).Exec()
  54. return
  55. }
  56. // 根据用户id删除该用户的所有关联关系
  57. func DelUserRecordByUserId(userId int) (err error) {
  58. o := orm.NewOrm()
  59. sql := ` DELETE FROM user_record WHERE user_id=? `
  60. _, err = o.Raw(sql, userId).Exec()
  61. return
  62. }
  63. // GetUserRecordListByUserIds 根据用户id字符串批量获取该用户的所有关联关系
  64. func GetUserRecordListByUserIds(userIds string) (items []*UserRecord, err error) {
  65. o := orm.NewOrm()
  66. sql := ` select * FROM user_record WHERE user_id in (` + userIds + `) `
  67. _, err = o.Raw(sql).QueryRows(&items)
  68. return
  69. }
  70. // 根据用户id字符串批量删除该用户的所有关联关系
  71. func DelUserRecordByUserIds(userIds string) (err error) {
  72. o := orm.NewOrm()
  73. sql := ` DELETE FROM user_record WHERE user_id in (` + userIds + `) `
  74. _, err = o.Raw(sql).Exec()
  75. return
  76. }
  77. // 根据用户id字符串获取用户绑定时间结构体
  78. type UserRecordRegisterSlice struct {
  79. UserId int `description:"用户id"`
  80. CreateTime time.Time `description:"用户第三方信息绑定注册时间"`
  81. }
  82. // 根据用户id字符串获取用户绑定时间
  83. func GetUserRecordRegisterByUserIds(userIds string) (items []*UserRecordRegisterSlice, err error) {
  84. o := orm.NewOrm()
  85. sql := `SELECT user_id,min(create_time) as create_time FROM user_record WHERE user_id in (` + userIds + `) group by user_id`
  86. _, err = o.Raw(sql).QueryRows(&items)
  87. return
  88. }
  89. // 根据用户id字符串获取用户绑定时间 (查研观向与买方研选小程序)
  90. func GetUserRecordRegisterByUserIdsMyfx(userIds string) (items []*UserRecordRegisterSlice, err error) {
  91. o := orm.NewOrm()
  92. sql := `SELECT user_id,min(create_time) as create_time FROM user_record WHERE user_id in (` + userIds + `) AND create_platform IN (4,10) group by user_id`
  93. _, err = o.Raw(sql).QueryRows(&items)
  94. return
  95. }
  96. // 根据手机号获取用户的openid
  97. func GeCygxtUserRecordListByMobile(bindAccount string) (items []*OpenIdList, err error) {
  98. var sql string
  99. o := orm.NewOrmUsingDB("hz_cygx")
  100. sql = `SELECT
  101. cr.open_id,
  102. cr.cygx_user_id as user_id
  103. FROM
  104. cygx_user_record as cr
  105. WHERE 1= 1 AND cygx_bind_account IN (` + bindAccount + `)`
  106. _, err = o.Raw(sql).QueryRows(&items)
  107. return
  108. }
  109. func GetWxOpenIdByMobileList(mobile string) (items []*OpenIdList, err error) {
  110. o := orm.NewOrmUsingDB("hz_cygx")
  111. sql := ` SELECT
  112. union_id,open_id,
  113. cygx_user_id AS user_id
  114. FROM cygx_user_record WHERE 1 = 1 AND cygx_bind_account IN (` + mobile + `)`
  115. _, err = o.Raw(sql).QueryRows(&items)
  116. return
  117. }