date = $date; $this->channelUid = $channelUid; } /** * Execute the job. * * @return void */ public function handle() { $date = Carbon::parse($this->date)->toDateString(); if (empty($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(); $orderTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid]); $orderSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]); $orderFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::FAIL]); $orderMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid]); $orderSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]); $androidTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']); $androidSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $androidFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::FAIL]); $androidMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']); $androidSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $iosTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']); $iosSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $iosFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::FAIL]); $iosMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']); $iosSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $h5Total = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']); $h5Success = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $h5Failed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::FAIL]); $h5Money = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']); $h5SuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $deductionTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]); $deductionSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); $deductionFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::FAIL]); $deductionMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]); $deductionSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); $item = [ 'date' => $date, 'order_total' => $orderTotal, 'order_success' => $orderSuccess, 'order_failed' => $orderFailed, 'order_money' => $orderMoney, 'order_success_money' => $orderSuccessMoney, 'android_total' => $androidTotal, 'android_success' => $androidSuccess, 'android_failed' => $androidFailed, 'android_money' => $androidMoney, 'android_success_money' => $androidSuccessMoney, 'ios_total' => $iosTotal, 'ios_success' => $iosSuccess, 'ios_failed' => $iosFailed, 'ios_money' => $iosMoney, 'ios_success_money' => $iosSuccessMoney, 'h5_total' => $h5Total, 'h5_success' => $h5Success, 'h5_failed' => $h5Failed, 'h5_money' => $h5Money, 'h5_success_money' => $h5SuccessMoney, 'deduction_total' => $deductionTotal, 'deduction_success' => $deductionSuccess, 'deduction_failed' => $deductionFailed, 'deduction_money' => $deductionMoney, 'deduction_success_money' => $deductionSuccessMoney, ]; $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 = StatisticsPayModel::whereDate('date', '=', $date) ->where('channel_uid', '=', $sqlChannelUid) ->first(); } else { $info = StatisticsPayModel::whereDate('date', '=', $date) ->whereNull('channel_uid') ->where('mom_app_uid', '=', '') ->first(); } if (!empty($info)) { $info->order_total = $orderTotal; $info->order_success = $orderSuccess; $info->order_failed = $orderFailed; $info->order_money = $orderMoney; $info->order_success_money = $orderSuccessMoney; $info->android_total = $androidTotal; $info->android_success = $androidSuccess; $info->android_failed = $androidFailed; $info->android_money = $androidMoney; $info->android_success_money = $androidSuccessMoney; $info->ios_total = $iosTotal; $info->ios_success = $iosSuccess; $info->ios_failed = $iosFailed; $info->ios_money = $iosMoney; $info->ios_success_money = $iosSuccessMoney; $info->h5_total = $h5Total; $info->h5_success = $h5Success; $info->h5_failed = $h5Failed; $info->h5_money = $h5Money; $info->h5_success_money = $h5SuccessMoney; $info->deduction_total = $deductionTotal; $info->deduction_success = $deductionSuccess; $info->deduction_failed = $deductionFailed; $info->deduction_money = $deductionMoney; $info->deduction_success_money = $deductionSuccessMoney; $result = $info->save(); } else { $result = StatisticsPayModel::create($item); } // Todo. self::notify(); return boolval($result); } /** * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg. */ private function notify() { // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg. } /** * 获取仓库. * @return StatisticsPayRepository */ private function getRepo() { if (!$this->repo instanceof StatisticsPayRepository) { $this->repo = new StatisticsPayRepository(); } return $this->repo; } }