BaseTotalHourDataReportLogic.php 7.7 KB

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