123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- package service
- import (
- "context"
- "time"
- "go-common/app/interface/main/history/model"
- hisapi "go-common/app/service/main/history/api/grpc"
- history "go-common/app/service/main/history/model"
- "go-common/library/log"
- "go-common/library/net/metadata"
- "github.com/pkg/errors"
- )
- func (s *Service) serviceRun(f func()) {
- select {
- case s.serviceChan <- f:
- default:
- log.Error("serviceChan full")
- }
- }
- func (s *Service) serviceproc() {
- for {
- f := <-s.serviceChan
- f()
- }
- }
- func (s *Service) serviceAdd(arg *model.History) {
- s.serviceRun(func() {
- h := arg.ConvertServiceType()
- arg := &hisapi.AddHistoryReq{
- Mid: h.Mid,
- Business: h.Business,
- Kid: h.Kid,
- Aid: h.Aid,
- Sid: h.Sid,
- Epid: h.Epid,
- Cid: h.Cid,
- SubType: h.SubType,
- Device: h.Device,
- Progress: h.Progress,
- ViewAt: h.ViewAt,
- }
- s.hisRPC.AddHistory(context.Background(), arg)
- })
- }
- func (s *Service) serviceAdds(mid int64, hs []*model.History) {
- s.serviceRun(func() {
- arg := &hisapi.AddHistoriesReq{}
- for _, a := range hs {
- h := a.ConvertServiceType()
- arg.Histories = append(arg.Histories, &hisapi.AddHistoryReq{
- Mid: mid,
- Business: h.Business,
- Kid: h.Kid,
- Aid: h.Aid,
- Sid: h.Sid,
- Epid: h.Epid,
- Cid: h.Cid,
- SubType: h.SubType,
- Device: h.Device,
- Progress: h.Progress,
- ViewAt: h.ViewAt,
- })
- }
- s.hisRPC.AddHistories(context.Background(), arg)
- })
- }
- func (s *Service) serviceDel(ctx context.Context, mid int64, his []*model.History) error {
- arg := &hisapi.DelHistoriesReq{
- Mid: mid,
- Records: []*hisapi.DelHistoriesReq_Record{},
- }
- var aids []int64
- for _, v := range his {
- if v.TP == model.TypePGC || v.TP == model.TypeUGC {
- aids = append(aids, v.Aid)
- }
- }
- seasonMap := make(map[int64]*model.BangumiSeason)
- if len(aids) > 0 {
- seasonMap, _ = s.season(ctx, mid, aids, metadata.String(ctx, metadata.RemoteIP))
- }
- for _, h := range his {
- if value, ok := seasonMap[h.Aid]; ok && value != nil {
- arg.Records = append(arg.Records, &hisapi.DelHistoriesReq_Record{
- Business: model.BusinessByTP(model.TypePGC),
- ID: value.ID,
- })
- log.Warn("seasonMap(%d,%v)season:%d", mid, h.Aid, value.ID)
- }
- arg.Records = append(arg.Records, &hisapi.DelHistoriesReq_Record{
- Business: model.BusinessByTP(h.TP),
- ID: h.Aid,
- })
- }
- if _, err := s.hisRPC.DelHistories(ctx, arg); err != nil {
- log.Error("s.hisRPC.DelHistories(%+v %+v) err:%+v", his, arg, errors.WithStack(err))
- return err
- }
- return nil
- }
- func (s *Service) serviceClear(mid int64, tps []int8) {
- s.serviceRun(func() {
- arg := &hisapi.ClearHistoryReq{
- Mid: mid,
- }
- for _, t := range tps {
- arg.Businesses = append(arg.Businesses, model.BusinessByTP(t))
- }
- s.hisRPC.ClearHistory(context.Background(), arg)
- })
- }
- func (s *Service) serviceDels(ctx context.Context, mid int64, aids []int64, typ int8) error {
- arg := &hisapi.DelHistoriesReq{
- Mid: mid,
- Records: []*hisapi.DelHistoriesReq_Record{},
- }
- seasonMap := make(map[int64]*model.BangumiSeason)
- if typ == 0 {
- seasonMap, _ = s.season(ctx, mid, aids, metadata.String(ctx, metadata.RemoteIP))
- }
- b := model.BusinessByTP(typ)
- for _, aid := range aids {
- if value, ok := seasonMap[aid]; ok && value != nil {
- arg.Records = append(arg.Records, &hisapi.DelHistoriesReq_Record{
- Business: model.BusinessByTP(model.TypePGC),
- ID: value.ID,
- })
- log.Warn("seasonMap(%d,%v)season:%d", mid, aid, value.ID)
- }
- arg.Records = append(arg.Records, &hisapi.DelHistoriesReq_Record{
- Business: b,
- ID: aid,
- })
- }
- if _, err := s.hisRPC.DelHistories(ctx, arg); err != nil {
- log.Error("s.hisRPC.DelHistories(%v %+v) err:%+v", mid, arg, errors.WithStack(err))
- return err
- }
- return nil
- }
- func (s *Service) serviceHide(mid int64, hide bool) {
- s.serviceRun(func() {
- arg := &hisapi.UpdateUserHideReq{
- Mid: mid,
- Hide: hide,
- }
- s.hisRPC.UpdateUserHide(context.Background(), arg)
- })
- }
- func (s *Service) serviceHistoryCursor(c context.Context, mid int64, kid int64, businesses []string, business string, viewAt int64, ps int) ([]*model.Resource, error) {
- if viewAt == 0 {
- viewAt = time.Now().Unix()
- }
- arg := &hisapi.UserHistoriesReq{
- Mid: mid,
- Businesses: businesses,
- Business: business,
- Kid: kid,
- ViewAt: viewAt,
- Ps: int64(ps),
- }
- reply, err := s.hisRPC.UserHistories(c, arg)
- if err != nil {
- log.Error("s.hisRPC.UserHistories(%+v) err:%+v", arg, err)
- return nil, err
- }
- if reply == nil {
- return nil, err
- }
- his := make([]*model.Resource, 0)
- for _, v := range reply.Histories {
- tp, _ := model.CheckBusiness(v.Business)
- his = append(his, &model.Resource{
- Mid: v.Mid,
- Oid: v.Aid,
- Sid: v.Sid,
- Epid: v.Epid,
- TP: tp,
- Business: v.Business,
- STP: int8(v.SubType),
- Cid: v.Cid,
- DT: int8(v.Device),
- Pro: int64(v.Progress),
- Unix: v.ViewAt,
- })
- }
- return his, nil
- }
- func (s *Service) servicePnPsCursor(c context.Context, mid int64, businesses []string, pn, ps int) ([]*model.History, []int64, error) {
- if pn*ps > 1000 {
- return nil, nil, nil
- }
- arg := &hisapi.UserHistoriesReq{
- Mid: mid,
- Businesses: businesses,
- Ps: int64(pn * ps),
- ViewAt: time.Now().Unix(),
- }
- reply, err := s.hisRPC.UserHistories(c, arg)
- if err != nil {
- log.Error("s.hisRPC.UserHistories(%+v) err:%+v", arg, err)
- return nil, nil, err
- }
- if reply == nil {
- return nil, nil, err
- }
- size := len(reply.Histories)
- start := (pn - 1) * ps
- end := start + ps - 1
- switch {
- case size > start && size > end:
- reply.Histories = reply.Histories[start : end+1]
- case size > start && size <= end:
- reply.Histories = reply.Histories[start:]
- default:
- reply.Histories = make([]*history.History, 0)
- }
- var epids []int64
- his := make([]*model.History, 0)
- for _, v := range reply.Histories {
- tp, _ := model.CheckBusiness(v.Business)
- if tp == model.TypePGC {
- epids = append(epids, v.Epid)
- }
- his = append(his, &model.History{
- Mid: v.Mid,
- Aid: v.Aid,
- Sid: v.Sid,
- Epid: v.Epid,
- TP: tp,
- Business: v.Business,
- STP: int8(v.SubType),
- Cid: v.Cid,
- DT: int8(v.Device),
- Pro: int64(v.Progress),
- Unix: v.ViewAt,
- })
- }
- return his, epids, nil
- }
- func (s *Service) servicePosition(c context.Context, mid int64, business string, kids []int64) (map[int64]*model.History, error) {
- arg := &hisapi.HistoriesReq{
- Mid: mid,
- Business: business,
- Kids: kids,
- }
- reply, err := s.hisRPC.Histories(c, arg)
- if err != nil {
- log.Error("s.hisRPC.Histories(%+v) err:%+v", arg, err)
- return nil, err
- }
- if reply == nil {
- return nil, err
- }
- now := time.Now().Unix() - 8*60*60
- his := make(map[int64]*model.History)
- for _, v := range reply.Histories {
- if business == model.BusinessByTP(model.TypeUGC) && v.ViewAt < now {
- continue
- }
- tp, _ := model.CheckBusiness(v.Business)
- his[v.Aid] = &model.History{
- Mid: v.Mid,
- Aid: v.Aid,
- Sid: v.Sid,
- Epid: v.Epid,
- TP: tp,
- Business: v.Business,
- STP: int8(v.SubType),
- Cid: v.Cid,
- DT: int8(v.Device),
- Pro: int64(v.Progress),
- Unix: v.ViewAt,
- }
- }
- return his, nil
- }
- func (s *Service) serviceHistoryType(c context.Context, mid int64, business string, kids []int64) ([]*model.History, error) {
- arg := &hisapi.HistoriesReq{
- Mid: mid,
- Business: business,
- Kids: kids,
- }
- reply, err := s.hisRPC.Histories(c, arg)
- if err != nil {
- log.Error("s.hisRPC.Histories(%+v) err:%+v", arg, err)
- return nil, err
- }
- if reply == nil {
- return nil, err
- }
- his := make([]*model.History, 0)
- for _, v := range reply.Histories {
- tp, _ := model.CheckBusiness(v.Business)
- his = append(his, &model.History{
- Mid: v.Mid,
- Aid: v.Aid,
- Sid: v.Sid,
- Epid: v.Epid,
- TP: tp,
- Business: v.Business,
- STP: int8(v.SubType),
- Cid: v.Cid,
- DT: int8(v.Device),
- Pro: int64(v.Progress),
- Unix: v.ViewAt,
- })
- }
- return his, nil
- }
- func (s *Service) serviceHideState(c context.Context, mid int64) (int64, error) {
- arg := &hisapi.UserHideReq{
- Mid: mid,
- }
- reply, err := s.hisRPC.UserHide(c, arg)
- if err != nil {
- log.Error("s.hisRPC.UserHide(%d) err:%+v", mid, err)
- return 0, err
- }
- if !reply.Hide {
- return 0, nil
- }
- return 1, nil
- }
|