Просмотр исходного кода

Merge branch 'master' of https://app.yhyue.com/moapp/jypkg into master

zhangxinlei1996 2 лет назад
Родитель
Сommit
e72924b35a
4 измененных файлов с 276 добавлено и 54 удалено
  1. 210 0
      compatible/compatible.go
  2. 60 0
      compatible/compatible_test.go
  3. 6 0
      identity/identity.go
  4. 0 54
      public/public.go

+ 210 - 0
compatible/compatible.go

@@ -0,0 +1,210 @@
+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"
+	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
+)
+
+type Compatible struct {
+	Mgo          *MongodbSim
+	BaseService  *mysql.Mysql
+	MainMysql    *mysql.Mysql
+	Middleground *Middleground
+}
+
+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,
+		Middleground: middleground,
+	}
+}
+
+//根据mgo库user表_id,或者企业身份的职位id查询
+func (c *Compatible) Select(id string, field interface{}) *map[string]interface{} {
+	var identity *pb.Identity
+	if !IsObjectIdHex(id) {
+		identity = c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
+	}
+	return c.selectDo(id, identity, field)
+}
+
+//根据mgo库user表_id,或者企业身份的企业员工id查询
+func (c *Compatible) SelectByEntUserId(id string, field interface{}) *map[string]interface{} {
+	var identity *pb.Identity
+	if !IsObjectIdHex(id) {
+		identity = c.Middleground.UserCenter.IdentityByEntUserId(util.Int64All(id))
+	}
+	return c.selectDo(id, identity, field)
+}
+
+//
+func (c *Compatible) selectDo(id string, identity *pb.Identity, fd interface{}) *map[string]interface{} {
+	field := ObjToOth(fd)
+	user := map[string]interface{}{}
+	if field == nil || len(*field) == 0 {
+		return &user
+	}
+	(*field)["_id"] = 0
+	if IsObjectIdHex(id) {
+		temp, ok := c.Mgo.FindById("user", id, field)
+		if ok && temp != nil && len(*temp) > 0 {
+			user = *temp
+		}
+	} else {
+		if identity == nil {
+			return &user
+		}
+		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", "s_jpushid", "s_opushid", "s_appponetype", "i_applystatus"} {
+				if v, ok := (*field)[k]; ok {
+					ufd[k] = v
+				}
+			}
+			if len(ufd) > 0 {
+				ufd["_id"] = 0
+				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
+					}
+				}
+			}
+		}
+		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": 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 {
+						i_type := util.IntAll(v["i_type"])
+						if i_type == 0 {
+							if (*field)["o_entniche"] != nil {
+								user["o_entniche"] = v["o_entniche"]
+							}
+						} else if i_type == 1 {
+							if (*field)["o_vipjy"] != nil {
+								user["o_vipjy"] = v["o_entniche"]
+							}
+							if (*field)["o_member_jy"] != nil {
+								user["o_member_jy"] = v["o_entniche"]
+							}
+						} else if i_type == 2 {
+							if (*field)["o_jy"] != nil {
+								user["o_jy"] = v["o_entniche"]
+							}
+						}
+					}
+				}
+			}
+			delete((*field), "o_jy")
+			delete((*field), "o_vipjy")
+			delete((*field), "o_member_jy")
+			delete((*field), "o_entniche")
+			if len((*field)) > 0 {
+				ent_user, ok := c.Mgo.FindOneByField("ent_user", map[string]interface{}{
+					"i_userid": identity.EntUserId,
+				}, field)
+				if ok && ent_user != nil && len(*ent_user) > 0 {
+					for k, v := range *ent_user {
+						user[k] = v
+					}
+				}
+			}
+		}
+	}
+	return &user
+}
+
+//根据mgo库user表_id,或者企业身份的职位id更新
+func (c *Compatible) Update(id string, update map[string]interface{}) bool {
+	var identity *pb.Identity
+	if !IsObjectIdHex(id) {
+		identity = c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
+	}
+	return c.updateDo(id, identity, update)
+}
+
+//根据mgo库user表_id,或者企业身份的企业员工id更新
+func (c *Compatible) UpdateByEntUserId(id string, update map[string]interface{}) bool {
+	var identity *pb.Identity
+	if !IsObjectIdHex(id) {
+		identity = c.Middleground.UserCenter.IdentityByEntUserId(util.Int64All(id))
+	}
+	return c.updateDo(id, identity, update)
+}
+
+//
+func (c *Compatible) updateDo(id string, identity *pb.Identity, update map[string]interface{}) bool {
+	if IsObjectIdHex(id) {
+		return c.Mgo.UpdateById("user", id, update)
+	} else {
+		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
+	}
+}

+ 60 - 0
compatible/compatible_test.go

@@ -0,0 +1,60 @@
+package compatible
+
+import (
+	"log"
+	"testing"
+
+	. "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
+	middleground *Middleground
+)
+
+func init() {
+	mgo = &MongodbSim{
+		MongodbAddr: "192.168.3.206:27080",
+		Size:        2,
+		DbName:      "qfw",
+	}
+	mgo.InitPool()
+	mainMysql = &Mysql{
+		Address:      "192.168.3.11:3366",
+		UserName:     "root",
+		PassWord:     "Topnet123",
+		DBName:       "jianyu",
+		MaxOpenConns: 2,
+		MaxIdleConns: 2,
+	}
+	mainMysql.Init()
+	baseService = &Mysql{
+		Address:      "192.168.3.217:4000",
+		UserName:     "root",
+		PassWord:     "=PDT49#80Z!RVv52_z",
+		DBName:       "base_service",
+		MaxOpenConns: 2,
+		MaxIdleConns: 2,
+	}
+	baseService.Init()
+	middleground = NewMiddleground([]string{"192.168.3.149:2379"}).RegUserCenter("usercenter.rpc")
+}
+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,
+		},
+	}))
+}

+ 6 - 0
identity/identity.go

@@ -51,6 +51,12 @@ func (i *IdentityInfo) Switch(sess *httpsession.Session) bool {
 			ok = true
 		}
 	} else {
+		m["entId"] = i.EntId
+		m["entUserId"] = i.EntUserId
+		m["entName"] = i.Name
+		m["entUserName"] = i.EntUserName
+		m["frameworkEntId"] = i.EntId
+		m["frameworkEntName"] = i.Name
 		m["userId"] = strconv.FormatInt(i.PositionId, 10)
 		m["entAccountId"] = i.EntAccountId
 		ok = true

+ 0 - 54
public/public.go

@@ -277,57 +277,3 @@ func isEmail(value string) bool {
 	var emailPattern = regexp.MustCompile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
 	return emailPattern.MatchString(value)
 }
-
-func GetCompatibleUser(mgo *MongodbSim, userId string, field map[string]int) *map[string]interface{} {
-	user := map[string]interface{}{}
-	if field == nil || len(field) == 0 {
-		return &user
-	}
-	field["_id"] = 0
-	if IsObjectIdHex(userId) {
-		temp, ok := mgo.FindById("user", userId, field)
-		if ok && temp != nil && len(*temp) > 0 {
-			user = *temp
-		}
-	} else {
-		if field["o_entniche"] > 0 || field["o_vipjy"] > 0 || field["o_member_jy"] > 0 || field["o_jy"] > 0 {
-			entniche_rule, ok := mgo.Find("entniche_rule", map[string]interface{}{
-				"i_userid": util.IntAll(userId),
-			}, nil, `{"o_entniche":1}`, false, -1, -1)
-			if ok && entniche_rule != nil && len(*entniche_rule) > 0 {
-				for _, v := range *entniche_rule {
-					i_type := util.IntAll(v["i_type"])
-					if i_type == 0 {
-						if field["o_entniche"] > 0 {
-							user["o_entniche"] = v["o_entniche"]
-						}
-					} else if i_type == 1 {
-						if field["o_vipjy"] > 0 {
-							user["o_vipjy"] = v["o_entniche"]
-						}
-						if field["o_member_jy"] > 0 {
-							user["o_member_jy"] = v["o_entniche"]
-						}
-					} else if i_type == 2 {
-						if field["o_jy"] > 0 {
-							user["o_jy"] = v["o_entniche"]
-						}
-					}
-				}
-			}
-		}
-		delete(field, "o_jy")
-		delete(field, "o_vipjy")
-		delete(field, "o_member_jy")
-		delete(field, "o_entniche")
-		ent_user, ok := mgo.FindOneByField("ent_user", map[string]interface{}{
-			"i_userid": util.IntAll(userId),
-		}, field)
-		if ok && ent_user != nil && len(*ent_user) > 0 {
-			for k, v := range *ent_user {
-				user[k] = v
-			}
-		}
-	}
-	return &user
-}