BaseTotalDayLogic.php 7.1 KB

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