BaseTotalDayDataReportLogic.php 6.8 KB

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