GameTotalMonthDataReportLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\v1\logic\tool\dataReport;
  3. use support\think\Db;
  4. class GameTotalMonthDataReportLogic extends BaseDataReportLogic
  5. {
  6. protected string $table = "game_total_month";
  7. protected string $group = 'game_id, agent_id, site_id, tmonth';
  8. protected array $data;
  9. protected string $month;
  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');
  21. $eDate = date('Y-m');
  22. }
  23. for ($date = $sDate; $date <= $eDate; $date = date('Y-m', strtotime($date . '+1 month'))){
  24. $this->month = $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. // 重跑上个整月
  41. if(($day==1 || $day==15) && $hour==4 && $i<10){
  42. $this->month = date('Y-m', strtotime("-1 month"));
  43. $this->initStart();
  44. }
  45. }
  46. protected function initStart(): void
  47. {
  48. // 重置数据
  49. $this->data = [];
  50. $this->regMonth();
  51. $this->loginMonth();
  52. $this->regPay();
  53. $this->payTotal();
  54. $this->roleCreate();
  55. $this->mediaCost();
  56. $this->replaceData(['tmonth'=>$this->month]);
  57. }
  58. protected function regMonth()
  59. {
  60. $sTime = strtotime("{$this->month}-01 00:00:00");
  61. $eTime = strtotime(date('Y-m-t 23:59:59', strtotime($sTime)));
  62. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->month));
  63. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  64. $where = [
  65. ['reg_time', 'between', [$sTime, $eTime]],
  66. ];
  67. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  68. $this->pushData($result);
  69. }
  70. protected function loginMonth()
  71. {
  72. $tb = "sdk_active_log_" . date('Ym', strtotime($this->month)); // 按天去重
  73. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, count(distinct uid) as login_total";
  74. $result = Db::connect('db_game_log')->table($tb)->field($filed)->group($this->group)->select();
  75. $this->pushData($result);
  76. }
  77. protected function regPay()
  78. {
  79. $tb = "sdk_order_success";
  80. $sDate = "{$this->month}-01 00:00:00";
  81. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  82. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num";
  83. $where = [
  84. ['reg_date', 'between', [$sDate, $eDate]],
  85. ['pay_date', 'between', [$sDate, $eDate]],
  86. ];
  87. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  88. $this->pushData($result);
  89. }
  90. protected function payTotal()
  91. {
  92. $tb = "sdk_order_success";
  93. $sDate = "{$this->month}-01 00:00:00";
  94. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  95. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  96. $where = [
  97. ['pay_date', 'between', [$sDate, $eDate]],
  98. ];
  99. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  100. $this->pushData($result);
  101. }
  102. protected function roleCreate(): void
  103. {
  104. $sDate = "{$this->month}-01 00:00:00";
  105. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  106. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, COUNT(DISTINCT uid) as role_create_user";
  107. $where = [
  108. ['create_time', 'between', [$sDate, $eDate]],
  109. ];
  110. // 安卓
  111. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  112. $this->pushData($result);
  113. // iOS
  114. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  115. $this->pushData($result);
  116. }
  117. protected function mediaCost(): void
  118. {
  119. $sDate = "{$this->month}-01";
  120. $eDate = date('Y-m-t', strtotime($this->month));
  121. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, SUM(money) as cost";
  122. $where = [
  123. ['tdate', 'between', [$sDate, $eDate]],
  124. ];
  125. $result = Db::connect('db_advert')->table("media_cost")->field($filed)->where($where)->group($this->group)->select();
  126. $this->pushData($result);
  127. }
  128. }