order.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package archive
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/model/archive"
  6. "go-common/app/interface/main/creative/model/game"
  7. "go-common/app/interface/main/creative/model/order"
  8. "go-common/app/service/main/archive/api"
  9. arcMdl "go-common/app/service/main/archive/model/archive"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. // ExecuteOrders fn
  15. func (s *Service) ExecuteOrders(c context.Context, mid int64, ip string) (orders []*order.Order, err error) {
  16. if orders, err = s.order.ExecuteOrders(c, mid, ip); err != nil {
  17. log.Error("s.order.ExecuteOrders mid(%d) err(%v)", mid, err)
  18. return
  19. }
  20. return
  21. }
  22. // Oasis for orders.
  23. func (s *Service) Oasis(c context.Context, mid int64, ip string) (oa *order.Oasis, err error) {
  24. if oa, err = s.order.Oasis(c, mid, ip); err != nil {
  25. log.Error("s.order.Oasis mid(%d) err(%v)", mid, err)
  26. return
  27. }
  28. return
  29. }
  30. // ArcOrderGameInfo fn
  31. // platform 1:android;2:ios
  32. func (s *Service) ArcOrderGameInfo(c context.Context, aid int64, platform int, ip string) (gameInfo *game.Info, err error) {
  33. var (
  34. orderID, gameBaseID int64
  35. beginDate xtime.Time
  36. )
  37. if orderID, _, gameBaseID, err = s.order.OrderByAid(c, aid); err != nil {
  38. log.Error("s.order.OrderByAid aid(%d)|ip(%s)|err(%+v)", aid, ip, err)
  39. err = ecode.NothingFound
  40. return
  41. }
  42. if gameBaseID == 0 || orderID == 0 {
  43. log.Error("s.order.OrderByAid aid(%d)|ip(%s) not found", aid, ip)
  44. err = ecode.NothingFound
  45. return
  46. }
  47. if gameInfo, err = s.game.Info(c, gameBaseID, platform, ip); err != nil {
  48. log.Error("s.game.Info aid(%d)|orderID(%d)|gameBaseID(%d)|ip(%s)|err(%+v)", aid, orderID, gameBaseID, ip, err)
  49. err = ecode.NothingFound
  50. return
  51. }
  52. if !gameInfo.IsOnline {
  53. log.Error("s.game.Info IsOnline is false aid(%d)|orderID(%d)|gameBaseID(%d)|ip(%s)|err(%+v)", aid, orderID, gameBaseID, ip, err)
  54. err = ecode.NothingFound
  55. return
  56. }
  57. if beginDate, err = s.order.LaunchTime(c, orderID, ip); err != nil {
  58. log.Error("s.order.LaunchTime aid(%d)|orderID(%d)|gameBaseID(%d)|ip(%s)|err(%+v)", aid, orderID, gameBaseID, ip, err)
  59. return
  60. }
  61. gameInfo.BeginDate = beginDate
  62. gameInfo.BaseID = gameBaseID
  63. return
  64. }
  65. // ArcCommercial fn
  66. func (s *Service) ArcCommercial(c context.Context, aid int64, ip string) (cm *archive.Commercial, err error) {
  67. var (
  68. orderID, gameBaseID int64
  69. beginDate xtime.Time
  70. a *api.Arc
  71. cache = true
  72. )
  73. // try cache
  74. if cm, err = s.arc.ArcCMCache(c, aid); err != nil {
  75. err = nil
  76. cache = false
  77. } else if cm != nil {
  78. s.pCacheHit.Incr("cmarc_cache")
  79. cache = false
  80. return
  81. }
  82. s.pCacheMiss.Incr("cmarc_cache")
  83. // get archive
  84. a, err = s.arc.Archive(c, aid, ip)
  85. if err != nil {
  86. log.Error("arcCommercial aid(%d)|ip(%s)|err(%+v)", aid, ip, err)
  87. err = ecode.NothingFound
  88. return
  89. }
  90. if a == nil {
  91. log.Error("arcCommercial nil aid(%d)|ip(%s)|err(%+v)", aid, ip, err)
  92. err = ecode.NothingFound
  93. return
  94. }
  95. // add cache
  96. defer func() {
  97. if cache {
  98. s.addCache(func() {
  99. if cm == nil {
  100. cm = &archive.Commercial{}
  101. }
  102. s.arc.AddArcCMCache(context.Background(), aid, cm)
  103. })
  104. }
  105. }()
  106. // check order or porder
  107. if a.OrderID > 0 {
  108. // order
  109. if orderID, _, gameBaseID, err = s.order.OrderByAid(c, aid); err != nil {
  110. log.Error("arcCommercial aid(%d)|ip(%s)|error(%+v)", aid, ip, err)
  111. err = ecode.NothingFound
  112. return
  113. }
  114. if gameBaseID == 0 || orderID == 0 {
  115. log.Error("arcCommercial aid(%d)|ip(%s) not found", aid, ip)
  116. err = ecode.NothingFound
  117. return
  118. }
  119. if beginDate, err = s.order.LaunchTime(c, orderID, ip); err != nil {
  120. log.Error("arcCommercial get launch time failed. aid(%d)|orderID(%d)|gameBaseID(%d)|ip(%s)|error(%+v)", aid, orderID, gameBaseID, ip, err)
  121. return
  122. }
  123. // check time
  124. if time.Now().Unix() < beginDate.Time().Unix() {
  125. log.Error("arcCommercial launch time invalid. aid(%d)|orderID(%d)|gameBaseID(%d) beginDate(%+v)", aid, orderID, gameBaseID, beginDate)
  126. err = ecode.NothingFound
  127. return
  128. }
  129. cm = &archive.Commercial{}
  130. cm.AID = a.Aid
  131. cm.OrderID = orderID
  132. cm.GameID = gameBaseID
  133. } else if a.AttrVal(arcMdl.AttrBitIsPorder) == arcMdl.AttrYes {
  134. // porder
  135. var pd *archive.Porder
  136. if pd, err = s.arc.Porder(c, aid); err != nil {
  137. log.Error("arcCommercial aid(%d) error(%v)", aid, err)
  138. err = ecode.NothingFound
  139. return
  140. }
  141. if pd == nil {
  142. log.Error("arcCommercial porder not show aid(%d)", aid)
  143. err = ecode.CreativePorderForbidShowFront
  144. return
  145. }
  146. cm = &archive.Commercial{}
  147. cm.AID = a.Aid
  148. cm.POrderID = pd.ID
  149. cm.GameID = pd.BrandID
  150. } else {
  151. log.Error("arcCommercial is not commercial. aid(%d)|ip(%s)|", aid, ip)
  152. err = ecode.NothingFound
  153. return
  154. }
  155. return
  156. }
  157. // UpValidate func
  158. func (s *Service) UpValidate(c context.Context, mid int64, ip string) (uv *order.UpValidate, err error) {
  159. if uv, err = s.order.UpValidate(c, mid, ip); err != nil {
  160. log.Error("s.order.UpValidate mid(%d)|ip(%s)|err(%v)", mid, ip, err)
  161. }
  162. return
  163. }