package compatible import ( "encoding/json" "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) if field == nil || len(*field) == 0 { return nil } if IsObjectIdHex(id) { temp, _ := c.Mgo.FindById("user", id, field) return temp } else { if identity == nil { return nil } user := map[string]interface{}{} if identity.UserId > 0 { ufd := map[string]interface{}{} for _, k := range []string{"s_myemail", "s_company", "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 { fms := []string{} fmp := map[int]map[string]interface{}{} for k, v := range *field { if util.IntAll(v) <= 0 { continue } if k == "o_entniche" || strings.HasPrefix(k, "o_entniche.") { if fmp[0] == nil { fmp[0] = map[string]interface{}{"_id": 0} } fmp[0][k] = 1 } else if k == "o_vipjy" || strings.HasPrefix(k, "o_vipjy.") { if fmp[1] == nil { fmp[1] = map[string]interface{}{"_id": 0} } fms = append(fms, "o_vipjy") fmp[1][strings.ReplaceAll(k, "o_vipjy", "o_entniche")] = 1 } else if k == "o_member_jy" || strings.HasPrefix(k, "o_member_jy.") { if fmp[1] == nil { fmp[1] = map[string]interface{}{"_id": 0} } fms = append(fms, "o_member_jy") fmp[1][strings.ReplaceAll(k, "o_member_jy", "o_entniche")] = 1 } else if k == "o_jy" || strings.HasPrefix(k, "o_jy.") { if fmp[2] == nil { fmp[2] = map[string]interface{}{"_id": 0} } fmp[2][strings.ReplaceAll(k, "o_jy", "o_entniche")] = 1 } else { continue } } for k, v := range fmp { entniche_rule, ok := c.Mgo.FindOneByField("entniche_rule", map[string]interface{}{ "i_userid": identity.EntUserId, "i_type": k, }, v) if ok && entniche_rule != nil && len(*entniche_rule) > 0 { if k == 0 { user["o_entniche"] = (*entniche_rule)["o_entniche"] } else if k == 1 { for _, vv := range fms { user[vv] = (*entniche_rule)["o_entniche"] } } else if k == 2 { user["o_jy"] = (*entniche_rule)["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 } } } } if len(user) == 0 { user["0"] = 0 } 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 { b, err := json.Marshal(v) if err != nil { return false } else if err = json.Unmarshal(b, &vm); err != nil { return false } else if vm == nil || len(vm) == 0 { 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, true, false) } return ok1 || ok2 } }