wangchuanjin 2 年之前
父节点
当前提交
a58b1468e9
共有 2 个文件被更改,包括 107 次插入27 次删除
  1. 87 21
      compatible/compatible.go
  2. 20 6
      compatible/compatible_test.go

+ 87 - 21
compatible/compatible.go

@@ -1,26 +1,36 @@
 package compatible
 
 import (
+	"log"
+	"strings"
+
 	util "app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/mysql"
+	. "app.yhyue.com/moapp/jypkg/middleground"
 )
 
 type Compatible struct {
-	Mgo                    *MongodbSim
-	BaseService, MainMysql *mysql.Mysql
+	Mgo          *MongodbSim
+	BaseService  *mysql.Mysql
+	MainMysql    *mysql.Mysql
+	Middleground *Middleground
 }
 
-func NewCompatible(mgo *MongodbSim, baseService, mainMysql *mysql.Mysql) *Compatible {
+func NewCompatible(mgo *MongodbSim, baseService, mainMysql *mysql.Mysql, middleground *Middleground) *Compatible {
+	if middleground.UserCenter == nil {
+		log.Fatalln("NewCompatible的时候,Middleground没有注册用户中台!")
+	}
 	return &Compatible{
-		Mgo:         mgo,
-		BaseService: baseService,
-		MainMysql:   mainMysql,
+		Mgo:          mgo,
+		BaseService:  baseService,
+		MainMysql:    mainMysql,
+		Middleground: middleground,
 	}
 }
 
-//
-func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]interface{} {
+//查询
+func (c *Compatible) Select(id string, fd interface{}) *map[string]interface{} {
 	field := ObjToOth(fd)
 	user := map[string]interface{}{}
 	if field == nil || len(*field) == 0 {
@@ -33,13 +43,11 @@ func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]in
 			user = *temp
 		}
 	} else {
-		list := c.BaseService.SelectBySql(`select a.ent_id,b.phone,a.user_id from base_position a inner join base_user b on (a.id=? and a.user_id=b.id)`, util.IntAll(id))
-		if list == nil || len(*list) == 0 {
+		identity := c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
+		if identity == nil {
 			return &user
 		}
-		phone := util.ObjToString((*list)[0]["phone"])
-		entId := util.IntAll((*list)[0]["ent_id"])
-		if baseUserId := util.IntAll((*list)[0]["user_id"]); baseUserId > 0 {
+		if identity.UserId > 0 {
 			ufd := map[string]interface{}{}
 			for _, k := range []string{"s_m_openid", "s_phone", "s_m_phone", "s_unionid", "s_headimage", "s_headimageurl", "s_nickname", "l_registedate", "s_appversion", "i_ispush", "s_jyname"} {
 				if v, ok := (*field)[k]; ok {
@@ -48,7 +56,7 @@ func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]in
 			}
 			if len(ufd) > 0 {
 				ufd["_id"] = 0
-				data, ok := c.Mgo.FindOneByField("user", map[string]interface{}{"base_user_id": baseUserId}, ufd)
+				data, ok := c.Mgo.FindOneByField("user", map[string]interface{}{"base_user_id": identity.UserId}, ufd)
 				if ok && data != nil && len(*data) > 0 {
 					for k, v := range *data {
 						user[k] = v
@@ -56,12 +64,10 @@ func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]in
 				}
 			}
 		}
-		entUsers := c.MainMysql.SelectBySql(`select id from entniche_user where phone=? and ent_id=?`, phone, entId)
-		if entUsers != nil && len(*entUsers) > 0 {
-			entUserId := util.IntAll((*entUsers)[0]["id"])
+		if identity.EntUserId > 0 {
 			if (*field)["o_entniche"] != nil || (*field)["o_vipjy"] != nil || (*field)["o_member_jy"] != nil || (*field)["o_jy"] != nil {
 				entniche_rule, ok := c.Mgo.Find("entniche_rule", map[string]interface{}{
-					"i_userid": entUserId,
+					"i_userid": identity.EntUserId,
 				}, nil, `{"_id":0,"o_entniche":1,"i_type":1}`, false, -1, -1)
 				if ok && entniche_rule != nil && len(*entniche_rule) > 0 {
 					for _, v := range *entniche_rule {
@@ -91,7 +97,7 @@ func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]in
 			delete((*field), "o_entniche")
 			if len((*field)) > 0 {
 				ent_user, ok := c.Mgo.FindOneByField("ent_user", map[string]interface{}{
-					"i_userid": entUserId,
+					"i_userid": identity.EntUserId,
 				}, field)
 				if ok && ent_user != nil && len(*ent_user) > 0 {
 					for k, v := range *ent_user {
@@ -104,6 +110,66 @@ func (c *Compatible) GetCompatibleUser(id string, fd interface{}) *map[string]in
 	return &user
 }
 
-//
-func (c *Compatible) UpdateCompatibleUser() {
+//更新
+func (c *Compatible) Update(id string, update map[string]interface{}) bool {
+	if IsObjectIdHex(id) {
+		return c.Mgo.UpdateById("user", id, update)
+	} else {
+		identity := c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
+		if identity == nil {
+			return false
+		}
+		up1, up2 := map[string]interface{}{}, map[string]interface{}{}
+		var tp int
+		for k, v := range update {
+			vm, ok := v.(map[string]interface{})
+			if !ok {
+				return false
+			}
+			up11, up22 := map[string]interface{}{}, map[string]interface{}{}
+			key := ""
+			for kk, vv := range vm {
+				if (key == "" || key == "o_jy") && (kk == "o_jy" || strings.HasPrefix(kk, "o_jy.")) {
+					key = "o_jy"
+					up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
+					tp = 2
+				} else if (key == "" || key == "o_vipjy") && (kk == "o_vipjy" || strings.HasPrefix(kk, "o_vipjy.")) {
+					key = "o_vipjy"
+					up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
+					tp = 1
+				} else if (key == "" || key == "o_member_jy") && (kk == "o_member_jy" || strings.HasPrefix(kk, "o_member_jy.")) {
+					key = "o_member_jy"
+					up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
+					tp = 1
+				} else if (key == "" || key == "o_entniche") && (kk == "o_entniche" || strings.HasPrefix(kk, "o_entniche.")) {
+					key = "o_entniche"
+					up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
+					tp = 0
+				} else {
+					up11[kk] = vv
+				}
+			}
+			if len(up11) > 0 {
+				up1[k] = up11
+			}
+			if len(up22) > 0 {
+				up2[k] = up22
+			}
+		}
+		ok1, ok2 := false, false
+		if len(up1) > 0 {
+			ok1 = c.Mgo.Update("ent_user", map[string]interface{}{
+				"i_entid":  identity.EntId,
+				"i_userid": identity.EntUserId,
+			}, up1, true, false)
+		}
+		if len(up2) > 0 {
+			ok2 = c.Mgo.Update("entniche_rule", map[string]interface{}{
+				"i_entid":  identity.EntId,
+				"i_userid": identity.EntUserId,
+				"i_type":   tp,
+			}, up2, false, false)
+		}
+		return ok1 || ok2
+	}
 }

+ 20 - 6
compatible/compatible_test.go

@@ -6,12 +6,14 @@ import (
 
 	. "app.yhyue.com/moapp/jybase/mongodb"
 	. "app.yhyue.com/moapp/jybase/mysql"
+	. "app.yhyue.com/moapp/jypkg/middleground"
 )
 
 var (
-	mgo         *MongodbSim
-	baseService *Mysql
-	mainMysql   *Mysql
+	mgo          *MongodbSim
+	baseService  *Mysql
+	mainMysql    *Mysql
+	middleground *Middleground
 )
 
 func init() {
@@ -39,8 +41,20 @@ func init() {
 		MaxIdleConns: 2,
 	}
 	baseService.Init()
+	middleground = NewMiddleground([]string{"192.168.3.149:2379"}).RegUserCenter("usercenter.rpc")
 }
-func TestGetCompatibleUser(t *testing.T) {
-	c := NewCompatible(mgo, baseService, mainMysql)
-	log.Println(c.GetCompatibleUser("5d6378301c298a5aac7b5402", `{"s_phone":1,"i_vip_status":1,"i_member_status":1,"o_vipjy":1,"o_member_jy":1}`))
+func TestSelect(t *testing.T) {
+	c := NewCompatible(mgo, baseService, mainMysql, middleground)
+	log.Println(c.Select("5d6378301c298a5aac7b5402", `{"s_phone":1,"i_vip_status":1,"i_member_status":1,"o_vipjy":1,"o_member_jy":1}`))
+}
+func TestUpdate(t *testing.T) {
+	c := NewCompatible(mgo, baseService, mainMysql, middleground)
+	log.Println(c.Update("935", map[string]interface{}{
+		"$inc": map[string]interface{}{
+			"test": 1,
+		},
+		"$set": map[string]interface{}{
+			"o_vipjy.test": 1,
+		},
+	}))
 }