12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package service
- import (
- "context"
- "github.com/gogf/gf/v2/frame/g"
- "jylua-api/internal/dao"
- "jylua-api/internal/model"
- "sync"
- )
- type (
- luaPathTree struct {
- mTree map[int]*pathObj
- mLock *sync.Mutex
- }
- pathObj struct {
- Path string
- Pid int
- BScan bool
- }
- )
- var (
- JyLuaPathTree = &luaPathTree{
- mTree: map[int]*pathObj{},
- mLock: &sync.Mutex{},
- }
- )
- func (tree *luaPathTree) GetPath(gid int, path string, force bool) string {
- tree.mLock.Lock()
- defer tree.mLock.Unlock()
- ctx := context.Background()
- if len(tree.mTree) == 0 || force {
- //初始化
- var group []*model.JyapiGroup
- err := dao.JyapiGroup.Ctx(ctx).Where("status=1").Order("pid ASC").Scan(&group)
- if err == nil {
- for _, v1 := range group {
- tree.mTree[v1.Id] = &pathObj{v1.Path, v1.Pid, false}
- }
- for _, v1 := range group {
- pid := v1.Pid
- for {
- if p, ok := tree.mTree[pid]; ok {
- tree.mTree[v1.Id].Path = p.Path + tree.mTree[v1.Id].Path
- pid = p.Pid
- if p.BScan {
- tree.mTree[v1.Id].BScan = true
- break
- }
- } else {
- tree.mTree[v1.Id].BScan = true
- break
- }
- }
- }
- } else {
- g.Log().Error(ctx, err)
- }
- }
- var fPatch string
- if pObj, ok := tree.mTree[gid]; ok {
- fPatch = pObj.Path
- }
- return fPatch + path
- }
|