dash.go 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "gadmin/config"
  7. "gadmin/internal/admin/consts"
  8. "gadmin/internal/admin/forms"
  9. "gadmin/internal/gorm/model"
  10. "gadmin/internal/gorm/query"
  11. "gadmin/package/gmdata"
  12. "gadmin/utility"
  13. "gadmin/utility/player"
  14. "github.com/gin-gonic/gin"
  15. "github.com/jinzhu/now"
  16. "github.com/sirupsen/logrus"
  17. "github.com/spf13/cast"
  18. "github.com/xuri/excelize/v2"
  19. "gorm.io/gorm"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // Dash Dash服务
  26. var Dash = new(sDash)
  27. type sDash struct{}
  28. // QueryDisconnectList 断线重连统计列表
  29. func (s *sDash) QueryDisconnectList(params forms.DisconnectListReq) (resp forms.DisconnectListRes, err error) {
  30. var (
  31. rdb = query.Use(config.DB).ReportDayDisconnect
  32. m = rdb.Select(rdb.ALL, rdb.ID, rdb.Date,
  33. rdb.DisCount.Sum().As("dis_count"),
  34. rdb.DisUsers.Sum().As("dis_users"),
  35. rdb.ReCount.Sum().As("re_count"),
  36. rdb.ReUsers.Sum().As("re_users"),
  37. rdb.NewDisCount.Sum().As("new_dis_count"),
  38. rdb.NewDisUsers.Sum().As("new_dis_users"),
  39. rdb.NewReCount.Sum().As("new_re_count"),
  40. rdb.NewReUsers.Sum().As("new_re_users"),
  41. rdb.MobileDisCount.Sum().As("mobile_dis_count"),
  42. rdb.MobileDisUsers.Sum().As("mobile_dis_users"),
  43. rdb.MobileReCount.Sum().As("mobile_re_count"),
  44. rdb.MobileReUsers.Sum().As("mobile_re_users"),
  45. rdb.WifiDisCount.Sum().As("wifi_dis_count"),
  46. rdb.WifiDisUsers.Sum().As("wifi_dis_users"),
  47. rdb.WifiReCount.Sum().As("wifi_re_count"),
  48. rdb.WifiReUsers.Sum().As("wifi_re_users"),
  49. )
  50. )
  51. switch params.ChannelId {
  52. case consts.ChannelIdNone:
  53. // 不选择渠道
  54. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  55. // 所有的广告渠道
  56. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  57. default:
  58. // 指定渠道
  59. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  60. }
  61. if params.ServerId > 0 {
  62. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  63. }
  64. resp.Data, err = m.
  65. Where(rdb.Date.Between(params.Day, params.EndDay)).
  66. Group(rdb.Date).
  67. Order(rdb.Date.Desc()).
  68. Find()
  69. if err != nil {
  70. return resp, err
  71. }
  72. for _, v := range resp.Data {
  73. v.Date = utility.ParseDate(v.Date)
  74. }
  75. resp.Total = int64(len(resp.Data))
  76. return
  77. }
  78. // 统计分组
  79. const (
  80. SevenLogGroupBasic = 1 // 七日登陆基本信息统计
  81. SevenLogGroupTask = 2 // 任务完成度统计
  82. SevenLogGroupAward = 3 // 活跃度奖励完成度统计
  83. )
  84. // QuerySevenAward 七日任务奖励统计
  85. func (s *sDash) QuerySevenAward(params forms.SevenAwardReq) (resp forms.SevenAwardRespData, err error) {
  86. var (
  87. rdb = query.Use(config.DB).ReportDaySeven
  88. days = 0
  89. )
  90. days, err = utility.GetDaysBetween2Date("2006-01-02", params.Day, params.EndDay)
  91. if err != nil {
  92. return resp, err
  93. }
  94. days += 1
  95. rm := rdb.
  96. Where(rdb.Org.Eq(SevenLogGroupAward)).
  97. Where(rdb.Date.Between(params.Day, params.EndDay))
  98. if params.ServerId > 0 {
  99. rm = rm.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  100. }
  101. results, err := rm.Order(rdb.Date, rdb.TaskID).Find()
  102. if err != nil {
  103. return resp, err
  104. }
  105. findResult := func(taskID int64) *forms.SevenAwardItem {
  106. for _, v := range resp.Data {
  107. if v.TaskID == taskID {
  108. return v
  109. }
  110. }
  111. return nil
  112. }
  113. for _, result := range results {
  114. task := gmdata.GetNoviceAward(result.TaskID)
  115. if task == nil {
  116. continue
  117. }
  118. // 查询单天
  119. if days == 1 {
  120. item := &forms.SevenAwardItem{
  121. ID: result.ID,
  122. TaskID: task.ID,
  123. SucCount: result.TaskSucCount,
  124. FirstSucCount: result.TaskSucCount,
  125. AdvSucDays: result.AdvSucDay,
  126. }
  127. resp.Data = append(resp.Data, item)
  128. continue
  129. }
  130. vDate, _ := now.Parse(result.Date)
  131. resultDate := vDate.Format("2006-01-02")
  132. // 查询范围天
  133. // 首天
  134. if params.Day == resultDate {
  135. item := &forms.SevenAwardItem{
  136. ID: result.ID,
  137. TaskID: task.ID,
  138. SucCount: result.TaskSucCount,
  139. FirstSucCount: result.TaskSucCount,
  140. AdvSucDays: result.AdvSucDay,
  141. }
  142. resp.Data = append(resp.Data, item)
  143. } else {
  144. item := findResult(task.ID)
  145. item.SucCount += result.TaskSucCount
  146. item.AdvSucDays += result.AdvSucDay
  147. // 末尾天
  148. if params.EndDay == resultDate {
  149. item.AdvSucDays = item.AdvSucDays / float64(days)
  150. }
  151. }
  152. }
  153. var (
  154. lists = make(map[int64]*forms.SevenAwardItem)
  155. baseMap = make(map[int64]int)
  156. )
  157. for _, datum := range resp.Data {
  158. if _, ok := baseMap[datum.TaskID]; ok {
  159. baseMap[datum.TaskID] += 1
  160. lists[datum.TaskID].AdvSucDays += datum.AdvSucDays
  161. lists[datum.TaskID].FirstSucCount += datum.FirstSucCount
  162. lists[datum.TaskID].SucCount += datum.SucCount
  163. } else {
  164. baseMap[datum.TaskID] = 1
  165. lists[datum.TaskID] = datum
  166. }
  167. }
  168. resp.Data = nil
  169. for _, item := range lists {
  170. item.AdvSucDays /= float64(baseMap[item.TaskID])
  171. resp.Data = append(resp.Data, item)
  172. }
  173. sort.Slice(resp.Data, func(i, j int) bool { return resp.Data[i].TaskID < resp.Data[j].TaskID })
  174. resp.Total = int64(len(gmdata.NoviceActivityAward))
  175. return
  176. }
  177. // QuerySevenTask 七日任务基础信息
  178. func (s *sDash) QuerySevenTask(params forms.SevenTaskReq) (resp forms.SevenTaskRespData, err error) {
  179. var (
  180. rdb = query.Use(config.DB).ReportDaySeven
  181. days = 0
  182. offset = (params.Page - 1) * params.PerPage
  183. startTID = offset + 1
  184. endTID = params.PerPage + offset
  185. )
  186. days, err = utility.GetDaysBetween2Date("2006-01-02", params.Day, params.EndDay)
  187. if err != nil {
  188. return resp, err
  189. }
  190. days += 1
  191. rm := rdb.
  192. Where(rdb.Org.Eq(SevenLogGroupTask)).
  193. Where(rdb.Date.Between(params.Day, params.EndDay)).
  194. Where(rdb.TaskID.Between(startTID, endTID))
  195. if params.ServerId > 0 {
  196. rm = rm.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  197. }
  198. results, err := rm.Order(rdb.Date, rdb.TaskID).Find()
  199. if err != nil {
  200. return resp, err
  201. }
  202. findResult := func(taskID int64) *forms.SevenTaskItem {
  203. for _, v := range resp.Data {
  204. if v.TaskID == taskID {
  205. return v
  206. }
  207. }
  208. return nil
  209. }
  210. for _, result := range results {
  211. task := gmdata.GetNoviceTask(result.TaskID)
  212. if task == nil {
  213. continue
  214. }
  215. // 查询单天
  216. if days == 1 {
  217. item := &forms.SevenTaskItem{
  218. ID: result.ID,
  219. TaskID: task.ID,
  220. Describe: strings.ReplaceAll(task.Describe, "[0]", strconv.FormatInt(task.TaskNum, 10)),
  221. DayValue: task.DayValue,
  222. SucCount: result.TaskSucCount,
  223. FirstSucCount: result.TaskSucCount,
  224. AdvSucDays: result.AdvSucDay,
  225. }
  226. resp.Data = append(resp.Data, item)
  227. continue
  228. }
  229. vDate, _ := now.Parse(result.Date)
  230. resultDate := vDate.Format("2006-01-02")
  231. // 查询范围天
  232. // 首天
  233. if params.Day == resultDate {
  234. item := &forms.SevenTaskItem{
  235. ID: result.ID,
  236. TaskID: task.ID,
  237. Describe: strings.ReplaceAll(task.Describe, "[0]", strconv.FormatInt(task.TaskNum, 10)),
  238. DayValue: task.DayValue,
  239. SucCount: result.TaskSucCount,
  240. FirstSucCount: result.TaskSucCount,
  241. AdvSucDays: result.AdvSucDay,
  242. }
  243. resp.Data = append(resp.Data, item)
  244. } else {
  245. item := findResult(task.ID)
  246. item.SucCount += result.TaskSucCount
  247. item.AdvSucDays += result.AdvSucDay
  248. // 末尾天
  249. if params.EndDay == resultDate {
  250. item.AdvSucDays = item.AdvSucDays / float64(days)
  251. }
  252. }
  253. }
  254. var (
  255. lists = make(map[int64]*forms.SevenTaskItem)
  256. baseMap = make(map[int64]int)
  257. )
  258. for _, datum := range resp.Data {
  259. if _, ok := baseMap[datum.TaskID]; ok {
  260. baseMap[datum.TaskID] += 1
  261. lists[datum.TaskID].AdvSucDays += datum.AdvSucDays
  262. lists[datum.TaskID].FirstSucCount += datum.FirstSucCount
  263. lists[datum.TaskID].SucCount += datum.SucCount
  264. } else {
  265. baseMap[datum.TaskID] = 1
  266. lists[datum.TaskID] = datum
  267. }
  268. }
  269. resp.Data = nil
  270. for _, item := range lists {
  271. item.AdvSucDays /= float64(baseMap[item.TaskID])
  272. resp.Data = append(resp.Data, item)
  273. }
  274. sort.Slice(resp.Data, func(i, j int) bool { return resp.Data[i].TaskID < resp.Data[j].TaskID })
  275. resp.Total = int64(len(gmdata.NoviceTaskList))
  276. return
  277. }
  278. // QuerySevenBasic 七日任务基础信息
  279. func (s *sDash) QuerySevenBasic(params forms.SevenBasicReq) (resp forms.SevenBasicRespData, err error) {
  280. var (
  281. rdb = query.Use(config.DB).ReportDaySeven
  282. b = query.Use(config.DB).ReportDayBasic
  283. m = rdb.
  284. Select(rdb.ID, rdb.Date,
  285. rdb.PlayerCount.Sum().As("player_count"),
  286. rdb.NewPlayerCount.Sum().As("new_player_count"),
  287. rdb.OpenCount.Sum().As("open_count"),
  288. rdb.AllTaskSucCount.Sum().As("all_task_suc_count"),
  289. rdb.Unlock1.Sum().As("unlock1"),
  290. rdb.Unlock2.Sum().As("unlock2"),
  291. rdb.Unlock3.Sum().As("unlock3"),
  292. rdb.Unlock4.Sum().As("unlock4"),
  293. rdb.Unlock5.Sum().As("unlock5"),
  294. rdb.Unlock6.Sum().As("unlock6"),
  295. rdb.Unlock7.Sum().As("unlock7"),
  296. rdb.TaskID.Sum().As("task_id"),
  297. ).
  298. Where(rdb.Org.Eq(SevenLogGroupBasic)).
  299. Group(rdb.Date).
  300. Order(rdb.Date.Desc()).
  301. Limit(int(params.PerPage)).
  302. Offset(int((params.Page - 1) * params.PerPage))
  303. )
  304. switch params.ChannelId {
  305. case consts.ChannelIdNone:
  306. // 不选择渠道
  307. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  308. // 所有的广告渠道
  309. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  310. default:
  311. // 指定渠道
  312. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  313. }
  314. if params.ServerId > 0 {
  315. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  316. }
  317. results, err := m.Find()
  318. if err != nil {
  319. return resp, err
  320. }
  321. for _, result := range results {
  322. var validCount int64
  323. mb := b.
  324. Select(b.ValidCount.Sum().As("valid_count")).
  325. Where(b.Date.Eq(utility.ParseDate(result.Date)))
  326. if params.ChannelId != "" {
  327. mb = mb.Where(b.ChannelID.Eq(params.ChannelId))
  328. }
  329. if params.ServerId > 0 {
  330. mb = mb.Where(b.ServerID.Eq(int32(params.ServerId)))
  331. }
  332. basic, err := mb.Group(b.Date).Order(b.Date.Desc()).First()
  333. if err == nil && basic != nil {
  334. validCount = basic.ValidCount
  335. }
  336. resp.Data = append(resp.Data, forms.SevenBasicItem{
  337. ID: result.ID,
  338. Date: utility.ParseDate(result.Date),
  339. PlayerCount: result.PlayerCount,
  340. ValidCount: validCount,
  341. NewPlayerCount: result.NewPlayerCount,
  342. OpenCount: result.OpenCount,
  343. AllTaskSucCount: result.AllTaskSucCount,
  344. Unlock1: result.Unlock1,
  345. Unlock2: result.Unlock2,
  346. Unlock3: result.Unlock3,
  347. Unlock4: result.Unlock4,
  348. Unlock5: result.Unlock5,
  349. Unlock6: result.Unlock6,
  350. Unlock7: result.Unlock7,
  351. })
  352. }
  353. resp.Total, err = m.Count()
  354. return
  355. }
  356. type AdvEchartsInfo struct {
  357. Days []string `json:"days"`
  358. PositionID []int64 `json:"PositionID"`
  359. ShowTimes []int64 `json:"ShowTimes"`
  360. RewardTimes []int64 `json:"RewardTimes"`
  361. ShowUsers []int64 `json:"ShowUsers"`
  362. RewardUsers []int64 `json:"RewardUsers"`
  363. ClickTimes []int64 `json:"ClickTimes"`
  364. ClickUsers []int64 `json:"ClickUsers"`
  365. }
  366. type AdvEchartsData struct {
  367. Info AdvEchartsInfo `json:"info"`
  368. }
  369. // QueryAdvEcharts 广告点位统计折线
  370. func (s *sDash) QueryAdvEcharts(params forms.QueryAdvEchartsReq) (respData AdvEchartsData, err error) {
  371. resp := AdvEchartsInfo{}
  372. rdb := query.Use(config.DB).ReportDayAdvBasic
  373. m := rdb.
  374. Select(rdb.ID, rdb.Date, rdb.PositionID,
  375. rdb.ShowTimes.Sum().As("show_times"),
  376. rdb.ClickTimes.Sum().As("click_times"),
  377. rdb.RewardTimes.Sum().As("reward_times"),
  378. rdb.ShowUsers.Sum().As("show_users"),
  379. rdb.ClickUsers.Sum().As("click_users"),
  380. rdb.RewardUsers.Sum().As("reward_users"),
  381. ).
  382. Where(rdb.PositionID.Eq(params.PositionId), rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay))
  383. switch params.ChannelId {
  384. case "":
  385. // 不选择渠道
  386. case "1":
  387. // 所有的广告渠道
  388. m = m.Where(rdb.ChannelID.Neq("0"))
  389. default:
  390. // 指定渠道
  391. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  392. }
  393. if params.ServerId > 0 {
  394. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  395. }
  396. result, err := m.Order(rdb.Date).Group(rdb.Date).Find() // .Order(rdb.Date.Desc())
  397. if err != nil {
  398. return
  399. }
  400. for _, v := range result {
  401. if v != nil {
  402. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  403. resp.PositionID = append(resp.PositionID, v.PositionID)
  404. resp.ShowTimes = append(resp.ShowTimes, v.ShowTimes)
  405. resp.RewardTimes = append(resp.RewardTimes, v.RewardTimes)
  406. resp.ShowUsers = append(resp.ShowUsers, v.ShowUsers)
  407. resp.RewardUsers = append(resp.RewardUsers, v.RewardUsers)
  408. resp.ClickTimes = append(resp.ClickTimes, v.ClickTimes)
  409. resp.ClickUsers = append(resp.ClickUsers, v.ClickUsers)
  410. }
  411. }
  412. respData.Info = resp
  413. return
  414. }
  415. type R struct {
  416. EventId int64
  417. Count int64
  418. }
  419. func (s *sDash) QueryAdvReport(params forms.AdvReportReq) (resp forms.AdvReportRespData, err error) {
  420. var (
  421. positionIds []int64
  422. p = query.Use(config.DB).ReportDayAdvBasic
  423. mp = p.Select(p.PositionID.Distinct())
  424. )
  425. if params.ServerId > 0 {
  426. mp = mp.Where(p.ServerID.Eq(int32(params.ServerId)))
  427. }
  428. if params.Flag > -1 {
  429. mp = mp.Where(p.Flag.Eq(params.Flag))
  430. }
  431. err = mp.Where(p.Date.Eq(params.Day)).Pluck(p.PositionID, &positionIds)
  432. if err != nil {
  433. logrus.WithField("from", "QueryAdvReport Pluck").Error(err)
  434. return
  435. }
  436. for _, positionId := range positionIds {
  437. rdb := query.Use(config.DB).ReportDayAdvBasic
  438. m := rdb.
  439. Select(rdb.ID, rdb.Date, rdb.PositionID,
  440. rdb.ShowTimes.Sum().As("show_times"),
  441. rdb.ClickTimes.Sum().As("click_times"),
  442. rdb.RewardTimes.Sum().As("reward_times"),
  443. rdb.ShowUsers.Sum().As("show_users"),
  444. rdb.ClickUsers.Sum().As("click_users"),
  445. rdb.RewardUsers.Sum().As("reward_users"),
  446. ).
  447. Where(rdb.Date.Eq(params.Day), rdb.PositionID.Eq(positionId))
  448. switch params.ChannelId {
  449. case consts.ChannelIdNone:
  450. // 不选择渠道
  451. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  452. // 所有的广告渠道
  453. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  454. default:
  455. // 指定渠道
  456. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  457. }
  458. if params.ServerId > 0 {
  459. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  460. }
  461. if params.Flag > -1 {
  462. m = m.Where(rdb.Flag.Eq(int32(params.Flag)))
  463. }
  464. results, err := m.Order(rdb.Date.Desc()).
  465. Group(rdb.Date).
  466. Limit(params.PerPage).
  467. Offset((params.Page - 1) * params.PerPage).
  468. Find()
  469. if err != nil {
  470. return resp, err
  471. }
  472. for _, result := range results {
  473. resp.Data = append(resp.Data, forms.AdvReportItem{
  474. ID: result.ID,
  475. PositionId: result.PositionID,
  476. ShowTimes: result.ShowTimes,
  477. ClickTimes: result.ClickTimes,
  478. RewardTimes: result.RewardTimes,
  479. ShowUsers: result.ShowUsers,
  480. ClickUsers: result.ClickUsers,
  481. RewardUsers: result.RewardUsers,
  482. })
  483. }
  484. }
  485. resp.Total = int64(len(positionIds))
  486. if err != nil {
  487. return resp, err
  488. }
  489. return
  490. }
  491. func (s *sDash) QueryFirstAdvStat(ctx *gin.Context, params forms.FirstAdvReq) (resp *model.ReportDayFirstAdv, err error) {
  492. rdb := query.Use(config.DB).ReportDayFirstAdv
  493. m := rdb.
  494. Select(rdb.ALL,
  495. rdb.Goto.Sum().As("goto"),
  496. rdb.A.Sum().As("a"),
  497. rdb.B.Sum().As("b"),
  498. rdb.C.Sum().As("c"),
  499. rdb.Waive.Sum().As("waive"),
  500. rdb.EquipNow.Sum().As("equip_now"),
  501. )
  502. switch params.ChannelId {
  503. case "":
  504. // 不选择渠道
  505. case "1":
  506. // 所有的广告渠道
  507. m = m.Where(rdb.ChannelID.Neq("0"))
  508. default:
  509. // 指定渠道
  510. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  511. }
  512. if params.ServerId > 0 {
  513. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  514. }
  515. resp, err = m.First()
  516. if err != nil {
  517. return resp, err
  518. }
  519. return
  520. }
  521. func (s *sDash) QueryFirstAdv(ctx *gin.Context, params forms.FirstAdvReq) (resp forms.FirstAdvRespData, err error) {
  522. rdb := query.Use(config.DB).ReportDayFirstAdv
  523. m := rdb.
  524. Select(rdb.ALL,
  525. rdb.Goto.Sum().As("goto"),
  526. rdb.A.Sum().As("a"),
  527. rdb.B.Sum().As("b"),
  528. rdb.C.Sum().As("c"),
  529. rdb.Waive.Sum().As("waive"),
  530. rdb.EquipNow.Sum().As("equip_now"),
  531. ).
  532. WithContext(ctx)
  533. switch params.ChannelId {
  534. case consts.ChannelIdNone:
  535. // 不选择渠道
  536. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  537. // 所有的广告渠道
  538. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  539. default:
  540. // 指定渠道
  541. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  542. }
  543. if params.ServerId > 0 {
  544. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  545. }
  546. results, err := m.Order(rdb.Date.Desc()).
  547. Group(rdb.Date).
  548. Limit(int(params.PerPage)).
  549. Offset(int((params.Page - 1) * params.PerPage)).
  550. Find()
  551. if err != nil {
  552. return resp, err
  553. }
  554. for _, result := range results {
  555. date := utility.ParseDate(result.Date)
  556. resp.Data = append(resp.Data, forms.FirstAdvItem{
  557. ID: result.ID,
  558. Date: date,
  559. Goto: result.Goto,
  560. A: result.A,
  561. B: result.B,
  562. C: result.C,
  563. Waive: result.Waive,
  564. EquipNow: result.EquipNow,
  565. })
  566. }
  567. resp.Total, err = rdb.Order(rdb.Date.Desc()).Group(rdb.Date).Count()
  568. return
  569. }
  570. func (s *sDash) QueryAdvSumm(ctx *gin.Context, params forms.AdvSummReq) (resp forms.AdvSummRespData, err error) {
  571. rdb := query.Use(config.DB).ReportDayAdvSumm
  572. m := rdb.
  573. Select(rdb.ALL,
  574. rdb.RewardTimes.Sum().As("reward_times"),
  575. rdb.RewardUsers.Sum().As("reward_users"),
  576. rdb.NewTimes.Sum().As("new_times"),
  577. rdb.NewUsers.Sum().As("new_users"),
  578. rdb.OldTimes.Sum().As("old_times"),
  579. rdb.OldUsers.Sum().As("old_users"),
  580. )
  581. switch params.ChannelId {
  582. case consts.ChannelIdNone:
  583. // 不选择渠道
  584. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  585. // 所有的广告渠道
  586. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  587. default:
  588. // 指定渠道
  589. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  590. }
  591. if params.ServerId > 0 {
  592. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  593. }
  594. if params.Flag > -1 {
  595. m = m.Where(rdb.Flag.Eq(params.Flag))
  596. }
  597. // 导出
  598. if params.IsExport == 1 {
  599. results, err := m.Order(rdb.Date.Desc()).Group(rdb.Date).Find()
  600. if err != nil {
  601. return resp, err
  602. }
  603. f := excelize.NewFile()
  604. f.SetColWidth("Sheet1", "A", "A", 15)
  605. f.SetColWidth("Sheet1", "B", "B", 12)
  606. f.SetColWidth("Sheet1", "C", "C", 12)
  607. f.SetColWidth("Sheet1", "D", "D", 12)
  608. f.SetColWidth("Sheet1", "E", "E", 12)
  609. f.SetColWidth("Sheet1", "F", "F", 12)
  610. f.SetColWidth("Sheet1", "G", "G", 12)
  611. f.SetColWidth("Sheet1", "H", "H", 12)
  612. f.SetColWidth("Sheet1", "I", "I", 12)
  613. f.SetColWidth("Sheet1", "J", "J", 12)
  614. f.SetColWidth("Sheet1", "K", "K", 12)
  615. f.SetColWidth("Sheet1", "L", "L", 12)
  616. f.SetColWidth("Sheet1", "M", "M", 12)
  617. f.SetColWidth("Sheet1", "N", "N", 12)
  618. f.SetColWidth("Sheet1", "O", "O", 12)
  619. f.SetColWidth("Sheet1", "P", "P", 12)
  620. // 创建一个工作表
  621. f.SetCellValue("Sheet1", "A1", "日期")
  622. f.SetCellValue("Sheet1", "B1", "总曝光次数")
  623. f.SetCellValue("Sheet1", "C1", "总曝光人数")
  624. f.SetCellValue("Sheet1", "D1", "总平均频次")
  625. f.SetCellValue("Sheet1", "E1", "总活跃人数")
  626. f.SetCellValue("Sheet1", "F1", "总覆盖率")
  627. f.SetCellValue("Sheet1", "G1", "新注册广告次数")
  628. f.SetCellValue("Sheet1", "H1", "新注册广告人数")
  629. f.SetCellValue("Sheet1", "I1", "新注册平均频次")
  630. f.SetCellValue("Sheet1", "J1", "新注册人数")
  631. f.SetCellValue("Sheet1", "K1", "新注册覆盖率")
  632. f.SetCellValue("Sheet1", "L1", "老玩家广告次数")
  633. f.SetCellValue("Sheet1", "M1", "老玩家广告人数")
  634. f.SetCellValue("Sheet1", "N1", "老玩家平均频次")
  635. f.SetCellValue("Sheet1", "O1", "老玩家人数")
  636. f.SetCellValue("Sheet1", "P1", "老玩家覆盖率")
  637. for i, result := range results {
  638. newCount := DayBasic.GetPlayerCount(params.ServerId, result.Date, params.ChannelId, params.Flag, "new_count")
  639. //oldCount := DayBasic.GetPlayerCount(params.ServerId, result.Date, params.ChannelId, params.Flag, "old_count")
  640. activeCount := DayBasic.GetPlayerCount(params.ServerId, result.Date, params.ChannelId, params.Flag, "active_count")
  641. date, _ := now.ParseInLocation(time.Local, result.Date)
  642. // 日期
  643. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), date.Format("2006-01-02"))
  644. // 总曝光次数
  645. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), result.RewardTimes)
  646. // 总曝光人数
  647. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), result.RewardUsers)
  648. // 总平均频次
  649. if result.RewardTimes == 0 || result.RewardUsers == 0 {
  650. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), "0")
  651. } else {
  652. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), utility.Round(float64(result.RewardTimes)/float64(result.RewardUsers)))
  653. }
  654. // 总活跃人数
  655. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), activeCount)
  656. // 总覆盖率
  657. if result.RewardUsers == 0 || activeCount == 0 {
  658. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i+2), "0")
  659. } else {
  660. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i+2), fmt.Sprintf("%v%s", utility.Round((float64(result.RewardUsers)/float64(activeCount))*100), "%"))
  661. }
  662. // 新注册广告次数
  663. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", i+2), result.NewTimes)
  664. // 新注册广告人数
  665. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i+2), result.NewUsers)
  666. // 新注册平均频次
  667. if result.NewTimes == 0 || result.NewUsers == 0 {
  668. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i+2), "0")
  669. } else {
  670. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i+2), utility.Round(float64(result.NewTimes)/float64(result.NewUsers)))
  671. }
  672. // 新注册人数
  673. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i+2), newCount)
  674. // 新注册覆盖率
  675. if result.NewUsers == 0 || newCount == 0 {
  676. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", i+2), "0")
  677. } else {
  678. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.NewUsers)/float64(newCount)*100), "%"))
  679. }
  680. // 老玩家广告次数
  681. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", i+2), result.OldTimes)
  682. // 老玩家广告人数
  683. if activeCount == 0 {
  684. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", i+2), "0")
  685. } else {
  686. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", i+2), activeCount-newCount)
  687. }
  688. // 老玩家平均频次
  689. if result.OldTimes == 0 || result.OldUsers == 0 {
  690. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", i+2), "0")
  691. } else {
  692. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", i+2), utility.Round(float64(result.OldTimes)/float64(result.OldUsers)))
  693. }
  694. // 老玩家人数
  695. if activeCount == 0 {
  696. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", i+2), "0")
  697. } else {
  698. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", i+2), activeCount-result.OldUsers)
  699. }
  700. // 老玩家覆盖率
  701. if activeCount == 0 || result.OldUsers == 0 {
  702. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", i+2), "-")
  703. } else {
  704. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", i+2), fmt.Sprintf("%v%s", utility.Round((float64(result.OldUsers)/float64(activeCount))*100), "%"))
  705. }
  706. }
  707. // 设置工作簿的默认工作表
  708. f.SetActiveSheet(1)
  709. ctx.Header("Content-Type", "application/vnd.ms-excel")
  710. ctx.Header("Content-Disposition", fmt.Sprintf("attachment;filename=广告综合统计导出%s.xlsx", time.Now().Format("20060102150405")))
  711. f.WriteTo(ctx.Writer)
  712. return resp, err
  713. }
  714. results, err := m.Order(rdb.Date.Desc()).
  715. Group(rdb.Date).
  716. Limit(int(params.PerPage)).
  717. Offset(int((params.Page - 1) * params.PerPage)).
  718. Find()
  719. if err != nil {
  720. return resp, err
  721. }
  722. for _, result := range results {
  723. date := utility.ParseDate(result.Date)
  724. resp.Data = append(resp.Data, forms.AdvSummItem{
  725. ID: result.ID,
  726. Date: date,
  727. NewCount: DayBasic.GetPlayerCount(params.ServerId, date, params.ChannelId, params.Flag, "new_count"),
  728. OldCount: DayBasic.GetPlayerCount(params.ServerId, date, params.ChannelId, params.Flag, "old_count"),
  729. ActiveCount: DayBasic.GetPlayerCount(params.ServerId, date, params.ChannelId, params.Flag, "active_count"),
  730. RewardTimes: result.RewardTimes,
  731. RewardUsers: result.RewardUsers,
  732. NewTimes: result.NewTimes,
  733. NewUsers: result.NewUsers,
  734. OldTimes: result.OldTimes,
  735. OldUsers: result.OldUsers,
  736. })
  737. }
  738. resp.Total, err = rdb.Order(rdb.Date.Desc()).Group(rdb.Date).Count()
  739. return
  740. }
  741. func (s *sDash) QueryAdvUserDetails(params forms.AdvUserDetailsReq) (resp forms.AdvUserReportRespData, err error) {
  742. type R2 struct {
  743. Count int64
  744. PositionId int64
  745. }
  746. var (
  747. models []*forms.AdvUserReportItem
  748. eids = []int64{1, 2, 4}
  749. sql string
  750. )
  751. push := func(eventId, positionId, count int64) {
  752. for _, v := range models {
  753. if v.PositionId == positionId {
  754. switch eventId {
  755. case 1:
  756. v.ShowTimes += count
  757. case 2:
  758. v.ClickTimes += count
  759. case 4:
  760. v.RewardTimes += count
  761. }
  762. return
  763. }
  764. }
  765. v2 := new(forms.AdvUserReportItem)
  766. v2.ID = int64(len(models)) + 1
  767. v2.PositionId = positionId
  768. switch eventId {
  769. case 1:
  770. v2.ShowTimes += count
  771. case 2:
  772. v2.ClickTimes += count
  773. case 4:
  774. v2.RewardTimes += count
  775. }
  776. models = append(models, v2)
  777. }
  778. for _, eventId := range eids {
  779. var results []R2
  780. sql = fmt.Sprintf("SELECT COUNT(*) AS `count`, `position_id` FROM `advertisement_logs` where user_id = %v and event_id = %v GROUP BY position_id ORDER BY `count` desc",
  781. params.UserId, eventId)
  782. if len(params.EventAt) == 2 {
  783. sql = fmt.Sprintf("SELECT COUNT(*) AS `count`, `position_id` FROM `advertisement_logs` where user_id = %v and event_id = %v and event_at >= %v and event_at <= %v GROUP BY position_id ORDER BY `count` desc",
  784. params.UserId, eventId, params.EventAt[0], params.EventAt[1])
  785. }
  786. tx := config.DB.Model(&model.AdvertisementLog{}).Raw(sql).Scan(&results)
  787. if tx.Error != nil {
  788. logrus.Errorln(tx.Error)
  789. continue
  790. }
  791. for _, result := range results {
  792. push(eventId, result.PositionId, result.Count)
  793. }
  794. }
  795. resp.Data = models
  796. resp.Total = int64(len(models))
  797. if err != nil {
  798. return resp, err
  799. }
  800. return
  801. }
  802. func (s *sDash) QueryAdvDetailsReq(params forms.AdvDetailsReq) (resp forms.AdvReportRespData, err error) {
  803. day, _ := now.Parse(params.Day)
  804. begin, end := now.With(day).BeginningOfDay().Unix(), now.With(day).EndOfDay().Unix()
  805. daysInfo := strings.Split(params.Days, "_")
  806. var (
  807. uBegin int64
  808. uEnd int64
  809. )
  810. if len(daysInfo) >= 2 {
  811. uBegin, uEnd = now.With(day).BeginningOfDay().AddDate(0, 0, 0-cast.ToInt(daysInfo[0])).Unix(), now.With(day).EndOfDay().AddDate(0, 0, 0-cast.ToInt(daysInfo[1])).Unix()
  812. } else {
  813. if strings.Contains(params.Days, "_") { // > 28
  814. uEnd = now.With(day).EndOfDay().AddDate(0, 0, 0-cast.ToInt(daysInfo[0])).Unix()
  815. } else {
  816. uBegin, uEnd = now.With(day).BeginningOfDay().AddDate(0, 0, 0-cast.ToInt(daysInfo[0])).Unix(), now.With(day).EndOfDay().AddDate(0, 0, 0-cast.ToInt(daysInfo[0])).Unix()
  817. }
  818. }
  819. results := s.getDayAdvLogDetails(params.ServerId, uBegin, uEnd, begin, end, params.ChannelId)
  820. for i, result := range results {
  821. resp.Data = append(resp.Data, forms.AdvReportItem{
  822. ID: int64(i),
  823. PositionId: result.PositionID,
  824. ShowTimes: result.ShowTimes,
  825. ClickTimes: result.ClickTimes,
  826. RewardTimes: result.RewardTimes,
  827. ShowUsers: result.ShowUsers,
  828. ClickUsers: result.ClickUsers,
  829. RewardUsers: result.RewardUsers,
  830. })
  831. }
  832. resp.Total = int64(len(results))
  833. if err != nil {
  834. return resp, err
  835. }
  836. return
  837. }
  838. func (s *sDash) QueryAllEventItems() (data interface{}, err error) {
  839. ec := query.Use(config.DB).ReportEvent
  840. data, err = ec.Find()
  841. return
  842. }
  843. func (s *sDash) getDayAdvLogDetails(serverId int, ubegin, uend, begin, end int64, channelId string) (data []*model.ReportDayAdvBasic) {
  844. advLog := query.Use(config.DB).AdvertisementLog
  845. advLogm := advLog.Select(advLog.PositionID.Distinct())
  846. var positionIds []int64
  847. if serverId > 0 {
  848. advLogm = advLogm.Where(advLog.ServerID.Eq(int32(serverId)))
  849. }
  850. err := advLogm.Where(advLog.EventAt.Between(int32(begin), int32(end))).Pluck(advLog.PositionID, &positionIds)
  851. if err != nil {
  852. logrus.Errorln(err)
  853. return
  854. }
  855. var cls = Channel.GetIdsByTypeToString(channelId)
  856. for _, pid := range positionIds {
  857. arl := &model.ReportDayAdvBasic{PositionID: pid}
  858. var results []R
  859. var sql string
  860. if ubegin > 0 {
  861. switch channelId {
  862. case consts.ChannelIdNone:
  863. // 不选择渠道
  864. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs where event_at >= %d and event_at <= %d and position_id = %d and user_created_at>= %d and user_created_at <= %d ",
  865. begin, end, pid, ubegin, uend)
  866. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  867. // 所有的广告渠道
  868. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <= %d and position_id = %d and user_created_at>= %d and user_created_at <= %d and player_channel.channel_id in (%v)",
  869. begin, end, pid, ubegin, uend, cls)
  870. default:
  871. // 指定渠道
  872. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <= %d and position_id = %d and user_created_at>= %d and user_created_at <= %d and player_channel.channel_id = '%v'",
  873. begin, end, pid, ubegin, uend, channelId)
  874. }
  875. } else {
  876. switch channelId {
  877. case consts.ChannelIdNone:
  878. // 不选择渠道
  879. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs where event_at >= %d and event_at <= %d and position_id = %d and user_created_at <= %d ",
  880. begin, end, pid, uend)
  881. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  882. // 所有的广告渠道
  883. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <= %d and position_id = %d and user_created_at <= %d and player_channel.channel_id in (%v)",
  884. begin, end, pid, uend, cls)
  885. default:
  886. // 指定渠道
  887. sql = fmt.Sprintf("select event_id, count(1) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <= %d and position_id = %d and user_created_at <= %d and player_channel.channel_id = '%v'",
  888. begin, end, pid, uend, channelId)
  889. }
  890. }
  891. if serverId > 0 {
  892. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  893. }
  894. sql = sql + " group by event_id"
  895. tx := config.DB.Model(&model.AdvertisementLog{}).Raw(sql).Scan(&results)
  896. if tx.Error != nil {
  897. logrus.Errorln(tx.Error)
  898. continue
  899. }
  900. hasVal := false
  901. for _, result := range results {
  902. switch result.EventId {
  903. case 1:
  904. arl.ShowTimes = result.Count
  905. hasVal = true
  906. case 2:
  907. arl.ClickTimes = result.Count
  908. hasVal = true
  909. case 4:
  910. arl.RewardTimes = result.Count
  911. hasVal = true
  912. }
  913. }
  914. var results2 []R
  915. if ubegin > 0 {
  916. switch channelId {
  917. case consts.ChannelIdNone:
  918. // 不选择渠道
  919. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs where event_at >= %d and event_at <=%d and position_id = %d and user_created_at>= %d and user_created_at <= %d ",
  920. begin, end, pid, ubegin, uend)
  921. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  922. // 所有的广告渠道
  923. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <=%d and position_id = %d and user_created_at>= %d and user_created_at <= %d and player_channel.channel_id in (%v)",
  924. begin, end, pid, ubegin, uend, cls)
  925. default:
  926. // 指定渠道
  927. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <=%d and position_id = %d and user_created_at>= %d and user_created_at <= %d and player_channel.channel_id = '%v' ",
  928. begin, end, pid, ubegin, uend, channelId)
  929. }
  930. } else {
  931. switch channelId {
  932. case consts.ChannelIdNone:
  933. // 不选择渠道
  934. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs where event_at >= %d and event_at <=%d and position_id = %d and user_created_at <= %d",
  935. begin, end, pid, uend)
  936. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  937. // 所有的广告渠道
  938. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <=%d and position_id = %d and user_created_at <= %d and player_channel.channel_id in (%v)",
  939. begin, end, pid, uend, cls)
  940. default:
  941. // 指定渠道
  942. sql = fmt.Sprintf("select event_id, count(distinct(user_id)) as count from advertisement_logs LEFT JOIN player_channel ON advertisement_logs.user_id = player_channel.playerid where event_at >= %d and event_at <=%d and position_id = %d and user_created_at <= %d and player_channel.channel_id = '%v'",
  943. begin, end, pid, uend, channelId)
  944. }
  945. }
  946. if serverId > 0 {
  947. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  948. }
  949. sql = sql + " group by event_id"
  950. tx2 := config.DB.Model(&model.AdvertisementLog{}).Raw(sql).Scan(&results2)
  951. if tx2.Error != nil {
  952. logrus.Errorln(tx.Error)
  953. continue
  954. }
  955. for _, result := range results2 {
  956. switch result.EventId {
  957. case 1:
  958. arl.ShowUsers = result.Count
  959. hasVal = true
  960. case 2:
  961. arl.ClickUsers = result.Count
  962. hasVal = true
  963. case 4:
  964. arl.RewardUsers = result.Count
  965. hasVal = true
  966. }
  967. }
  968. if hasVal {
  969. data = append(data, arl)
  970. }
  971. }
  972. return data
  973. }
  974. type BossReportInfo struct {
  975. Days []string `json:"days"`
  976. Players []int64 `json:"players"`
  977. MaxStage []int64 `json:"maxStage"`
  978. ChallengeCount []int64 `json:"challengeCount"`
  979. ReviveCount []int64 `json:"reviveCount"`
  980. RepelCount []int64 `json:"repelCount"`
  981. AwardCount []int64 `json:"awardCount"`
  982. MinuteMix1Count []int64 `json:"minuteMix1Count"`
  983. MinuteMix2Count []int64 `json:"minuteMix2Count"`
  984. MinuteMix3Count []int64 `json:"minuteMix3Count"`
  985. MinuteCount []int64 `json:"minuteCount"`
  986. ExchangeData []string `json:"exchangeData"`
  987. AvgChallengeCount []int64 `json:"avgChallengeCount"`
  988. AvgReviveCount []int64 `json:"avgReviveCount"`
  989. AvgRepelCount []int64 `json:"avgRepelCount"`
  990. AvgAwardCount []int64 `json:"avgAwardCount"`
  991. AvgMinuteMix1Count []float64 `json:"avgMinuteMix1Count"`
  992. AvgMinuteMix2Count []float64 `json:"avgMinuteMix2Count"`
  993. AvgMinuteMix3Count []float64 `json:"avgMinuteMix3Count"`
  994. AvgMinuteCount []int64 `json:"avgMinuteCount"`
  995. }
  996. type BossLogItem struct {
  997. Day string `json:"days"`
  998. Players int64 `json:"players"`
  999. MaxStage int64 `json:"maxStage"`
  1000. ChallengeCount int64 `json:"challengeCount"`
  1001. ReviveCount int64 `json:"reviveCount"`
  1002. RepelCount int64 `json:"repelCount"`
  1003. AwardCount int64 `json:"awardCount"`
  1004. MinuteMix1Count int64 `json:"minuteMix1Count"`
  1005. MinuteMix2Count int64 `json:"minuteMix2Count"`
  1006. MinuteMix3Count int64 `json:"minuteMix3Count"`
  1007. MinuteCount int64 `json:"minuteCount"`
  1008. ExchangeData string `json:"exchangeData"`
  1009. AvgChallengeCount float64 `json:"avgChallengeCount"`
  1010. AvgReviveCount float64 `json:"avgReviveCount"`
  1011. AvgRepelCount float64 `json:"avgRepelCount"`
  1012. AvgAwardCount float64 `json:"avgAwardCount"`
  1013. AvgMinuteMix1Count float64 `json:"avgMinuteMix1Count"`
  1014. AvgMinuteMix2Count float64 `json:"avgMinuteMix2Count"`
  1015. AvgMinuteMix3Count float64 `json:"avgMinuteMix3Count"`
  1016. AvgMinuteCount int64 `json:"avgMinuteCount"`
  1017. }
  1018. type BossInfo struct {
  1019. Info BossReportInfo `json:"info"`
  1020. Rows []BossLogItem `json:"rows"`
  1021. }
  1022. // QueryBossLog 统计暗影突袭信息
  1023. func (s *sDash) QueryBossLog(params forms.BossReportReq) (respData BossInfo, err error) {
  1024. DB, err := player.GetDBByServerID(params.ServerId)
  1025. if err != nil {
  1026. return
  1027. }
  1028. resp := BossReportInfo{}
  1029. boss := query.Use(config.GDBGroup[DB]).Bosswar
  1030. rdb := query.Use(config.DB).ReportDayBoss
  1031. type ReportDayBoss struct {
  1032. model.ReportDayBoss
  1033. Exchange map[int]int
  1034. }
  1035. var result []*ReportDayBoss
  1036. m := rdb.Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay)).Order(rdb.Date.Desc())
  1037. switch params.ChannelId {
  1038. case consts.ChannelIdNone:
  1039. // 不选择渠道
  1040. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1041. // 所有的广告渠道
  1042. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1043. default:
  1044. // 指定渠道
  1045. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  1046. }
  1047. if params.ServerId > 0 {
  1048. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  1049. }
  1050. add := func(row *model.ReportDayBoss) {
  1051. var (
  1052. exist bool
  1053. )
  1054. for _, v := range result {
  1055. if v.Date == row.Date {
  1056. exist = true
  1057. v.PlayerCount += row.PlayerCount
  1058. v.ChallengeCount += row.ChallengeCount
  1059. v.MaxStage += row.MaxStage
  1060. v.ReviveCount += row.ReviveCount
  1061. v.RepelCount += row.RepelCount
  1062. v.AwardCount += row.AwardCount
  1063. v.MinuteMix1 += row.MinuteMix1
  1064. v.MinuteMix2 += row.MinuteMix2
  1065. v.MinuteMix3 += row.MinuteMix3
  1066. v.TotalMinute += row.TotalMinute
  1067. var tempExchange = make(map[int]int)
  1068. if row.ExchangeData != "" {
  1069. if err = json.Unmarshal([]byte(row.ExchangeData), &tempExchange); err != nil {
  1070. logrus.Warnf("ExchangeData Unmarshal err :%+v", err)
  1071. return
  1072. }
  1073. }
  1074. if v.Exchange == nil {
  1075. v.Exchange = make(map[int]int)
  1076. }
  1077. for key, value := range tempExchange {
  1078. if _, ok := v.Exchange[key]; ok {
  1079. v.Exchange[key] += value
  1080. } else {
  1081. v.Exchange[key] = value
  1082. }
  1083. }
  1084. }
  1085. }
  1086. if exist == false {
  1087. v := new(ReportDayBoss)
  1088. v.Date = row.Date
  1089. v.ChannelID = row.ChannelID
  1090. v.PlayerCount += row.PlayerCount
  1091. v.ChallengeCount += row.ChallengeCount
  1092. v.MaxStage += row.MaxStage
  1093. v.ReviveCount += row.ReviveCount
  1094. v.RepelCount += row.RepelCount
  1095. v.AwardCount += row.AwardCount
  1096. v.MinuteMix1 += row.MinuteMix1
  1097. v.MinuteMix2 += row.MinuteMix2
  1098. v.MinuteMix3 += row.MinuteMix3
  1099. v.TotalMinute += row.TotalMinute
  1100. var tempExchange = make(map[int]int)
  1101. if row.ExchangeData != "" {
  1102. if err = json.Unmarshal([]byte(row.ExchangeData), &tempExchange); err != nil {
  1103. logrus.Warnf("ExchangeData Unmarshal err :%+v", err)
  1104. return
  1105. }
  1106. }
  1107. if v.Exchange == nil {
  1108. v.Exchange = make(map[int]int)
  1109. }
  1110. for key, value := range tempExchange {
  1111. if _, ok := v.Exchange[key]; ok {
  1112. v.Exchange[key] += value
  1113. } else {
  1114. v.Exchange[key] = value
  1115. }
  1116. }
  1117. result = append(result, v)
  1118. }
  1119. }
  1120. distributes, err := m.Find()
  1121. if err != nil {
  1122. return
  1123. }
  1124. for _, distribute := range distributes {
  1125. add(distribute)
  1126. }
  1127. for _, v := range result {
  1128. if v != nil {
  1129. exchangeData, err := json.Marshal(v.Exchange)
  1130. if err != nil {
  1131. logrus.Warnf("exchangeData Marshal failed:%+v", err)
  1132. return respData, err
  1133. }
  1134. v.ExchangeData = string(exchangeData)
  1135. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  1136. resp.Players = append(resp.Players, v.PlayerCount)
  1137. resp.MaxStage = append(resp.MaxStage, v.MaxStage)
  1138. resp.ChallengeCount = append(resp.ChallengeCount, v.ChallengeCount)
  1139. resp.ReviveCount = append(resp.ReviveCount, v.ReviveCount)
  1140. resp.RepelCount = append(resp.RepelCount, int64(v.RepelCount))
  1141. resp.AwardCount = append(resp.AwardCount, int64(v.AwardCount))
  1142. resp.MinuteMix1Count = append(resp.MinuteMix1Count, v.MinuteMix1)
  1143. resp.MinuteMix2Count = append(resp.MinuteMix2Count, v.MinuteMix2)
  1144. resp.MinuteMix3Count = append(resp.MinuteMix3Count, v.MinuteMix3)
  1145. resp.MinuteCount = append(resp.MinuteCount, v.TotalMinute)
  1146. resp.ExchangeData = append(resp.ExchangeData, v.ExchangeData)
  1147. var players int64 = 1
  1148. if v.PlayerCount > 1 {
  1149. players = v.PlayerCount
  1150. }
  1151. resp.AvgChallengeCount = append(resp.AvgChallengeCount, v.ChallengeCount*100/players)
  1152. resp.AvgReviveCount = append(resp.AvgReviveCount, v.ReviveCount*100/players)
  1153. resp.AvgRepelCount = append(resp.AvgRepelCount, int64(v.RepelCount)*100/players)
  1154. resp.AvgAwardCount = append(resp.AvgAwardCount, int64(v.AwardCount)*100/players)
  1155. minutes := v.MinuteMix1 + v.MinuteMix2 + v.MinuteMix3
  1156. if minutes < 1 {
  1157. minutes = 1
  1158. }
  1159. resp.AvgMinuteMix1Count = append(resp.AvgMinuteMix1Count, float64(v.MinuteMix1*100/minutes))
  1160. resp.AvgMinuteMix2Count = append(resp.AvgMinuteMix2Count, float64(v.MinuteMix2*100/minutes))
  1161. resp.AvgMinuteMix3Count = append(resp.AvgMinuteMix3Count, float64(v.MinuteMix3*100/minutes))
  1162. resp.AvgMinuteCount = append(resp.AvgMinuteCount, v.TotalMinute/players)
  1163. // 最大阶段
  1164. var maxStage int64
  1165. stamp, _ := now.ParseInLocation(time.Local, v.Date)
  1166. first, err := boss.Where(boss.YearDay.Eq(int32(utility.GetYearDay2(&stamp)))).First()
  1167. if err == nil && first != nil {
  1168. maxStage = int64(first.Fighting)
  1169. }
  1170. item := BossLogItem{
  1171. Day: strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""),
  1172. Players: v.PlayerCount,
  1173. MaxStage: maxStage,
  1174. ChallengeCount: v.ChallengeCount,
  1175. ReviveCount: v.ReviveCount,
  1176. RepelCount: int64(v.RepelCount),
  1177. AwardCount: int64(v.AwardCount),
  1178. MinuteMix1Count: int64(v.MinuteMix1),
  1179. MinuteMix2Count: int64(v.MinuteMix2),
  1180. MinuteMix3Count: int64(v.MinuteMix3),
  1181. MinuteCount: v.TotalMinute,
  1182. }
  1183. item.AvgChallengeCount = float64(v.ChallengeCount*100/players) / 100
  1184. item.AvgReviveCount = float64(v.ReviveCount*100/players) / 100
  1185. item.AvgRepelCount = float64(int64(v.RepelCount)*100/players) / 100
  1186. item.AvgAwardCount = float64(int64(v.AwardCount)*100/players) / 100
  1187. item.ExchangeData = v.ExchangeData
  1188. item.AvgMinuteMix1Count = float64(v.MinuteMix1 * 100 / minutes)
  1189. item.AvgMinuteMix2Count = float64(v.MinuteMix2 * 100 / minutes)
  1190. item.AvgMinuteMix3Count = float64(v.MinuteMix3 * 100 / minutes)
  1191. item.AvgMinuteCount = int64(v.TotalMinute*100/players) / 100
  1192. respData.Rows = append(respData.Rows, item)
  1193. }
  1194. }
  1195. respData.Info = resp
  1196. return
  1197. }
  1198. type IdiomReportInfo struct {
  1199. Days []string `json:"days"`
  1200. Players []int64 `json:"players"`
  1201. BreakCount []int64 `json:"breakCount"`
  1202. TipsCount []int64 `json:"tipsCount"`
  1203. IntegralCount []int64 `json:"integralCount"`
  1204. ReceiveCount []int64 `json:"receiveCount"`
  1205. InkAdvCount []int64 `json:"inkAdvCount"`
  1206. InkDrillCount []int64 `json:"inkDrillCount"`
  1207. RankingCount []int64 `json:"rankingCount"`
  1208. AwardCount []int64 `json:"awardCount"`
  1209. GearData []string `json:"gearData"`
  1210. AvgBreakCount []int64 `json:"avgBreakCount"`
  1211. AvgInkAdvCount []int64 `json:"avgInkAdvCount"`
  1212. AvgInkDrillCount []int64 `json:"avgInkDrillCount"`
  1213. AvgTipsCount []int64 `json:"avgTipsCount"`
  1214. AvgIntegralCount []int64 `json:"avgIntegralCount"`
  1215. AvgReceiveCount []int64 `json:"avgReceiveCount"`
  1216. AvgRankingCount []int64 `json:"avgRankingCount"`
  1217. AvgAwardCount []int64 `json:"avgAwardCount"`
  1218. }
  1219. type IdiomLogItem struct {
  1220. Day string `json:"days"`
  1221. Players int64 `json:"players"`
  1222. BreakCount int64 `json:"breakCount"`
  1223. TipsCount int64 `json:"tipsCount"`
  1224. IntegralCount int64 `json:"integralCount"`
  1225. ReceiveCount int64 `json:"receiveCount"`
  1226. InkAdvCount int64 `json:"inkAdvCount"`
  1227. InkDrillCount int64 `json:"inkDrillCount"`
  1228. RankingCount int64 `json:"rankingCount"`
  1229. AwardCount int64 `json:"awardCount"`
  1230. GearData string `json:"gearData"`
  1231. AvgBreakCount float64 `json:"avgBreakCount"`
  1232. AvgInkAdvCount float64 `json:"avgInkAdvCount"`
  1233. AvgInkDrillCount float64 `json:"avgInkDrillCount"`
  1234. AvgTipsCount float64 `json:"avgTipsCount"`
  1235. AvgIntegralCount float64 `json:"avgIntegralCount"`
  1236. AvgReceiveCount float64 `json:"avgReceiveCount"`
  1237. AvgRankingCount float64 `json:"avgRankingCount"`
  1238. AvgAwardCount float64 `json:"avgAwardCount"`
  1239. }
  1240. type IdiomInfo struct {
  1241. Info IdiomReportInfo `json:"info"`
  1242. Rows []IdiomLogItem `json:"rows"`
  1243. }
  1244. // QueryIdiomLog 统计金榜题名信息
  1245. func (s *sDash) QueryIdiomLog(params forms.IdiomReportReq) (respData IdiomInfo, err error) {
  1246. resp := IdiomReportInfo{}
  1247. rdb := query.Use(config.DB).ReportDayIdiom
  1248. type Gear struct {
  1249. GearId int `json:"GearId"`
  1250. Count int `json:"Count"`
  1251. }
  1252. type ReportDayIdiom struct {
  1253. model.ReportDayIdiom
  1254. Gear []*Gear
  1255. }
  1256. findFail := func(id int, lst []*Gear) *Gear {
  1257. for _, v := range lst {
  1258. if v.GearId == id {
  1259. return v
  1260. }
  1261. }
  1262. return nil
  1263. }
  1264. var result []*ReportDayIdiom
  1265. m := rdb.Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay)).Order(rdb.Date.Desc())
  1266. switch params.ChannelId {
  1267. case consts.ChannelIdNone:
  1268. // 不选择渠道
  1269. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1270. // 所有的广告渠道
  1271. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1272. default:
  1273. // 指定渠道
  1274. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  1275. }
  1276. if params.ServerId > 0 {
  1277. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  1278. }
  1279. add := func(row *model.ReportDayIdiom) {
  1280. var exist bool
  1281. for _, v := range result {
  1282. if v.Date == row.Date {
  1283. exist = true
  1284. v.PlayerCount += row.PlayerCount
  1285. v.BreakCount += row.BreakCount
  1286. v.TipsCount += row.TipsCount
  1287. v.IntegralCount += row.IntegralCount
  1288. v.ReceiveCount += row.ReceiveCount
  1289. v.InkAdvCount += row.InkAdvCount
  1290. v.InkDrillCount += row.InkDrillCount
  1291. v.RankingCount += row.RankingCount
  1292. v.AwardCount += row.AwardCount
  1293. var tempGear []*Gear
  1294. if row.GearData != "" {
  1295. if err = json.Unmarshal([]byte(row.GearData), &tempGear); err != nil {
  1296. logrus.Warnf("GearData Unmarshal err :%+v", err)
  1297. return
  1298. }
  1299. }
  1300. for _, temp := range tempGear {
  1301. cas := findFail(temp.GearId, v.Gear)
  1302. if cas != nil {
  1303. cas.Count += temp.Count
  1304. } else {
  1305. v.Gear = append(v.Gear, &Gear{
  1306. GearId: temp.GearId,
  1307. Count: temp.Count,
  1308. })
  1309. }
  1310. }
  1311. }
  1312. }
  1313. if exist == false {
  1314. v := new(ReportDayIdiom)
  1315. v.Date = row.Date
  1316. v.ChannelID = row.ChannelID
  1317. v.PlayerCount += row.PlayerCount
  1318. v.BreakCount += row.BreakCount
  1319. v.TipsCount += row.TipsCount
  1320. v.IntegralCount += row.IntegralCount
  1321. v.ReceiveCount += row.ReceiveCount
  1322. v.InkAdvCount += row.InkAdvCount
  1323. v.InkDrillCount += row.InkDrillCount
  1324. v.RankingCount += row.RankingCount
  1325. v.AwardCount += row.AwardCount
  1326. var tempGear []*Gear
  1327. if row.GearData != "" {
  1328. if err = json.Unmarshal([]byte(row.GearData), &tempGear); err != nil {
  1329. logrus.Warnf("GearData2 Unmarshal err :%+v", err)
  1330. return
  1331. }
  1332. }
  1333. for _, temp := range tempGear {
  1334. cas := findFail(temp.GearId, v.Gear)
  1335. if cas != nil {
  1336. cas.Count += temp.Count
  1337. } else {
  1338. v.Gear = append(v.Gear, &Gear{
  1339. GearId: temp.GearId,
  1340. Count: temp.Count,
  1341. })
  1342. }
  1343. }
  1344. result = append(result, v)
  1345. }
  1346. }
  1347. distributes, err := m.Find()
  1348. if err != nil {
  1349. return
  1350. }
  1351. for _, distribute := range distributes {
  1352. add(distribute)
  1353. }
  1354. for _, v := range result {
  1355. if v != nil {
  1356. gearData, err := json.Marshal(v.Gear)
  1357. if err != nil {
  1358. logrus.Warnf("gearData Marshal failed:%+v", err)
  1359. return respData, err
  1360. }
  1361. v.GearData = string(gearData)
  1362. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  1363. resp.Players = append(resp.Players, v.PlayerCount)
  1364. resp.BreakCount = append(resp.BreakCount, v.BreakCount)
  1365. resp.TipsCount = append(resp.TipsCount, v.TipsCount)
  1366. resp.IntegralCount = append(resp.IntegralCount, v.IntegralCount)
  1367. resp.ReceiveCount = append(resp.ReceiveCount, v.ReceiveCount)
  1368. resp.InkAdvCount = append(resp.InkAdvCount, v.InkAdvCount)
  1369. resp.InkDrillCount = append(resp.InkDrillCount, v.InkDrillCount)
  1370. resp.RankingCount = append(resp.RankingCount, v.RankingCount)
  1371. resp.AwardCount = append(resp.AwardCount, v.AwardCount)
  1372. resp.GearData = append(resp.GearData, v.GearData)
  1373. var players int64 = 1
  1374. if v.PlayerCount > 1 {
  1375. players = v.PlayerCount
  1376. }
  1377. resp.AvgBreakCount = append(resp.AvgBreakCount, v.BreakCount*100/players)
  1378. resp.AvgInkAdvCount = append(resp.AvgInkAdvCount, v.InkAdvCount*100/players)
  1379. resp.AvgInkDrillCount = append(resp.AvgInkDrillCount, v.InkDrillCount*100/players)
  1380. resp.AvgTipsCount = append(resp.AvgTipsCount, v.TipsCount*100/players)
  1381. resp.AvgIntegralCount = append(resp.AvgIntegralCount, v.IntegralCount*100/players)
  1382. resp.AvgReceiveCount = append(resp.AvgReceiveCount, v.ReceiveCount*100/players)
  1383. resp.AvgRankingCount = append(resp.AvgRankingCount, v.RankingCount*100/players)
  1384. resp.AvgAwardCount = append(resp.AvgAwardCount, v.AwardCount*100/players)
  1385. item := IdiomLogItem{
  1386. Day: strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""),
  1387. Players: v.PlayerCount,
  1388. BreakCount: v.BreakCount,
  1389. InkAdvCount: v.InkAdvCount,
  1390. InkDrillCount: v.InkDrillCount,
  1391. TipsCount: v.TipsCount,
  1392. IntegralCount: v.IntegralCount,
  1393. ReceiveCount: v.ReceiveCount,
  1394. RankingCount: v.RankingCount,
  1395. AwardCount: v.AwardCount,
  1396. }
  1397. item.AvgBreakCount = float64(v.BreakCount*100/players) / 100
  1398. item.AvgInkAdvCount = float64(v.InkAdvCount*100/players) / 100
  1399. item.AvgInkDrillCount = float64(v.InkDrillCount*100/players) / 100
  1400. item.AvgTipsCount = float64(v.TipsCount*100/players) / 100
  1401. item.AvgIntegralCount = float64(v.IntegralCount*100/players) / 100
  1402. item.AvgReceiveCount = float64(v.ReceiveCount*100/players) / 100
  1403. item.AvgRankingCount = float64(v.RankingCount*100/players) / 100
  1404. item.AvgAwardCount = float64(v.AwardCount*100/players) / 100
  1405. item.GearData = v.GearData
  1406. respData.Rows = append(respData.Rows, item)
  1407. }
  1408. }
  1409. respData.Info = resp
  1410. return
  1411. }
  1412. type DuelReportInfo struct {
  1413. Days []string `json:"days"`
  1414. Players []int64 `json:"players"`
  1415. RefreshCount []int64 `json:"refreshCount"`
  1416. DuelCount []int64 `json:"duelCount"`
  1417. DuelFreeCount []int64 `json:"duelFreeCount"`
  1418. DuelBattleCount []int64 `json:"duelBattleCount"`
  1419. DuelRevengeCount []int64 `json:"duelRevengeCount"`
  1420. BattleAdvCount []int64 `json:"battleAdvCount"`
  1421. BattleDrillCount []int64 `json:"battleDrillCount"`
  1422. AdjustmentCount []int64 `json:"adjustmentCount"`
  1423. RevengeCount []int64 `json:"revengeCount"`
  1424. RankingCount []int64 `json:"rankingCount"`
  1425. AwardCount []int64 `json:"awardCount"`
  1426. AdjustDuel []string `json:"adjustDuel"`
  1427. AvgRefreshCount []int64 `json:"avgRefreshCount"`
  1428. AvgDuelCount []int64 `json:"avgDuelCount"`
  1429. AvgDuelFreeCount []int64 `json:"avgDuelFreeCount"`
  1430. AvgDuelBattleCount []int64 `json:"avgDuelBattleCount"`
  1431. AvgDuelRevengeCount []int64 `json:"avgDuelRevengeCount"`
  1432. AvgBattleAdvCount []int64 `json:"avgBattleAdvCount"`
  1433. AvgBattleDrillCount []int64 `json:"avgBattleDrillCount"`
  1434. AvgAdjustmentCount []int64 `json:"avgAdjustmentCount"`
  1435. AvgRevengeCount []int64 `json:"avgRevengeCount"`
  1436. AvgRankingCount []int64 `json:"avgRankingCount"`
  1437. AvgAwardCount []int64 `json:"avgAwardCount"`
  1438. }
  1439. type DuelLogItem struct {
  1440. Day string `json:"days"`
  1441. Players int64 `json:"players"`
  1442. RefreshCount int64 `json:"refreshCount"`
  1443. DuelCount int64 `json:"duelCount"`
  1444. DuelFreeCount int64 `json:"duelFreeCount"`
  1445. DuelBattleCount int64 `json:"duelBattleCount"`
  1446. DuelRevengeCount int64 `json:"duelRevengeCount"`
  1447. BattleAdvCount int64 `json:"battleAdvCount"`
  1448. BattleDrillCount int64 `json:"battleDrillCount"`
  1449. AdjustmentCount int64 `json:"adjustmentCount"`
  1450. RevengeCount int64 `json:"revengeCount"`
  1451. RankingCount int64 `json:"rankingCount"`
  1452. AwardCount int64 `json:"awardCount"`
  1453. AdjustDuel string `json:"adjustDuel"`
  1454. AvgRefreshCount float64 `json:"avgRefreshCount"`
  1455. AvgDuelCount float64 `json:"avgDuelCount"`
  1456. AvgDuelFreeCount float64 `json:"avgDuelFreeCount"`
  1457. AvgDuelBattleCount float64 `json:"avgDuelBattleCount"`
  1458. AvgDuelRevengeCount float64 `json:"avgDuelRevengeCount"`
  1459. AvgBattleAdvCount float64 `json:"avgBattleAdvCount"`
  1460. AvgBattleDrillCount float64 `json:"avgBattleDrillCount"`
  1461. AvgAdjustmentCount float64 `json:"avgAdjustmentCount"`
  1462. AvgRevengeCount float64 `json:"avgRevengeCount"`
  1463. AvgRankingCount float64 `json:"avgRankingCount"`
  1464. AvgAwardCount float64 `json:"avgAwardCount"`
  1465. }
  1466. type DuelInfo struct {
  1467. Info DuelReportInfo `json:"info"`
  1468. Rows []DuelLogItem `json:"rows"`
  1469. }
  1470. // QueryDuelLog 统计狭路对决信息
  1471. func (s *sDash) QueryDuelLog(params forms.DuelReportReq) (respData DuelInfo, err error) {
  1472. resp := DuelReportInfo{}
  1473. rdb := query.Use(config.DB).ReportDayDuel
  1474. m := rdb.Select(rdb.ALL, rdb.ID, rdb.Date,
  1475. rdb.PlayerCount.Sum().As("player_count"),
  1476. rdb.RefreshCount.Sum().As("refresh_count"),
  1477. rdb.DuelCount.Sum().As("duel_count"),
  1478. rdb.DuelFreeCount.Sum().As("duel_free_count"),
  1479. rdb.DuelBattleCount.Sum().As("duel_battle_count"),
  1480. rdb.DuelRevengeCount.Sum().As("duel_revenge_count"),
  1481. rdb.BattleAdvCount.Sum().As("battle_adv_count"),
  1482. rdb.BattleDrillCount.Sum().As("battle_drill_count"),
  1483. rdb.AdjustmentCount.Sum().As("adjustment_count"),
  1484. rdb.RevengeCount.Sum().As("revenge_count"),
  1485. rdb.RankingCount.Sum().As("ranking_count"),
  1486. rdb.AwardCount.Sum().As("award_count"),
  1487. ).Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay))
  1488. switch params.ChannelId {
  1489. case consts.ChannelIdNone:
  1490. // 不选择渠道
  1491. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1492. // 所有的广告渠道
  1493. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1494. default:
  1495. // 指定渠道
  1496. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  1497. }
  1498. if params.ServerId > 0 {
  1499. m = m.Where(rdb.ServerID.Eq(int64(params.ServerId)))
  1500. }
  1501. result, err := m.Group(rdb.Date).Order(rdb.Date.Desc()).Find()
  1502. if err != nil {
  1503. return respData, err
  1504. }
  1505. for _, v := range result {
  1506. if v != nil {
  1507. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  1508. resp.Players = append(resp.Players, v.PlayerCount)
  1509. resp.RefreshCount = append(resp.RefreshCount, v.RefreshCount)
  1510. resp.DuelCount = append(resp.DuelCount, v.DuelCount)
  1511. resp.DuelFreeCount = append(resp.DuelFreeCount, v.DuelFreeCount)
  1512. resp.DuelBattleCount = append(resp.DuelBattleCount, v.DuelBattleCount)
  1513. resp.DuelRevengeCount = append(resp.DuelRevengeCount, v.DuelRevengeCount)
  1514. resp.BattleAdvCount = append(resp.BattleAdvCount, v.BattleAdvCount)
  1515. resp.BattleDrillCount = append(resp.BattleDrillCount, v.BattleDrillCount)
  1516. resp.AdjustmentCount = append(resp.AdjustmentCount, v.AdjustmentCount)
  1517. resp.RevengeCount = append(resp.RevengeCount, v.RevengeCount)
  1518. resp.RankingCount = append(resp.RankingCount, v.RankingCount)
  1519. resp.AwardCount = append(resp.AwardCount, v.AwardCount)
  1520. resp.AdjustDuel = append(resp.AdjustDuel, fmt.Sprintf("%.2f", float64(v.AdjustmentCount)/float64(v.DuelCount)*100)+"%")
  1521. var players int64 = 1
  1522. if v.PlayerCount > 1 {
  1523. players = v.PlayerCount
  1524. }
  1525. resp.AvgRefreshCount = append(resp.AvgRefreshCount, v.RefreshCount*100/players)
  1526. resp.AvgDuelCount = append(resp.AvgDuelCount, v.DuelCount*100/players)
  1527. resp.AvgDuelFreeCount = append(resp.AvgDuelFreeCount, v.DuelFreeCount*100/players)
  1528. resp.AvgDuelBattleCount = append(resp.AvgDuelBattleCount, v.DuelBattleCount*100/players)
  1529. resp.AvgDuelRevengeCount = append(resp.AvgDuelRevengeCount, v.DuelRevengeCount*100/players)
  1530. resp.AvgBattleAdvCount = append(resp.AvgBattleAdvCount, v.BattleAdvCount*100/players)
  1531. resp.AvgBattleDrillCount = append(resp.AvgBattleDrillCount, v.BattleDrillCount*100/players)
  1532. resp.AvgAdjustmentCount = append(resp.AvgAdjustmentCount, v.AdjustmentCount*100/players)
  1533. resp.AvgRevengeCount = append(resp.AvgRevengeCount, v.RevengeCount*100/players)
  1534. resp.AvgRankingCount = append(resp.AvgRankingCount, v.RankingCount*100/players)
  1535. resp.AvgAwardCount = append(resp.AvgAwardCount, v.AwardCount*100/players)
  1536. item := DuelLogItem{
  1537. Day: strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""),
  1538. Players: v.PlayerCount,
  1539. RefreshCount: v.RefreshCount,
  1540. DuelCount: v.DuelCount,
  1541. DuelFreeCount: v.DuelFreeCount,
  1542. DuelBattleCount: v.DuelBattleCount,
  1543. DuelRevengeCount: v.DuelRevengeCount,
  1544. BattleAdvCount: v.BattleAdvCount,
  1545. BattleDrillCount: v.BattleDrillCount,
  1546. AdjustmentCount: v.AdjustmentCount,
  1547. RevengeCount: v.RevengeCount,
  1548. RankingCount: v.RankingCount,
  1549. AwardCount: v.AwardCount,
  1550. AdjustDuel: fmt.Sprintf("%.2f", float64(v.AdjustmentCount)/float64(v.DuelCount)*100) + "%",
  1551. }
  1552. item.AvgRefreshCount = float64(v.RefreshCount*100/players) / 100
  1553. item.AvgDuelCount = float64(v.DuelCount*100/players) / 100
  1554. item.AvgDuelFreeCount = float64(v.DuelFreeCount*100/players) / 100
  1555. item.AvgDuelBattleCount = float64(v.DuelBattleCount*100/players) / 100
  1556. item.AvgDuelRevengeCount = float64(v.DuelRevengeCount*100/players) / 100
  1557. item.AvgBattleAdvCount = float64(v.BattleAdvCount*100/players) / 100
  1558. item.AvgBattleDrillCount = float64(v.BattleDrillCount*100/players) / 100
  1559. item.AvgAdjustmentCount = float64(v.AdjustmentCount*100/players) / 100
  1560. item.AvgRevengeCount = float64(v.RevengeCount*100/players) / 100
  1561. item.AvgRankingCount = float64(v.RankingCount*100/players) / 100
  1562. item.AvgAwardCount = float64(v.AwardCount*100/players) / 100
  1563. respData.Rows = append(respData.Rows, item)
  1564. }
  1565. }
  1566. respData.Info = resp
  1567. return
  1568. }
  1569. type ExpeditionReportInfo struct {
  1570. Days []string `json:"days"`
  1571. Players []int64 `json:"players"`
  1572. BreakCount []int64 `json:"breakCount"`
  1573. SweepCount []int64 `json:"sweepCount"`
  1574. StarsAdvCount []int64 `json:"starsAdvCount"`
  1575. StarsDrillCount []int64 `json:"starsDrillCount"`
  1576. FailData []string `json:"failData"`
  1577. AvgBreakCount []int64 `json:"avgBreakCount"`
  1578. AvgSweepCount []int64 `json:"avgSweepCount"`
  1579. AvgStarsAdvCount []int64 `json:"avgStarsAdvCount"`
  1580. AvgStarsDrillCount []int64 `json:"avgStarsDrillCount"`
  1581. }
  1582. type ExpeditionLogItem struct {
  1583. Day string `json:"days"`
  1584. Players int64 `json:"players"`
  1585. BreakCount int64 `json:"breakCount"`
  1586. SweepCount int64 `json:"sweepCount"`
  1587. StarsAdvCount int64 `json:"starsAdvCount"`
  1588. StarsDrillCount int64 `json:"starsDrillCount"`
  1589. OutputData string `json:"outputData"`
  1590. AvgBreakCount float64 `json:"avgBreakCount"`
  1591. AvgSweepCount float64 `json:"avgSweepCount"`
  1592. AvgStarsAdvCount float64 `json:"avgStarsAdvCount"`
  1593. AvgStarsDrillCount float64 `json:"avgStarsDrillCount"`
  1594. }
  1595. type ExpeditionInfo struct {
  1596. Info ExpeditionReportInfo `json:"info"`
  1597. Rows []ExpeditionLogItem `json:"rows"`
  1598. }
  1599. // QueryExpeditionLog 统计远征信息
  1600. func (s *sDash) QueryExpeditionLog(params forms.ExpeditionReportReq) (respData ExpeditionInfo, err error) {
  1601. resp := ExpeditionReportInfo{}
  1602. rdb := query.Use(config.DB).ReportDayExpedition
  1603. type Fail struct {
  1604. Floor int `json:"floor"`
  1605. Count int `json:"count"`
  1606. }
  1607. type ReportDayExpedition struct {
  1608. model.ReportDayExpedition
  1609. Fail []*Fail
  1610. Output map[int]int
  1611. }
  1612. findFail := func(id int, lst []*Fail) *Fail {
  1613. for _, v := range lst {
  1614. if v.Floor == id {
  1615. return v
  1616. }
  1617. }
  1618. return nil
  1619. }
  1620. var result []*ReportDayExpedition
  1621. m := rdb.Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay)).Order(rdb.Date.Desc())
  1622. switch params.ChannelId {
  1623. case consts.ChannelIdNone:
  1624. // 不选择渠道
  1625. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1626. // 所有的广告渠道
  1627. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1628. default:
  1629. // 指定渠道
  1630. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  1631. }
  1632. if params.ServerId > 0 {
  1633. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  1634. }
  1635. add := func(row *model.ReportDayExpedition) {
  1636. var (
  1637. exist bool
  1638. )
  1639. for _, v := range result {
  1640. if v.Date == row.Date {
  1641. exist = true
  1642. v.PlayerCount += row.PlayerCount
  1643. v.BreakCount += row.BreakCount
  1644. v.SweepCount += row.SweepCount
  1645. v.StarsAdvCount += row.StarsAdvCount
  1646. v.StarsDrillCount += row.StarsDrillCount
  1647. var tempFail []*Fail
  1648. if row.FailData != "" {
  1649. if err = json.Unmarshal([]byte(row.FailData), &tempFail); err != nil {
  1650. logrus.Warnf("FailData Unmarshal err :%+v", err)
  1651. return
  1652. }
  1653. }
  1654. for _, temp := range tempFail {
  1655. cas := findFail(temp.Floor, v.Fail)
  1656. if cas != nil {
  1657. cas.Count += temp.Count
  1658. } else {
  1659. v.Fail = append(v.Fail, &Fail{
  1660. Floor: temp.Floor,
  1661. Count: temp.Count,
  1662. })
  1663. }
  1664. }
  1665. var tempOutput = make(map[int]int)
  1666. if row.OutputData != "" {
  1667. if err = json.Unmarshal([]byte(row.OutputData), &tempOutput); err != nil {
  1668. logrus.Warnf("OutputData Unmarshal err :%+v", err)
  1669. return
  1670. }
  1671. }
  1672. if v.Output == nil {
  1673. v.Output = make(map[int]int)
  1674. }
  1675. for key, value := range tempOutput {
  1676. if _, ok := v.Output[key]; ok {
  1677. v.Output[key] += value
  1678. } else {
  1679. v.Output[key] = value
  1680. }
  1681. }
  1682. }
  1683. }
  1684. if exist == false {
  1685. v := new(ReportDayExpedition)
  1686. v.Date = row.Date
  1687. v.ChannelID = row.ChannelID
  1688. v.PlayerCount += row.PlayerCount
  1689. v.BreakCount += row.BreakCount
  1690. v.SweepCount += row.SweepCount
  1691. v.StarsAdvCount += row.StarsAdvCount
  1692. v.StarsDrillCount += row.StarsDrillCount
  1693. var tempFail []*Fail
  1694. if row.FailData != "" {
  1695. if err = json.Unmarshal([]byte(row.FailData), &tempFail); err != nil {
  1696. logrus.Warnf("FailData2 Unmarshal err :%+v", err)
  1697. return
  1698. }
  1699. }
  1700. for _, temp := range tempFail {
  1701. cas := findFail(temp.Floor, v.Fail)
  1702. if cas != nil {
  1703. cas.Count += temp.Count
  1704. } else {
  1705. v.Fail = append(v.Fail, &Fail{
  1706. Floor: temp.Floor,
  1707. Count: temp.Count,
  1708. })
  1709. }
  1710. }
  1711. var tempOutput = make(map[int]int)
  1712. if row.OutputData != "" {
  1713. if err = json.Unmarshal([]byte(row.OutputData), &tempOutput); err != nil {
  1714. logrus.Warnf("OutputData2 Unmarshal err :%+v", err)
  1715. return
  1716. }
  1717. }
  1718. if v.Output == nil {
  1719. v.Output = make(map[int]int)
  1720. }
  1721. for key, value := range tempOutput {
  1722. if _, ok := v.Output[key]; ok {
  1723. v.Output[key] += value
  1724. } else {
  1725. v.Output[key] = value
  1726. }
  1727. }
  1728. result = append(result, v)
  1729. }
  1730. }
  1731. distributes, err := m.Find()
  1732. if err != nil {
  1733. return
  1734. }
  1735. for _, distribute := range distributes {
  1736. add(distribute)
  1737. }
  1738. for _, v := range result {
  1739. if v != nil {
  1740. failData, err := json.Marshal(v.Fail)
  1741. if err != nil {
  1742. logrus.Warnf("failData Marshal failed:%+v", err)
  1743. return respData, err
  1744. }
  1745. v.FailData = string(failData)
  1746. outputData, err := json.Marshal(v.Output)
  1747. if err != nil {
  1748. logrus.Warnf("outputData Marshal failed:%+v", err)
  1749. return respData, err
  1750. }
  1751. v.OutputData = string(outputData)
  1752. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  1753. resp.Players = append(resp.Players, v.PlayerCount)
  1754. resp.BreakCount = append(resp.BreakCount, v.BreakCount)
  1755. resp.SweepCount = append(resp.SweepCount, v.SweepCount)
  1756. resp.StarsAdvCount = append(resp.StarsAdvCount, v.StarsAdvCount)
  1757. resp.StarsDrillCount = append(resp.StarsDrillCount, v.StarsDrillCount)
  1758. resp.FailData = append(resp.FailData, v.FailData)
  1759. var players int64 = 1
  1760. if v.PlayerCount > 1 {
  1761. players = v.PlayerCount
  1762. }
  1763. resp.AvgBreakCount = append(resp.AvgBreakCount, v.BreakCount*100/players)
  1764. resp.AvgSweepCount = append(resp.AvgSweepCount, v.SweepCount*100/players)
  1765. resp.AvgStarsAdvCount = append(resp.AvgStarsAdvCount, v.StarsAdvCount*100/players)
  1766. resp.AvgStarsDrillCount = append(resp.AvgStarsDrillCount, v.StarsDrillCount*100/players)
  1767. item := ExpeditionLogItem{
  1768. Day: strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""),
  1769. Players: v.PlayerCount,
  1770. BreakCount: v.BreakCount,
  1771. SweepCount: v.SweepCount,
  1772. StarsAdvCount: v.StarsAdvCount,
  1773. StarsDrillCount: v.StarsDrillCount,
  1774. OutputData: v.OutputData,
  1775. }
  1776. item.AvgBreakCount = float64(v.BreakCount*100/players) / 100
  1777. item.AvgSweepCount = float64(v.SweepCount*100/players) / 100
  1778. item.AvgStarsAdvCount = float64(v.StarsAdvCount*100/players) / 100
  1779. item.AvgStarsDrillCount = float64(v.StarsDrillCount*100/players) / 100
  1780. respData.Rows = append(respData.Rows, item)
  1781. }
  1782. }
  1783. respData.Info = resp
  1784. return
  1785. }
  1786. // QueryExpeditionFloor 获取所有材料
  1787. func (s *sDash) QueryExpeditionFloor(params forms.ExpeditionFloorReq) (resp *model.ReportDayExpedition, err error) {
  1788. q := query.Use(config.DB).ReportDayExpedition
  1789. m := q.Where(q.Date.Eq(params.Day))
  1790. switch params.ChannelId {
  1791. case consts.ChannelIdNone:
  1792. // 不选择渠道
  1793. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1794. // 所有的广告渠道
  1795. m = m.Where(q.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1796. default:
  1797. // 指定渠道
  1798. m = m.Where(q.ChannelID.Eq(params.ChannelId))
  1799. }
  1800. if params.ServerId > 0 {
  1801. m = m.Where(q.ServerID.Eq(int32(params.ServerId)))
  1802. }
  1803. type Fail struct {
  1804. Floor int `json:"floor"`
  1805. Count int `json:"count"`
  1806. }
  1807. var fail []*Fail
  1808. findFail := func(id int, lst []*Fail) *Fail {
  1809. for _, v := range lst {
  1810. if v.Floor == id {
  1811. return v
  1812. }
  1813. }
  1814. return nil
  1815. }
  1816. lists, err := m.Find()
  1817. if err != nil {
  1818. return nil, err
  1819. }
  1820. resp = new(model.ReportDayExpedition)
  1821. for k, row := range lists {
  1822. if k == 0 {
  1823. resp = row
  1824. }
  1825. var tempFail []*Fail
  1826. if row.FailData != "" {
  1827. if err = json.Unmarshal([]byte(row.FailData), &tempFail); err != nil {
  1828. logrus.Warnf("FailData2 Unmarshal err :%+v", err)
  1829. return
  1830. }
  1831. }
  1832. for _, temp := range tempFail {
  1833. cas := findFail(temp.Floor, fail)
  1834. if cas != nil {
  1835. cas.Count += temp.Count
  1836. } else {
  1837. fail = append(fail, &Fail{
  1838. Floor: temp.Floor,
  1839. Count: temp.Count,
  1840. })
  1841. }
  1842. }
  1843. }
  1844. failData, err := json.Marshal(fail)
  1845. if err != nil {
  1846. logrus.Warnf("failData Marshal failed:%+v", err)
  1847. return resp, err
  1848. }
  1849. resp.FailData = string(failData)
  1850. return
  1851. }
  1852. func (s *sDash) QueryGoodsReport(params forms.GoodsReportReq) (resp forms.GoodsReportRespData, err error) {
  1853. var (
  1854. positionIds []int
  1855. p = query.Use(config.DB).ReportDayGoodsBasic
  1856. pm = p.Select(p.PositionID.Distinct())
  1857. )
  1858. if params.ServerId > 0 {
  1859. pm = pm.Where(p.ServerID.Eq(int32(params.ServerId)))
  1860. }
  1861. err = pm.Where(p.Date.Eq(params.Day)).Pluck(p.PositionID, &positionIds)
  1862. if err != nil {
  1863. logrus.WithField("from", "QueryAdvReport Pluck").Error(err)
  1864. return
  1865. }
  1866. for _, positionId := range positionIds {
  1867. rdb := query.Use(config.DB).ReportDayGoodsBasic
  1868. m := rdb.
  1869. Select(rdb.ID, rdb.Date, rdb.PositionID,
  1870. rdb.ShowTimes.Sum().As("show_times"),
  1871. rdb.ClickTimes.Sum().As("click_times"),
  1872. rdb.SuccessTimes.Sum().As("success_times"),
  1873. rdb.ShowUsers.Sum().As("show_users"),
  1874. rdb.ClickUsers.Sum().As("click_users"),
  1875. rdb.SuccessUsers.Sum().As("success_users"),
  1876. ).
  1877. Where(rdb.PositionID.Eq(int64(positionId)), rdb.Date.Eq(params.Day))
  1878. switch params.ChannelId {
  1879. case consts.ChannelIdNone:
  1880. // 不选择渠道
  1881. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  1882. // 所有的广告渠道
  1883. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  1884. default:
  1885. // 指定渠道
  1886. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  1887. }
  1888. if params.ServerId > 0 {
  1889. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  1890. }
  1891. results, err := m.
  1892. Order(rdb.Date.Desc()).
  1893. Group(rdb.Date).
  1894. Limit(params.PerPage).
  1895. Offset((params.Page - 1) * params.PerPage).
  1896. Find() // .Order(rdb.Date.Desc())
  1897. if err != nil {
  1898. return resp, err
  1899. }
  1900. for _, result := range results {
  1901. var comment = "充值"
  1902. if result.PositionID/10000 == 1 {
  1903. comment = "礼包"
  1904. }
  1905. comment += fmt.Sprintf(",商品id(%d)", result.PositionID%1000)
  1906. if result.PositionID%10000/1000 == 2 {
  1907. comment += ",双倍获取"
  1908. }
  1909. resp.Data = append(resp.Data, forms.GoodsReportItem{
  1910. ID: result.ID,
  1911. PositionId: result.PositionID,
  1912. GoodsId: result.PositionID % 1000,
  1913. ShowTimes: result.ShowTimes,
  1914. ClickTimes: result.ClickTimes,
  1915. SuccessTimes: result.SuccessTimes,
  1916. ShowUsers: result.ShowUsers,
  1917. ClickUsers: result.ClickUsers,
  1918. SuccessUsers: result.SuccessUsers,
  1919. Comment: comment,
  1920. })
  1921. }
  1922. resp.Total = int64(len(results))
  1923. if err != nil {
  1924. return resp, err
  1925. }
  1926. }
  1927. return
  1928. }
  1929. type GudongReportInfo struct {
  1930. Days []string `json:"days"`
  1931. Players []int64 `json:"players"`
  1932. Jinglis []int64 `json:"jinglis"`
  1933. AdJinglis []int64 `json:"adJinglis"`
  1934. DiamondJinglis []int64 `json:"diamondJinglis"`
  1935. AdDiamonds []int64 `json:"adDiamonds"`
  1936. RmbDiamonds []int64 `json:"rmbDiamonds"`
  1937. SpeedUps []int64 `json:"speedUps"`
  1938. BoxDetails []string `json:"boxDetails"`
  1939. IdentifyGoods []int64 `json:"identifyGoods"`
  1940. UpgradeGoods []int64 `json:"upgradeGoods"`
  1941. ShowRevenue []int64 `json:"showRevenues"`
  1942. Ranking []int64 `json:"rankings"`
  1943. RankingBenifit []int64 `json:"rankingBenifit"`
  1944. ExchangeGoods []string `json:"exchangeGoods"`
  1945. SellGoods []int64 `json:"sellGoods"`
  1946. GetGoods []int64 `json:"getGoods"`
  1947. AvgJinglis []int64 `json:"avgJinglis"`
  1948. AvgAdJinglis []int64 `json:"avgAdJinglis"`
  1949. AvgDiamondJinglis []int64 `json:"avgDiamondJinglis"`
  1950. AvgRmbJinglis []int64 `json:"avgRmbJinglis"`
  1951. AvgSpeedUps []int64 `json:"avgSpeedUps"`
  1952. AvgBoxDetails []string `json:"avgBoxDetails"`
  1953. AvgIdentifyGoods []int64 `json:"avgIdentifyGoods"`
  1954. AvgUpgradeGoods []int64 `json:"avgUpgradeGoods"`
  1955. AvgShowRevenue []int64 `json:"avgShowRevenues"`
  1956. AvgRanking []int64 `json:"avgRankings"`
  1957. AvgRankingBenifit []int64 `json:"avgRankingBenifit"`
  1958. AvgSellGoods []int64 `json:"avgSellGoods"`
  1959. }
  1960. type GudongLogItem struct {
  1961. Day string `json:"days"`
  1962. Players int64 `json:"players"`
  1963. Jinglis int64 `json:"jinglis"`
  1964. AdJinglis int64 `json:"adJinglis"`
  1965. DiamondJinglis int64 `json:"diamondJinglis"`
  1966. AdDiamonds int64 `json:"adDiamonds"`
  1967. RmbDiamonds int64 `json:"rmbDiamonds"`
  1968. SpeedUps int64 `json:"speedUps"`
  1969. BoxDetails string `json:"boxDetails"`
  1970. IdentifyGoods int64 `json:"identifyGoods"`
  1971. UpgradeGoods int64 `json:"upgradeGoods"`
  1972. ShowRevenue int64 `json:"showRevenues"`
  1973. Ranking int64 `json:"rankings"`
  1974. RankingBenifit int64 `json:"rankingBenifit"`
  1975. ExchangeGoods string `json:"exchangeGoods"`
  1976. SellGoods int64 `json:"sellGoods"`
  1977. GetGoods int64 `json:"getGoods"`
  1978. GetGoodsDetail string `json:"getGoodsDetail"`
  1979. AvgJinglis float64 `json:"avgJinglis"`
  1980. AvgAdJinglis float64 `json:"avgAdJinglis"`
  1981. AvgDiamondJinglis float64 `json:"avgDiamondJinglis"`
  1982. AvgAdDiamonds float64 `json:"avgAdDiamonds"`
  1983. AvgRmbDiamonds float64 `json:"avgRmbDiamonds"`
  1984. AvgSpeedUps float64 `json:"avgSpeedUps"`
  1985. AvgBoxDetails float64 `json:"avgBoxDetails"`
  1986. AvgIdentifyGoods float64 `json:"avgIdentifyGoods"`
  1987. AvgUpgradeGoods float64 `json:"avgUpgradeGoods"`
  1988. AvgShowRevenue float64 `json:"avgShowRevenues"`
  1989. AvgRanking float64 `json:"avgRankings"`
  1990. AvgRankingBenifit float64 `json:"avgRankingBenifit"`
  1991. AvgSellGoods float64 `json:"avgSellGoods"`
  1992. AvgGetGoods float64 `json:"avgGetGoods"`
  1993. }
  1994. type GudongInfo struct {
  1995. Info GudongReportInfo `json:"info"`
  1996. Rows []GudongLogItem `json:"rows"`
  1997. }
  1998. // 查询古董的统计信息
  1999. func (s *sDash) QueryGudongLog(params forms.GoodsReportReq) (respData GudongInfo, err error) {
  2000. resp := GudongReportInfo{}
  2001. rdb := query.Use(config.DB).ReportDayGudong
  2002. type Goods map[string]int
  2003. type ReportDayGudong struct {
  2004. model.ReportDayGudong
  2005. Exchange Goods
  2006. SpeedUp Goods
  2007. Detail Goods
  2008. }
  2009. findGoods := func(id string, lst Goods) bool {
  2010. for k, _ := range lst {
  2011. if k == id {
  2012. return true
  2013. }
  2014. }
  2015. return false
  2016. }
  2017. var result []*ReportDayGudong
  2018. m := rdb.Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay)).Order(rdb.Date.Desc())
  2019. switch params.ChannelId {
  2020. case consts.ChannelIdNone:
  2021. // 不选择渠道
  2022. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2023. // 所有的广告渠道
  2024. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2025. default:
  2026. // 指定渠道
  2027. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2028. }
  2029. if params.ServerId > 0 {
  2030. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2031. }
  2032. add := func(row *model.ReportDayGudong) {
  2033. var (
  2034. exist bool
  2035. )
  2036. for _, v := range result {
  2037. if v.Date == row.Date {
  2038. exist = true
  2039. v.PlayerCount += row.PlayerCount
  2040. v.JingliCount += row.JingliCount
  2041. v.AdJingli += row.AdJingli
  2042. v.DiamondJingli += row.DiamondJingli
  2043. v.AdDiamond += row.AdDiamond
  2044. v.RmbDiamond += row.RmbDiamond
  2045. v.SpeedUpBoxDetail += row.SpeedUpBoxDetail
  2046. v.IdentifyGoods += row.IdentifyGoods
  2047. v.UpgradeGoods += row.UpgradeGoods
  2048. v.ShowRevenue += row.ShowRevenue
  2049. v.Ranking += row.Ranking
  2050. v.RankingBenifit += row.RankingBenifit
  2051. v.SpeedUpBox += row.SpeedUpBox
  2052. v.SellGoods += row.SellGoods
  2053. v.GetGoods += row.GetGoods
  2054. var tempExchange Goods
  2055. if row.ExchangeGoods != "" && row.ExchangeGoods != "null" {
  2056. if err = json.Unmarshal([]byte(row.ExchangeGoods), &tempExchange); err != nil {
  2057. logrus.Errorf("ExchangeGoods Unmarshal err :%+v", err)
  2058. return
  2059. }
  2060. }
  2061. for eventId, num := range tempExchange {
  2062. if findGoods(eventId, v.Exchange) {
  2063. v.Exchange[eventId] += num
  2064. } else {
  2065. v.Exchange = Goods{eventId: num}
  2066. }
  2067. }
  2068. var tempSpeedUp Goods
  2069. if row.SpeedUpBoxDetail != "" && row.SpeedUpBoxDetail != "null" {
  2070. if err = json.Unmarshal([]byte(row.SpeedUpBoxDetail), &tempSpeedUp); err != nil {
  2071. logrus.Warnf("SpeedUpBoxDetail Unmarshal err :%+v", err)
  2072. return
  2073. }
  2074. }
  2075. for eventId, num := range tempSpeedUp {
  2076. if findGoods(eventId, v.SpeedUp) {
  2077. v.SpeedUp[eventId] += num
  2078. } else {
  2079. v.SpeedUp = Goods{eventId: num}
  2080. }
  2081. }
  2082. var tempDetail Goods
  2083. if row.GetGoodsDetail != "" && row.GetGoodsDetail != "null" {
  2084. if err = json.Unmarshal([]byte(row.GetGoodsDetail), &tempDetail); err != nil {
  2085. logrus.Warnf("GetGoodsDetail Unmarshal err :%+v", err)
  2086. return
  2087. }
  2088. }
  2089. for eventId, num := range tempDetail {
  2090. if findGoods(eventId, v.Detail) {
  2091. v.Detail[eventId] += num
  2092. } else {
  2093. v.Detail = Goods{eventId: num}
  2094. }
  2095. }
  2096. }
  2097. }
  2098. if exist == false {
  2099. v := new(ReportDayGudong)
  2100. v.Date = row.Date
  2101. v.ChannelID = row.ChannelID
  2102. v.PlayerCount += row.PlayerCount
  2103. v.JingliCount += row.JingliCount
  2104. v.AdJingli += row.AdJingli
  2105. v.DiamondJingli += row.DiamondJingli
  2106. v.AdDiamond += row.AdDiamond
  2107. v.RmbDiamond += row.RmbDiamond
  2108. v.SpeedUpBoxDetail += row.SpeedUpBoxDetail
  2109. v.IdentifyGoods += row.IdentifyGoods
  2110. v.UpgradeGoods += row.UpgradeGoods
  2111. v.ShowRevenue += row.ShowRevenue
  2112. v.Ranking += row.Ranking
  2113. v.RankingBenifit += row.RankingBenifit
  2114. v.SpeedUpBox += row.SpeedUpBox
  2115. v.SellGoods += row.SellGoods
  2116. v.GetGoods += row.GetGoods
  2117. var tempExchange Goods
  2118. if row.ExchangeGoods != "" && row.ExchangeGoods != "null" {
  2119. if err = json.Unmarshal([]byte(row.ExchangeGoods), &tempExchange); err != nil {
  2120. logrus.Warnf("ExchangeGoods Unmarshal err2 :%+v", err)
  2121. return
  2122. }
  2123. }
  2124. for eventId, num := range tempExchange {
  2125. if findGoods(eventId, v.Exchange) {
  2126. v.Exchange[eventId] += num
  2127. } else {
  2128. v.Exchange = Goods{eventId: num}
  2129. }
  2130. }
  2131. var tempSpeedUp Goods
  2132. if row.SpeedUpBoxDetail != "" && row.SpeedUpBoxDetail != "null" {
  2133. if err = json.Unmarshal([]byte(row.SpeedUpBoxDetail), &tempSpeedUp); err != nil {
  2134. logrus.Warnf("SpeedUpBoxDetail Unmarshal err :%+v", err)
  2135. return
  2136. }
  2137. }
  2138. for eventId, num := range tempSpeedUp {
  2139. if findGoods(eventId, v.SpeedUp) {
  2140. v.SpeedUp[eventId] += num
  2141. } else {
  2142. v.SpeedUp = Goods{eventId: num}
  2143. }
  2144. }
  2145. var tempDetail Goods
  2146. if row.GetGoodsDetail != "" && row.GetGoodsDetail != "null" {
  2147. if err = json.Unmarshal([]byte(row.GetGoodsDetail), &tempDetail); err != nil {
  2148. logrus.Warnf("GetGoodsDetail Unmarshal err :%+v", err)
  2149. return
  2150. }
  2151. }
  2152. for eventId, num := range tempDetail {
  2153. if findGoods(eventId, v.Detail) {
  2154. v.Detail[eventId] += num
  2155. } else {
  2156. v.Detail = Goods{eventId: num}
  2157. }
  2158. }
  2159. result = append(result, v)
  2160. }
  2161. }
  2162. distributes, err := m.Find()
  2163. if err != nil {
  2164. return
  2165. }
  2166. for _, distribute := range distributes {
  2167. add(distribute)
  2168. }
  2169. for _, v := range result {
  2170. if v != nil {
  2171. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  2172. resp.Players = append(resp.Players, v.PlayerCount)
  2173. resp.Jinglis = append(resp.Jinglis, v.JingliCount)
  2174. resp.AdJinglis = append(resp.AdJinglis, v.AdJingli)
  2175. resp.DiamondJinglis = append(resp.DiamondJinglis, v.DiamondJingli)
  2176. resp.AdDiamonds = append(resp.AdDiamonds, v.AdDiamond)
  2177. resp.RmbDiamonds = append(resp.RmbDiamonds, v.RmbDiamond)
  2178. resp.SpeedUps = append(resp.SpeedUps, v.SpeedUpBox)
  2179. // 加速宝箱
  2180. speedUpBoxDetail, err := json.Marshal(v.SpeedUp)
  2181. if err != nil {
  2182. logrus.Warnf("speedUpBoxDetail Marshal failed:%+v", err)
  2183. return respData, err
  2184. }
  2185. v.SpeedUpBoxDetail = string(speedUpBoxDetail)
  2186. resp.BoxDetails = append(resp.BoxDetails, v.SpeedUpBoxDetail)
  2187. // 兑换
  2188. exchange, err := json.Marshal(v.Exchange)
  2189. if err != nil {
  2190. logrus.Warnf("Exchange Marshal failed:%+v", err)
  2191. return respData, err
  2192. }
  2193. v.ExchangeGoods = string(exchange)
  2194. resp.BoxDetails = append(resp.BoxDetails, v.ExchangeGoods)
  2195. // 获取物品
  2196. detail, err := json.Marshal(v.Detail)
  2197. if err != nil {
  2198. logrus.Warnf("Detail Marshal failed:%+v", err)
  2199. return respData, err
  2200. }
  2201. v.GetGoodsDetail = string(detail)
  2202. resp.IdentifyGoods = append(resp.IdentifyGoods, v.IdentifyGoods)
  2203. resp.UpgradeGoods = append(resp.UpgradeGoods, v.UpgradeGoods)
  2204. resp.ShowRevenue = append(resp.ShowRevenue, v.ShowRevenue)
  2205. resp.Ranking = append(resp.Ranking, v.Ranking)
  2206. resp.RankingBenifit = append(resp.RankingBenifit, v.RankingBenifit)
  2207. resp.SellGoods = append(resp.SellGoods, v.SellGoods)
  2208. resp.GetGoods = append(resp.GetGoods, v.GetGoods)
  2209. var players int64 = 1
  2210. if v.PlayerCount > 1 {
  2211. players = v.PlayerCount
  2212. }
  2213. exchangeInfo := strings.ReplaceAll(v.ExchangeGoods, "Event", "")
  2214. exchangeInfo = strings.ReplaceAll(exchangeInfo, `"`, "")
  2215. item := GudongLogItem{
  2216. Day: strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""),
  2217. Players: v.PlayerCount,
  2218. Jinglis: v.JingliCount,
  2219. AdJinglis: v.AdJingli,
  2220. DiamondJinglis: v.DiamondJingli,
  2221. AdDiamonds: v.AdDiamond,
  2222. RmbDiamonds: v.RmbDiamond,
  2223. SpeedUps: v.SpeedUpBox,
  2224. BoxDetails: v.SpeedUpBoxDetail,
  2225. IdentifyGoods: v.IdentifyGoods,
  2226. UpgradeGoods: v.UpgradeGoods,
  2227. ShowRevenue: v.ShowRevenue,
  2228. Ranking: v.Ranking,
  2229. RankingBenifit: v.RankingBenifit,
  2230. ExchangeGoods: exchangeInfo,
  2231. SellGoods: v.SellGoods,
  2232. GetGoods: v.GetGoods,
  2233. }
  2234. item.GetGoodsDetail = strings.ReplaceAll(v.GetGoodsDetail, "Event", "")
  2235. item.GetGoodsDetail = strings.ReplaceAll(item.GetGoodsDetail, `"`, "")
  2236. item.AvgJinglis = float64(v.JingliCount*100/players) / 100
  2237. item.AvgAdJinglis = float64(v.AdJingli*100/players) / 100
  2238. item.AvgDiamondJinglis = float64(v.DiamondJingli*100/players) / 100
  2239. item.AvgAdDiamonds = float64(v.AdDiamond*100/players) / 100
  2240. item.AvgRmbDiamonds = float64(v.RmbDiamond*100/players) / 100
  2241. item.AvgUpgradeGoods = float64(v.UpgradeGoods*100/players) / 100
  2242. item.AvgSpeedUps = float64(v.SpeedUpBox*100/players) / 100
  2243. item.AvgIdentifyGoods = float64(v.IdentifyGoods*100/players) / 100
  2244. item.AvgShowRevenue = float64(int64(v.ShowRevenue)*100/int64(players)) / 100
  2245. item.AvgRanking = float64(v.Ranking*100/players) / 100
  2246. item.AvgRankingBenifit = float64(v.RankingBenifit*100/players) / 100
  2247. item.AvgSellGoods = float64(v.SellGoods*100/players) / 100
  2248. item.AvgGetGoods = float64(v.GetGoods*100/players) / 100
  2249. respData.Rows = append(respData.Rows, item)
  2250. }
  2251. }
  2252. respData.Info = resp
  2253. return
  2254. }
  2255. var (
  2256. LoginSuccessEventId = consts.LoginEventEntryPack // 获取到entryPack // 4.22前是接收到http登录成功后
  2257. )
  2258. type CountResult struct {
  2259. Count int64
  2260. }
  2261. type UserIdResult struct {
  2262. UserId int64
  2263. }
  2264. func (s *sDash) GetNewUserCount(serverId int, begin, end int64) (count int64) {
  2265. var result CountResult
  2266. loginModel := &model.LoginLog{}
  2267. logDB := model.TableOfYearMonth(loginModel.TableName(), time.Now())(config.DB)
  2268. loginQuery := query.Use(logDB).LoginLog.Table(logDB.Statement.Table)
  2269. loginQueryDo := loginQuery.Select(loginQuery.UserID.Distinct().Count().As("count")).Where(loginQuery.UserCreatedAt.Between(int32(begin), int32(end)))
  2270. if serverId > 0 {
  2271. loginQueryDo = loginQueryDo.Where(loginQuery.ServerID.Eq(int32(serverId)))
  2272. }
  2273. err := loginQueryDo.Scan(&result)
  2274. if err != nil {
  2275. logrus.Errorf("GetNewUserCount err: %v", err)
  2276. return 0
  2277. }
  2278. return result.Count
  2279. }
  2280. func (s *sDash) GetActiveUserCount(serverId int, ubegin, uend, begin, end int64) (count int64) {
  2281. var result []UserIdResult
  2282. loginModel := &model.LoginLog{}
  2283. logDB := model.TableOfYearMonth(loginModel.TableName(), time.Now())(config.DB)
  2284. loginQuery := query.Use(logDB).LoginLog.Table(logDB.Statement.Table)
  2285. loginQueryDo := loginQuery.Select(loginQuery.UserID).
  2286. Where(loginQuery.EventID.Eq(int32(LoginSuccessEventId)), loginQuery.EventAt.Between(int32(begin), int32(end)))
  2287. if ubegin != 0 && uend != 0 {
  2288. loginQueryDo = loginQueryDo.Where(loginQuery.UserCreatedAt.Between(int32(ubegin), int32(uend)))
  2289. }
  2290. if serverId != 0 {
  2291. loginQueryDo = loginQueryDo.Where(loginQuery.ServerID.Eq(int32(serverId)))
  2292. }
  2293. err := loginQueryDo.Group(loginQuery.UserID).Having(loginQuery.ID.Sum().Gt(1)).Scan(&result)
  2294. if err != nil {
  2295. logrus.Errorf("GetNewUserCount err: %v", err)
  2296. }
  2297. //if ubegin == 0 && uend == 0 {
  2298. // sql := fmt.Sprintf("select user_id from login_logs where event_id = %d and event_at >= %d and event_at <= %d", LoginSuccessEventId, begin, end)
  2299. // if serverId > 0 {
  2300. // sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2301. // }
  2302. // sql = sql + " group by user_id having sum(1)>=1"
  2303. // tx = config.DB.Model(&model.LoginLog{}).Raw(sql).Scan(&result)
  2304. //} else {
  2305. // sql := fmt.Sprintf("select user_id from login_logs where user_created_at >= %d and user_created_at <= %d and event_id = %d and event_at >= %d and event_at <= %d", ubegin, uend, LoginSuccessEventId, begin, end)
  2306. // if serverId > 0 {
  2307. // sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2308. // }
  2309. // sql = sql + " group by user_id having sum(1)>=1"
  2310. // tx = config.DB.Model(&model.LoginLog{}).Raw(sql).Scan(&result)
  2311. //}
  2312. //if tx.Error != nil {
  2313. // logrus.Errorf("GetNewUserCount err: %v", tx.Error)
  2314. //}
  2315. return int64(len(result))
  2316. }
  2317. func (s *sDash) GetGuideFinishedCount(serverId int, ubegin, uend, begin, end int64, channelId string) (count int64) {
  2318. var result CountResult
  2319. if channelId != "" {
  2320. sql := fmt.Sprintf("select count(distinct(user_id)) as count from chapter_logs_0 LEFT JOIN player_channel ON chapter_logs_0.user_id = player_channel.playerid where event_at >= %d and event_at <= %d and user_created_at >= %d and user_created_at <= %d and event_id = 11 and player_channel.channel_id = '%v' ",
  2321. ubegin, uend, begin, end, channelId)
  2322. if serverId > 0 {
  2323. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2324. }
  2325. tx := config.DB.Model(&model.ChapterLog{}).Raw(sql).Scan(&result)
  2326. if tx.Error != nil {
  2327. logrus.Errorf("GetNewUserCount err: %v", tx.Error)
  2328. return 0
  2329. }
  2330. } else {
  2331. sql := fmt.Sprintf("select count(distinct(user_id)) as count from chapter_logs_0 where event_at >= %d and event_at <= %d and user_created_at >= %d and user_created_at <= %d and event_id = 11", ubegin, uend, begin, end)
  2332. if serverId > 0 {
  2333. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2334. }
  2335. tx := config.DB.Model(&model.ChapterLog{}).Raw(sql).Scan(&result)
  2336. if tx.Error != nil {
  2337. logrus.Errorf("GetNewUserCount2 err: %v", tx.Error)
  2338. return 0
  2339. }
  2340. }
  2341. return result.Count
  2342. }
  2343. type DurationResult struct {
  2344. Count int64
  2345. }
  2346. func (s *sDash) GetUserOnlineDuration(serverId int, ubegin, uend, begin, end int64) int64 {
  2347. var result DurationResult
  2348. var tx *gorm.DB
  2349. if ubegin == 0 && uend == 0 {
  2350. sql := fmt.Sprintf("select sum(duration) as count from online_duration_logs where logout_at >= %d and logout_at <= %d", begin, end)
  2351. if serverId > 0 {
  2352. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2353. }
  2354. tx = config.DB.Model(&model.LoginLog{}).Raw(sql).Scan(&result)
  2355. } else {
  2356. sql := fmt.Sprintf("select sum(duration) as count from online_duration_logs where user_created_at >= %d and user_created_at <= %d and logout_at >= %d and logout_at <= %d", ubegin, uend, begin, end)
  2357. if serverId > 0 {
  2358. sql = fmt.Sprintf("%v and server_id = %v", sql, serverId)
  2359. }
  2360. tx = config.DB.Model(&model.LoginLog{}).Raw(sql).Scan(&result)
  2361. }
  2362. if tx.Error != nil {
  2363. logrus.Errorf("GetNewUserCount err: %v", tx.Error)
  2364. }
  2365. return result.Count
  2366. }
  2367. func (s *sDash) GetOnlineNums(serverId int) (int64, int64) {
  2368. type stampResult struct {
  2369. Stamp int64
  2370. Count int64
  2371. }
  2372. var result stampResult
  2373. tx := config.DB.Raw(fmt.Sprintf("select stamp, sum(nums) as count from online_nums_logs where server_id = %v group by stamp order by stamp desc limit 1", serverId)).Scan(&result)
  2374. if tx.Error != nil {
  2375. logrus.Errorf("GetOnlineNums err:%v", tx.Error)
  2376. return result.Stamp, result.Count
  2377. }
  2378. return result.Stamp, result.Count
  2379. }
  2380. func (s *sDash) QueryBasicInfo(serverId int, day string) (resp []forms.BasicInfoItem, err error) {
  2381. stamp, nums := s.GetOnlineNums(serverId)
  2382. var onlineTips = "当前在线人数"
  2383. if stamp > 0 {
  2384. onlineTips = fmt.Sprintf("当前在线人数(%s)", utility.FormatSecond(time.Unix(stamp, 0)))
  2385. }
  2386. resp = append(resp, forms.BasicInfoItem{
  2387. ID: 0,
  2388. Name: onlineTips,
  2389. Count: int64(nums),
  2390. })
  2391. begin, end := now.BeginningOfDay().Unix(), now.EndOfDay().Unix()
  2392. newCount := s.GetNewUserCount(serverId, begin, end)
  2393. newActive := s.GetActiveUserCount(serverId, 0, 0, begin, end)
  2394. newActiveDuration := s.GetUserOnlineDuration(serverId, begin, end, begin, end)
  2395. activeDuration := s.GetUserOnlineDuration(serverId, 0, 0, begin, end)
  2396. if activeDuration < 1 {
  2397. activeDuration = 1
  2398. }
  2399. resp = append(resp, forms.BasicInfoItem{ID: 1, Name: "今日新增玩家", Count: int64(newCount)})
  2400. resp = append(resp, forms.BasicInfoItem{ID: 2, Name: "新增玩家时长", Count: newActiveDuration})
  2401. resp = append(resp, forms.BasicInfoItem{ID: 3, Name: "活跃玩家", Count: int64(newActive)})
  2402. resp = append(resp, forms.BasicInfoItem{ID: 4, Name: "活跃玩家时长(分钟)", Count: activeDuration / 60})
  2403. return
  2404. }
  2405. func (s *sDash) QueryBasicReport(ctx *gin.Context, params forms.BasicReportReq) (resp forms.BasicReportRespData, err error) {
  2406. rdb := query.Use(config.DB).ReportDayBasic
  2407. m := rdb.
  2408. Select(rdb.ALL,
  2409. rdb.OldCount.Sum().As("old_count"),
  2410. rdb.NewCount.Sum().As("new_count"),
  2411. rdb.ValidCount.Sum().As("valid_count"),
  2412. rdb.ActiveCount.Sum().As("active_count"),
  2413. rdb.NewDuration.Sum().As("new_duration"),
  2414. rdb.ValidDuration.Sum().As("valid_duration"),
  2415. rdb.ActiveDuration.Sum().As("active_duration"),
  2416. rdb.Active1Day.Sum().As("active_1_day"),
  2417. rdb.Active2Day.Sum().As("active_2_day"),
  2418. rdb.Active3Day.Sum().As("active_3_day"),
  2419. rdb.Active4Day.Sum().As("active_4_day"),
  2420. rdb.Active5Day.Sum().As("active_5_day"),
  2421. rdb.Active6Day.Sum().As("active_6_day"),
  2422. rdb.Active7Day.Sum().As("active_7_day"),
  2423. rdb.Active14Day.Sum().As("active_14_day"),
  2424. rdb.Active30Day.Sum().As("active_30_day"),
  2425. )
  2426. switch params.ChannelId {
  2427. case consts.ChannelIdNone:
  2428. // 不选择渠道
  2429. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2430. // 所有的广告渠道
  2431. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2432. default:
  2433. // 指定渠道
  2434. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2435. }
  2436. if params.ServerId > 0 {
  2437. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2438. }
  2439. if params.Flag > -1 {
  2440. m = m.Where(rdb.Flag.Eq(params.Flag))
  2441. }
  2442. // 导出
  2443. if params.IsExport == 1 {
  2444. results, err := m.Order(rdb.Date.Desc()).Group(rdb.Date).Find()
  2445. if err != nil {
  2446. return resp, err
  2447. }
  2448. f := excelize.NewFile()
  2449. f.SetColWidth("Sheet1", "A", "A", 15)
  2450. f.SetColWidth("Sheet1", "B", "B", 12)
  2451. f.SetColWidth("Sheet1", "C", "C", 12)
  2452. f.SetColWidth("Sheet1", "D", "D", 12)
  2453. f.SetColWidth("Sheet1", "E", "E", 20)
  2454. f.SetColWidth("Sheet1", "F", "F", 20)
  2455. f.SetColWidth("Sheet1", "G", "G", 20)
  2456. f.SetColWidth("Sheet1", "H", "H", 20)
  2457. f.SetColWidth("Sheet1", "I", "I", 20)
  2458. f.SetColWidth("Sheet1", "J", "J", 20)
  2459. f.SetColWidth("Sheet1", "K", "K", 20)
  2460. f.SetColWidth("Sheet1", "L", "L", 20)
  2461. f.SetColWidth("Sheet1", "M", "M", 12)
  2462. f.SetColWidth("Sheet1", "N", "N", 12)
  2463. f.SetColWidth("Sheet1", "O", "O", 12)
  2464. f.SetColWidth("Sheet1", "P", "P", 12)
  2465. f.SetColWidth("Sheet1", "Q", "Q", 12)
  2466. f.SetColWidth("Sheet1", "R", "R", 12)
  2467. f.SetColWidth("Sheet1", "S", "S", 12)
  2468. f.SetColWidth("Sheet1", "T", "T", 12)
  2469. f.SetColWidth("Sheet1", "U", "U", 12)
  2470. // 创建一个工作表
  2471. f.SetCellValue("Sheet1", "A1", "日期")
  2472. f.SetCellValue("Sheet1", "B1", "新增")
  2473. f.SetCellValue("Sheet1", "C1", "有效新增")
  2474. f.SetCellValue("Sheet1", "D1", "活跃")
  2475. f.SetCellValue("Sheet1", "E1", "新增时长(分钟) ")
  2476. f.SetCellValue("Sheet1", "F1", "人均新增时长(分钟)")
  2477. f.SetCellValue("Sheet1", "G1", "有效新增时长(分钟)")
  2478. f.SetCellValue("Sheet1", "H1", "人均有效新增时长(分钟)")
  2479. f.SetCellValue("Sheet1", "I1", "老玩家时长(分钟)")
  2480. f.SetCellValue("Sheet1", "J1", "人均老玩家时长(分钟)")
  2481. f.SetCellValue("Sheet1", "K1", "总时长(分钟) ")
  2482. f.SetCellValue("Sheet1", "L1", "人均总时长(分钟) ")
  2483. f.SetCellValue("Sheet1", "M1", "次留率")
  2484. f.SetCellValue("Sheet1", "N1", "2日留")
  2485. f.SetCellValue("Sheet1", "O1", "3日留")
  2486. f.SetCellValue("Sheet1", "P1", "4日留")
  2487. f.SetCellValue("Sheet1", "Q1", "5日留")
  2488. f.SetCellValue("Sheet1", "R1", "6日留")
  2489. f.SetCellValue("Sheet1", "S1", "7日留")
  2490. f.SetCellValue("Sheet1", "T1", "14日留")
  2491. f.SetCellValue("Sheet1", "U1", "30日留")
  2492. for i, result := range results {
  2493. date, _ := now.ParseInLocation(time.Local, result.Date)
  2494. // 日期
  2495. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), date.Format("2006-01-02"))
  2496. // 新增
  2497. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), result.NewCount)
  2498. // 有效新增
  2499. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), result.ValidCount)
  2500. // 活跃
  2501. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), result.ActiveCount)
  2502. // 新增时长(分钟)
  2503. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), result.NewDuration/60)
  2504. // 人均新增时长(分钟)
  2505. if result.NewCount == 0 {
  2506. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i+2), "0")
  2507. } else {
  2508. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i+2), utility.Round(float64(result.NewDuration)/float64(result.NewCount)/60))
  2509. }
  2510. // 有效新增时长(分钟)
  2511. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", i+2), result.ValidDuration/60)
  2512. // 人均有效新增时长(分钟)
  2513. if result.ValidCount == 0 {
  2514. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i+2), "0")
  2515. } else {
  2516. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i+2), utility.Round(float64(result.ValidDuration)/float64(result.ValidCount)/60))
  2517. }
  2518. // 老玩家时长(分钟)
  2519. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i+2), (result.ActiveDuration-result.NewDuration)/60)
  2520. // 人均老玩家时长(分钟)
  2521. if result.ActiveDuration < 1 || result.NewDuration < 1 || result.ActiveCount-result.NewCount < 1 {
  2522. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i+2), "0")
  2523. } else {
  2524. logrus.Warnf("result:%+v", result)
  2525. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i+2), utility.Round(float64(result.ActiveDuration-result.NewDuration)/float64(result.ActiveCount-result.NewCount)/60))
  2526. }
  2527. // 总时长(分钟)
  2528. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", i+2), result.ActiveDuration/60)
  2529. // 人均总时长(分钟)
  2530. if result.ActiveDuration < 1 || result.ActiveCount < 1 {
  2531. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", i+2), "0")
  2532. } else {
  2533. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", i+2), utility.Round(float64(result.ActiveDuration)/float64(result.ActiveCount)/60))
  2534. }
  2535. // 次留率
  2536. if result.Active1Day < 1 || result.NewCount < 1 {
  2537. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", i+2), "0")
  2538. } else {
  2539. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active1Day)/float64(result.NewCount)*100), "%"))
  2540. }
  2541. // 2日留
  2542. if result.Active2Day < 1 || result.NewCount < 1 {
  2543. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", i+2), "0")
  2544. } else {
  2545. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active2Day)/float64(result.NewCount)*100), "%"))
  2546. }
  2547. // 3日留
  2548. if result.Active3Day < 1 || result.NewCount < 1 {
  2549. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", i+2), "0")
  2550. } else {
  2551. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active3Day)/float64(result.NewCount)*100), "%"))
  2552. }
  2553. // 4日留
  2554. if result.Active4Day < 1 || result.NewCount < 1 {
  2555. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", i+2), "0")
  2556. } else {
  2557. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active4Day)/float64(result.NewCount)*100), "%"))
  2558. }
  2559. // 5日留
  2560. if result.Active5Day < 1 || result.NewCount < 1 {
  2561. f.SetCellValue("Sheet1", fmt.Sprintf("Q%d", i+2), "0")
  2562. } else {
  2563. f.SetCellValue("Sheet1", fmt.Sprintf("Q%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active5Day)/float64(result.NewCount)*100), "%"))
  2564. }
  2565. // 6日留
  2566. if result.Active6Day < 1 || result.NewCount < 1 {
  2567. f.SetCellValue("Sheet1", fmt.Sprintf("R%d", i+2), "0")
  2568. } else {
  2569. f.SetCellValue("Sheet1", fmt.Sprintf("R%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active6Day)/float64(result.NewCount)*100), "%"))
  2570. }
  2571. // 7日留
  2572. if result.Active7Day < 1 || result.NewCount < 1 {
  2573. f.SetCellValue("Sheet1", fmt.Sprintf("S%d", i+2), "0")
  2574. } else {
  2575. f.SetCellValue("Sheet1", fmt.Sprintf("S%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active7Day)/float64(result.NewCount)*100), "%"))
  2576. }
  2577. // 14日留
  2578. if result.Active14Day < 1 || result.NewCount < 1 {
  2579. f.SetCellValue("Sheet1", fmt.Sprintf("T%d", i+2), "0")
  2580. } else {
  2581. f.SetCellValue("Sheet1", fmt.Sprintf("T%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active14Day)/float64(result.NewCount)*100), "%"))
  2582. }
  2583. // 30日留
  2584. if result.Active30Day < 1 || result.NewCount < 1 {
  2585. f.SetCellValue("Sheet1", fmt.Sprintf("U%d", i+2), "0")
  2586. } else {
  2587. f.SetCellValue("Sheet1", fmt.Sprintf("U%d", i+2), fmt.Sprintf("%v%s", utility.Round(float64(result.Active30Day)/float64(result.NewCount)*100), "%"))
  2588. }
  2589. }
  2590. // 设置工作簿的默认工作表
  2591. f.SetActiveSheet(1)
  2592. ctx.Header("Content-Type", "application/vnd.ms-excel")
  2593. ctx.Header("Content-Disposition", fmt.Sprintf("attachment;filename=日统计导出%s.xlsx", time.Now().Format("20060102150405")))
  2594. f.WriteTo(ctx.Writer)
  2595. return resp, err
  2596. }
  2597. results, err := m.Order(rdb.Date.Desc()).
  2598. Group(rdb.Date).
  2599. Limit(int(params.PerPage)).
  2600. Offset(int((params.Page - 1) * params.PerPage)).
  2601. Find()
  2602. if err != nil {
  2603. return resp, err
  2604. }
  2605. for _, result := range results {
  2606. resp.Data = append(resp.Data, forms.BasicReportItem{
  2607. ID: result.ID,
  2608. Date: utility.ParseDate(result.Date),
  2609. OldCount: result.OldCount,
  2610. NewCount: result.NewCount,
  2611. ValidCount: result.ValidCount,
  2612. ActiveCount: result.ActiveCount,
  2613. NewDuration: result.NewDuration,
  2614. ValidDuration: result.ValidDuration,
  2615. ActiveDuration: result.ActiveDuration,
  2616. Day1Active: result.Active1Day,
  2617. Day2Active: result.Active2Day,
  2618. Day3Active: result.Active3Day,
  2619. Day4Active: result.Active4Day,
  2620. Day5Active: result.Active5Day,
  2621. Day6Active: result.Active6Day,
  2622. Day7Active: result.Active7Day,
  2623. Day14Active: result.Active14Day,
  2624. Day30Active: result.Active30Day,
  2625. })
  2626. }
  2627. resp.Total, err = rdb.Count()
  2628. return
  2629. }
  2630. type QueryBasicRetentionInfo struct {
  2631. Days []string `json:"days"`
  2632. NewCount []int64 `json:"newCount"`
  2633. OldCount []int64 `json:"old_count"`
  2634. Active1Day []int64 `json:"Active1Day"`
  2635. Active7Day []int64 `json:"Active7Day"`
  2636. Active14Day []int64 `json:"Active14Day"`
  2637. Active30Day []int64 `json:"Active30Day"`
  2638. AvgActive1Day []string `json:"AvgActive1Day"`
  2639. AvgActive7Day []string `json:"AvgActive7Day"`
  2640. AvgActive14Day []string `json:"AvgActive14Day"`
  2641. AvgActive30Day []string `json:"AvgActive30Day"`
  2642. }
  2643. type QueryBasicRetentionData struct {
  2644. Info QueryBasicRetentionInfo `json:"info"`
  2645. }
  2646. // QueryBasicRetention 查询基础留存率
  2647. func (s *sDash) QueryBasicRetention(params forms.QueryBasicRetentionReq) (respData QueryBasicRetentionData, err error) {
  2648. resp := QueryBasicRetentionInfo{}
  2649. rdb := query.Use(config.DB).ReportDayBasic
  2650. m := rdb.
  2651. Select(rdb.ALL,
  2652. rdb.NewCount.Sum().As("new_count"),
  2653. rdb.ValidCount.Sum().As("valid_count"),
  2654. rdb.ActiveCount.Sum().As("active_count"),
  2655. rdb.NewDuration.Sum().As("new_duration"),
  2656. rdb.ValidDuration.Sum().As("valid_duration"),
  2657. rdb.OldCount.Sum().As("old_count"),
  2658. rdb.ActiveDuration.Sum().As("active_duration"),
  2659. rdb.Active1Day.Sum().As("active_1_day"),
  2660. rdb.Active2Day.Sum().As("active_2_day"),
  2661. rdb.Active3Day.Sum().As("active_3_day"),
  2662. rdb.Active4Day.Sum().As("active_4_day"),
  2663. rdb.Active5Day.Sum().As("active_5_day"),
  2664. rdb.Active6Day.Sum().As("active_6_day"),
  2665. rdb.Active7Day.Sum().As("active_7_day"),
  2666. rdb.Active14Day.Sum().As("active_14_day"),
  2667. rdb.Active30Day.Sum().As("active_30_day"),
  2668. ).Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay))
  2669. switch params.ChannelId {
  2670. case consts.ChannelIdNone:
  2671. // 不选择渠道
  2672. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2673. // 所有的广告渠道
  2674. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2675. default:
  2676. // 指定渠道
  2677. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2678. }
  2679. if params.ServerId > 0 {
  2680. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2681. }
  2682. result, err := m.Order(rdb.Date).Group(rdb.Date).Find() // .Order(rdb.Date.Desc())
  2683. if err != nil {
  2684. return
  2685. }
  2686. for _, v := range result {
  2687. if v != nil {
  2688. resp.Days = append(resp.Days, strings.ReplaceAll(v.Date, "T00:00:00+08:00", ""))
  2689. resp.OldCount = append(resp.OldCount, v.OldCount)
  2690. resp.NewCount = append(resp.NewCount, v.NewCount)
  2691. resp.Active1Day = append(resp.Active1Day, v.Active1Day)
  2692. resp.Active7Day = append(resp.Active7Day, v.Active7Day)
  2693. resp.Active14Day = append(resp.Active14Day, v.Active14Day)
  2694. resp.Active30Day = append(resp.Active30Day, v.Active30Day)
  2695. var newCount int64 = 1
  2696. if v.NewCount > 1 {
  2697. newCount = v.NewCount
  2698. } else {
  2699. resp.AvgActive1Day = append(resp.AvgActive1Day, "0")
  2700. resp.AvgActive7Day = append(resp.AvgActive7Day, "0")
  2701. resp.AvgActive14Day = append(resp.AvgActive14Day, "0")
  2702. resp.AvgActive30Day = append(resp.AvgActive30Day, "0")
  2703. continue
  2704. }
  2705. resp.AvgActive1Day = append(resp.AvgActive1Day, utility.Round((float64(v.Active1Day))*100/float64(newCount), int64(1)))
  2706. resp.AvgActive7Day = append(resp.AvgActive7Day, utility.Round((float64(v.Active7Day))*100/float64(newCount), int64(1)))
  2707. resp.AvgActive14Day = append(resp.AvgActive14Day, utility.Round((float64(v.Active14Day))*100/float64(newCount), int64(1)))
  2708. resp.AvgActive30Day = append(resp.AvgActive30Day, utility.Round((float64(v.Active30Day))*100/float64(newCount), int64(1)))
  2709. }
  2710. }
  2711. respData.Info = resp
  2712. return
  2713. }
  2714. func (s *sDash) QueryConditionReport(params forms.ConditionReportReq) (resp forms.ConditionReportRespData, err error) {
  2715. rdb := query.Use(config.DB).ReportDayEventBasic
  2716. m := rdb.
  2717. Select(rdb.ALL,
  2718. rdb.Count_.Sum().As("count"),
  2719. rdb.Active1Day.Sum().As("active_1_day"),
  2720. rdb.Active2Day.Sum().As("active_2_day"),
  2721. rdb.Active3Day.Sum().As("active_3_day"),
  2722. rdb.Active4Day.Sum().As("active_4_day"),
  2723. rdb.Active5Day.Sum().As("active_5_day"),
  2724. rdb.Active6Day.Sum().As("active_6_day"),
  2725. rdb.Active7Day.Sum().As("active_7_day"),
  2726. rdb.Active14Day.Sum().As("active_14_day"),
  2727. rdb.Active30Day.Sum().As("active_30_day"),
  2728. )
  2729. switch params.ChannelId {
  2730. case consts.ChannelIdNone:
  2731. // 不选择渠道
  2732. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2733. // 所有的广告渠道
  2734. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2735. default:
  2736. // 指定渠道
  2737. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2738. }
  2739. if params.ServerId > 0 {
  2740. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2741. }
  2742. if params.Flag > -1 {
  2743. m = m.Where(rdb.Flag.Eq(params.Flag))
  2744. }
  2745. resp.Total, err = m.Order(rdb.Date.Desc()).Group(rdb.Date).Count()
  2746. results, err := m.Limit(int(params.PerPage)).Offset(int((params.Page - 1) * params.PerPage)).Find()
  2747. if err != nil {
  2748. return resp, err
  2749. }
  2750. for _, result := range results {
  2751. date := utility.ParseDate(result.Date)
  2752. begin, end, err := utility.GetBeginAndEndOfDay(date)
  2753. if err != nil {
  2754. return resp, err
  2755. }
  2756. lcount := s.GetGuideFinishedCount(params.ServerId, begin, end, begin, end, params.ChannelId)
  2757. resp.Data = append(resp.Data, forms.ConditionReportItem{
  2758. ID: result.ID,
  2759. ConditionId: result.ConditionID,
  2760. Name: result.Name,
  2761. Date: date,
  2762. GuideFinishedCount: lcount,
  2763. NewCount: result.Count_,
  2764. Day1Active: result.Active1Day,
  2765. Day2Active: result.Active2Day,
  2766. Day3Active: result.Active3Day,
  2767. Day4Active: result.Active4Day,
  2768. Day5Active: result.Active5Day,
  2769. Day6Active: result.Active6Day,
  2770. Day7Active: result.Active7Day,
  2771. Day14Active: result.Active14Day,
  2772. Day30Active: result.Active30Day,
  2773. })
  2774. }
  2775. return
  2776. }
  2777. func (s *sDash) GetEventUserCount(uBegin, uEnd, evBegin, evEnd int64, chapter, roomId, event, difficulty int64, extra string) (int64, error) {
  2778. var results []resultUserId
  2779. sqlStr := "select a.user_id from chapter_logs a where a.user_created_at >= %d and a.user_created_at <=%d and a.chapter_id = %d and a.room_id = %d and a.event_id = %d "
  2780. if extra != "" {
  2781. sqlStr = sqlStr + " and a.extra='%s' "
  2782. }
  2783. sqlStr += " and a.event_at >= %d and a.event_at <=%d and a.difficulty = %d group by a.user_id"
  2784. var tx *gorm.DB
  2785. if extra != "" {
  2786. tx = config.DB.Raw(fmt.Sprintf(sqlStr,
  2787. uBegin, uEnd, chapter, roomId, event, extra, evBegin, evEnd, difficulty)).
  2788. Scan(&results)
  2789. } else {
  2790. tx = config.DB.Raw(fmt.Sprintf(sqlStr,
  2791. uBegin, uEnd, chapter, roomId, event, evBegin, evEnd, difficulty)).
  2792. Scan(&results)
  2793. }
  2794. if tx.Error != nil {
  2795. return 0, tx.Error
  2796. }
  2797. return int64(len(results)), nil
  2798. }
  2799. type resultUserId struct {
  2800. UserId int64
  2801. }
  2802. // 注册当天满足某个事件的用户,然后在某个日期内的活动数量
  2803. func (s *sDash) GetEventUserOnlineCount(uBengin, uEnd, evBegin, evEnd, begin, end int64, chapter, roomId, event int64, extra string) (int64, error) {
  2804. var results []resultUserId
  2805. sqlStr := "select a.user_id from chapter_logs_%d a left join online_duration_logs b on a.user_id = b.user_id where a.user_created_at >= %d and a.user_created_at <=%d and a.chapter_id = %d and a.room_id = %d and a.event_id = %d "
  2806. if extra != "" {
  2807. sqlStr = sqlStr + " and a.extra='%s' "
  2808. }
  2809. sqlStr += " and a.event_at >= %d and a.event_at <=%d and b.logout_at >= %d and b.logout_at <= %d group by a.user_id having max(b.id) > 0 "
  2810. var tx *gorm.DB
  2811. if extra != "" {
  2812. tx = config.DB.Raw(fmt.Sprintf(sqlStr,
  2813. chapter, uBengin, uEnd, chapter, roomId, event, extra, evBegin, evEnd, begin, end)).
  2814. Scan(&results)
  2815. } else {
  2816. tx = config.DB.Raw(fmt.Sprintf(sqlStr,
  2817. chapter, uBengin, uEnd, chapter, roomId, event, evBegin, evEnd, begin, end)).
  2818. Scan(&results)
  2819. }
  2820. if tx.Error != nil {
  2821. return 0, tx.Error
  2822. }
  2823. return int64(len(results)), nil
  2824. }
  2825. func (s *sDash) QueryChapterPassLog(params forms.GoodsReportReq) (resp interface{}, err error) {
  2826. var (
  2827. rdb = query.Use(config.DB).ReportDayChapterPass
  2828. m = rdb.Where(rdb.Date.Between(params.Day, params.EndDay))
  2829. )
  2830. switch params.ChannelId {
  2831. case consts.ChannelIdNone:
  2832. // 不选择渠道
  2833. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2834. // 所有的广告渠道
  2835. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2836. default:
  2837. // 指定渠道
  2838. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2839. }
  2840. if params.ServerId > 0 {
  2841. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2842. }
  2843. if params.Flag > -1 {
  2844. m = m.Where(rdb.Flag.Eq(int32(params.Flag)))
  2845. }
  2846. result, err := m.Find()
  2847. if err != nil {
  2848. return
  2849. }
  2850. type ChapterItem struct {
  2851. ID int64 `json:"id"`
  2852. Index int64 `json:"index"`
  2853. Diff int64 `json:"diff"`
  2854. Nums int64 `json:"nums"`
  2855. Users int64 `json:"users"`
  2856. NumsNew int64 `json:"numsNew"`
  2857. PassNums int64 `json:"passNums"`
  2858. UsersNew int64 `json:"usersNew"`
  2859. PassUsers int64 `json:"passUsers"`
  2860. PassNumsNew int64 `json:"passNumsNew"`
  2861. PassUsersNew int64 `json:"passUsersNew"`
  2862. SettleNums int64 `json:"settleNums"`
  2863. SettleUsers int64 `json:"settleUsers"`
  2864. SettleNumsNew int64 `json:"settleNumsNew"`
  2865. SettleUsersNew int64 `json:"settleUsersNew"`
  2866. }
  2867. if result == nil || len(result) == 0 {
  2868. resp = nil
  2869. return
  2870. }
  2871. var allData []ChapterItem
  2872. for i := 0; i < len(result); i++ {
  2873. var chapterItems []ChapterItem
  2874. if err = json.Unmarshal([]byte(result[i].Data), &chapterItems); err != nil {
  2875. logrus.Infof("result json.Unmarshal err:%+v", err)
  2876. return
  2877. }
  2878. for c := 0; c < len(chapterItems); c++ {
  2879. chapterItems[c].Index = (chapterItems[c].ID * 1000) + chapterItems[c].Diff
  2880. allData = append(allData, chapterItems[c])
  2881. }
  2882. }
  2883. transform := make(map[int64]ChapterItem, 0)
  2884. for i := 0; i < len(allData); i++ {
  2885. _, ok := transform[allData[i].Index]
  2886. if ok {
  2887. transform[allData[i].Index] = ChapterItem{
  2888. ID: allData[i].ID,
  2889. Diff: allData[i].Diff,
  2890. Index: allData[i].Index,
  2891. Nums: allData[i].Nums + transform[allData[i].Index].Nums,
  2892. Users: allData[i].Users + transform[allData[i].Index].Users,
  2893. NumsNew: allData[i].NumsNew + transform[allData[i].Index].NumsNew,
  2894. PassNums: allData[i].PassNums + transform[allData[i].Index].PassNums,
  2895. UsersNew: allData[i].UsersNew + transform[allData[i].Index].UsersNew,
  2896. PassUsers: allData[i].PassUsers + transform[allData[i].Index].PassUsers,
  2897. PassNumsNew: allData[i].PassNumsNew + transform[allData[i].Index].PassNumsNew,
  2898. PassUsersNew: allData[i].PassUsersNew + transform[allData[i].Index].PassUsersNew,
  2899. SettleNums: allData[i].SettleNums + transform[allData[i].Index].SettleNums,
  2900. SettleUsers: allData[i].SettleUsers + transform[allData[i].Index].SettleUsers,
  2901. SettleNumsNew: allData[i].SettleNumsNew + transform[allData[i].Index].SettleNumsNew,
  2902. SettleUsersNew: allData[i].SettleUsersNew + transform[allData[i].Index].SettleUsersNew,
  2903. }
  2904. } else {
  2905. transform[allData[i].Index] = allData[i]
  2906. }
  2907. }
  2908. passT := make([]ChapterItem, 0)
  2909. for k, _ := range transform {
  2910. passT = append(passT, transform[k])
  2911. }
  2912. sort.Slice(passT, func(i, j int) bool {
  2913. return passT[i].Index < passT[j].Index
  2914. })
  2915. resp = passT
  2916. return
  2917. }
  2918. type RoleMapInfo struct {
  2919. Days []string `json:"days"`
  2920. Info map[string][]int64 `json:"info"`
  2921. }
  2922. func (s *sDash) QueryUserRolesMap(params forms.GoodsReportReq) (RoleMapInfo, error) {
  2923. resp := RoleMapInfo{Info: map[string][]int64{
  2924. "player1": {},
  2925. "player2": {},
  2926. "player3": {},
  2927. "player4": {},
  2928. "player5": {},
  2929. "player6": {},
  2930. "player7": {},
  2931. "player8": {},
  2932. "player9": {},
  2933. "player10": {},
  2934. "player11": {},
  2935. "player12": {},
  2936. "player13": {},
  2937. "player14": {},
  2938. }}
  2939. rdb := query.Use(config.DB).ReportDayHaveRole
  2940. m := rdb.Select(rdb.Date).Where(rdb.Date.Gte(params.Day), rdb.Date.Lt(params.EndDay))
  2941. switch params.ChannelId {
  2942. case consts.ChannelIdNone:
  2943. // 不选择渠道
  2944. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2945. // 所有的广告渠道
  2946. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2947. default:
  2948. // 指定渠道
  2949. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  2950. }
  2951. if params.ServerId > 0 {
  2952. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2953. }
  2954. result, err := m.Group(rdb.Date).Order(rdb.Date).Find()
  2955. if err != nil {
  2956. logrus.Warnf("QueryUserRolesMap Group err:%+v", err)
  2957. return resp, err
  2958. }
  2959. for _, v := range result {
  2960. date := strings.ReplaceAll(v.Date, "T00:00:00+08:00", "")
  2961. resp.Days = append(resp.Days, date)
  2962. m2 := rdb.Select(rdb.Data).Where(rdb.Date.Eq(date))
  2963. switch params.ChannelId {
  2964. case consts.ChannelIdNone:
  2965. // 不选择渠道
  2966. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  2967. // 所有的广告渠道
  2968. m2 = m2.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  2969. default:
  2970. // 指定渠道
  2971. m2 = m2.Where(rdb.ChannelID.Eq(params.ChannelId))
  2972. }
  2973. if params.ServerId > 0 {
  2974. m2 = m2.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  2975. }
  2976. var tmpRoles = make(map[int64]int64)
  2977. rows, err := m2.Find()
  2978. if err != nil {
  2979. logrus.Warnf("QueryUserRolesMap Data err:%+v", err)
  2980. return resp, err
  2981. }
  2982. for _, row := range rows {
  2983. var tmpRole = make(map[int64]int64)
  2984. if err = json.Unmarshal([]byte(row.Data), &tmpRole); err != nil {
  2985. logrus.Warnf("QueryUserRolesMap Unmarshal err:%+v", err)
  2986. return resp, err
  2987. }
  2988. for roleId, num := range tmpRole {
  2989. tmpRoles[roleId] += num
  2990. }
  2991. }
  2992. for roleId, num := range tmpRoles {
  2993. switch roleId {
  2994. case 1:
  2995. resp.Info["player1"] = append(resp.Info["player1"], num)
  2996. case 2:
  2997. resp.Info["player2"] = append(resp.Info["player2"], num)
  2998. case 3:
  2999. resp.Info["player3"] = append(resp.Info["player3"], num)
  3000. case 4:
  3001. resp.Info["player4"] = append(resp.Info["player4"], num)
  3002. case 5:
  3003. resp.Info["player5"] = append(resp.Info["player5"], num)
  3004. case 6:
  3005. resp.Info["player6"] = append(resp.Info["player6"], num)
  3006. case 7:
  3007. resp.Info["player7"] = append(resp.Info["player7"], num)
  3008. case 8:
  3009. resp.Info["player8"] = append(resp.Info["player8"], num)
  3010. case 9:
  3011. resp.Info["player9"] = append(resp.Info["player9"], num)
  3012. case 10:
  3013. resp.Info["player10"] = append(resp.Info["player10"], num)
  3014. case 11:
  3015. resp.Info["player11"] = append(resp.Info["player11"], num)
  3016. case 12:
  3017. resp.Info["player12"] = append(resp.Info["player12"], num)
  3018. case 13:
  3019. resp.Info["player13"] = append(resp.Info["player13"], num)
  3020. case 14:
  3021. resp.Info["player14"] = append(resp.Info["player14"], num)
  3022. }
  3023. }
  3024. }
  3025. return resp, err
  3026. }
  3027. var (
  3028. deadEvent = 6
  3029. )
  3030. type Result struct {
  3031. EventId int64 `json:"event_id"`
  3032. Cost float64 `json:"cost"`
  3033. Count int64 `json:"count"`
  3034. Times int64 `json:"times"`
  3035. }
  3036. type Result2 struct {
  3037. RoomId int64 `json:"room_id"`
  3038. Val int64 `json:"val"`
  3039. }
  3040. type Result3 struct {
  3041. ChapterId int64
  3042. UserCount int64
  3043. DieNums int64
  3044. Difficulty int64
  3045. }
  3046. func (s *sDash) QueryLoginLog(params forms.LoginlogReq) (resp forms.LoginEventRespData, err error) {
  3047. var (
  3048. loginModel = &model.LoginLog{}
  3049. q = config.DB.Scopes(model.TableOfYearMonth(loginModel.TableName(), time.Now())).
  3050. Select("`user_id`,`event_id`,AVG(`cost_time`) AS `cost`,COUNT(distinct(`user_id`)) as count, count(1) as times")
  3051. results []Result
  3052. data []forms.LoginEventData
  3053. )
  3054. begin, end, err := utility.GetBeginAndEndOfDay(params.Day)
  3055. if err != nil {
  3056. return resp, err
  3057. }
  3058. q = q.Where("event_at <= ? and event_at >=? ", end, begin)
  3059. if params.EventId != 0 {
  3060. q = q.Where("event_id = ?", params.EventId)
  3061. }
  3062. if params.UserType == 1 {
  3063. q = q.Where("user_created_at >= ? and user_created_at <=?", begin, end)
  3064. } else if params.UserType == 2 {
  3065. q = q.Where("user_created_at <=?", begin)
  3066. }
  3067. if params.ServerId > 0 {
  3068. q = q.Where("server_id =?", params.ServerId)
  3069. }
  3070. switch params.ChannelId {
  3071. case consts.ChannelIdNone:
  3072. // 不选择渠道
  3073. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3074. // 所有的广告渠道
  3075. q = q.Where("channel_id in(?)", Channel.GetIdsByType(params.ChannelId))
  3076. default:
  3077. // 指定渠道
  3078. q = q.Where("channel_id =?", params.ChannelId)
  3079. }
  3080. tx := q.Group("event_id").Scan(&results)
  3081. if tx.Error != nil {
  3082. return resp, tx.Error
  3083. }
  3084. for i, result := range results {
  3085. data = append(data, forms.LoginEventData{
  3086. Key: int64(i),
  3087. EventId: result.EventId,
  3088. EventName: s.GetLoginEventName(result.EventId),
  3089. UserCount: result.Count,
  3090. Count: result.Times,
  3091. Cost: int64(result.Cost),
  3092. })
  3093. }
  3094. lcount := s.GetGuideFinishedCount(params.ServerId, begin, end, begin, end, params.ChannelId)
  3095. if lcount > 0 {
  3096. data = append(data, forms.LoginEventData{
  3097. Key: int64(len(data)),
  3098. EventId: 100,
  3099. EventName: "引导关卡完成(新手)",
  3100. UserCount: lcount,
  3101. Count: lcount,
  3102. Cost: 0,
  3103. })
  3104. }
  3105. n, f := s.getNewConvCount(params.ServerId, params.Day, params.ChannelId)
  3106. if f.Goto > 0 {
  3107. data = append(data, forms.LoginEventData{
  3108. Key: int64(len(data)),
  3109. EventId: 101,
  3110. EventName: "到达新手武器界面",
  3111. UserCount: f.Goto,
  3112. Count: f.Goto,
  3113. Cost: 0,
  3114. })
  3115. }
  3116. if n.Wear1Count > 0 {
  3117. data = append(data, forms.LoginEventData{
  3118. Key: int64(len(data)),
  3119. EventId: 102,
  3120. EventName: "引导装备佩戴(1)",
  3121. UserCount: n.Wear1Users,
  3122. Count: n.Wear1Count,
  3123. Cost: 0,
  3124. })
  3125. }
  3126. if n.Wear2Count > 0 {
  3127. data = append(data, forms.LoginEventData{
  3128. Key: int64(len(data)),
  3129. EventId: 103,
  3130. EventName: "引导装备佩戴(2)",
  3131. UserCount: n.Wear2Users,
  3132. Count: n.Wear2Count,
  3133. Cost: 0,
  3134. })
  3135. }
  3136. if n.Wear3Count > 0 {
  3137. data = append(data, forms.LoginEventData{
  3138. Key: int64(len(data)),
  3139. EventId: 104,
  3140. EventName: "引导装备佩戴(3)",
  3141. UserCount: n.Wear3Users,
  3142. Count: n.Wear3Count,
  3143. Cost: 0,
  3144. })
  3145. }
  3146. if n.FirstChapterCount > 0 {
  3147. data = append(data, forms.LoginEventData{
  3148. Key: int64(len(data)),
  3149. EventId: 105,
  3150. EventName: "强制进入第一关卡",
  3151. UserCount: n.FirstChapterUsers,
  3152. Count: n.FirstChapterCount,
  3153. Cost: 0,
  3154. })
  3155. }
  3156. if n.Intensify1Count > 0 {
  3157. data = append(data, forms.LoginEventData{
  3158. Key: int64(len(data)),
  3159. EventId: 106,
  3160. EventName: "引导装备强化(1)",
  3161. UserCount: n.Intensify1Users,
  3162. Count: n.Intensify1Count,
  3163. Cost: 0,
  3164. })
  3165. }
  3166. if n.Intensify2Count > 0 {
  3167. data = append(data, forms.LoginEventData{
  3168. Key: int64(len(data)),
  3169. EventId: 107,
  3170. EventName: "引导装备强化(2)",
  3171. UserCount: n.Intensify2Users,
  3172. Count: n.Intensify2Count,
  3173. Cost: 0,
  3174. })
  3175. }
  3176. if n.Intensify3Count > 0 {
  3177. data = append(data, forms.LoginEventData{
  3178. Key: int64(len(data)),
  3179. EventId: 108,
  3180. EventName: "引导装备强化(3)",
  3181. UserCount: n.Intensify3Users,
  3182. Count: n.Intensify3Count,
  3183. Cost: 0,
  3184. })
  3185. }
  3186. if n.Intensify4Count > 0 {
  3187. data = append(data, forms.LoginEventData{
  3188. Key: int64(len(data)),
  3189. EventId: 109,
  3190. EventName: "引导装备强化(4)",
  3191. UserCount: n.Intensify4Users,
  3192. Count: n.Intensify4Count,
  3193. Cost: 0,
  3194. })
  3195. }
  3196. resp.Data = data
  3197. return
  3198. }
  3199. func (s *sDash) getNewConvCount(serverId int, day, channelId string) (newConv model.ReportDayNewConv, firstAdv model.ReportDayFirstAdv) {
  3200. n := config.DB.Table("report_day_new_conv").Select(
  3201. "sum(wear1_users) as wear1_users",
  3202. "sum(wear2_users) as wear2_users",
  3203. "sum(wear3_users) as wear3_users",
  3204. "sum(first_chapter_users) as first_chapter_users",
  3205. "sum(intensify1_users) as intensify1_users",
  3206. "sum(intensify2_users) as intensify2_users",
  3207. "sum(intensify3_users) as intensify3_users",
  3208. "sum(intensify4_users) as intensify4_users",
  3209. "sum(wear1_count) as wear1_count",
  3210. "sum(wear2_count) as wear2_count",
  3211. "sum(wear3_count) as wear3_count",
  3212. "sum(first_chapter_count) as first_chapter_count",
  3213. "sum(intensify1_count) as intensify1_count",
  3214. "sum(intensify2_count) as intensify2_count",
  3215. "sum(intensify3_count) as intensify3_count",
  3216. "sum(intensify4_count) as intensify4_count",
  3217. )
  3218. f := config.DB.Table("report_day_first_adv").Select(
  3219. "sum(goto) as goto",
  3220. )
  3221. if serverId > 0 {
  3222. n = n.Where("server_id = %v", serverId)
  3223. f = f.Where("server_id = %v", serverId)
  3224. }
  3225. switch channelId {
  3226. case "":
  3227. n.Where("date = ?", day).Scan(&newConv)
  3228. f.Where("date = ?", day).Scan(&firstAdv)
  3229. case "1":
  3230. // 所有的广告渠道
  3231. n.Where("date = ? and channel_id != ?", day, "0").Scan(&newConv)
  3232. f.Where("date = ? and channel_id != ?", day, "0").Scan(&firstAdv)
  3233. default:
  3234. // 指定渠道
  3235. n.Where("date = ? and channel_id = ?", day, channelId).Scan(&newConv)
  3236. f.Where("date = ? and channel_id = ?", day, channelId).Scan(&firstAdv)
  3237. }
  3238. return
  3239. }
  3240. func (s *sDash) QueryUserChapterLog(serverId int, day string, days, chapter_id, difficulty int64, channelId string, flag int32) (resp []forms.UserChapterItem, err error) {
  3241. begin, end, err := utility.GetBeginAndEndOfDay(day)
  3242. if err != nil {
  3243. return resp, err
  3244. }
  3245. userBegin := begin - int64(days)*24*60*60
  3246. userEnd := end - int64(days)*24*60*60
  3247. // 获取进入房间信息
  3248. var results []Result2
  3249. clog := model.ChapterLog{ChapterID: int32(chapter_id)}
  3250. query := config.DB.Scopes(model.ChapterLogTable(clog)).Select("room_id, count(distinct(user_id)) as val").Where("chapter_id = ? and difficulty= ? and room_id > 0", chapter_id, difficulty)
  3251. if days >= 7 { // 超过7天,就是7日后
  3252. query = query.Where("user_created_at <= ? and event_at >= ?", userEnd, begin)
  3253. } else {
  3254. query = query.Where("user_created_at >= ? and user_created_at <= ? and event_at >=? and event_at <= ?", userBegin, userEnd, begin, end)
  3255. }
  3256. switch channelId {
  3257. case consts.ChannelIdNone:
  3258. // 不选择渠道
  3259. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3260. query = query.Where("`channel_id` IN (?)", Channel.GetIdsByType(channelId))
  3261. default:
  3262. // 指定渠道
  3263. query = query.Where("`channel_id` = ? ", channelId)
  3264. }
  3265. if serverId > 0 {
  3266. query = query.Where(fmt.Sprintf("server_id = %v", serverId))
  3267. }
  3268. if flag > -1 {
  3269. query = query.Where(fmt.Sprintf("flag = %v", flag))
  3270. }
  3271. tx := query.Group("room_id").Order("room_id asc").Scan(&results)
  3272. if tx.Error != nil {
  3273. return resp, tx.Error
  3274. }
  3275. // 获取结算房间信息
  3276. var results2 []Result2
  3277. query2 := config.DB.Scopes(model.ChapterLogTable(clog)).Select("room_id, count(distinct(user_id)) as val").Where("chapter_id = ? and difficulty= ? and room_id >0 and event_id =11", chapter_id, difficulty)
  3278. if days >= 7 { // 超过7天,就是7日后
  3279. query2 = query2.Where("user_created_at <= ? and event_at >= ?", userEnd, begin)
  3280. } else {
  3281. query2 = query2.Where("user_created_at >= ? and user_created_at <= ? and event_at >=? and event_at <= ?", userBegin, userEnd, begin, end)
  3282. }
  3283. switch channelId {
  3284. case consts.ChannelIdNone:
  3285. // 不选择渠道
  3286. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3287. // 所有的广告渠道
  3288. query2 = query2.Where("`channel_id` IN (?)", Channel.GetIdsByType(channelId))
  3289. default:
  3290. // 指定渠道
  3291. query2 = query2.Where("`channel_id` = ? ", channelId)
  3292. }
  3293. if serverId > 0 {
  3294. query2 = query2.Where(fmt.Sprintf("server_id = %v", serverId))
  3295. }
  3296. if flag > -1 {
  3297. query2 = query2.Where(fmt.Sprintf("flag = %v", flag))
  3298. }
  3299. tx2 := query2.Group("room_id").Order("room_id asc").Scan(&results2)
  3300. if tx2.Error != nil {
  3301. return resp, tx.Error
  3302. }
  3303. if len(results) == 0 && len(results2) == 0 {
  3304. return
  3305. } else if len(results) == 0 {
  3306. for i, item := range results2 {
  3307. resp = append(resp, forms.UserChapterItem{Id: i, RoomId: int32(item.RoomId), SettelCount: int32(item.Val)})
  3308. }
  3309. return
  3310. } else if len(results2) == 0 {
  3311. for i, item := range results {
  3312. resp = append(resp, forms.UserChapterItem{Id: i, RoomId: int32(item.RoomId), Count: int32(item.Val)})
  3313. }
  3314. return
  3315. } else {
  3316. for i, j := 0, 0; i < len(results) && j < len(results2); {
  3317. k := i + 1 + j + 1
  3318. tmpItem := forms.UserChapterItem{Id: k}
  3319. if results[i].RoomId < results2[j].RoomId {
  3320. tmpItem.RoomId = int32(results[i].RoomId)
  3321. tmpItem.Count = int32(results[i].Val)
  3322. i++
  3323. } else if results[i].RoomId > results2[j].RoomId {
  3324. tmpItem.RoomId = int32(results2[j].RoomId)
  3325. tmpItem.Count = int32(results2[j].Val)
  3326. j++
  3327. } else {
  3328. tmpItem.RoomId = int32(results2[j].RoomId)
  3329. tmpItem.Count = int32(results[i].Val)
  3330. tmpItem.SettelCount = int32(results2[j].Val)
  3331. i++
  3332. j++
  3333. }
  3334. resp = append(resp, tmpItem)
  3335. }
  3336. }
  3337. return
  3338. }
  3339. // queryUserDieLog 查询每天的用户死亡信息
  3340. func (s *sDash) queryUserDieLog(serverId int, day, endDay string, chapterId, difficulty int, channelId string, flag int32) (nums, users, newNums, newUsers int, err error) {
  3341. begin, end, err := utility.GetBeginAndEndOfDay2(day, endDay)
  3342. if err != nil {
  3343. return 0, 0, 0, 0, err
  3344. }
  3345. var results []Result3
  3346. clog := model.ChapterLog{ChapterID: int32(chapterId)}
  3347. query := config.DB.Scopes(model.ChapterLogTable(clog)).
  3348. Select("chapter_id,difficulty, count(distinct(user_id)) as user_count, count(1) as die_nums").
  3349. Where("event_at >= ? and event_at <= ? and event_id = ? and difficulty = ?",
  3350. begin, end, deadEvent, difficulty)
  3351. switch channelId {
  3352. case consts.ChannelIdNone:
  3353. // 不选择渠道
  3354. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3355. // 所有的广告渠道
  3356. query = query.Where("`channel_id` IN (?)", Channel.GetIdsByType(channelId))
  3357. default:
  3358. // 指定渠道
  3359. query = query.Where("channel_id = ? ", channelId)
  3360. }
  3361. if serverId > 0 {
  3362. query = query.Where(fmt.Sprintf("server_id = %v", serverId))
  3363. }
  3364. if flag > -1 {
  3365. query = query.Where(fmt.Sprintf("flag = %v", flag))
  3366. }
  3367. tx := query.Group("chapter_id,difficulty").Scan(&results)
  3368. if tx.Error != nil {
  3369. return 0, 0, 0, 0, tx.Error
  3370. }
  3371. if len(results) > 0 {
  3372. newNums, newUsers = s.newUserDieLog(serverId, day, endDay, chapterId, difficulty, clog, channelId, flag)
  3373. return int(results[0].DieNums), int(results[0].UserCount), newNums, newUsers, nil
  3374. }
  3375. return 0, 0, 0, 0, nil
  3376. }
  3377. func (s *sDash) newUserDieLog(serverId int, day, endDay string, chapterId, difficulty int, clog model.ChapterLog, channelId string, flag int32) (newNums, newUsers int) {
  3378. begin, end, _ := utility.GetBeginAndEndOfDay2(day, endDay)
  3379. end = end + 1
  3380. // 同一天
  3381. if begin+86400 == end {
  3382. var results4 []Result3
  3383. query2 := config.DB.Scopes(model.ChapterLogTable(clog)).
  3384. Select("chapter_id,difficulty, count(distinct(user_id)) as user_count, count(1) as die_nums").
  3385. Where("event_at >= ? and event_at <= ? and event_id = ? and difficulty = ? and user_created_at >= ? and user_created_at <= ? ",
  3386. begin, end, deadEvent, difficulty, begin, end)
  3387. if channelId != "" {
  3388. query2 = query2.Where(fmt.Sprintf("`user_id` IN (SELECT `player_channel`.`playerid` FROM `player_channel` WHERE `player_channel`.`channel_id` = '%v')", channelId))
  3389. }
  3390. if serverId > 0 {
  3391. query2 = query2.Where(fmt.Sprintf("server_id = %v", serverId))
  3392. }
  3393. if flag > -1 {
  3394. query2 = query2.Where(fmt.Sprintf("flag = %v", flag))
  3395. }
  3396. tx2 := query2.Group("chapter_id,difficulty").Scan(&results4)
  3397. if tx2.Error != nil {
  3398. return 0, 0
  3399. }
  3400. if len(results4) == 0 {
  3401. return 0, 0
  3402. }
  3403. logrus.Warnf("results4:%+v", results4)
  3404. return int(results4[0].DieNums), int(results4[0].UserCount)
  3405. }
  3406. var (
  3407. dieNums int64
  3408. userCount int64
  3409. )
  3410. for i := begin; i < end; i += 86400 {
  3411. var results5 []Result3
  3412. query2 := config.DB.Scopes(model.ChapterLogTable(clog)).
  3413. Select("chapter_id,difficulty, count(distinct(user_id)) as user_count, count(1) as die_nums").
  3414. Where("event_at >= ? and event_at <= ? and event_id = ? and difficulty = ? and user_created_at >= ? and user_created_at <= ? ",
  3415. i, i+86400, deadEvent, difficulty, i, i+86400)
  3416. if channelId != "" {
  3417. query2 = query2.Where(fmt.Sprintf("`user_id` IN (SELECT `player_channel`.`playerid` FROM `player_channel` WHERE `player_channel`.`channel_id` = '%v')", channelId))
  3418. }
  3419. if serverId > 0 {
  3420. query2 = query2.Where(fmt.Sprintf("server_id = %v", serverId))
  3421. }
  3422. if flag > -1 {
  3423. query2 = query2.Where(fmt.Sprintf("flag = %v", flag))
  3424. }
  3425. tx2 := query2.Group("chapter_id,difficulty").Scan(&results5)
  3426. if tx2.Error != nil {
  3427. return 0, 0
  3428. }
  3429. if len(results5) > 0 {
  3430. dieNums += results5[0].DieNums
  3431. userCount += results5[0].UserCount
  3432. }
  3433. }
  3434. return int(dieNums), int(userCount)
  3435. }
  3436. //func (s *sDash) QueryUserDieLog(params forms.DieDataReq) (resp serializer.Response) {
  3437. // chapterMap := gmdata.GetChaptersMap()
  3438. // list := make([]forms.DieInfoItem, 0)
  3439. // for _, v := range chapterMap {
  3440. // dies, users, newDies, newUsers, err := s.queryUserDieLog(params.ServerId, params.Day, params.EndDay, int(v.ID), int(v.Difficulty), params.ChannelId, params.Flag)
  3441. // if err == nil {
  3442. // list = append(list, forms.DieInfoItem{
  3443. // Id: len(list),
  3444. // ChapterId: int(v.ID),
  3445. // Chapter: s.GetChapterName(v.ID, v.Difficulty),
  3446. // DifficultyIndex: int(v.Difficulty),
  3447. // Difficulty: gmdata.GetDifficultName(v.Difficulty),
  3448. // DieCount: dies,
  3449. // DieUserCount: users,
  3450. // NewDieCount: newDies,
  3451. // NewDieUserCount: newUsers,
  3452. // })
  3453. // }
  3454. // }
  3455. //
  3456. // for k, v := range list {
  3457. // list[k].Index = v.ChapterId*1000 + v.DifficultyIndex
  3458. // }
  3459. // sort.Sort(forms.DieInfoItemSlice(list))
  3460. //
  3461. // pageCount := int64(len(list))
  3462. // models := new(forms.ChapterReconnectRespData)
  3463. // models.Page = params.Page
  3464. // models.PerPage = params.PerPage
  3465. // models.PageCount = int64(math.Ceil(float64(pageCount) / float64(params.PerPage)))
  3466. //
  3467. // startInx := (models.Page - 1) * models.PerPage
  3468. // endInx := (models.Page) * models.PerPage
  3469. // if startInx < 0 {
  3470. // startInx = 0
  3471. // }
  3472. // if endInx > pageCount {
  3473. // endInx = pageCount
  3474. // }
  3475. // subList := list[startInx:endInx]
  3476. //
  3477. // models.List = subList
  3478. // return serializer.Suc(models)
  3479. //}
  3480. func (s *sDash) QueryConditionUsers(params forms.DieDataReq) (interface{}, error) {
  3481. var (
  3482. conditionRow = make(map[int64]*model.ReportDayEventBasic)
  3483. rdb = query.Use(config.DB).ReportDayEventBasic
  3484. m = rdb.WithContext(context.TODO()).Select(rdb.ALL,
  3485. rdb.Count_.Sum().As("count"),
  3486. rdb.Active1Day.Sum().As("active_1_day"),
  3487. rdb.Active2Day.Sum().As("active_2_day"),
  3488. rdb.Active3Day.Sum().As("active_3_day"),
  3489. rdb.Active4Day.Sum().As("active_4_day"),
  3490. rdb.Active5Day.Sum().As("active_5_day"),
  3491. rdb.Active6Day.Sum().As("active_6_day"),
  3492. rdb.Active7Day.Sum().As("active_7_day"),
  3493. rdb.Active14Day.Sum().As("active_14_day"),
  3494. rdb.Active30Day.Sum().As("active_30_day"),
  3495. )
  3496. )
  3497. switch params.ChannelId {
  3498. case consts.ChannelIdNone:
  3499. // 不选择渠道
  3500. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3501. // 所有的广告渠道
  3502. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(params.ChannelId)...))
  3503. default:
  3504. // 指定渠道
  3505. m = m.Where(rdb.ChannelID.Eq(params.ChannelId))
  3506. }
  3507. if params.ServerId > 0 {
  3508. m = m.Where(rdb.ServerID.Eq(int32(params.ServerId)))
  3509. }
  3510. res2, _ := m.
  3511. Where(rdb.Date.Eq(params.Day)).
  3512. Order(rdb.Date.Desc()).
  3513. Group(rdb.Date).Find()
  3514. for _, item := range res2 {
  3515. conditionRow[item.ConditionID] = item
  3516. }
  3517. db3 := query.Use(config.DB).ReportEvent
  3518. res, err := db3.Find()
  3519. if err != nil {
  3520. return nil, err
  3521. }
  3522. var resp []*forms.ConditionItem
  3523. for _, item := range res {
  3524. temp := forms.ConditionItem{
  3525. Id: int32(item.ID),
  3526. ConditionId: int32(item.ID),
  3527. Name: item.EventName,
  3528. }
  3529. if v, ok := conditionRow[item.ID]; ok {
  3530. temp.Val = int(v.Count_)
  3531. }
  3532. resp = append(resp, &temp)
  3533. }
  3534. return resp, err
  3535. }
  3536. func (s *sDash) GetLoginEventName(eventId int64) string {
  3537. for _, v := range config.LoginEvent {
  3538. if int64(v.ID) == eventId {
  3539. return v.Name
  3540. }
  3541. }
  3542. return ""
  3543. }
  3544. func (s *sDash) wrapperChaperName(id, diff int64) string {
  3545. c1 := "活动章节"
  3546. if id == 0 {
  3547. c1 = "新手引导章节"
  3548. } else if id < 100 {
  3549. c1 = "剧情章节"
  3550. }
  3551. return fmt.Sprintf("%s%d %s", c1, id, gmdata.GetDifficultName(diff))
  3552. }
  3553. func (s *sDash) GetChapterName(id, diff int64) string {
  3554. data := gmdata.GetChapterById(int32(id))
  3555. if data == nil || strings.Contains(data.Name, "未知") {
  3556. return s.wrapperChaperName(id, diff)
  3557. }
  3558. return fmt.Sprintf("%s %s", data.Name, gmdata.GetDifficultName(diff))
  3559. }
  3560. // QueryRolesLog
  3561. // 角色Id/1亿 = 角色类型 角色后四位(不包含后四位的最后一位)= 角色等级
  3562. func (s *sDash) QueryRolesLog(queryDate forms.HeroLevelDistributedReq) (resp forms.HeroLevelDistributedRespData, err error) {
  3563. rolesMap, allRoleType, allRoleName := getRoleInfos()
  3564. resp.HeroName = allRoleName
  3565. resp.Level = []string{}
  3566. for i := queryDate.StartLevel; i <= queryDate.EndLevel; i++ {
  3567. resp.Level = append(resp.Level, strconv.Itoa(i))
  3568. }
  3569. resp.Data = []*forms.HeroLevelDistributedItem{}
  3570. for _, roleType := range allRoleType {
  3571. data := &forms.HeroLevelDistributedItem{}
  3572. data.Name = rolesMap[roleType]
  3573. data.Type = "bar"
  3574. data.Data = []int{}
  3575. resp.Data = append(resp.Data, data)
  3576. }
  3577. rdb := query.Use(config.DB).RolesLog
  3578. m := rdb.Where(rdb.Date.Eq(queryDate.Day))
  3579. switch queryDate.ChannelId {
  3580. case consts.ChannelIdNone:
  3581. // 不选择渠道
  3582. case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
  3583. // 所有的广告渠道
  3584. m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(queryDate.ChannelId)...))
  3585. default:
  3586. // 指定渠道
  3587. m = m.Where(rdb.ChannelID.Eq(queryDate.ChannelId))
  3588. }
  3589. if queryDate.ServerId > 0 {
  3590. m = m.Where(rdb.ServerID.Eq(int32(queryDate.ServerId)))
  3591. }
  3592. results, err := m.Find()
  3593. if err != nil {
  3594. return resp, err
  3595. }
  3596. var roleLevelInfo = make(map[string]int)
  3597. for _, result := range results {
  3598. var tmpInfo = make(map[string]int)
  3599. json.Unmarshal([]byte(result.Collect), &tmpInfo)
  3600. for k, v := range tmpInfo {
  3601. roleLevelInfo[k] += v
  3602. }
  3603. }
  3604. for idx, roleType := range allRoleType {
  3605. resp.Data[idx].Data = getRoleCount(roleLevelInfo, roleType, resp.Level)
  3606. }
  3607. return
  3608. }
  3609. func getRoleCountByType(roleLevelInfo map[string]int, roleType int, level int) int {
  3610. var c int
  3611. for k, v := range roleLevelInfo {
  3612. idTemp, _ := strconv.Atoi(k)
  3613. role := player.GetRoleData(idTemp)
  3614. if role.Type == roleType && role.Level == level {
  3615. c += v
  3616. }
  3617. }
  3618. return c
  3619. }
  3620. func getRoleCount(roleLevelInfo map[string]int, roleType int, levelInfo []string) (result []int) {
  3621. for _, level := range levelInfo {
  3622. result = append(result, getRoleCountByType(roleLevelInfo, roleType, cast.ToInt(level)))
  3623. }
  3624. return
  3625. }
  3626. func getRoleInfos() (result map[int]string, allRoleType []int, allRoleName []string) {
  3627. result = map[int]string{}
  3628. for _, role := range gmdata.Roles {
  3629. roleType := int(role.Type)
  3630. if _, ok := result[roleType]; !ok {
  3631. result[roleType] = role.Name
  3632. allRoleType = append(allRoleType, roleType)
  3633. allRoleName = append(allRoleName, role.Name)
  3634. }
  3635. }
  3636. return
  3637. }