api.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. package datadao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "time"
  8. "go-common/app/interface/main/mcn/dao/cache"
  9. "go-common/app/interface/main/mcn/dao/global"
  10. "go-common/app/interface/main/mcn/model/datamodel"
  11. "go-common/app/interface/main/mcn/model/mcnmodel"
  12. "go-common/app/interface/main/mcn/tool/datacenter"
  13. tagmdl "go-common/app/interface/main/tag/api"
  14. "go-common/library/log"
  15. "go-common/library/sync/errgroup"
  16. )
  17. // const url for api
  18. const (
  19. APIMcnSummary = "http://berserker.bilibili.co/avenger/api/155/query" // 7 see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690
  20. APIIndexInc = "http://berserker.bilibili.co/avenger/api/156/query" // 3.1
  21. APIIndexSource = "http://berserker.bilibili.co/avenger/api/159/query" // 3.2
  22. APIPlaySource = "http://berserker.bilibili.co/avenger/api/161/query" // 3.3
  23. APIMcnFans = "http://berserker.bilibili.co/avenger/api/168/query" // 3.4
  24. APIMcnFansInc = "http://berserker.bilibili.co/avenger/api/171/query" // 3.5
  25. APIMcnFansDec = "http://berserker.bilibili.co/avenger/api/169/query" // 3.6
  26. APIMcnFansAttentionWay = "http://berserker.bilibili.co/avenger/api/170/query" // 3.7
  27. APIMcnFansSex = "http://berserker.bilibili.co/avenger/api/162/query" // 3.8
  28. APIMcnFansAge = "http://berserker.bilibili.co/avenger/api/163/query" // 3.8
  29. APIMcnFansPlayWay = "http://berserker.bilibili.co/avenger/api/164/query" // 3.8
  30. APIMcnFansArea = "http://berserker.bilibili.co/avenger/api/165/query" // 3.9
  31. APIMcnFansType = "http://berserker.bilibili.co/avenger/api/166/query" // 3.10
  32. APIMcnFansTag = "http://berserker.bilibili.co/avenger/api/167/query" // 3.11
  33. )
  34. func (d *Dao) callDataAPI(c context.Context, api string, query *datacenter.Query, res interface{}) (err error) {
  35. var response = &datacenter.Response{
  36. Result: res,
  37. }
  38. if query.Error() != nil {
  39. err = query.Error()
  40. log.Error("query error, err=%s", err)
  41. return
  42. }
  43. var params = url.Values{}
  44. params.Add("query", query.String())
  45. if err = d.Client.Get(c, api, params, response); err != nil {
  46. log.Error("fail to get response, err=%+v", err)
  47. return
  48. }
  49. if response.Code != http.StatusOK {
  50. err = fmt.Errorf("code:%d, msg:%s", response.Code, response.Msg)
  51. return
  52. }
  53. return
  54. }
  55. // GetMcnSummary 7
  56. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-7.mcn获取概要数据
  57. func (d *Dao) GetMcnSummary(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error) {
  58. res = new(mcnmodel.McnGetDataSummaryReply)
  59. var tmp []*datamodel.DmConMcnArchiveD
  60. var q = &datacenter.Query{}
  61. q.Select("*").Where(
  62. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  63. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  64. ).Limit(1, 0).Order("log_date desc")
  65. var api = APIMcnSummary
  66. if err = d.callDataAPI(c, api, q, &tmp); err != nil {
  67. log.Error("call data api fail, api=%s, err=%s", api, err)
  68. return
  69. }
  70. if len(tmp) > 0 {
  71. res.CopyFromDmConMcnArchiveD(tmp[0])
  72. }
  73. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
  74. return
  75. }
  76. //GetMcnSummaryCache GetMcnSummary with cache
  77. func (d *Dao) GetMcnSummaryCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error) {
  78. res = new(mcnmodel.McnGetDataSummaryReply)
  79. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetDataSummaryReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  80. return d.GetMcnSummary(c, signID, date)
  81. })
  82. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  83. log.Error("cache get err, err=%v", err)
  84. return
  85. }
  86. return
  87. }
  88. // GetIndexInc 3.1
  89. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.1.查询MCN增量趋势
  90. func (d *Dao) GetIndexInc(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexIncReply, err error) {
  91. res = new(mcnmodel.McnGetIndexIncReply)
  92. var q = &datacenter.Query{}
  93. q.Select("*").Where(
  94. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  95. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  96. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  97. ).Limit(30, 0).Order("log_date desc")
  98. var api = APIIndexInc
  99. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  100. log.Error("call data api fail, api=%s, err=%s", api, err)
  101. return
  102. }
  103. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
  104. return
  105. }
  106. //GetIndexIncCache GetIndexInc with cache
  107. func (d *Dao) GetIndexIncCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexIncReply, err error) {
  108. res = new(mcnmodel.McnGetIndexIncReply)
  109. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetIndexIncReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  110. return d.GetIndexInc(c, signID, date, tp)
  111. })
  112. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  113. log.Error("cache get err, err=%v", err)
  114. return
  115. }
  116. return
  117. }
  118. // GetIndexSource 3.2
  119. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.2.查询MCN下播放稿件来源所在分区
  120. func (d *Dao) GetIndexSource(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexSourceReply, err error) {
  121. res = new(mcnmodel.McnGetIndexSourceReply)
  122. var q = &datacenter.Query{}
  123. q.Select("*").Where(
  124. datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
  125. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  126. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  127. )
  128. var api = APIIndexSource
  129. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  130. log.Error("call data api fail, api=%s, err=%s", api, err)
  131. return
  132. }
  133. var tids []int64
  134. for _, v := range res.Result {
  135. tids = append(tids, v.TypeID)
  136. }
  137. tpNames := cache.GetTidNames(tids)
  138. for _, v := range res.Result {
  139. if tpName, ok := tpNames[v.TypeID]; ok {
  140. v.TypeName = tpName
  141. }
  142. }
  143. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
  144. return
  145. }
  146. //GetIndexSourceCache GetIndexSource with cache
  147. func (d *Dao) GetIndexSourceCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexSourceReply, err error) {
  148. res = new(mcnmodel.McnGetIndexSourceReply)
  149. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetIndexSourceReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  150. return d.GetIndexSource(c, signID, date, tp)
  151. })
  152. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  153. log.Error("cache get err, err=%v", err)
  154. return
  155. }
  156. return
  157. }
  158. // GetPlaySource 3.3
  159. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.3.查询MCN播放设备占比
  160. func (d *Dao) GetPlaySource(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetPlaySourceReply, err error) {
  161. res = new(mcnmodel.McnGetPlaySourceReply)
  162. var q = &datacenter.Query{}
  163. q.Select("*").Where(
  164. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  165. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  166. ).Limit(1, 0).Order("log_date desc")
  167. var api = APIPlaySource
  168. var tmp []*mcnmodel.McnGetPlaySourceReply
  169. if err = d.callDataAPI(c, api, q, &tmp); err != nil {
  170. log.Error("call data api fail, api=%s, err=%s", api, err)
  171. return
  172. }
  173. if len(tmp) > 0 {
  174. res = tmp[0]
  175. }
  176. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp)
  177. return
  178. }
  179. //GetPlaySourceCache GetPlaySource with cache
  180. func (d *Dao) GetPlaySourceCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetPlaySourceReply, err error) {
  181. res = new(mcnmodel.McnGetPlaySourceReply)
  182. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetPlaySourceReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  183. return d.GetPlaySource(c, signID, date)
  184. })
  185. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  186. log.Error("cache get err, err=%v", err)
  187. return
  188. }
  189. return
  190. }
  191. // GetMcnFans 3.4
  192. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.4.查询MCN粉丝数与活跃度
  193. func (d *Dao) GetMcnFans(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansReply, err error) {
  194. res = new(mcnmodel.McnGetMcnFansReply)
  195. var q = &datacenter.Query{}
  196. q.Select("*").Where(
  197. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  198. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  199. ).Limit(1, 0).Order("log_date desc")
  200. var tmp []*mcnmodel.McnGetMcnFansReply
  201. var api = APIMcnFans
  202. if err = d.callDataAPI(c, api, q, &tmp); err != nil {
  203. log.Error("call data api fail, api=%s, err=%s", api, err)
  204. return
  205. }
  206. if len(tmp) > 0 {
  207. res = tmp[0]
  208. }
  209. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
  210. return
  211. }
  212. //GetMcnFansCache GetMcnFans with cache
  213. func (d *Dao) GetMcnFansCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansReply, err error) {
  214. res = new(mcnmodel.McnGetMcnFansReply)
  215. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  216. return d.GetMcnFans(c, signID, date)
  217. })
  218. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  219. log.Error("cache get err, err=%v", err)
  220. return
  221. }
  222. return
  223. }
  224. // GetMcnFansInc 3.5
  225. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.5.查询MCN粉丝按天增量
  226. func (d *Dao) GetMcnFansInc(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansIncReply, err error) {
  227. res = new(mcnmodel.McnGetMcnFansIncReply)
  228. var q = &datacenter.Query{}
  229. q.Select("*").Where(
  230. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  231. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  232. ).Limit(30, 0).Order("log_date desc")
  233. var api = APIMcnFansInc
  234. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  235. log.Error("call data api fail, api=%s, err=%s", api, err)
  236. return
  237. }
  238. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, res.Result[0])
  239. return
  240. }
  241. //GetMcnFansIncCache GetMcnFansInc with cache
  242. func (d *Dao) GetMcnFansIncCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansIncReply, err error) {
  243. res = new(mcnmodel.McnGetMcnFansIncReply)
  244. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansIncReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  245. return d.GetMcnFansInc(c, signID, date)
  246. })
  247. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  248. log.Error("cache get err, err=%v", err)
  249. return
  250. }
  251. return
  252. }
  253. // GetMcnFansDec 3.6
  254. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.6.查询MCN粉丝取关数按天
  255. func (d *Dao) GetMcnFansDec(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansDecReply, err error) {
  256. res = new(mcnmodel.McnGetMcnFansDecReply)
  257. var q = &datacenter.Query{}
  258. q.Select("*").Where(
  259. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  260. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  261. ).Limit(30, 0).Order("log_date desc")
  262. var api = APIMcnFansDec
  263. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  264. log.Error("call data api fail, api=%s, err=%s", api, err)
  265. return
  266. }
  267. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, res.Result[0])
  268. return
  269. }
  270. //GetMcnFansDecCache GetMcnFansDec with cache
  271. func (d *Dao) GetMcnFansDecCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansDecReply, err error) {
  272. res = new(mcnmodel.McnGetMcnFansDecReply)
  273. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansDecReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  274. return d.GetMcnFansDec(c, signID, date)
  275. })
  276. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  277. log.Error("cache get err, err=%v", err)
  278. return
  279. }
  280. return
  281. }
  282. // GetMcnFansAttentionWay 3.7
  283. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.7.查询MCN粉丝关注渠道
  284. func (d *Dao) GetMcnFansAttentionWay(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansAttentionWayReply, err error) {
  285. res = new(mcnmodel.McnGetMcnFansAttentionWayReply)
  286. var q = &datacenter.Query{}
  287. q.Select("*").Where(
  288. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  289. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  290. ).Limit(1, 0).Order("log_date desc")
  291. var tmp []*mcnmodel.McnGetMcnFansAttentionWayReply
  292. var api = APIMcnFansAttentionWay
  293. if err = d.callDataAPI(c, api, q, &tmp); err != nil {
  294. log.Error("call data api fail, api=%s, err=%s", api, err)
  295. return
  296. }
  297. if len(tmp) > 0 {
  298. res = tmp[0]
  299. }
  300. //log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
  301. return
  302. }
  303. //GetMcnFansAttentionWayCache GetMcnFansAttentionWay with cache
  304. func (d *Dao) GetMcnFansAttentionWayCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansAttentionWayReply, err error) {
  305. res = new(mcnmodel.McnGetMcnFansAttentionWayReply)
  306. var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansAttentionWayReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
  307. return d.GetMcnFansAttentionWay(c, signID, date)
  308. })
  309. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  310. log.Error("cache get err, err=%v", err)
  311. return
  312. }
  313. return
  314. }
  315. // GetFansBaseFansAttr 3.8
  316. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.8.查询MCN粉丝/游客基本属性分析(性别占比 观众年龄 观看途径)
  317. func (d *Dao) GetFansBaseFansAttr(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetBaseFansAttrReply, err error) {
  318. res = new(mcnmodel.McnGetBaseFansAttrReply)
  319. var group, _ = errgroup.WithContext(c)
  320. group.Go(func() (err error) {
  321. var q = &datacenter.Query{}
  322. q.Select("*").Where(
  323. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  324. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  325. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  326. ).Limit(1, 0).Order("log_date desc")
  327. var api = APIMcnFansSex
  328. var tmp []*datamodel.DmConMcnFansSexW
  329. if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
  330. log.Error("call data api fail, api=%s, err=%s", api, err)
  331. return
  332. }
  333. if len(tmp) > 0 {
  334. res.FansSex = tmp[0]
  335. }
  336. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
  337. return
  338. })
  339. group.Go(func() (err error) {
  340. var q = &datacenter.Query{}
  341. q.Select("*").Where(
  342. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  343. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  344. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  345. ).Limit(1, 0).Order("log_date desc")
  346. var tmp []*datamodel.DmConMcnFansAgeW
  347. var api = APIMcnFansAge
  348. if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
  349. log.Error("call data api fail, api=%s, err=%s", api, err)
  350. return
  351. }
  352. if len(tmp) > 0 {
  353. res.FansAge = tmp[0]
  354. }
  355. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
  356. return
  357. })
  358. group.Go(func() (err error) {
  359. var q = &datacenter.Query{}
  360. q.Select("*").Where(
  361. datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
  362. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  363. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  364. ).Limit(1, 0).Order("log_date desc")
  365. var tmp []*datamodel.DmConMcnFansPlayWayW
  366. var api = APIMcnFansPlayWay
  367. if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
  368. log.Error("call data api fail, api=%s, err=%s", api, err)
  369. return
  370. }
  371. if len(tmp) > 0 {
  372. res.FansPlayWay = tmp[0]
  373. }
  374. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
  375. return
  376. })
  377. err = group.Wait()
  378. if err != nil {
  379. log.Error("fail to get data, err=%v", err)
  380. return
  381. }
  382. return
  383. }
  384. //GetFansBaseFansAttrCache GetFansBaseFansAttr with cache
  385. func (d *Dao) GetFansBaseFansAttrCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetBaseFansAttrReply, err error) {
  386. res = new(mcnmodel.McnGetBaseFansAttrReply)
  387. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetBaseFansAttrReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  388. return d.GetFansBaseFansAttr(c, signID, date, tp)
  389. })
  390. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  391. log.Error("cache get err, err=%v", err)
  392. return
  393. }
  394. return
  395. }
  396. // GetFansArea 3.9
  397. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.9.查询MCN粉丝/游客地区分布分析
  398. func (d *Dao) GetFansArea(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansAreaReply, err error) {
  399. res = new(mcnmodel.McnGetFansAreaReply)
  400. var q = &datacenter.Query{}
  401. q.Select("*").Where(
  402. datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
  403. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  404. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  405. )
  406. var api = APIMcnFansArea
  407. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  408. log.Error("call data api fail, api=%s, err=%s", api, err)
  409. return
  410. }
  411. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
  412. return
  413. }
  414. //GetFansAreaCache GetFansArea with cache
  415. func (d *Dao) GetFansAreaCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansAreaReply, err error) {
  416. res = new(mcnmodel.McnGetFansAreaReply)
  417. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansAreaReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  418. return d.GetFansArea(c, signID, date, tp)
  419. })
  420. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  421. log.Error("cache get err, err=%v", err)
  422. return
  423. }
  424. return
  425. }
  426. // GetFansType 3.10
  427. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.10.查询MCN粉丝/游客内容倾向分析
  428. func (d *Dao) GetFansType(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTypeReply, err error) {
  429. res = new(mcnmodel.McnGetFansTypeReply)
  430. var q = &datacenter.Query{}
  431. q.Select("*").Where(
  432. datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
  433. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  434. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  435. )
  436. var api = APIMcnFansType
  437. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  438. log.Error("call data api fail, api=%s, err=%s", api, err)
  439. return
  440. }
  441. var tids []int64
  442. for _, v := range res.Result {
  443. tids = append(tids, v.TypeID)
  444. }
  445. tpNames := cache.GetTidNames(tids)
  446. for _, v := range res.Result {
  447. if tpName, ok := tpNames[v.TypeID]; ok {
  448. v.TypeName = tpName
  449. }
  450. }
  451. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
  452. return
  453. }
  454. //GetFansTypeCache GetFansType with cache
  455. func (d *Dao) GetFansTypeCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTypeReply, err error) {
  456. res = new(mcnmodel.McnGetFansTypeReply)
  457. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansTypeReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  458. return d.GetFansType(c, signID, date, tp)
  459. })
  460. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  461. log.Error("cache get err, err=%v", err)
  462. return
  463. }
  464. return
  465. }
  466. // GetFansTag 3.11
  467. // see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.11.查询MCN粉丝/游客标签地图分析
  468. func (d *Dao) GetFansTag(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTagReply, err error) {
  469. res = new(mcnmodel.McnGetFansTagReply)
  470. var q = &datacenter.Query{}
  471. q.Select("*").Where(
  472. datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
  473. datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
  474. datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
  475. )
  476. var api = APIMcnFansTag
  477. if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
  478. log.Error("call data api fail, api=%s, err=%s", api, err)
  479. return
  480. }
  481. var tagIDs []int64
  482. for _, v := range res.Result {
  483. tagIDs = append(tagIDs, v.TagID)
  484. }
  485. var tagsReply *tagmdl.TagsReply
  486. if tagsReply, err = global.GetTagGRPC().Tags(c, &tagmdl.TagsReq{Tids: tagIDs}); err != nil {
  487. log.Error("tag(%+v) grpc client fail, err=%s", tagIDs, err)
  488. err = nil
  489. }
  490. for _, v := range res.Result {
  491. if tagsReply == nil {
  492. continue
  493. }
  494. if tag, ok := tagsReply.Tags[v.TagID]; ok {
  495. v.TagName = tag.Name
  496. }
  497. }
  498. //log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
  499. return
  500. }
  501. //GetFansTagCache GetFansTag with cache
  502. func (d *Dao) GetFansTagCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTagReply, err error) {
  503. res = new(mcnmodel.McnGetFansTagReply)
  504. var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansTagReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
  505. return d.GetFansTag(c, signID, date, tp)
  506. })
  507. if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
  508. log.Error("cache get err, err=%v", err)
  509. return
  510. }
  511. return
  512. }