123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\OfficialAccount;
- use App\Models\OfficialWebUserActionSetId;
- use App\Models\OrderSummary;
- use App\Service\OrderSummaryService;
- use Illuminate\Console\Command;
- class OrderSummaryStat extends Command
- {
- protected $signature = 'OrderSummaryStat {type?} {ref_date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
- protected $description = '订单每日概要数据统计';
- protected $type;
- protected $dateTime;
- protected $date = false;
- protected $limit = 50;
- protected $startId = 0;
- protected $appIdList = [];
- protected $adqAccountList = [];
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $time = date('m-d H:i:s');
- $this->info($time . ' 开始整理');
- $this->type = $this->argument('type') ? $this->argument('type') : 2;
- $this->dateTime = $this->argument('ref_date') ? $this->argument('ref_date') : null;
- // mp账号数据
- $this->mpData();
- // adq 账号数据
- $this->adqData();
- $this->info($time . ' 整理结束');
- return true;
- }
- private function mpData()
- {
- # 获取需要统计的公众账号列表
- do{
- $this->info('mp账号起始ID:'.$this->startId);
- $accountList = OfficialAccount::query()->selectRaw('id, mp_app_id as app_id')->where('enable', 1)
- ->where('id', '>', $this->startId)->limit($this->limit)->orderBy('id')->get();
- if(empty($accountList))
- break;
- $count = $accountList->count();
- $this->appIdList = $accountList->pluck('app_id');
- $this->startId = $accountList->max('id');
- $this->info('本次获取到的公众账号个数:'.$count);
- $this->getRefDate(1);
- } while ($count == $this->limit);
- }
- private function adqData()
- {
- # 获取需要统计的adq账号列表
- $this->startId = 0;
- do{
- $this->info('adq账号起始ID:'.$this->startId);
- $adqAccountList = OfficialWebUserActionSetId::query()->selectRaw('id, account_id')->where('enable', 1)
- ->where('id', '>', $this->startId)->limit($this->limit)->orderBy('id')->get();
- if(empty($adqAccountList))
- break;
- $adqCount = $adqAccountList->count();
- $this->adqAccountList = $adqAccountList->pluck('account_id');
- $this->startId = $adqAccountList->max('id');
- $this->info('本次获取到的adq账号个数:'.$adqCount);
- $this->getRefDate(2);
- } while ($adqCount == $this->limit);
- }
- private function getRefDate($type)
- {
- try {
- if($this->type==1) {
- for ($month = 1; $month <= 12; $month++) {
- $days = getDaysByMonth($this->dateTime, $month);
- foreach ($days as $day) {
- if($day > date("Y-m-d"))
- continue 2;
- $this->date = $day;
- if(1 == $type) {
- $this->mpStat();
- } else {
- $this->adqStat();
- }
- }
- }
- } elseif ($this->type==2) {
- if($this->date === false) {
- if(is_null($this->dateTime)) {
- $this->date = date('Y-m-d');
- } else {
- $this->date = $this->dateTime;
- }
- }
- if(1 == $type) {
- $this->mpStat();
- } else {
- $this->adqStat();
- }
- } elseif($this->type==3) {
- $this->date = date('Y-m-d', strtotime('-1 days'));
- if(1 == $type) {
- $this->mpStat();
- } else {
- $this->adqStat();
- }
- }
- } catch(\Exception $e) {
- Log::logError('统计日期处理发生异常', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine()
- ], 'AccountGetWithOrderSummary');
- }
- return true;
- }
- private function mpStat()
- {
- $this->info($this->date);
- # 按平台从订单表中获取某个公众号某一天的
- $this->info('需要统计的账号列表为:'.json_encode($this->appIdList).'【'.$this->date.'】');
- $statData = OrderSummaryService::mpStatByDate($this->appIdList, $this->date);
- # 数据写入到dj_order_summary表
- foreach ($statData as $datum) {
- $orderSource = $datum->order_source ?? '';
- $appId = $datum->bind_app_id ?? '';
- $corpid = $datum->corpid ?? '';
- if(empty($orderSource) || empty($appId) || empty($corpid)) continue;
- $insertData = [
- 'order_count' => $datum->order_count ?? 0,
- 'pay_uv' => $datum->pay_uv ?? 0,
- 'pay_pv' => $datum->pay_pv ?? 0,
- 'pay_money' => $datum->pay_money ? $datum->pay_money / 100 : 0,
- ];
- $result = OrderSummary::query()->updateOrCreate([
- 'order_source' => $orderSource, 'app_id' => $appId, 'ref_date' => $this->date, 'corpid' => $corpid
- ] , $insertData);
- if(!$result) {
- Log::logError('订单数据统计结果更新至表失败', $datum, 'AccountGetWithOrderSummary');
- }
- }
- return true;
- }
- private function adqStat()
- {
- $this->info($this->date);
- # 按平台从订单表中获取某个公众号某一天的
- $this->info('需要统计的账号列表为:'.json_encode($this->adqAccountList).'【'.$this->date.'】');
- $statData = OrderSummaryService::adqStatByDate($this->adqAccountList, $this->date);
- # 数据写入到dj_order_summary表
- foreach ($statData as $datum) {
- $orderSource = $datum->order_source ?? '';
- $accountId = $datum->adq_account_id ?? '';
- $corpid = $datum->corpid ?? '';
- if(empty($orderSource) || empty($accountId) || empty($corpid)) continue;
- $insertData = [
- 'order_count' => $datum->order_count ?? 0,
- 'pay_uv' => $datum->pay_uv ?? 0,
- 'pay_pv' => $datum->pay_pv ?? 0,
- 'pay_money' => $datum->pay_money ? $datum->pay_money / 100 : 0,
- ];
- $result = OrderSummary::query()->updateOrCreate([
- 'order_source' => $orderSource, 'account_id' => $accountId, 'ref_date' => $this->date, 'corpid' => $corpid
- ] , $insertData);
- if(!$result) {
- Log::logError('订单数据统计结果更新至表失败', $datum, 'AccountGetWithOrderSummary');
- }
- }
- return true;
- }
- }
|