BaseTotalDayDataReportLogic.php 6.9 KB

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