BaseTotalHourDataReportLogic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\v1\logic\tool\dataReport;
  3. use support\think\Db;
  4. class BaseTotalHourDataReportLogic extends BaseDataReportLogic
  5. {
  6. protected string $table = "base_total_hour";
  7. protected string $group = 'game_id, agent_id, site_id, tdate, thour';
  8. protected array $data;
  9. protected string $date;
  10. protected string $hour;
  11. public function run($params=[])
  12. {
  13. if(!empty($params['date'])){
  14. $sDate = is_array($params['date']) ? $params['date'][0] : $params['date'];
  15. $eDate = !empty($params['date'][1]) ? $params['date'][1] : $params['date'][0];
  16. // 检查日期格式是否正确
  17. if(!isValidDate($sDate) || !isValidDate($eDate)){
  18. return json_encode(["status"=>"error", "msg"=>"日期格式不正确"], 256);
  19. }
  20. $runDate = 1;
  21. }else{
  22. $sDate = date('Y-m-d');
  23. $eDate = date('Y-m-d');
  24. $runDate = 0;
  25. }
  26. for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 days'))){
  27. $this->date = $date;
  28. try {
  29. if($runDate){
  30. for ($hour = 0; $hour <= 23; $hour++){
  31. $this->hour = $hour;
  32. $this->initStart();
  33. }
  34. }else{
  35. $this->hour = date("H", time()-480);
  36. $this->initStart();
  37. }
  38. }catch (\Exception $e){
  39. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  40. }
  41. }
  42. $this->reRun();
  43. return json_encode(["status"=>"success", "msg"=>""], 256);
  44. }
  45. // 重跑
  46. protected function reRun(): void
  47. {
  48. if (date('H') == 8 && date('i') < 10) {
  49. for ($sHour = 0; $sHour <= 23; $sHour++) {
  50. $this->date = date('Y-m-d', strtotime("-1 days"));
  51. $this->hour = $sHour;
  52. $this->initStart();
  53. }
  54. }
  55. }
  56. protected function initStart(): void
  57. {
  58. $this->table = $this->table . "_" . date('Ym', strtotime($this->date));
  59. // 重置数据
  60. $this->data = [];
  61. $this->regData();
  62. $this->loginData();
  63. $this->oldLogin();
  64. $this->regPay();
  65. $this->payTotal();
  66. $this->roleCreate();
  67. $this->mediaCost();
  68. $this->replaceData(['tdate'=>$this->date, 'thour'=>$this->hour]);
  69. }
  70. protected function regData()
  71. {
  72. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  73. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  74. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->date));
  75. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  76. $where = [
  77. ['reg_time', 'between', [$sTime, $eTime]],
  78. ];
  79. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  80. $this->pushData($result);
  81. }
  82. protected function loginData()
  83. {
  84. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  85. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  86. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  87. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour,count(distinct uid) as login_total";
  88. $where = [
  89. ['login_time', 'between', [$sTime, $eTime]],
  90. ];
  91. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  92. $this->pushData($result);
  93. }
  94. protected function oldLogin()
  95. {
  96. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  97. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  98. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  99. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour,count(distinct uid) as old_login_total";
  100. $where = [
  101. ['login_time', 'between', [$sTime, $eTime]],
  102. ['reg_time', '<', strtotime($this->date)],
  103. ];
  104. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  105. $this->pushData($result);
  106. }
  107. // 付费统计 - 当天注册
  108. protected function regPay()
  109. {
  110. $tb = "sdk_order_success";
  111. $sTime = "{$this->date} {$this->hour}:00:00";
  112. $eTime = "{$this->date} {$this->hour}:59:59";
  113. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num";
  114. $where = [
  115. ['reg_date', 'between', [$sTime, $eTime]],
  116. ['pay_date', 'between', [$sTime, $eTime]],
  117. ];
  118. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  119. $this->pushData($result);
  120. }
  121. // 付费统计 - 总
  122. protected function payTotal(): void
  123. {
  124. $tb = "sdk_order_success";
  125. $sTime = "{$this->date} {$this->hour}:00:00";
  126. $eTime = "{$this->date} {$this->hour}:59:59";
  127. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  128. $where = [
  129. ['pay_date', 'between', [$sTime, $eTime]],
  130. ];
  131. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  132. $this->pushData($result);
  133. }
  134. /**
  135. * role_create_user 创建角色用户数
  136. */
  137. protected function roleCreate(): void
  138. {
  139. $sTime = "{$this->date} {$this->hour}:00:00";
  140. $eTime = "{$this->date} {$this->hour}:59:59";
  141. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, COUNT(DISTINCT uid) as role_create_user";
  142. $where = [
  143. ['create_time', 'between', [$sTime, $eTime]],
  144. ];
  145. // 安卓
  146. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  147. $this->pushData($result);
  148. // iOS
  149. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  150. $this->pushData($result);
  151. }
  152. protected function mediaCost(): void
  153. {
  154. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, SUM(money) as cost";
  155. $where = [
  156. "tdate" => $this->date,
  157. "thour" => $this->hour,
  158. ];
  159. $result = Db::connect('db_advert')->table("media_cost_hour")->field($filed)->where($where)->group($this->group)->select();
  160. $this->pushData($result);
  161. }
  162. }