BaseTotalHourLogic.php 6.6 KB

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