BaseTotalHourDataReportLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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->installData();
  62. $this->regData();
  63. $this->loginData();
  64. $this->oldLogin();
  65. $this->regPay();
  66. $this->payTotal();
  67. $this->roleCreate();
  68. $this->mediaCost();
  69. $this->replaceData(['tdate'=>$this->date, 'thour'=>$this->hour]);
  70. }
  71. // 激活数
  72. protected function installData()
  73. {
  74. $sTime = strtotime("{$this->date} 00:00:00");
  75. $eTime = strtotime("{$this->date} 23:59:59");
  76. $regTotalTb = "sdk_activate_log_" . date('Ym', strtotime($this->date));
  77. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, count(distinct concat(imei, oaid)) as install";
  78. $where = [
  79. ['activate_time', 'between', [$sTime, $eTime]],
  80. ];
  81. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  82. $this->pushData($result);
  83. }
  84. protected function regData()
  85. {
  86. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  87. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  88. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->date));
  89. $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";
  90. $where = [
  91. ['reg_time', 'between', [$sTime, $eTime]],
  92. ];
  93. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  94. $this->pushData($result);
  95. }
  96. protected function loginData()
  97. {
  98. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  99. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  100. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  101. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour,count(distinct uid) as login_total";
  102. $where = [
  103. ['login_time', 'between', [$sTime, $eTime]],
  104. ];
  105. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  106. $this->pushData($result);
  107. }
  108. protected function oldLogin()
  109. {
  110. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  111. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  112. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  113. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour,count(distinct uid) as old_login_total";
  114. $where = [
  115. ['login_time', 'between', [$sTime, $eTime]],
  116. ['reg_time', '<', strtotime($this->date)],
  117. ];
  118. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  119. $this->pushData($result);
  120. }
  121. // 付费统计 - 当天注册
  122. protected function regPay()
  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 reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num";
  128. $where = [
  129. ['reg_date', 'between', [$sTime, $eTime]],
  130. ['pay_date', 'between', [$sTime, $eTime]],
  131. ];
  132. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  133. $this->pushData($result);
  134. }
  135. // 付费统计 - 总
  136. protected function payTotal(): void
  137. {
  138. $tb = "sdk_order_success";
  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, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  142. $where = [
  143. ['pay_date', 'between', [$sTime, $eTime]],
  144. ];
  145. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  146. $this->pushData($result);
  147. }
  148. /**
  149. * role_create_user 创建角色用户数
  150. */
  151. protected function roleCreate(): void
  152. {
  153. $sTime = "{$this->date} {$this->hour}:00:00";
  154. $eTime = "{$this->date} {$this->hour}:59:59";
  155. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, COUNT(DISTINCT uid) as role_create_user";
  156. $where = [
  157. ['create_time', 'between', [$sTime, $eTime]],
  158. ];
  159. // 安卓
  160. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  161. $this->pushData($result);
  162. // iOS
  163. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  164. $this->pushData($result);
  165. }
  166. protected function mediaCost(): void
  167. {
  168. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, '{$this->hour}' as thour, SUM(money) as cost";
  169. $where = [
  170. "tdate" => $this->date,
  171. "thour" => $this->hour,
  172. ];
  173. $result = Db::connect('db_advert')->table("media_cost_hour")->field($filed)->where($where)->group($this->group)->select();
  174. $this->pushData($result);
  175. }
  176. }