user_record.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // 添加用户关系
  25. func AddUserRecord(record *UserRecord) (recordId int64, err error) {
  26. o := orm.NewOrmUsingDB("weekly_report")
  27. recordId, err = o.Insert(record)
  28. return
  29. }
  30. // 根据openid获取用户关系
  31. // 12是买方研选 create_platform
  32. func GetUserRecordByUnionId(unionId string) (item *UserRecord, err error) {
  33. o := orm.NewOrmUsingDB("weekly_report")
  34. sql := `SELECT * FROM user_record WHERE union_id=? ORDER BY user_id DESC LIMIT 1 `
  35. err = o.Raw(sql, unionId).QueryRow(&item)
  36. return
  37. }
  38. // 获取数量
  39. func GetUserRecordCount(openId string) (count int, err error) {
  40. o := orm.NewOrmUsingDB("weekly_report")
  41. sqlCount := ` SELECT COUNT(1) AS count FROM user_record WHERE open_id=? `
  42. err = o.Raw(sqlCount, openId).QueryRow(&count)
  43. return
  44. }
  45. // 根据用户id和平台id获取用户关系
  46. func GetUserRecordByUserOpenId(open_id string) (item *UserRecord, err error) {
  47. o := orm.NewOrmUsingDB("weekly_report")
  48. sql := `SELECT
  49. u.*
  50. FROM
  51. JOIN user_record AS u
  52. WHERE
  53. u.open_id = ?
  54. AND u.create_platform = 4
  55. AND u.bind_account != ''
  56. GROUP BY u.union_id`
  57. err = o.Raw(sql, open_id).QueryRow(&item)
  58. return
  59. }
  60. func GetUserRecordList(condition string, pars []interface{}) (items []*UserRecord, err error) {
  61. o := orm.NewOrmUsingDB("weekly_report")
  62. sql := ` SELECT *
  63. FROM user_record
  64. WHERE 1= 1 `
  65. if condition != "" {
  66. sql += condition
  67. }
  68. _, err = o.Raw(sql, pars).QueryRows(&items)
  69. return
  70. }
  71. // 根据openid解除绑定用户关系
  72. func UnBindUserRecordByOpenid(openId string) (err error) {
  73. o := orm.NewOrmUsingDB("weekly_report")
  74. msql := ` UPDATE user_record SET user_id = 0,bind_account="" WHERE open_id = ? `
  75. _, err = o.Raw(msql, openId).Exec()
  76. return
  77. }