BaseTotalDayLogic.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class BaseTotalDayLogic
  5. {
  6. protected string $table = "base_total_day";
  7. protected string $group = 'game_id, agent_id, site_id';
  8. protected array $data;
  9. protected string $date;
  10. public function run($params=[])
  11. {
  12. $sDate = $params['sdate'] ?? date('Y-m-d');
  13. $eDate = $params['edate'] ?? date('Y-m-d');
  14. for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
  15. $this->date = $date;
  16. $this->table = $this->table . "_" . date('Y', strtotime($this->date));
  17. try {
  18. $this->initStart();
  19. }catch (\Exception $e){
  20. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  21. }
  22. }
  23. return json_encode(["status"=>"success", "msg"=>""], 256);
  24. }
  25. protected function initStart(): void
  26. {
  27. // 重置数据
  28. $this->data = [];
  29. $this->regData();
  30. $this->loginData();
  31. $this->oldLoginData();
  32. $this->payRegData();
  33. $this->payTotalData();
  34. $this->roleCreateData();
  35. $this->siteAuth();
  36. $this->replaceData();
  37. }
  38. protected function regData()
  39. {
  40. $regTotalTb = "basic_reg_total_" . date('Y', strtotime($this->date));
  41. $filed = "{$this->group}, sum(reg_count) as reg_total, sum(login_count) as reg_login_total";
  42. $where = ['tdate' => $this->date];
  43. $result = Db::connect('db_data_report')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  44. $this->pushData($result);
  45. }
  46. protected function loginData()
  47. {
  48. $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
  49. $filed = "{$this->group},sum(login_count) as login_total";
  50. $where = ['tdate' => $this->date];
  51. $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group($this->group)->select();
  52. $this->pushData($result);
  53. }
  54. protected function oldLoginData()
  55. {
  56. $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
  57. $filed = "{$this->group},sum(login_count) as old_login_total";
  58. $where = [
  59. 'tdate' => $this->date,
  60. ['active', '>', 0]
  61. ];
  62. $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group('agent_id,site_id,game_id')->select();
  63. $this->pushData($result);
  64. }
  65. // 付费统计 - 当天注册
  66. protected function payRegData()
  67. {
  68. $tb = "sdk_order_success";
  69. $sDate = "{$this->date} 00:00:00";
  70. $eDate = "{$this->date} 23:59:59";
  71. $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct user_name) as reg_pay_num";
  72. $where = [
  73. ['reg_date', 'between', [$sDate, $eDate]],
  74. ['pay_date', 'between', [$sDate, $eDate]],
  75. ];
  76. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  77. $this->pushData($result);
  78. }
  79. // 付费统计 - 总
  80. protected function payTotalData(): void
  81. {
  82. $tb = "sdk_order_success";
  83. $sDate = "{$this->date} 00:00:00";
  84. $eDate = "{$this->date} 23:59:59";
  85. $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct user_name) as pay_num";
  86. $where = [
  87. ['pay_date', 'between', [$sDate, $eDate]],
  88. ];
  89. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  90. $this->pushData($result);
  91. }
  92. /**
  93. * role_create_user 创建角色用户数
  94. */
  95. protected function roleCreateData(): void
  96. {
  97. $sTime = $this->date . " 00:00:00";
  98. $eTime = $this->date . " 23:59:59";
  99. $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user";
  100. $where = [
  101. ['create_time', 'between', [$sTime, $eTime]],
  102. ];
  103. // 安卓
  104. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  105. $this->pushData($result);
  106. // iOS
  107. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  108. $this->pushData($result);
  109. }
  110. protected function siteAuth(): void
  111. {
  112. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  113. $agentListMap = array_column($agentList, null, "id");
  114. if($this->data){
  115. foreach ($this->data as &$item){
  116. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  117. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  118. }
  119. }
  120. }
  121. protected function replaceData(): bool
  122. {
  123. // 先删除,再写入
  124. Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete();
  125. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  126. }
  127. protected function pushData($rows): void
  128. {
  129. if ($rows) {
  130. foreach ($rows as $row) {
  131. $groupArr = explode(',', $this->group);
  132. $uniqueKey = "";
  133. foreach ($groupArr as $groupKey){
  134. $uniqueKey .= $row[$groupKey] . "-";
  135. }
  136. foreach ($row as $k => $v) {
  137. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  138. }
  139. }
  140. }
  141. }
  142. }