util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/mongodb"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "log"
  10. "new-userlocation/db"
  11. "sync"
  12. )
  13. var (
  14. ctx = context.Background()
  15. PositionMgoUserIdMap = sync.Map{}
  16. MgoPositionUserIdMap = sync.Map{}
  17. )
  18. type UserStandingId struct {
  19. Standing string
  20. BaseId int64
  21. }
  22. func init() {
  23. res, err := g.DB().Query(ctx, `SELECT * FROM dwd_mgo_position`)
  24. if err == nil && !res.IsEmpty() {
  25. for _, re := range res {
  26. if gconv.Int32(re["type"]) == 0 {
  27. MgoPositionUserIdMap.Store(gconv.String(re["mgoUserId"]), UserStandingId{
  28. gconv.String(re["positionId"]),
  29. gconv.Int64(re["baseUserId"]),
  30. })
  31. }
  32. PositionMgoUserIdMap.Store(gconv.String(re["positionId"]), UserStandingId{
  33. gconv.String(re["mgoUserId"]),
  34. gconv.Int64(re["baseUserId"]),
  35. })
  36. }
  37. }
  38. log.Println("初始化user", res.Len())
  39. }
  40. func GetPositionId(userid string) (string, int64, int64) {
  41. if userid == "" {
  42. return "", 0, 0
  43. }
  44. if !mongodb.IsObjectIdHex(userid) { //企业身份
  45. if standing, ok := PositionMgoUserIdMap.Load(userid); ok && standing != nil { //已缓存
  46. ds := standing.(UserStandingId)
  47. return ds.Standing, gconv.Int64(userid), ds.BaseId
  48. } else {
  49. res, err := g.DB("tidb").GetOne(ctx, fmt.Sprintf(`SELECT user_id,type FROM base_position WHERE id = %s`, userid))
  50. if err != nil {
  51. return "", 0, 0
  52. }
  53. bsId := gconv.Int64(res.Map()["user_id"])
  54. data, ok := db.MG.DB().FindOne("user", map[string]interface{}{
  55. "base_user_id": bsId,
  56. })
  57. if ok && data != nil {
  58. msId := common.InterfaceToStr((*data)["_id"])
  59. phone := common.If(common.InterfaceToStr((*data)["s_phone"]) != "", common.InterfaceToStr((*data)["s_phone"]), common.InterfaceToStr((*data)["s_m_phone"])).(string)
  60. PositionMgoUserIdMap.Store(userid, UserStandingId{
  61. msId,
  62. bsId,
  63. })
  64. _, err = g.DB().Exec(ctx, fmt.Sprintf(`INSERT INTO dwd_mgo_position VALUES ('%s', %s,%d,'%s',%d,%d) `, msId, userid, bsId, common.If(phone != "", phone, ""), gconv.Int64((*data)["l_registedate"]), gconv.Int64(res.Map()["type"])))
  65. if err != nil {
  66. log.Println("Exec INSERT INTO dwd_mgo_position err ", err.Error())
  67. }
  68. return msId, gconv.Int64(userid), bsId
  69. }
  70. }
  71. }
  72. //个人身份
  73. if standing, ok := MgoPositionUserIdMap.Load(userid); ok && standing != nil { //已缓存
  74. ds := standing.(UserStandingId)
  75. return userid, gconv.Int64(ds.Standing), ds.BaseId
  76. }
  77. data, ok := db.MG.DB().FindOne("user", map[string]interface{}{
  78. "_id": mongodb.StringTOBsonId(userid),
  79. })
  80. if ok && data != nil && len(*data) > 0 {
  81. baseId := gconv.Int64((*data)["base_user_id"])
  82. phone := common.If(common.InterfaceToStr((*data)["s_phone"]) != "", common.InterfaceToStr((*data)["s_phone"]), common.InterfaceToStr((*data)["s_m_phone"])).(string)
  83. if baseId > 0 {
  84. res, err := g.DB("tidb").GetOne(ctx, fmt.Sprintf(`SELECT id,type FROM base_position WHERE user_id = %d`, baseId))
  85. if err == nil && !res.IsEmpty() {
  86. id := common.Int64All(res.Map()["id"])
  87. log.Println("新增用户:", userid, id)
  88. MgoPositionUserIdMap.Store(userid, UserStandingId{
  89. gconv.String(id),
  90. baseId,
  91. })
  92. _, err = g.DB().Exec(ctx, fmt.Sprintf(`INSERT INTO dwd_mgo_position VALUES ('%s', %d,%d,'%s',%d,%d) `, userid, id, baseId, common.If(phone != "", phone, ""), gconv.Int64((*data)["l_registedate"]), gconv.Int32(res.Map()["type"])))
  93. if err != nil {
  94. log.Println("err Exec:", err.Error())
  95. }
  96. return userid, id, baseId
  97. }
  98. }
  99. }
  100. return "", 0, 0
  101. }