package service import ( "fmt" "slices" "sync" "yintai-api/app/api/request" "yintai-api/app/common/consts" "yintai-api/app/common/dao" err2 "yintai-api/app/common/err" "yintai-api/app/common/model" log "github.com/sirupsen/logrus" "github.com/spf13/cast" ) type Document struct{} func (r *Document) List(req request.DocumentList) (any, int64) { req.Pagination() lists, count, err := dao.NewDocument().List(req.Lang, cast.ToInt64(req.Category), req.FileName, req.Title, req.SubTitle, req.Desc, req.Page, req.Size, req.Sort) if err != nil { err2.DbErr(err) } listLang := make([]string, 0) for _, list := range lists { listLang = append(listLang, fmt.Sprintf("%d_%s", list.ID, list.Lang)) } res := make([]any, 0) var store sync.Map var wg sync.WaitGroup for _, list := range lists { wg.Add(1) go func(list *model.DocumentPage) { defer wg.Done() store.Store(fmt.Sprintf("%d_%s", list.ID, list.Lang), list.Format()) for _, lang := range consts.LangSupport { lang = fmt.Sprintf("%d_%s", list.ID, list.Lang) if !slices.Contains(listLang, lang) { log.Debugf("%s is writen", list.Lang) store.Store(lang, r.One(lang, list.ID)) } } }(list) } wg.Wait() written := make(map[string]bool) for _, list := range lists { for _, lang := range consts.LangSupport { lang = fmt.Sprintf("%d_%s", list.ID, list.Lang) if _, ok := written[lang]; ok { continue } v, ok := store.Load(lang) if !ok { continue } res = append(res, v) written[lang] = true } } return res, count } func (r *Document) One(lang string, id int64) map[string]any { x, err := dao.NewDocument().GetLang(lang, id) if err != nil { err2.DbErr(err) } return x.Format() } func (r *Document) Get(req request.DocumentDelete) any { res := make([]map[string]any, 0) for _, lang := range consts.LangSupport { res = append(res, r.One(lang, cast.ToInt64(req.ID))) } return res }