1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\Order;
- use App\RedisModel;
- use Illuminate\Console\Command;
- class UserAttrAnalysis extends Command
- {
- protected $signature = 'UserAttrAnalysis';
- protected $description = '由订单数据进行用户属性分析';
- protected $page = 1; // 当前页码数
- protected $pageSize = 5000; // 每页显示条数
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->info(date('m-d H:i:s') . ' 开始整理');
- $this->analysis();
- $this->info(date('m-d H:i:s') . ' 整理结束');
- }
- private function analysis()
- {
- try {
- $accountList = RedisModel::getAfterDecode(Account::XM_ACCOUNT_LIST_CONFIRM);
- # 获取对应的平台类型ID和渠道ID
- $accountData = Account::getAccountInfo($accountList);
- foreach($accountList as $appId) {
- $accounts = $accountData->where('app_id', $appId);
- foreach($accounts as $accountInfo) {
- if(
- isset($accountInfo->platform_id) && isset($accountInfo->channel_id) &&
- !empty($accountInfo->platform_id) && !empty($accountInfo->channel_id)
- ) {
- $this->page = 1;
- do{
- # 统计账号粉丝的充值数据
- $data = Order::userAnalysis(
- $accountInfo->platform_id, $accountInfo->channel_id, $this->page, $this->pageSize
- );
- $count = $data->count();
- $this->info('本次共获取'.$count.'条数据');
- if($count) {
- # 存入Redis供消费
- RedisModel::lPush(Order::USER_ATTR_DATA, json_encode(['app_id' => $appId, 'data' => $data->toArray()]));
- } else {
- Log::logError('未查询到订单信息', [
- 'app_id' => $appId,
- 'platform_id' => $accountInfo->platform_id,
- 'channel_id' => $accountInfo->channel_id,
- 'page' => $this->page
- ], 'UserAttrAnalysis-account');
- }
- $this->page++;
- } while($count == $this->pageSize);
- } else {
- Log::logError('账号的渠道信息获取失败', [
- 'app_id' => $appId,
- 'account_info' => $accountInfo
- ], 'UserAttrAnalysis');
- }
- }
- }
- } catch (\Exception $e) {
- Log::logError('账号的粉丝数据分析失败', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine()
- ], 'UserAttrAnalysis-Exception');
- }
- }
- }
|