GameTotalMonthLogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class GameTotalMonthLogic extends BaseLogic
  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 = is_array($params['date']) ? $params['date'][1] : $params['date'];
  15. }else{
  16. $sDate = date('Y-m');
  17. $eDate = date('Y-m');
  18. }
  19. for ($date = $sDate; $date <= $eDate; $date = date('Y-m', strtotime($date . '+1 month'))){
  20. $this->month = $date;
  21. try {
  22. $this->initStart();
  23. }catch (\Exception $e){
  24. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  25. }
  26. }
  27. $this->reRun();
  28. return json_encode(["status"=>"success", "msg"=>""], 256);
  29. }
  30. // 重跑
  31. protected function reRun(): void
  32. {
  33. $day = date('d');
  34. $hour = date('H');
  35. $i = date('i');
  36. // 重跑上个整月
  37. if(($day==1 || $day==15) && $hour==4 && $i<10){
  38. $this->month = date('Y-m', strtotime("-1 month"));
  39. $this->initStart();
  40. }
  41. }
  42. protected function initStart(): void
  43. {
  44. // 重置数据
  45. $this->data = [];
  46. $this->regMonth();
  47. $this->loginMonth();
  48. $this->regPay();
  49. $this->payTotal();
  50. $this->roleCreate();
  51. $this->mediaCost();
  52. $this->replaceData(['tmonth'=>$this->month]);
  53. }
  54. protected function regMonth()
  55. {
  56. $sTime = strtotime("{$this->month}-01 00:00:00");
  57. $eTime = strtotime(date('Y-m-t 23:59:59', strtotime($sTime)));
  58. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->month));
  59. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  60. $where = [
  61. ['reg_time', 'between', [$sTime, $eTime]],
  62. ];
  63. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  64. $this->pushData($result);
  65. }
  66. protected function loginMonth()
  67. {
  68. $tb = "sdk_active_log_" . date('Ym', strtotime($this->month)); // 按天去重
  69. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, count(distinct uid) as login_total";
  70. $result = Db::connect('db_game_log')->table($tb)->field($filed)->group($this->group)->select();
  71. $this->pushData($result);
  72. }
  73. protected function regPay()
  74. {
  75. $tb = "sdk_order_success";
  76. $sDate = "{$this->month}-01 00:00:00";
  77. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  78. $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";
  79. $where = [
  80. ['reg_date', 'between', [$sDate, $eDate]],
  81. ['pay_date', 'between', [$sDate, $eDate]],
  82. ];
  83. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  84. $this->pushData($result);
  85. }
  86. protected function payTotal()
  87. {
  88. $tb = "sdk_order_success";
  89. $sDate = "{$this->month}-01 00:00:00";
  90. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  91. $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";
  92. $where = [
  93. ['pay_date', 'between', [$sDate, $eDate]],
  94. ];
  95. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  96. $this->pushData($result);
  97. }
  98. protected function roleCreate(): void
  99. {
  100. $sDate = "{$this->month}-01 00:00:00";
  101. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  102. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, COUNT(DISTINCT uid) as role_create_user";
  103. $where = [
  104. ['create_time', 'between', [$sDate, $eDate]],
  105. ];
  106. // 安卓
  107. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  108. $this->pushData($result);
  109. // iOS
  110. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  111. $this->pushData($result);
  112. }
  113. protected function mediaCost(): void
  114. {
  115. $sDate = "{$this->month}-01";
  116. $eDate = date('Y-m-t', strtotime($this->month));
  117. $filed = "game_id, agent_id, site_id, '{$this->month}' as tmonth, SUM(money) as cost";
  118. $where = [
  119. ['tdate', 'between', [$sDate, $eDate]],
  120. ];
  121. $result = Db::connect('db_advert')->table("media_cost")->field($filed)->where($where)->group($this->group)->select();
  122. $this->pushData($result);
  123. }
  124. }