date = $date; $this->channelUid = $channelUid; } /** * Execute the job. * * @return void */ public function handle() { $date = Carbon::parse($this->date)->toDateString(); if (is_null($this->channelUid)) { // 统计全部. $result = self::runJob($date); } else { // 统计渠道或者官方 $result = self::runJob($date, $this->channelUid); } } /** * 执行统计. * @param string $date 日期 * @param null $channelUid 渠道UID * @return bool */ protected function runJob($date, $channelUid = null) { $date = Carbon::parse($date)->toDateString(); $total = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid]); $android = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'A']); $ios = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'I']); $h5 = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'H']); // 单独扣量的 $deductionTotal = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'is_deduction' => 0]); $deductionAndroid = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'is_deduction' => 0]); $deductionIos = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'is_deduction' => 0]); $deductionH5 = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'is_deduction' => 0]); $item = [ 'date' => $date, 'total' => $total, 'android' => $android, 'ios' => $ios, 'h5' => $h5, 'deduction_total' => $deductionTotal, 'deduction_android' => $deductionAndroid, 'deduction_ios' => $deductionIos, 'deduction_h5' => $deductionH5, ]; $sqlChannelUid = ''; $item['mom_app_uid'] = ''; // 预先设置母包UID $item['agent_uid'] = ''; // 预先定义代理UID if (!is_null($channelUid)) { $item['channel_uid'] = $channelUid; $sqlChannelUid = $channelUid; if (!empty($item['channel_uid'])) { $channelObj = ChannelModel::where(['uid' => $item['channel_uid']])->first(); if (!empty($channelObj)) { // 补充母包UID $item['mom_app_uid'] = $channelObj->mom_app_uid; $item['agent_uid'] = $channelObj->agent_uid; } } $info = StatisticsNewUsersModel::whereDate('date', '=', $date) ->where('channel_uid', '=', $sqlChannelUid) ->first(); } else { $info = StatisticsNewUsersModel::whereDate('date', '=', $date) ->whereNull('channel_uid') ->where('mom_app_uid', '=', '') ->first(); } if (!empty($info)) { $info->total = $total; $info->android = $android; $info->ios = $ios; $info->h5 = $h5; $info->deduction_total = $deductionTotal; $info->deduction_android = $deductionAndroid; $info->deduction_ios = $deductionIos; $info->deduction_h5 = $deductionH5; $result = $info->save(); } else { $result = StatisticsNewUsersModel::create($item); } // Todo. self::notify(); return boolval($result); } /** * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg. */ private function notify() { // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg. } /** * 获取仓库. * @return StatisticsNewUserRepository */ private function getRepo() { if (!$this->repo instanceof StatisticsNewUserRepository) { $this->repo = new StatisticsNewUserRepository(); } return $this->repo; } }