user_record.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_cygx/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. // 添加用户关系
  48. func AddUserRecord(record *UserRecord) (recordId int64, err error) {
  49. o := orm.NewOrmUsingDB("weekly_report")
  50. recordId, err = o.Insert(record)
  51. return
  52. }
  53. // 根据openid绑定用户关系
  54. func BindUserRecordByOpenid(userId int, openId, bindAccount string) (err error) {
  55. o := orm.NewOrmUsingDB("weekly_report")
  56. msql := " UPDATE user_record SET user_id = ?,bind_account=? WHERE open_id = ? "
  57. _, err = o.Raw(msql, userId, bindAccount, openId).Exec()
  58. return
  59. }
  60. // 修改用户微信信息
  61. func ModifyUserRecordInfo(openId, nickName, headimgUrl, city, province, country, sessionKey string, sex, userId int) (err error) {
  62. o := orm.NewOrmUsingDB("weekly_report")
  63. sql := `UPDATE user_record SET nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=?,session_key=? WHERE user_id=? and open_id=? `
  64. _, err = o.Raw(sql, nickName, headimgUrl, sex, city, province, country, sessionKey, userId, openId).Exec()
  65. return
  66. }
  67. // 修改用户微信信息
  68. func ModifyUserRecordByDetail(openId, unionId, nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
  69. o := orm.NewOrmUsingDB("weekly_report")
  70. sql := `UPDATE user_record SET union_id=?, nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? and open_id=? `
  71. _, err = o.Raw(sql, unionId, nickName, headimgUrl, sex, city, province, country, userId, openId).Exec()
  72. return
  73. }
  74. // 修改用户微信信息
  75. func ModifyUserRecordSessionKey(openId, sessionKey string) (err error) {
  76. o := orm.NewOrmUsingDB("weekly_report")
  77. sql := `UPDATE user_record SET session_key=? WHERE open_id=? `
  78. _, err = o.Raw(sql, sessionKey, openId).Exec()
  79. return
  80. }
  81. // 根据用户id和平台id获取用户关系
  82. func GetUserRecordByMobile(platform int, bindAccount string) (item *OpenIdList, err error) {
  83. var sql string
  84. sql = `SELECT open_id,cygx_user_id AS user_id FROM cygx_user_record WHERE 1 = 1 AND cygx_bind_account = ?`
  85. err = orm.NewOrm().Raw(sql, bindAccount).QueryRow(&item)
  86. return
  87. }
  88. // 获取该用户第一个的 三方信息(微信头像信息)
  89. func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
  90. o := orm.NewOrmUsingDB("weekly_report")
  91. sql := `SELECT * FROM user_record WHERE user_id = ? order by user_record_id asc`
  92. err = o.Raw(sql, userId).QueryRow(&item)
  93. return
  94. }
  95. // 根据手机号获取用户的openid
  96. func GetUserRecordListByMobile(platform int, bindAccount string) (items []*OpenIdList, err error) {
  97. var sql string
  98. //if utils.RunMode == "release" {
  99. // sql = `SELECT cr.open_id,user_id FROM user_record as u
  100. // INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id
  101. // WHERE create_platform=? AND bind_account IN (` + bindAccount + `)`
  102. //} else {
  103. // platform = 1
  104. // sql = `SELECT open_id,user_id FROM user_record WHERE create_platform =? AND bind_account IN (` + bindAccount + `)`
  105. //}
  106. sql = `SELECT
  107. cr.open_id,
  108. cr.cygx_user_id as user_id
  109. FROM
  110. cygx_user_record as cr
  111. WHERE 1= 1 AND cygx_bind_account IN (` + bindAccount + `)`
  112. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  113. return
  114. }
  115. //// 获取单个用户openid
  116. //func GetOpenIdDetailByMobile(platform int, bindAccount string) (item *OpenIdList, err error) {
  117. // o := orm.NewOrm()
  118. // var sql string
  119. // sql = `SELECT cr.open_id,wu.user_id FROM user_record as u
  120. // INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id
  121. // INNER JOIN wx_user AS wu ON wu.mobile = u.bind_account
  122. // WHERE create_platform=? AND u.bind_account = ?`
  123. // err = o.Raw(sql, platform, bindAccount).QueryRow(&item)
  124. // return
  125. //}
  126. func GetOpenIdByUserIds(ids string) (item []*OpenIdList, err error) {
  127. o := orm.NewOrm()
  128. var sql string
  129. sql = ` SELECT
  130. cr.open_id,
  131. cr.cygx_user_id as user_id
  132. FROM
  133. cygx_user_record as cr
  134. WHERE 1= 1 AND cygx_user_id IN (` + ids + `)`
  135. _, err = o.Raw(sql).QueryRows(&item)
  136. return
  137. }
  138. //// 根据手机号获取用户的openid
  139. //func GetUserRecordListByMobileArr(bindAccount []string) (items []*OpenIdList, err error) {
  140. // lenarr := len(bindAccount)
  141. // if lenarr == 0 {
  142. // return
  143. // }
  144. // var condition string
  145. // var pars []interface{}
  146. // condition = ` AND u.bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  147. // pars = append(pars, bindAccount)
  148. // sql := `SELECT cr.open_id,u.user_id
  149. // FROM user_record as u
  150. // INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id
  151. // WHERE u.create_platform=4 ` + condition
  152. // _, err = orm.NewOrm().Raw(sql, pars).QueryRows(&items)
  153. // return
  154. //}
  155. // 根据手机号获取用户的openid
  156. func GetUserRecordListByMobileArr(mobiles []string) (items []*OpenIdList, err error) {
  157. o := orm.NewOrm()
  158. lenarr := len(mobiles)
  159. if lenarr == 0 {
  160. return
  161. }
  162. var condition string
  163. var pars []interface{}
  164. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  165. pars = append(pars, mobiles)
  166. sql := `SELECT
  167. u.open_id,
  168. u.cygx_user_id AS user_id
  169. FROM
  170. cygx_user_record AS u
  171. WHERE
  172. 1 = 1 ` + condition
  173. _, err = o.Raw(sql, pars).QueryRows(&items)
  174. return
  175. }
  176. // 根据用户id和平台id获取用户关系
  177. func GetMfyxUserRecordByMobile(platform int, bindAccount string) (item *OpenIdList, err error) {
  178. var sql string
  179. sql = `SELECT open_id,cygx_user_id AS user_id FROM cygx_mfyx_gzh_user_record WHERE 1 = 1 AND cygx_bind_account = ?`
  180. err = orm.NewOrm().Raw(sql, bindAccount).QueryRow(&item)
  181. return
  182. }
  183. // 根据手机号获取mfyx用户的openid
  184. func GetMfyxUserRecordListByMobileArr(mobiles []string) (items []*OpenIdList, err error) {
  185. o := orm.NewOrm()
  186. lenarr := len(mobiles)
  187. if lenarr == 0 {
  188. return
  189. }
  190. var condition string
  191. var pars []interface{}
  192. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  193. pars = append(pars, mobiles)
  194. sql := `SELECT
  195. u.open_id,
  196. u.cygx_user_id AS user_id
  197. FROM
  198. cygx_mfyx_gzh_user_record AS u
  199. WHERE
  200. 1 = 1 ` + condition
  201. _, err = o.Raw(sql, pars).QueryRows(&items)
  202. return
  203. }
  204. func GetMfyxWxOpenIdByMobileList(mobile string) (items []*OpenIdList, err error) {
  205. sliceMobile := strings.Split(mobile, ",")
  206. var mobiles []string
  207. for _, v := range sliceMobile {
  208. mobiles = append(mobiles, v)
  209. }
  210. o := orm.NewOrm()
  211. lenarr := len(mobiles)
  212. if lenarr == 0 {
  213. return
  214. }
  215. var condition string
  216. var pars []interface{}
  217. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  218. pars = append(pars, mobiles)
  219. sql := `SELECT
  220. u.open_id,
  221. u.cygx_user_id AS user_id
  222. FROM
  223. cygx_mfyx_gzh_user_record AS u
  224. WHERE
  225. 1 = 1 ` + condition
  226. _, err = o.Raw(sql, pars).QueryRows(&items)
  227. return
  228. }