GameTotalMonthLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class GameTotalMonthLogic
  5. {
  6. protected string $table = "game_total_month";
  7. protected string $group = 'game_id, agent_id, site_id';
  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->siteAuth();
  53. $this->replaceData();
  54. }
  55. protected function regMonth()
  56. {
  57. $sTime = strtotime("{$this->month}-01 00:00:00");
  58. $eTime = strtotime(date('Y-m-t 23:59:59', strtotime($sTime)));
  59. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->month));
  60. $filed = "{$this->group}, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  61. $where = [
  62. ['reg_time', 'between', [$sTime, $eTime]],
  63. ];
  64. $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  65. $this->pushData($result);
  66. }
  67. protected function loginMonth()
  68. {
  69. $tb = "sdk_active_log_" . date('Ym', strtotime($this->month)); // 按天去重
  70. $filed = "{$this->group}, count(distinct uid) as login_total";
  71. $result = Db::connect('db_game_log')->table($tb)->field($filed)->group($this->group)->select();
  72. $this->pushData($result);
  73. }
  74. protected function regPay()
  75. {
  76. $tb = "sdk_order_success";
  77. $sDate = "{$this->month}-01 00:00:00";
  78. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  79. $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num";
  80. $where = [
  81. 'reg_date' => ['between', [$sDate, $eDate]],
  82. 'pay_date' => ['between', [$sDate, $eDate]],
  83. ];
  84. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  85. $this->pushData($result);
  86. }
  87. protected function payTotal()
  88. {
  89. $tb = "sdk_order_success";
  90. $sDate = "{$this->month}-01 00:00:00";
  91. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  92. $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  93. $where = [
  94. 'pay_date' => ['between', [$sDate, $eDate]],
  95. ];
  96. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  97. $this->pushData($result);
  98. }
  99. protected function roleCreate(): void
  100. {
  101. $sDate = "{$this->month}-01 00:00:00";
  102. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  103. $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user";
  104. $where = [
  105. ['create_time', 'between', [$sDate, $eDate]],
  106. ];
  107. // 安卓
  108. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  109. $this->pushData($result);
  110. // iOS
  111. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  112. $this->pushData($result);
  113. }
  114. protected function mediaCost(): void
  115. {
  116. $sDate = "{$this->month}-01";
  117. $eDate = date('Y-m-t', strtotime($this->month));
  118. $filed = "{$this->group}, SUM(money) as cost";
  119. $where = [
  120. 'tdate' => ['between', [$sDate, $eDate]],
  121. ];
  122. $result = Db::connect('db_advert')->table("media_cost")->field($filed)->where($where)->group($this->group)->select();
  123. $this->pushData($result);
  124. }
  125. protected function siteAuth(): void
  126. {
  127. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  128. $agentListMap = array_column($agentList, null, "id");
  129. if($this->data){
  130. foreach ($this->data as &$item){
  131. $item['tdate'] = $this->month;
  132. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  133. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  134. }
  135. }
  136. }
  137. protected function replaceData(): bool
  138. {
  139. // 先删除,再写入
  140. Db::connect('db_data_report')->table($this->table)->where(['tmonth' => $this->month])->delete();
  141. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  142. }
  143. protected function pushData($rows): void
  144. {
  145. if ($rows) {
  146. foreach ($rows as $row) {
  147. $groupArr = explode(',', $this->group);
  148. $uniqueKey = "";
  149. foreach ($groupArr as $groupKey){
  150. $uniqueKey .= $row[$groupKey] . "-";
  151. }
  152. foreach ($row as $k => $v) {
  153. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  154. }
  155. }
  156. }
  157. }
  158. }