statistics.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package up
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/admin/main/mcn/model"
  7. dtmdl "go-common/app/interface/main/mcn/model/datamodel"
  8. ifmdl "go-common/app/interface/main/mcn/model/mcnmodel"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/ecode"
  11. "go-common/library/net/metadata"
  12. xtime "go-common/library/time"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _mcnDataOverviewSQL = `SELECT mcns, sign_ups, sign_ups_incr, fans_50, fans_10, fans_1, fans_incr_50, fans_incr_10, fans_incr_1 FROM mcn_data_overview WHERE generate_date = ?`
  17. _mcnRankFansOverviewSQL = `SELECT id, sign_id, mid, data_view, data_type, rank, fans_incr, fans FROM mcn_rank_fans_overview WHERE data_view IN (1,2,3,4) AND data_type = ? AND generate_date = ? ORDER BY rank ASC limit ?`
  18. _mcnRankArchiveLikesOverviewSQL = `SELECT id, mcn_mid, up_mid, sign_id, avid, tid, rank, data_type, likes, plays FROM mcn_rank_archive_likes_overview WHERE data_type = ? AND generate_date = ? ORDER BY rank ASC limit ?`
  19. _mcnDataTypeSummarySQL = `SELECT id, tid, data_view, data_type, amount FROM mcn_data_type_summary WHERE data_view IN (1,2,3,4) AND data_type IN (1,2) AND generate_date = ?`
  20. _arcTopURL = "/x/internal/mcn/rank/archive_likes"
  21. _dataFansURL = "/x/internal/mcn/data/fans"
  22. _dataFansBaseAttrURL = "/x/internal/mcn/data/fans/base/attr"
  23. _dataFansAreaURL = "/x/internal/mcn/data/fans/area"
  24. _dataFansTypeURL = "/x/internal/mcn/data/fans/type"
  25. _dataFansTagURL = "/x/internal/mcn/data/fans/tag"
  26. )
  27. // McnDataOverview .
  28. func (d *Dao) McnDataOverview(c context.Context, date xtime.Time) (m *model.McnDataOverview, err error) {
  29. row := d.db.QueryRow(c, _mcnDataOverviewSQL, date)
  30. m = new(model.McnDataOverview)
  31. if err = row.Scan(&m.Mcns, &m.SignUps, &m.SignUpsIncr, &m.Fans50, &m.Fans10, &m.Fans1, &m.FansIncr50, &m.FansIncr10, &m.FansIncr1); err != nil {
  32. if err == xsql.ErrNoRows {
  33. err = nil
  34. m = nil
  35. return
  36. }
  37. }
  38. return
  39. }
  40. // McnRankFansOverview .
  41. func (d *Dao) McnRankFansOverview(c context.Context, dataType model.DataType, date xtime.Time, topLen int) (mrf map[int8][]*model.McnRankFansOverview, mids []int64, err error) {
  42. rows, err := d.db.Query(c, _mcnRankFansOverviewSQL, dataType, date, topLen*4)
  43. if err != nil {
  44. return
  45. }
  46. defer rows.Close()
  47. mrf = make(map[int8][]*model.McnRankFansOverview, topLen*4)
  48. for rows.Next() {
  49. rf := new(model.McnRankFansOverview)
  50. err = rows.Scan(&rf.ID, &rf.SignID, &rf.Mid, &rf.DataView, &rf.DataType, &rf.Rank, &rf.FansIncr, &rf.Fans)
  51. if err != nil {
  52. if err == xsql.ErrNoRows {
  53. err = nil
  54. }
  55. return
  56. }
  57. mids = append(mids, rf.Mid)
  58. mrf[rf.DataView] = append(mrf[rf.DataView], rf)
  59. }
  60. err = rows.Err()
  61. return
  62. }
  63. // McnRankArchiveLikesOverview .
  64. func (d *Dao) McnRankArchiveLikesOverview(c context.Context, dataType model.DataType, date xtime.Time, topLen int) (ras []*model.McnRankArchiveLikesOverview, mids, avids, tids []int64, err error) {
  65. rows, err := d.db.Query(c, _mcnRankArchiveLikesOverviewSQL, dataType, date, topLen)
  66. if err != nil {
  67. return
  68. }
  69. defer rows.Close()
  70. for rows.Next() {
  71. ra := new(model.McnRankArchiveLikesOverview)
  72. err = rows.Scan(&ra.ID, &ra.McnMid, &ra.UpMid, &ra.SignID, &ra.Avid, &ra.Tid, &ra.Rank, &ra.DataType, &ra.Likes, &ra.Plays)
  73. if err != nil {
  74. if err == xsql.ErrNoRows {
  75. err = nil
  76. }
  77. return
  78. }
  79. ras = append(ras, ra)
  80. mids = append(mids, ra.McnMid)
  81. mids = append(mids, ra.UpMid)
  82. avids = append(avids, ra.Avid)
  83. tids = append(tids, int64(ra.Tid))
  84. }
  85. err = rows.Err()
  86. return
  87. }
  88. // McnDataTypeSummary .
  89. func (d *Dao) McnDataTypeSummary(c context.Context, date xtime.Time) (mmd map[string][]*model.McnDataTypeSummary, tids []int64, err error) {
  90. rows, err := d.db.Query(c, _mcnDataTypeSummarySQL, date)
  91. if err != nil {
  92. return
  93. }
  94. defer rows.Close()
  95. mmd = make(map[string][]*model.McnDataTypeSummary)
  96. for rows.Next() {
  97. md := new(model.McnDataTypeSummary)
  98. err = rows.Scan(&md.ID, &md.Tid, &md.DataView, &md.DataType, &md.Amount)
  99. if err != nil {
  100. if err == xsql.ErrNoRows {
  101. err = nil
  102. }
  103. return
  104. }
  105. tids = append(tids, int64(md.Tid))
  106. mmd[fmt.Sprintf("%d-%d", md.DataView, md.DataType)] = append(mmd[fmt.Sprintf("%d-%d", md.DataView, md.DataType)], md)
  107. }
  108. err = rows.Err()
  109. return
  110. }
  111. // ArcTopDataStatistics .
  112. func (d *Dao) ArcTopDataStatistics(c context.Context, arg *model.McnGetRankReq) (reply *model.McnGetRankUpFansReply, err error) {
  113. params := url.Values{}
  114. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  115. params.Set("tid", fmt.Sprintf("%d", arg.Tid))
  116. params.Set("data_type", fmt.Sprintf("%d", arg.DataType))
  117. var res struct {
  118. Code int `json:"code"`
  119. Data *model.McnGetRankUpFansReply `json:"data"`
  120. }
  121. if err = d.client.Get(c, d.arcTopURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  122. return
  123. }
  124. if res.Code != 0 {
  125. err = errors.Wrapf(ecode.Int(res.Code), "arcRankFansTop d.client.Get(%s,%d)", d.arcTopURL+"?"+params.Encode(), res.Code)
  126. }
  127. reply = res.Data
  128. if reply != nil {
  129. for _, v := range reply.Result {
  130. v.PlayAccumulate = int64(v.Stat.View)
  131. }
  132. }
  133. return
  134. }
  135. // DataFans .
  136. func (d *Dao) DataFans(c context.Context, arg *model.McnCommonReq) (reply *dtmdl.DmConMcnFansD, err error) {
  137. params := url.Values{}
  138. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  139. var res struct {
  140. Code int `json:"code"`
  141. Data *ifmdl.McnGetMcnFansReply `json:"data"`
  142. }
  143. if err = d.client.Get(c, d.dataFansURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  144. return
  145. }
  146. if res.Code != 0 {
  147. err = errors.Wrapf(ecode.Int(res.Code), "dataFans d.client.Get(%s,%d)", d.dataFansURL+"?"+params.Encode(), res.Code)
  148. }
  149. if res.Data == nil {
  150. return
  151. }
  152. reply = &dtmdl.DmConMcnFansD{
  153. LogDate: res.Data.LogDate,
  154. FansAll: res.Data.FansAll,
  155. FansInc: res.Data.FansInc,
  156. ActFans: res.Data.ActFans,
  157. FansDecAll: res.Data.FansDecAll,
  158. FansDec: res.Data.FansDec,
  159. }
  160. return
  161. }
  162. // DataFansBaseAttr .
  163. func (d *Dao) DataFansBaseAttr(c context.Context, arg *model.McnCommonReq) (sex *dtmdl.DmConMcnFansSexW, age *dtmdl.DmConMcnFansAgeW, playWay *dtmdl.DmConMcnFansPlayWayW, err error) {
  164. params := url.Values{}
  165. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  166. params.Set("user_type", ifmdl.UserTypeFans)
  167. var res struct {
  168. Code int `json:"code"`
  169. Data *ifmdl.McnGetBaseFansAttrReply `json:"data"`
  170. }
  171. if err = d.client.Get(c, d.dataFansBaseAttrURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  172. return
  173. }
  174. if res.Code != 0 {
  175. err = errors.Wrapf(ecode.Int(res.Code), "aataFansBaseAttr d.client.Get(%s,%d)", d.dataFansBaseAttrURL+"?"+params.Encode(), res.Code)
  176. }
  177. if res.Data == nil {
  178. return
  179. }
  180. sex = res.Data.FansSex
  181. age = res.Data.FansAge
  182. playWay = res.Data.FansPlayWay
  183. return
  184. }
  185. // DataFansArea .
  186. func (d *Dao) DataFansArea(c context.Context, arg *model.McnCommonReq) (reply []*dtmdl.DmConMcnFansAreaW, err error) {
  187. params := url.Values{}
  188. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  189. params.Set("user_type", ifmdl.UserTypeFans)
  190. var res struct {
  191. Code int `json:"code"`
  192. Data *ifmdl.McnGetFansAreaReply `json:"data"`
  193. }
  194. if err = d.client.Get(c, d.dataFansAreaURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  195. return
  196. }
  197. if res.Code != 0 {
  198. err = errors.Wrapf(ecode.Int(res.Code), "dataFansArea d.client.Get(%s,%d)", d.dataFansAreaURL+"?"+params.Encode(), res.Code)
  199. }
  200. if res.Data == nil {
  201. return
  202. }
  203. reply = res.Data.Result
  204. return
  205. }
  206. // DataFansType .
  207. func (d *Dao) DataFansType(c context.Context, arg *model.McnCommonReq) (reply []*dtmdl.DmConMcnFansTypeW, err error) {
  208. params := url.Values{}
  209. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  210. params.Set("user_type", ifmdl.UserTypeFans)
  211. var res struct {
  212. Code int `json:"code"`
  213. Data *ifmdl.McnGetFansTypeReply `json:"data"`
  214. }
  215. if err = d.client.Get(c, d.dataFansTypeURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  216. return
  217. }
  218. if res.Code != 0 {
  219. err = errors.Wrapf(ecode.Int(res.Code), "dataFansType d.client.Get(%s,%d)", d.dataFansTypeURL+"?"+params.Encode(), res.Code)
  220. }
  221. if res.Data == nil {
  222. return
  223. }
  224. reply = res.Data.Result
  225. return
  226. }
  227. // DataFansTag .
  228. func (d *Dao) DataFansTag(c context.Context, arg *model.McnCommonReq) (reply []*dtmdl.DmConMcnFansTagW, err error) {
  229. params := url.Values{}
  230. params.Set("sign_id", fmt.Sprintf("%d", arg.SignID))
  231. params.Set("user_type", ifmdl.UserTypeFans)
  232. var res struct {
  233. Code int `json:"code"`
  234. Data *ifmdl.McnGetFansTagReply `json:"data"`
  235. }
  236. if err = d.client.Get(c, d.dataFansTagURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  237. return
  238. }
  239. if res.Code != 0 {
  240. err = errors.Wrapf(ecode.Int(res.Code), "dataFansTag d.client.Get(%s,%d)", d.dataFansTagURL+"?"+params.Encode(), res.Code)
  241. }
  242. if res.Data == nil {
  243. return
  244. }
  245. reply = res.Data.Result
  246. return
  247. }