promote.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package operation
  2. import (
  3. "context"
  4. "regexp"
  5. "strconv"
  6. opmdl "go-common/app/interface/main/web-show/model/operation"
  7. "go-common/app/service/main/archive/api"
  8. comarcmdl "go-common/app/service/main/archive/model/archive"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. var (
  13. _emptyPromoteMap = make(map[string][]*opmdl.Promote)
  14. _avReg = regexp.MustCompile(`video\/av[0-9]+`)
  15. )
  16. // Promote Service
  17. func (s *Service) Promote(c context.Context, arg *opmdl.ArgPromote) (res map[string][]*opmdl.Promote, err error) {
  18. var (
  19. ok bool
  20. arcs map[int64]*api.Arc
  21. arc *api.Arc
  22. aid int64
  23. aids []int64
  24. )
  25. opMap := s.operation(arg.Tp, arg.Rank, arg.Count)
  26. for _, ops := range opMap {
  27. for _, op := range ops {
  28. if aid, err = s.regAid(op.Link); err != nil {
  29. log.Error("service.regAid error(%v)", err)
  30. continue
  31. }
  32. op.Aid = aid
  33. aids = append(aids, aid)
  34. }
  35. }
  36. argAids := &comarcmdl.ArgAids2{
  37. Aids: aids,
  38. }
  39. if arcs, err = s.arcRPC.Archives3(c, argAids); err != nil {
  40. log.Error("s.arcRPC.Archives2(arcAids:(%v), arcs), err(%v)", aids, err)
  41. res = _emptyPromoteMap
  42. return
  43. }
  44. res = make(map[string][]*opmdl.Promote)
  45. for rk, ops := range opMap {
  46. promotes := make([]*opmdl.Promote, 0, len(ops))
  47. for _, op := range ops {
  48. if arc, ok = arcs[op.Aid]; !ok {
  49. continue
  50. }
  51. promote := &opmdl.Promote{
  52. IsAd: int8(op.Ads),
  53. Archive: arc,
  54. }
  55. promotes = append(promotes, promote)
  56. }
  57. res[rk] = promotes
  58. }
  59. return
  60. }
  61. // regAid Service
  62. func (s *Service) regAid(link string) (aid int64, err error) {
  63. avStr := _avReg.FindString(link)
  64. if avStr != "" {
  65. aidStr := avStr[8:]
  66. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
  67. log.Error("strconv.ParseInt error(%v)", err)
  68. return
  69. }
  70. } else {
  71. err = ecode.ArchiveNotExist
  72. }
  73. return
  74. }