package service import ( "encoding/json" "fmt" "math/rand" "strconv" "strings" "time" MC "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/redis" "bp.jydev.jianyu360.cn/BaseService/userCenter/entity" "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb" . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter" "github.com/zeromicro/go-zero/core/logx" ) // RenewWorkDesktopMenuModeOrCommonly 工作桌面--菜单当前选择模式--全部:all/可用:usable // 工作桌面--常用功能更新 // 工作桌面--常用功能列表 func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq) (r *WorkDesktopComprehensiveResp) { r = &WorkDesktopComprehensiveResp{} switch in.ActionMode { case "commonlyRenew": //常用功能更新 //in.MenuIds 不为空:更新 if b, m := entity.CommonlyUpdate(in); !b { r.ErrorCode = -1 r.ErrorMsg = m } case "commonlyList": //查询常用功能列表 in.ActionMode = "commonlyRenew" var ( pIds []string ) logx.Info("--------------------:", fmt.Sprintf(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = '%s' AND appid = %s AND field = '%s' AND userid = '%s' ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId)) //常用功能查看 existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND userid = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId) if existingData == nil || len(*existingData) == 0 { //P278原查询逻辑 existingData = entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode) } if existingData != nil && len(*existingData) > 0 { eData := (*existingData)[0] if MC.ObjToString(eData["value"]) != "" { for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") { if pv == "" { continue } pIds = append(pIds, pv) } } } else { if entity.ConfigJson.CommonlyIds != "" && len(strings.Split(entity.ConfigJson.CommonlyIds, ",")) > 0 { var ids []string for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") { ids = append(ids, encrypt.SE.EncodeString(v)) } in.MenuIds = strings.Join(ids, ",") //初始化 常用功能 if b, _ := entity.CommonlyUpdate(in); b { pIds = strings.Split(entity.ConfigJson.CommonlyIds, ",") } else { logx.Info("初始化常用功能异常") } } } if len(pIds) > 0 { //P278 身份切换 positionType := `` switch in.PositionType { //职位类型 0:个人 1:企业 case "1": positionType = `AND available >= 2` default: positionType = `AND available <= 2` } //常用功能存储是三级菜单id,需要四级菜单信息也查出来 //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0 var menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableInside, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ",")) //外网访问 if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut && !in.IntranetBool { menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableOut, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ",")) } menuData := entity.BaseMysql.SelectBySql(menuSql) if menuData != nil && len(*menuData) > 0 { var ( entId, _ = strconv.ParseInt(in.EntId, 10, 64) newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64) entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64) accountId, _ = strconv.ParseInt(in.AccountId, 10, 64) entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64) positionType, _ = strconv.ParseInt(in.PositionType, 10, 64) positionId, _ = strconv.ParseInt(in.PositionId, 10, 64) m = &entity.WorkDesktopMenu{ MenuTree: []*entity.JYMenu{}, UserId: in.UserId, NewUserId: newUserId, AppId: in.AppId, EntId: entId, EntUserId: entUserId, Platform: in.Platform, AccountId: accountId, EntAccountId: entAccountId, PositionType: positionType, PositionId: positionId, IntranetBool: in.IntranetBool, MgoUserId: in.MgoUserId, } childMenus = map[int][]*entity.JYMenu{} ) //是否存在父级 parentIsExists := map[int]bool{} for _, mv := range *menuData { //id id := MC.IntAll(mv["id"]) //parentId parentId := MC.IntAll(mv["parentid"]) parentIsExists[id] = true menu := &entity.JYMenu{ Id: MC.IntAll(mv["id"]), Name: MC.ObjToString(mv["name"]), Match: MC.ObjToString(mv["match"]), OrderId: MC.IntAll(mv["orderid"]), ParentId: parentId, PowerIds: MC.ObjToString(mv["powerids"]), CheckCode: MC.IntAll(mv["checkcode"]), Icon: MC.ObjToString(mv["icon"]), AppType: MC.ObjToString(mv["apptype"]), OpenType: MC.ObjToString(mv["opentype"]), Status: MC.IntAll(mv["status"]), PermissionCode: MC.ObjToString(mv["permissioncode"]), CapitalCode: MC.ObjToString(mv["capitalcode"]), Authority: MC.IntAll(mv["authority"]), Level: MC.IntAll(mv["level"]), Exclude: MC.ObjToString(mv["exclude"]), } var OpenType = map[string]string{} if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil { menu.OpenType = OpenType[m.Platform] } switch in.Platform { case "WX": menu.Url = MC.ObjToString(mv["wxurl"]) menu.PowerIds = MC.ObjToString(mv["wxpids"]) case "APP": menu.Url = MC.ObjToString(mv["appurl"]) menu.PowerIds = MC.ObjToString(mv["apppids"]) default: menu.Url = MC.ObjToString(mv["pcurl"]) } if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" { additional := map[string]entity.Additional{} if json.Unmarshal([]byte(additionalInfo), &additional) == nil { menu.AdditionalInfo = map[string]entity.Additional{ m.Platform: additional[m.Platform], } } } //外网不一致, if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut { if parentIsExists[parentId] { childMenus[parentId] = append(childMenus[parentId], menu) } else { m.MenuTree = append(m.MenuTree, menu) } } else { if menu.Level == 4 { childMenus[parentId] = append(childMenus[parentId], menu) } else { m.MenuTree = append(m.MenuTree, menu) } } } //常用功能格式化 menu, saveIds, delBool := m.CommonlyFormat(childMenus) //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据 if delBool && len(saveIds) > 0 { in.MenuIds = strings.Join(saveIds, ",") go func(in *WorkDesktopComprehensiveReq) { if b, _ := entity.CommonlyUpdate(in); !b { logx.Info("常用功能-更新数据失败") } }(in) } r.Data = menu } } case "menuMode": //菜单模式更新 if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") { r.ErrorCode = -1 r.ErrorMsg = "菜单模式-参数异常" } else { menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid =? AND field = ? LIMIT 1`, in.AppId, in.NewUserId, in.ActionMode) if menuModes != nil && len(*menuModes) > 0 { menuMode := (*menuModes)[0] if MC.ObjToString(menuMode["value"]) != in.MenuMode { if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 { r.ErrorCode = -1 r.ErrorMsg = "菜单模式-更新异常" } } } else { if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{ "appid": in.AppId, "base_userid": in.NewUserId, "field": in.ActionMode, "platform": in.Platform, "value": in.MenuMode, }) < 0 { r.ErrorCode = -1 r.ErrorMsg = "菜单模式-插入异常" } } } } return } // GetWorkDesktopMenuMode 获取工作菜单模式 func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) { if in.UserId == "" { return "", fmt.Errorf("参数异常") } str = "all" menuModes := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid=? AND field = 'menuMode' AND platform = ? LIMIT 1`, in.AppId, in.NewUserId, in.Platform) if menuModes != nil && len(*menuModes) > 0 { menuMode := (*menuModes)[0] if MC.ObjToString(menuMode["value"]) != "" { str = MC.ObjToString(menuMode["value"]) } } return } // GetWordDesktopMenuTree 获取工作桌面菜单树 func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) { //P278 身份切换 此处逻辑已不需要 //if in.EntId != "" { // //判断商机管理用户是否切换企业 // userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId) // redisEntId := redis.GetInt(entity.RedisCode, userEntIdKey) // entId, err := strconv.Atoi(in.EntId) // if err == nil && int64(redisEntId) >= 0 && redisEntId != entId { // //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存 // entity.ClearUserPowerFunc(in.UserId, in.AppId, in.EntId) // } //} t1 := time.Now() //菜单redis缓存 var ( menuList []*pb.MenuList ) RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, entity.ConfigJson.MenuCacheKey, in.UserId) logx.Info("RedisMenuKey:", RedisMenuKey) menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey) if in.WorkStatus == 0 && err == nil && len(*menuBytes) > 0 { if json.Unmarshal(*menuBytes, &menuList) == nil { return menuList, nil } } entId, _ := strconv.ParseInt(in.EntId, 10, 64) newUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64) entUserId, _ := strconv.ParseInt(in.EntUserId, 10, 64) accountId, _ := strconv.ParseInt(in.AccountId, 10, 64) entAccountId, _ := strconv.ParseInt(in.EntAccountId, 10, 64) positionType, _ := strconv.ParseInt(in.PositionType, 10, 64) positionId, _ := strconv.ParseInt(in.PositionId, 10, 64) wdm := &entity.WorkDesktopMenu{ MenuTree: []*entity.JYMenu{}, UserId: in.UserId, //userId 会取消,职位id NewUserId: newUserId, AppId: in.AppId, EntId: entId, EntUserId: entUserId, Platform: in.Platform, IntranetBool: in.IntranetBool, WorkStatus: in.WorkStatus, AccountId: accountId, EntAccountId: entAccountId, PositionType: positionType, //职位类型 0:个人 1:企业 PositionId: positionId, MgoUserId: in.MgoUserId, } logx.Info("--------wdm--MgoUserId-------:", wdm.MgoUserId) //获取菜单树的数据 if err := wdm.GetMenuTreeData(); err != nil { return nil, err } logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1)) //jyMenu := wdm.WorkMenuTree(0) //菜单树生成 wdm.MenuTree = wdm.WorkMenuTree(0) logx.Info("菜单树生成耗时:", time.Since(t1)) menuList, err = wdm.WorkMenuFormat() logx.Info("菜单格式化耗时:", time.Since(t1)) if err == nil && len(menuList) > 0 && in.WorkStatus == 0 { //我的页面in.WorkStatus == 2 菜单暂不存缓存 if menuBytes, err := json.Marshal(menuList); err == nil { redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60) if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil { logx.Info("工作桌面菜单 redis缓存异常") } } else { logx.Info("菜单数据序列化异常") } } logx.Info("整体耗时:", time.Since(t1)) return menuList, err }