dash.go 129 KB

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