user_record.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_mfyx/utils"
  5. "strings"
  6. "time"
  7. )
  8. type UserRecord struct {
  9. UserRecordId int `orm:"column(user_record_id);pk"`
  10. OpenId string `description:"用户openid,最大长度:32"`
  11. UnionId string `description:"用户unionid,最大长度:64"`
  12. Subscribe int `description:"是否关注"`
  13. NickName string `descritpion:"用户昵称,最大长度:32"`
  14. RealName string `descritpion:"用户实际名称,最大长度:32"`
  15. BindAccount string `descritpion:"绑定时的账号,最大长度:128"`
  16. Sex int `descritpion:"普通用户性别,1为男性,2为女性"`
  17. Province string `description:"普通用户个人资料填写的省份,最大长度:30"`
  18. City string `description:"普通用户个人资料填写的城市,最大长度:30"`
  19. Country string `description:"国家,如中国为CN,最大长度:30"`
  20. Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"`
  21. CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"`
  22. CreatePlatform int `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1"`
  23. SessionKey string `description:"微信小程序会话密钥,最大长度:255"`
  24. UserId int `description:"用户id"`
  25. }
  26. // 根据openid获取用户关系
  27. func GetUserRecordByOpenId(openId string) (item *UserRecord, err error) {
  28. o := orm.NewOrmUsingDB("weekly_report")
  29. sql := `SELECT * FROM user_record WHERE open_id=? `
  30. err = o.Raw(sql, openId).QueryRow(&item)
  31. return
  32. }
  33. // 根据openid解除绑定用户关系
  34. func UnBindUserRecordByOpenid(openId string) (err error) {
  35. o := orm.NewOrmUsingDB("weekly_report")
  36. msql := ` UPDATE user_record SET user_id = 0,bind_account="" WHERE open_id = ? `
  37. _, err = o.Raw(msql, openId).Exec()
  38. return
  39. }
  40. // 根据用户id和平台id获取用户关系
  41. func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
  42. o := orm.NewOrmUsingDB("weekly_report")
  43. sql := `SELECT * FROM user_record WHERE user_id=? AND create_platform = ?`
  44. err = o.Raw(sql, userId, platform).QueryRow(&item)
  45. return
  46. }
  47. // 根据用户id和平台id获取用户关系
  48. func GetUserRecordByUnionId(unionId string, platform int) (item *UserRecord, err error) {
  49. o := orm.NewOrmUsingDB("weekly_report")
  50. sql := `SELECT * FROM user_record WHERE union_id =? AND create_platform = ?`
  51. err = o.Raw(sql, unionId, platform).QueryRow(&item)
  52. return
  53. }
  54. // 添加用户关系
  55. func AddUserRecord(record *UserRecord) (recordId int64, err error) {
  56. o := orm.NewOrmUsingDB("weekly_report")
  57. recordId, err = o.Insert(record)
  58. return
  59. }
  60. // 根据openid绑定用户关系
  61. func BindUserRecordByOpenid(userId int, openId, bindAccount string) (err error) {
  62. o := orm.NewOrmUsingDB("weekly_report")
  63. msql := " UPDATE user_record SET user_id = ?,bind_account=? WHERE open_id = ? "
  64. _, err = o.Raw(msql, userId, bindAccount, openId).Exec()
  65. return
  66. }
  67. // 修改用户微信信息
  68. func ModifyUserRecordInfo(openId, nickName, headimgUrl, city, province, country, sessionKey string, sex, userId int) (err error) {
  69. o := orm.NewOrmUsingDB("weekly_report")
  70. sql := `UPDATE user_record SET nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=?,session_key=? WHERE user_id=? and open_id=? `
  71. _, err = o.Raw(sql, nickName, headimgUrl, sex, city, province, country, sessionKey, userId, openId).Exec()
  72. return
  73. }
  74. // 修改用户微信信息
  75. func ModifyUserRecordByDetail(openId, unionId, nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
  76. o := orm.NewOrmUsingDB("weekly_report")
  77. sql := `UPDATE user_record SET union_id=?, nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? and open_id=? `
  78. _, err = o.Raw(sql, unionId, nickName, headimgUrl, sex, city, province, country, userId, openId).Exec()
  79. return
  80. }
  81. // 修改用户微信信息
  82. func ModifyUserRecordSessionKey(openId, sessionKey string) (err error) {
  83. o := orm.NewOrmUsingDB("weekly_report")
  84. sql := `UPDATE user_record SET session_key=? WHERE open_id=? `
  85. _, err = o.Raw(sql, sessionKey, openId).Exec()
  86. return
  87. }
  88. // 根据用户id和平台id获取用户关系
  89. func GetUserRecordByMobile(platform int, bindAccount string) (item *OpenIdList, err error) {
  90. var sql string
  91. sql = `SELECT open_id,cygx_user_id AS user_id FROM cygx_mfyx_gzh_user_record WHERE 1 = 1 AND cygx_bind_account = ?`
  92. err = orm.NewOrm().Raw(sql, bindAccount).QueryRow(&item)
  93. return
  94. }
  95. // 获取该用户第一个的 三方信息(微信头像信息)
  96. func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
  97. o := orm.NewOrmUsingDB("weekly_report")
  98. sql := `SELECT * FROM user_record WHERE user_id = ? order by user_record_id asc`
  99. err = o.Raw(sql, userId).QueryRow(&item)
  100. return
  101. }
  102. // 根据手机号获取用户的openid
  103. func GetUserRecordListByMobile(platform int, bindAccount string) (items []*OpenIdList, err error) {
  104. var sql string
  105. sql = `SELECT
  106. cr.open_id,
  107. cr.cygx_user_id as user_id
  108. FROM
  109. cygx_user_record as cr
  110. WHERE 1= 1 AND cygx_bind_account IN (` + bindAccount + `)`
  111. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  112. return
  113. }
  114. func GetOpenIdByUserIds(ids string) (item []*OpenIdList, err error) {
  115. o := orm.NewOrm()
  116. var sql string
  117. sql = ` SELECT
  118. cr.open_id,
  119. cr.cygx_user_id as user_id
  120. FROM
  121. cygx_user_record as cr
  122. WHERE 1= 1 AND cygx_user_id IN (` + ids + `)`
  123. _, err = o.Raw(sql).QueryRows(&item)
  124. return
  125. }
  126. // 根据手机号获取用户的openid
  127. func GetMfyxUserRecordListByMobileArr(mobiles []string) (items []*OpenIdList, err error) {
  128. o := orm.NewOrm()
  129. lenarr := len(mobiles)
  130. if lenarr == 0 {
  131. return
  132. }
  133. var condition string
  134. var pars []interface{}
  135. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  136. pars = append(pars, mobiles)
  137. sql := `SELECT
  138. u.open_id,
  139. u.cygx_user_id AS user_id
  140. FROM
  141. cygx_mfyx_gzh_user_record AS u
  142. WHERE
  143. 1 = 1 ` + condition
  144. _, err = o.Raw(sql, pars).QueryRows(&items)
  145. return
  146. }
  147. func GetMfyxWxOpenIdByMobileList(mobile string) (items []*OpenIdList, err error) {
  148. if mobile == "" {
  149. return
  150. }
  151. sliceMobile := strings.Split(mobile, ",")
  152. var mobiles []string
  153. for _, v := range sliceMobile {
  154. mobiles = append(mobiles, v)
  155. }
  156. o := orm.NewOrm()
  157. lenarr := len(mobiles)
  158. if lenarr == 0 {
  159. return
  160. }
  161. var condition string
  162. var pars []interface{}
  163. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  164. pars = append(pars, mobiles)
  165. sql := `SELECT
  166. u.open_id,
  167. u.cygx_user_id AS user_id
  168. FROM
  169. cygx_mfyx_gzh_user_record AS u
  170. WHERE
  171. 1 = 1 ` + condition
  172. _, err = o.Raw(sql, pars).QueryRows(&items)
  173. return
  174. }