BaseTotalDayLogic.php 5.9 KB

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