date = $date; $this->table = $this->table . "_" . date('Y', strtotime($this->date)); try { $this->initStart(); }catch (\Exception $e){ return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256); } } $this->reRun(); return json_encode(["status"=>"success", "msg"=>""], 256); } // 重跑 protected function reRun(): void { $day = date('d'); $hour = date('H'); $i = date('i'); $t = date('t'); // 月末,重跑整月 if($day==$t){ if($hour==1 && $i<10){ for ($d=1; $d<=$t; $d++){ $this->date = date('Y-m-d', strtotime("-{$d} day")); $this->initStart(); } } }else{ // 重跑前3天 if($hour==8 && $i<10){ for ($d=1; $d<=3; $d++){ $this->date = date('Y-m-d', strtotime("-{$d} day")); $this->initStart(); } } } } protected function initStart(): void { // 重置数据 $this->data = []; $this->regData(); $this->loginData(); $this->oldLogin(); $this->regPay(); $this->payTotal(); $this->roleCreate(); $this->siteAuth(); $this->replaceData(); } protected function regData() { $sTime = strtotime("{$this->date} 00:00:00"); $eTime = strtotime("{$this->date} 23:59:59"); $regTotalTb = "sdk_reg_log_" . date('Ym', strtotime($this->date)); $filed = "{$this->group}, count(distinct uid) as reg_total, count(distinct imei) as reg_dev"; $where = [ ['reg_time', 'between', [$sTime, $eTime]], ]; $result = Db::connect('db_game_log')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } protected function loginData() { $sTime = strtotime("{$this->date} 00:00:00"); $eTime = strtotime("{$this->date} 23:59:59"); $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date)); $filed = "{$this->group},count(distinct uid) as login_total"; $where = [ ['login_time', 'between', [$sTime, $eTime]], ]; $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } protected function oldLogin() { $sTime = strtotime("{$this->date} 00:00:00"); $eTime = strtotime("{$this->date} 23:59:59"); $loginTb = "sdk_login_log_" . date('Ym', strtotime($this->date)); $filed = "{$this->group},count(distinct uid) as old_login_total"; $where = [ ['login_time', 'between', [$sTime, $eTime]], ['reg_time', '<', strtotime($this->date)], ]; $result = Db::connect('db_game_log')->table($loginTb)->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } // 付费统计 - 当天注册 protected function regPay() { $tb = "sdk_order_success"; $sDate = "{$this->date} 00:00:00"; $eDate = "{$this->date} 23:59:59"; $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct uid) as reg_pay_num"; $where = [ ['reg_date', 'between', [$sDate, $eDate]], ['pay_date', 'between', [$sDate, $eDate]], ]; $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } // 付费统计 - 总 protected function payTotal(): void { $tb = "sdk_order_success"; $sDate = "{$this->date} 00:00:00"; $eDate = "{$this->date} 23:59:59"; $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num"; $where = [ ['pay_date', 'between', [$sDate, $eDate]], ]; $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } /** * role_create_user 创建角色用户数 */ protected function roleCreate(): void { $sTime = $this->date . " 00:00:00"; $eTime = $this->date . " 23:59:59"; $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user"; $where = [ ['create_time', 'between', [$sTime, $eTime]], ]; // 安卓 $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); // iOS $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select(); $this->pushData($result); } protected function siteAuth(): void { $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id"); $agentListMap = array_column($agentList, null, "id"); if($this->data){ foreach ($this->data as &$item){ $item['tdate'] = $this->date; $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人 $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体 } } } protected function replaceData(): bool { // 先删除,再写入 Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete(); return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false; } protected function pushData($rows): void { if ($rows) { foreach ($rows as $row) { $groupArr = explode(',', $this->group); $uniqueKey = ""; foreach ($groupArr as $groupKey){ $uniqueKey .= $row[$groupKey] . "-"; } foreach ($row as $k => $v) { $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v; } } } } }