RecordPlatformData.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Services;
  3. use App\Facade\TableName;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\DB;
  6. class RecordPlatformData
  7. {
  8. // 注册留存 $date = Y-m-d
  9. public function Retain($time1,$time2,$field)
  10. {
  11. //$time1 = Carbon::parse($time1)->format('Y-m-d');
  12. $time2 = $time2->format('Ymd');
  13. $RecordPlatformDataModel = new \App\Models\RecordPlatformData();
  14. $count = DB::connection('read')->table(TableName::QPAccountsDB().'AccountsInfo as ai')
  15. ->whereExists(function ($query) use ($time2) {
  16. $query->from(TableName::QPRecordDB() . 'RecordUserGamePlay as rl')
  17. ->select('rl.UserID')
  18. // ->whereRaw("rl.UserID=ai.UserID and DateID=CONVERT(VARCHAR(100),DATEADD(dd,$dateNum,'$date'),112)");
  19. ->whereRaw("rl.UserID=ai.UserID and DateID=$time2");
  20. })
  21. ->whereDate('RegisterDate', Carbon::parse($time1)->format('Y-m-d'))
  22. ->selectRaw("count(ai.UserID) $field,Channel")
  23. ->groupBy('Channel')
  24. ->get();
  25. $GroupChannelList = $count->map(function ($value) {
  26. return (array)$value;
  27. })->toArray();
  28. $AllChannelList = $count->sum($field);
  29. foreach ($GroupChannelList as $GroupChannel) {
  30. $firstChannel = $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1])->first();
  31. if ($firstChannel) {
  32. $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1])->update([$field=>$GroupChannel[$field]]);
  33. }else{
  34. $RecordPlatformDataModel->insert(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1,$field=>$GroupChannel[$field]]);
  35. }
  36. }
  37. $firstAllChannel = $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $time1])->first();
  38. if ($firstAllChannel) {
  39. $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $time1])->update([$field=>$AllChannelList]);
  40. }else{
  41. $RecordPlatformDataModel->insert(['Channel' => -1, 'DateID' => $time1,$field=>$AllChannelList]);
  42. }
  43. return true;
  44. }
  45. // 活跃留存 $date = Y-m-d
  46. public function activeRetain($time1,$time2,$field)
  47. {
  48. $time2 = $time2->format('Ymd');
  49. $RecordPlatformDataModel = new \App\Models\RecordPlatformData();
  50. $count = DB::connection('read')->table(TableName::QPRecordDB().'RecordUserLogin as rul')
  51. ->join(TableName::QPAccountsDB().'AccountsInfo as ai','rul.UserID','ai.UserID')
  52. ->whereExists(function ($query) use ( $time2) {
  53. $query->from(TableName::QPRecordDB() . 'RecordUserLogin as rl')
  54. ->select('rl.UserID')
  55. ->whereRaw("rl.UserID=rul.UserID and DateID=$time2");
  56. })
  57. ->where('DateID', $time1)
  58. ->selectRaw("count(rul.UserID) $field,Channel")
  59. ->groupBy('Channel')
  60. ->get();
  61. $GroupChannelList = $count->map(function ($value) {
  62. return (array)$value;
  63. })->toArray();
  64. $AllChannelList = $count->sum($field);
  65. foreach ($GroupChannelList as $GroupChannel) {
  66. $firstChannel = $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1])->first();
  67. if ($firstChannel) {
  68. $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1])->update([$field=>$GroupChannel[$field]]);
  69. }else{
  70. $RecordPlatformDataModel->insert(['Channel' => $GroupChannel['Channel'], 'DateID' => $time1,$field=>$GroupChannel[$field]]);
  71. }
  72. }
  73. $firstAllChannel = $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $time1])->first();
  74. if ($firstAllChannel) {
  75. $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $time1])->update([$field=>$AllChannelList]);
  76. }else{
  77. $RecordPlatformDataModel->insert(['Channel' => -1, 'DateID' => $time1,$field=>$AllChannelList]);
  78. }
  79. return true;
  80. }
  81. // 付费留存 $date = Y-m-d
  82. /**
  83. * @param $time1 // 充值日期
  84. * @param $time2 // 充值第二天日期
  85. * @param $field // 查找/修改 的字段
  86. * @return bool
  87. */
  88. public function payRetain($time1,$time2,$field)
  89. {
  90. $time2 = $time2->format('Ymd');
  91. $RecordPlatformDataModel = new \App\Models\RecordPlatformData();
  92. $dateID = $time1;
  93. $count = DB::connection('read')->table(TableName::QPRecordDB().'RecordUserDataStatisticsNew as record')
  94. ->join(TableName::QPAccountsDB().'AccountsInfo as ai','record.UserID','ai.UserID')
  95. ->whereExists(function ($query) use ($time2) {
  96. $query->from(TableName::QPRecordDB() . 'RecordUserGamePlay as rl')
  97. ->select('rl.UserID')
  98. ->whereRaw("rl.UserID=record.UserID and DateID=$time2");
  99. })
  100. ->where('Recharge','>',0)
  101. ->where('DateID', $time1)
  102. ->where([
  103. ['ai.RegisterDate', '>=', Carbon::createFromFormat('Ymd', $time1)->format('Y-m-d')],
  104. ['ai.RegisterDate', '<', Carbon::createFromFormat('Ymd', $time1)->addDay(1)
  105. ->format('Y-m-d')]
  106. ])
  107. ->selectRaw("count(record.UserID) $field,Channel")
  108. ->groupBy('Channel')
  109. ->get();
  110. $GroupChannelList = $count->map(function ($value) {
  111. return (array)$value;
  112. })->toArray();
  113. $AllChannelList = $count->sum($field);
  114. foreach ($GroupChannelList as $GroupChannel) {
  115. $firstChannel = $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $dateID])->first();
  116. if ($firstChannel) {
  117. $RecordPlatformDataModel->where(['Channel' => $GroupChannel['Channel'], 'DateID' => $dateID])->update([$field=>$GroupChannel[$field]]);
  118. }else{
  119. $RecordPlatformDataModel->insert(['Channel' => $GroupChannel['Channel'], 'DateID' => $dateID,$field=>$GroupChannel[$field]]);
  120. }
  121. }
  122. $firstAllChannel = $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $dateID])->first();
  123. if ($firstAllChannel) {
  124. $RecordPlatformDataModel->where(['Channel' => -1, 'DateID' => $dateID])->update([$field=>$AllChannelList]);
  125. }else{
  126. $RecordPlatformDataModel->insert(['Channel' => -1, 'DateID' => $dateID,$field=>$AllChannelList]);
  127. }
  128. return true;
  129. }
  130. }