BaseTotalHourLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class BaseTotalHourLogic
  5. {
  6. protected string $table = "base_total_hour";
  7. protected string $group = 'game_id, agent_id, site_id';
  8. protected array $data;
  9. protected string $date;
  10. protected string $hour;
  11. public function run($params=[])
  12. {
  13. if(!empty($params['date'])){
  14. $sDate = is_array($params['date']) ? $params['date'][0] : $params['date'];
  15. $eDate = is_array($params['date']) ? $params['date'][1] : $params['date'];
  16. $runDate = 1;
  17. }else{
  18. $sDate = date('Y-m-d');
  19. $eDate = date('Y-m-d');
  20. $runDate = 0;
  21. }
  22. for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
  23. $this->date = $date;
  24. $this->table = $this->table . "_" . date('Ym', strtotime($this->date));
  25. try {
  26. if($runDate){
  27. for ($hour = 0; $hour <= 23; $hour++){
  28. $this->hour = $hour;
  29. $this->initStart();
  30. }
  31. }else{
  32. $this->hour = date("H", time()-480);
  33. $this->initStart();
  34. }
  35. }catch (\Exception $e){
  36. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  37. }
  38. }
  39. $this->reRun();
  40. return json_encode(["status"=>"success", "msg"=>""], 256);
  41. }
  42. // 重跑
  43. protected function reRun(): void
  44. {
  45. if (date('H') == 8 && date('i') < 10) {
  46. for ($sHour = 0; $sHour <= 23; $sHour++) {
  47. $this->hour = $sHour;
  48. $this->initStart();
  49. }
  50. }
  51. }
  52. protected function initStart(): void
  53. {
  54. // 重置数据
  55. $this->data = [];
  56. $this->regData();
  57. $this->loginData();
  58. $this->oldLogin();
  59. $this->regPay();
  60. $this->payTotal();
  61. $this->roleCreate();
  62. $this->mediaCost();
  63. $this->siteAuth();
  64. $this->replaceData();
  65. }
  66. protected function regData()
  67. {
  68. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  69. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  70. $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->date));
  71. $filed = "{$this->group}, count(distinct uid) as reg_total, count(distinct imei) as reg_dev";
  72. $where = [
  73. ['reg_time', 'between', [$sTime, $eTime]],
  74. ];
  75. $result = Db::connect('db_data_report')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
  76. $this->pushData($result);
  77. }
  78. protected function loginData()
  79. {
  80. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  81. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  82. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  83. $filed = "{$this->group},count(distinct uid) as login_total";
  84. $where = [
  85. ['login_time', 'between', [$sTime, $eTime]],
  86. ];
  87. $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  88. $this->pushData($result);
  89. }
  90. protected function oldLogin()
  91. {
  92. $sTime = strtotime("{$this->date} {$this->hour}:00:00");
  93. $eTime = strtotime("{$this->date} {$this->hour}:59:59");
  94. $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date));
  95. $filed = "{$this->group},count(distinct uid) as old_login_total";
  96. $where = [
  97. ['login_time', 'between', [$sTime, $eTime]],
  98. ['reg_time', '<', strtotime($this->date)],
  99. ];
  100. $result = Db::connect('db_data_report')->table($loginTb)->field($filed)->where($where)->group($this->group)->select();
  101. $this->pushData($result);
  102. }
  103. // 付费统计 - 当天注册
  104. protected function regPay()
  105. {
  106. $tb = "sdk_order_success";
  107. $sTime = "{$this->date} {$this->hour}:00:00";
  108. $eTime = "{$this->date} {$this->hour}:59:59";
  109. $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num";
  110. $where = [
  111. ['reg_date', 'between', [$sTime, $eTime]],
  112. ['pay_date', 'between', [$sTime, $eTime]],
  113. ];
  114. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  115. $this->pushData($result);
  116. }
  117. // 付费统计 - 总
  118. protected function payTotal(): void
  119. {
  120. $tb = "sdk_order_success";
  121. $sTime = "{$this->date} {$this->hour}:00:00";
  122. $eTime = "{$this->date} {$this->hour}:59:59";
  123. $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  124. $where = [
  125. ['pay_date', 'between', [$sTime, $eTime]],
  126. ];
  127. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
  128. $this->pushData($result);
  129. }
  130. /**
  131. * role_create_user 创建角色用户数
  132. */
  133. protected function roleCreate(): void
  134. {
  135. $sTime = "{$this->date} {$this->hour}:00:00";
  136. $eTime = "{$this->date} {$this->hour}:59:59";
  137. $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user";
  138. $where = [
  139. ['create_time', 'between', [$sTime, $eTime]],
  140. ];
  141. // 安卓
  142. $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
  143. $this->pushData($result);
  144. // iOS
  145. $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
  146. $this->pushData($result);
  147. }
  148. protected function mediaCost(): void
  149. {
  150. $filed = "{$this->group}, SUM(money) as cost";
  151. $where = [
  152. "tdate" => $this->date,
  153. "thour" => $this->hour,
  154. ];
  155. $result = Db::connect('db_advert')->table("media_cost_hour")->field($filed)->where($where)->group($this->group)->select();
  156. $this->pushData($result);
  157. }
  158. protected function siteAuth(): void
  159. {
  160. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  161. $agentListMap = array_column($agentList, null, "id");
  162. if($this->data){
  163. foreach ($this->data as &$item){
  164. $item['tdate'] = $this->date;
  165. $item['thour'] = $this->hour;
  166. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  167. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  168. }
  169. }
  170. }
  171. protected function replaceData(): bool
  172. {
  173. // 先删除,再写入
  174. Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date, 'thour' => $this->hour])->delete();
  175. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  176. }
  177. protected function pushData($rows): void
  178. {
  179. if ($rows) {
  180. foreach ($rows as $row) {
  181. $groupArr = explode(',', $this->group);
  182. $uniqueKey = "";
  183. foreach ($groupArr as $groupKey){
  184. $uniqueKey .= $row[$groupKey] . "-";
  185. }
  186. foreach ($row as $k => $v) {
  187. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  188. }
  189. }
  190. }
  191. }
  192. }