123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package util
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/mongodb"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "log"
- "new-userlocation/db"
- "sync"
- )
- var (
- ctx = context.Background()
- PositionMgoUserIdMap = sync.Map{}
- MgoPositionUserIdMap = sync.Map{}
- )
- type UserStandingId struct {
- Standing string
- BaseId int64
- }
- func init() {
- res, err := g.DB().Query(ctx, `SELECT * FROM dwd_mgo_position`)
- if err == nil && !res.IsEmpty() {
- for _, re := range res {
- if gconv.Int32(re["type"]) == 0 {
- MgoPositionUserIdMap.Store(gconv.String(re["mgoUserId"]), UserStandingId{
- gconv.String(re["positionId"]),
- gconv.Int64(re["baseUserId"]),
- })
- }
- PositionMgoUserIdMap.Store(gconv.String(re["positionId"]), UserStandingId{
- gconv.String(re["mgoUserId"]),
- gconv.Int64(re["baseUserId"]),
- })
- }
- }
- log.Println("初始化user", res.Len())
- }
- func GetPositionId(userid string) (string, int64, int64) {
- if userid == "" {
- return "", 0, 0
- }
- if !mongodb.IsObjectIdHex(userid) { //企业身份
- if standing, ok := PositionMgoUserIdMap.Load(userid); ok && standing != nil { //已缓存
- ds := standing.(UserStandingId)
- return ds.Standing, gconv.Int64(userid), ds.BaseId
- } else {
- res, err := g.DB("tidb").GetOne(ctx, fmt.Sprintf(`SELECT user_id,type FROM base_position WHERE id = %s`, userid))
- if err != nil {
- return "", 0, 0
- }
- bsId := gconv.Int64(res.Map()["user_id"])
- data, ok := db.MG.DB().FindOne("user", map[string]interface{}{
- "base_user_id": bsId,
- })
- if ok && data != nil {
- msId := common.InterfaceToStr((*data)["_id"])
- phone := common.If(common.InterfaceToStr((*data)["s_phone"]) != "", common.InterfaceToStr((*data)["s_phone"]), common.InterfaceToStr((*data)["s_m_phone"])).(string)
- PositionMgoUserIdMap.Store(userid, UserStandingId{
- msId,
- bsId,
- })
- _, 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"])))
- if err != nil {
- log.Println("Exec INSERT INTO dwd_mgo_position err ", err.Error())
- }
- return msId, gconv.Int64(userid), bsId
- }
- }
- }
- //个人身份
- if standing, ok := MgoPositionUserIdMap.Load(userid); ok && standing != nil { //已缓存
- ds := standing.(UserStandingId)
- return userid, gconv.Int64(ds.Standing), ds.BaseId
- }
- data, ok := db.MG.DB().FindOne("user", map[string]interface{}{
- "_id": mongodb.StringTOBsonId(userid),
- })
- if ok && data != nil && len(*data) > 0 {
- baseId := gconv.Int64((*data)["base_user_id"])
- phone := common.If(common.InterfaceToStr((*data)["s_phone"]) != "", common.InterfaceToStr((*data)["s_phone"]), common.InterfaceToStr((*data)["s_m_phone"])).(string)
- if baseId > 0 {
- res, err := g.DB("tidb").GetOne(ctx, fmt.Sprintf(`SELECT id,type FROM base_position WHERE user_id = %d`, baseId))
- if err == nil && !res.IsEmpty() {
- id := common.Int64All(res.Map()["id"])
- log.Println("新增用户:", userid, id)
- MgoPositionUserIdMap.Store(userid, UserStandingId{
- gconv.String(id),
- baseId,
- })
- _, 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"])))
- if err != nil {
- log.Println("err Exec:", err.Error())
- }
- return userid, id, baseId
- }
- }
- }
- return "", 0, 0
- }
|