user_record.go 7.7 KB

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