service.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package view
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/app-view/conf"
  7. accdao "go-common/app/interface/main/app-view/dao/account"
  8. actdao "go-common/app/interface/main/app-view/dao/act"
  9. addao "go-common/app/interface/main/app-view/dao/ad"
  10. aidao "go-common/app/interface/main/app-view/dao/ai"
  11. arcdao "go-common/app/interface/main/app-view/dao/archive"
  12. assdao "go-common/app/interface/main/app-view/dao/assist"
  13. audiodao "go-common/app/interface/main/app-view/dao/audio"
  14. bandao "go-common/app/interface/main/app-view/dao/bangumi"
  15. coindao "go-common/app/interface/main/app-view/dao/coin"
  16. creativedao "go-common/app/interface/main/app-view/dao/creative"
  17. dmdao "go-common/app/interface/main/app-view/dao/dm"
  18. elcdao "go-common/app/interface/main/app-view/dao/elec"
  19. favdao "go-common/app/interface/main/app-view/dao/favorite"
  20. gamedao "go-common/app/interface/main/app-view/dao/game"
  21. livedao "go-common/app/interface/main/app-view/dao/live"
  22. locdao "go-common/app/interface/main/app-view/dao/location"
  23. managerdao "go-common/app/interface/main/app-view/dao/manager"
  24. rgndao "go-common/app/interface/main/app-view/dao/region"
  25. reldao "go-common/app/interface/main/app-view/dao/relation"
  26. rscdao "go-common/app/interface/main/app-view/dao/resource"
  27. searchdao "go-common/app/interface/main/app-view/dao/search"
  28. spdao "go-common/app/interface/main/app-view/dao/special"
  29. tagdao "go-common/app/interface/main/app-view/dao/tag"
  30. thumbupdao "go-common/app/interface/main/app-view/dao/thumbup"
  31. ugcpaydao "go-common/app/interface/main/app-view/dao/ugcpay"
  32. vipdao "go-common/app/interface/main/app-view/dao/vip"
  33. "go-common/app/interface/main/app-view/model"
  34. elecmdl "go-common/app/interface/main/app-view/model/elec"
  35. "go-common/app/interface/main/app-view/model/live"
  36. "go-common/app/interface/main/app-view/model/manager"
  37. "go-common/app/interface/main/app-view/model/region"
  38. "go-common/app/interface/main/app-view/model/special"
  39. "go-common/app/interface/main/app-view/model/view"
  40. "go-common/app/service/main/archive/model/archive"
  41. shareclient "go-common/app/service/main/share/api"
  42. "go-common/library/conf/env"
  43. "go-common/library/log"
  44. "go-common/library/stat/prom"
  45. )
  46. var (
  47. _elecTypeIds = []int16{
  48. 20, 154, 156, // dance
  49. 31, 30, 59, 29, 28, // music
  50. 26, 22, 126, 127, // guichu
  51. 24, 25, 47, 27, // animae
  52. 17, 18, 16, 65, 136, 19, 121, 171, 172, 173, // game
  53. 37, 124, 122, 39, 96, 95, 98, // tech
  54. 71, 137, 131, // yule
  55. 157, 158, 159, 164, // fashion
  56. 82, 128, // movie and tv
  57. 138, 21, 75, 76, 161, 162, 163, 174, // life
  58. 153, 168, // guo man
  59. 85, 86, 182, 183, 184, // film and television
  60. }
  61. )
  62. // Service is view service
  63. type Service struct {
  64. c *conf.Config
  65. pHit *prom.Prom
  66. pMiss *prom.Prom
  67. prom *prom.Prom
  68. // dao
  69. accDao *accdao.Dao
  70. arcDao *arcdao.Dao
  71. tagDao *tagdao.Dao
  72. favDao *favdao.Dao
  73. banDao *bandao.Dao
  74. elcDao *elcdao.Dao
  75. rgnDao *rgndao.Dao
  76. liveDao *livedao.Dao
  77. assDao *assdao.Dao
  78. adDao *addao.Dao
  79. rscDao *rscdao.Dao
  80. relDao *reldao.Dao
  81. coinDao *coindao.Dao
  82. audioDao *audiodao.Dao
  83. actDao *actdao.Dao
  84. thumbupDao *thumbupdao.Dao
  85. gameDao *gamedao.Dao
  86. shareClient shareclient.ShareClient
  87. vipDao *vipdao.Dao
  88. mngDao *managerdao.Dao
  89. spDao *spdao.Dao
  90. dmDao *dmdao.Dao
  91. aiDao *aidao.Dao
  92. creativeDao *creativedao.Dao
  93. search *searchdao.Dao
  94. ugcpayDao *ugcpaydao.Dao
  95. locDao *locdao.Dao
  96. // region
  97. tick time.Duration
  98. region map[int8]map[int]*region.Region
  99. // elec
  100. allowTypeIds map[int16]struct{}
  101. // live cache
  102. liveCache map[int64]*live.Live
  103. // chan
  104. inCh chan interface{}
  105. dmRegion map[int16]struct{}
  106. // vip active cache
  107. vipActiveCache map[int]string
  108. vipTick time.Duration
  109. // mamager cache
  110. RelateCache []*manager.Relate
  111. specialCache map[int64]*special.Card
  112. specialMids map[int64]struct{}
  113. // player icon
  114. playerIcon *view.PlayerIcon
  115. // view relate game from AI
  116. RelateGameCache map[int64]int64
  117. // bnj caches
  118. BnjMainView *archive.View3
  119. BnjLists []*archive.View3
  120. BnjElecInfo *elecmdl.Info
  121. BnjWhiteMid map[int64]struct{}
  122. BnjIsGrey bool
  123. }
  124. // New new archive
  125. func New(c *conf.Config) (s *Service) {
  126. s = &Service{
  127. c: c,
  128. pHit: prom.CacheHit,
  129. pMiss: prom.CacheMiss,
  130. prom: prom.BusinessInfoCount,
  131. // dao
  132. accDao: accdao.New(c),
  133. arcDao: arcdao.New(c),
  134. tagDao: tagdao.New(c),
  135. favDao: favdao.New(c),
  136. banDao: bandao.New(c),
  137. elcDao: elcdao.New(c),
  138. rgnDao: rgndao.New(c),
  139. liveDao: livedao.New(c),
  140. assDao: assdao.New(c),
  141. adDao: addao.New(c),
  142. rscDao: rscdao.New(c),
  143. relDao: reldao.New(c),
  144. coinDao: coindao.New(c),
  145. audioDao: audiodao.New(c),
  146. actDao: actdao.New(c),
  147. thumbupDao: thumbupdao.New(c),
  148. gameDao: gamedao.New(c),
  149. vipDao: vipdao.New(c),
  150. mngDao: managerdao.New(c),
  151. spDao: spdao.New(c),
  152. dmDao: dmdao.New(c),
  153. aiDao: aidao.New(c),
  154. creativeDao: creativedao.New(c),
  155. search: searchdao.New(c),
  156. ugcpayDao: ugcpaydao.New(c),
  157. locDao: locdao.New(c),
  158. // region
  159. tick: time.Duration(c.Tick),
  160. region: map[int8]map[int]*region.Region{},
  161. // live cache
  162. liveCache: map[int64]*live.Live{},
  163. // chan
  164. inCh: make(chan interface{}, 1024),
  165. allowTypeIds: map[int16]struct{}{},
  166. dmRegion: map[int16]struct{}{},
  167. specialMids: map[int64]struct{}{},
  168. // vip
  169. vipActiveCache: make(map[int]string),
  170. vipTick: time.Duration(c.VipTick),
  171. // manager
  172. RelateCache: []*manager.Relate{},
  173. specialCache: map[int64]*special.Card{},
  174. // player icon
  175. playerIcon: &view.PlayerIcon{},
  176. }
  177. for _, id := range _elecTypeIds {
  178. s.allowTypeIds[id] = struct{}{}
  179. }
  180. for _, id := range c.DMRegion {
  181. s.dmRegion[id] = struct{}{}
  182. }
  183. var err error
  184. if s.shareClient, err = shareclient.NewClient(nil); err != nil {
  185. panic(fmt.Sprintf("env:%s no share-service", env.DeployEnv))
  186. }
  187. // load data
  188. s.loadLive()
  189. s.loadRegion()
  190. s.loadPlayerIcon()
  191. s.loadVIPActive()
  192. s.loadManager()
  193. s.loadRelateGame()
  194. s.loadBnj2019Infos()
  195. go s.infocproc()
  196. go s.tickproc()
  197. go s.vipproc()
  198. go s.bnjTickproc()
  199. return s
  200. }
  201. // Ping is dao ping.
  202. func (s *Service) Ping(c context.Context) (err error) {
  203. return s.arcDao.Ping(c)
  204. }
  205. func (s *Service) bnjTickproc() {
  206. for {
  207. time.Sleep(time.Duration(s.c.Bnj2019.Tick))
  208. err := s.loadBnj2019Infos()
  209. if err != nil {
  210. log.Error("bnj load error(%v)", err)
  211. }
  212. }
  213. }
  214. // tickproc tick load cache.
  215. func (s *Service) tickproc() {
  216. for {
  217. time.Sleep(s.tick)
  218. s.loadRegion()
  219. s.loadLive()
  220. s.loadPlayerIcon()
  221. s.loadManager()
  222. s.loadRelateGame()
  223. }
  224. }
  225. // vipproc tick load vip cache.
  226. func (s *Service) vipproc() {
  227. for {
  228. time.Sleep(s.vipTick)
  229. s.loadVIPActive()
  230. }
  231. }
  232. // loadVIPActive tick load vip active cache.
  233. func (s *Service) loadVIPActive() {
  234. var (
  235. va map[int]string
  236. err error
  237. )
  238. va = make(map[int]string)
  239. if va[view.VIPActiveView], err = s.vipDao.VIPActive(context.TODO(), view.VIPActiveView); err != nil {
  240. log.Error("s.vipDao.VIPActinve(%d) error(%v)", view.VIPActiveView, err)
  241. return
  242. }
  243. s.vipActiveCache = va
  244. log.Info("load vip active success")
  245. }
  246. func (s *Service) loadRegion() {
  247. res, err := s.rgnDao.Seconds(context.TODO())
  248. if err != nil {
  249. log.Error("%+v", err)
  250. return
  251. }
  252. s.region = res
  253. }
  254. func (s *Service) loadLive() {
  255. if s.c.Env == model.EnvHK {
  256. return
  257. }
  258. living, err := s.liveDao.Living(context.TODO())
  259. if err != nil {
  260. log.Error("%+v", err)
  261. return
  262. }
  263. tmp := map[int64]*live.Live{}
  264. for _, v := range living {
  265. tmp[v.Mid] = v
  266. }
  267. s.liveCache = tmp
  268. }
  269. func (s *Service) loadManager() {
  270. r, err := s.mngDao.Relate(context.TODO())
  271. if err != nil {
  272. log.Error("%+v", err)
  273. return
  274. }
  275. s.RelateCache = r
  276. sp, err := s.spDao.Card(context.TODO())
  277. if err != nil {
  278. log.Error("%+v", err)
  279. return
  280. }
  281. s.specialCache = sp
  282. midsM, err := s.creativeDao.Special(context.Background())
  283. if err != nil {
  284. log.Error("%+v", err)
  285. return
  286. }
  287. log.Info("load special mids(%+v)", midsM)
  288. s.specialMids = midsM
  289. }
  290. func (s *Service) loadRelateGame() {
  291. g, err := s.aiDao.Av2Game(context.TODO())
  292. if err != nil {
  293. log.Error("%+v", err)
  294. return
  295. }
  296. s.RelateGameCache = g
  297. }
  298. func (s *Service) loadPlayerIcon() {
  299. res, err := s.rscDao.PlayerIcon(context.TODO())
  300. if err != nil {
  301. log.Error("%+v", err)
  302. return
  303. }
  304. if res != nil {
  305. s.playerIcon = &view.PlayerIcon{URL1: res.URL1, Hash1: res.Hash1, URL2: res.URL2, Hash2: res.Hash2, CTime: res.CTime}
  306. } else {
  307. s.playerIcon = nil
  308. }
  309. }
  310. func (s *Service) relateCache(c context.Context, plat int8, build int, now time.Time, aid int64, tids []int64, rid int32) (relate *manager.Relate) {
  311. rs := s.RelateCache
  312. rls := make([]*manager.Relate, 0, len(rs))
  313. if len(rs) != 0 {
  314. LOOP:
  315. for _, r := range rs {
  316. if vs, ok := r.Versions[plat]; ok {
  317. for _, v := range vs {
  318. if model.InvalidBuild(build, v.Build, v.Condition) {
  319. continue LOOP
  320. }
  321. }
  322. if (r.STime == 0 || now.After(r.STime.Time())) && (r.ETime == 0 || now.Before(r.ETime.Time())) {
  323. rls = append(rls, r)
  324. }
  325. }
  326. }
  327. }
  328. for _, r := range rls {
  329. if _, ok := r.Aids[aid]; ok {
  330. relate = r
  331. break
  332. }
  333. if len(tids) != 0 {
  334. for _, tid := range tids {
  335. if _, ok := r.Tids[tid]; ok {
  336. relate = r
  337. break
  338. }
  339. }
  340. }
  341. if _, ok := r.Rids[int64(rid)]; ok {
  342. relate = r
  343. break
  344. }
  345. }
  346. return
  347. }
  348. func (s Service) relateGame(c context.Context, aid int64) (id int64) {
  349. id = s.RelateGameCache[aid]
  350. return
  351. }