123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package service
- import (
- "context"
- "go-common/app/interface/main/dm2/model"
- "go-common/library/log"
- )
- // getSubtitlDraft get a subtitle
- // cache throught
- func (s *Service) getSubtitlDraft(c context.Context, oid int64, tp int32, mid int64, lanCode uint8) (draft *model.Subtitle, err error) {
- var (
- cacheErr bool
- )
- if draft, err = s.dao.SubtitleDraftCache(c, oid, tp, mid, lanCode); err != nil {
- cacheErr = true
- err = nil
- }
- if draft != nil {
- if draft.ID <= 0 {
- draft = nil
- err = nil
- }
- return
- }
- if draft, err = s.dao.GetSubtitleDraft(c, oid, tp, mid, lanCode); err != nil {
- log.Error("params(oid:%v,tp:%v,mid:%v,lanCode:%v).error(%v)", oid, tp, mid, lanCode, err)
- return
- }
- if draft == nil {
- draft = &model.Subtitle{
- Oid: oid,
- Type: tp,
- Mid: mid,
- Lan: lanCode,
- }
- }
- if !cacheErr {
- temp := draft
- s.cache.Do(c, func(ctx context.Context) {
- s.dao.SetSubtitleDraftCache(ctx, temp)
- })
- }
- if draft.ID <= 0 {
- draft = nil
- err = nil
- }
- return
- }
- // GetSubtitle get a subtitle
- func (s *Service) getSubtitle(c context.Context, oid int64, subtitleID int64) (subtitle *model.Subtitle, err error) {
- var (
- cacheErr bool
- )
- if subtitle, err = s.dao.SubtitleCache(c, oid, subtitleID); err != nil {
- cacheErr = true
- err = nil
- }
- if subtitle != nil {
- if subtitle.Empty {
- subtitle = nil
- err = nil
- }
- return
- }
- if subtitle, err = s.dao.GetSubtitle(c, oid, subtitleID); err != nil {
- log.Error("params(oid:%v, subtitleID:%v).error(%v)", oid, subtitleID, err)
- return
- }
- if subtitle == nil {
- subtitle = &model.Subtitle{
- Oid: oid,
- ID: subtitleID,
- Empty: true,
- }
- }
- if !cacheErr {
- temp := subtitle
- s.cache.Do(c, func(ctx context.Context) {
- s.dao.SetSubtitleCache(ctx, temp)
- })
- }
- if subtitle.Empty {
- subtitle = nil
- err = nil
- }
- return
- }
- // getSubtitles 不保证顺序
- func (s *Service) getSubtitles(c context.Context, oid int64, subtitleIds []int64) (subtitles map[int64]*model.Subtitle, err error) {
- var (
- hits map[int64]*model.Subtitle
- missed []int64
- cacheErr bool
- missedSubtitles []*model.Subtitle
- )
- if hits, missed, err = s.dao.SubtitlesCache(c, oid, subtitleIds); err != nil {
- cacheErr = true
- err = nil
- }
- subtitles = make(map[int64]*model.Subtitle)
- for _, subtitle := range hits {
- if subtitle.Empty {
- missed = append(missed, subtitle.ID)
- continue
- }
- subtitles[subtitle.ID] = subtitle
- }
- if len(missed) > 0 {
- if missedSubtitles, err = s.dao.GetSubtitles(c, oid, missed); err != nil {
- log.Error("getSubtitles(oid:%v,subtitleIds:%v),error(%v)", oid, subtitleIds, err)
- return
- }
- }
- for _, subtitle := range missedSubtitles {
- subtitles[subtitle.ID] = subtitle
- }
- if !cacheErr {
- for _, subtitle := range missedSubtitles {
- temp := subtitle
- s.cache.Do(c, func(ctx context.Context) {
- s.dao.SetSubtitleCache(ctx, temp)
- })
- }
- }
- return
- }
|