BaseTotalDayDataReportLogic.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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->regData();
  65. $this->loginData();
  66. $this->oldLogin();
  67. $this->regPay();
  68. $this->payTotal();
  69. $this->roleCreate();
  70. $this->replaceData(['tdate'=>$this->date]);
  71. }
  72. protected function regData()
  73. {
  74. $sTime = strtotime("{$this->date} 00:00:00");
  75. $eTime = strtotime("{$this->date} 23:59:59");
  76. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->date));
  77. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  78. $where = [
  79. ['reg_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 loginData()
  85. {
  86. $sTime = strtotime("{$this->date} 00:00:00");
  87. $eTime = strtotime("{$this->date} 23:59:59");
  88. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  89. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate,count(distinct uid) as login_total";
  90. $where = [
  91. ['login_time', 'between', [$sTime, $eTime]],
  92. ];
  93. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  94. $this->pushData($result);
  95. }
  96. protected function oldLogin()
  97. {
  98. $sTime = strtotime("{$this->date} 00:00:00");
  99. $eTime = strtotime("{$this->date} 23:59:59");
  100. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  101. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate,count(distinct uid) as old_login_total";
  102. $where = [
  103. ['login_time', 'between', [$sTime, $eTime]],
  104. ['reg_time', '<', strtotime($this->date)],
  105. ];
  106. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  107. $this->pushData($result);
  108. }
  109. // 付费统计 - 当天注册
  110. protected function regPay()
  111. {
  112. $tb = "sdk_order_success";
  113. $sDate = "{$this->date} 00:00:00";
  114. $eDate = "{$this->date} 23:59:59";
  115. $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";
  116. $where = [
  117. ['reg_date', 'between', [$sDate, $eDate]],
  118. ['pay_date', 'between', [$sDate, $eDate]],
  119. ];
  120. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  121. $this->pushData($result);
  122. }
  123. // 付费统计 - 总
  124. protected function payTotal(): void
  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 pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  130. $where = [
  131. ['pay_date', 'between', [$sDate, $eDate]],
  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. * role_create_user 创建角色用户数
  138. */
  139. protected function roleCreate(): void
  140. {
  141. $sTime = $this->date . " 00:00:00";
  142. $eTime = $this->date . " 23:59:59";
  143. $filed = "game_id, agent_id, site_id, '{$this->date}' as tdate, COUNT(DISTINCT uid) as role_create_user";
  144. $where = [
  145. ['create_time', 'between', [$sTime, $eTime]],
  146. ];
  147. // 安卓
  148. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  149. $this->pushData($result);
  150. // iOS
  151. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  152. $this->pushData($result);
  153. }
  154. }