123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\FansData;
- use App\Models\Order;
- use App\Models\SunnyTask;
- use App\Services\SunService;
- use Illuminate\Console\Command;
- class SunnyTaskFinish extends Command
- {
- protected $signature = 'SunnyTaskFinish {type}';
- protected $description = '阳关平台任务处理-存储响应数据到平台';
- protected $type;
- protected $typeData;
- protected $platform;
- public function __construct()
- {
- parent::__construct();
- $this->typeData = SunService::TYPE_DATA;
- $this->platform = SunService::PLATFORM_ID;
- }
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->type = $this->argument('type');
- if(!in_array($this->type, $this->typeData)) {
- $this->info('任务类型【'.$this->type.'】非法');
- return false;
- }
- $this->info(date('m-d H:i:s') . ' 开始整理');
- while(true){
- $result = $this->getTask();
- if($result===false) {
- sleep(240);
- }
- sleep(5);
- }
- }
- public function getTask()
- {
- $this->info($this->type);
- # 按照任务类型获取任务数据
- $taskInfo = SunnyTask::getTaskFile($this->type);
- if(empty($taskInfo)) return false;
- # 根据任务类型处理数据
- switch($this->type) {
- case SunnyTask::CHANNEL_TYPE:
- $result = $this->accountData($taskInfo);
- break;
- case SunnyTask::ORDER_TYPE:
- $result = $this->getOrderList($taskInfo);
- break;
- case SunnyTask::FANS_DATA_TYPE:
- $result = $this->getFansDayCollect($taskInfo);
- break;
- }
- return $result;
- }
- /*
- * 账号数据获取
- * */
- public function accountData($taskObj)
- {
- try {
- $this->info($taskObj->path);
- $handle = @fopen($taskObj->path, "r");
- $total = 0;
- if ($handle) {
- while (($buffer = fgets($handle, 4096)) !== false) {
- $total++;
- $item = json_decode($buffer, true);
- $insertData = [
- 'platform_id' => $this->platform,
- 'channel_id' => $item['channel_id'],
- 'site_domain' => $item['domain'],
- 'username' => $item['username'],
- 'name' => $item['wx_nickname'],
- 'app_id' => $item['app_id'],
- 'raw_id' => null,
- 'nickname' => $item['wx_nickname'],
- 'platform_created_at' => null
- ];
- # 创建或更新
- Account::updateOrCreate(
- ['platform_id'=>$this->platform, 'name'=>$item['wx_nickname']], $insertData
- );
- }
- if (!feof($handle)) {
- Log::logError('账号数据读取失败', $taskObj->toArray(), 'SunnyAccountData');
- }
- }
- $this->info('共有数据'.$total.'条');
- # 更新任务状态为已完成
- $result = SunnyTask::taskFinish($taskObj);
- if(!$result) {
- Log::logError('任务状态变更失败', $taskObj->toArray(), 'SunnyAccountData');
- }
- $this->info('此次获取条数:'.$total);
- } catch (\Exception $e) {
- Log::logError('阳光账号信息获取失败', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'task' => $taskObj->toArray()
- ], 'SunnyAccountData');
- return false;
- }
- return true;
- }
- /*
- * 订单数据获取
- * */
- public function getOrderList($taskObj)
- {
- try {
- $orderType = SunService::ORDER_TYPE;
- $orderStatus = SunService::ORDER_STATUS;
- $this->info($taskObj->path);
- $handle = @fopen($taskObj->path, "r");
- $total = 0;
- if ($handle) {
- # 获取渠道信息
- $channelList = Account::select(['channel_id', 'app_id'])
- ->where('enable', 1)
- ->where('platform_id', $this->platform)
- ->get();
- while (($buffer = fgets($handle, 4096)) !== false) {
- $total++;
- $item = json_decode($buffer, true);
- $type = isset($orderType[$item['type']]) ? $orderType[$item['type']] : null;
- $status = isset($orderStatus[$item['state']]) ? $orderStatus[$item['state']] : null;
- if(is_null($type) || is_null($status)) {
- Log::logError('订单数据异常', $item, 'SunOrderList');
- }
- $channelId = isset($item['channel_id']) ? $item['channel_id'] : null;
- $appIdInfo = $channelList->where('channel_id', $channelId)->first();
- $appId = isset($appIdInfo->app_id) ? $appIdInfo->app_id : null;
- $insertData = [
- 'platform_id' => $this->platform,
- 'channel_id' => $channelId,
- 'app_id' => $appId,
- 'order_id' => $item['merchant_id'],
- 'border_id' => $item['transaction_id'],
- 'type' => $type,
- 'member_openid' => isset($item['openid']) ? $item['openid'] : null,
- 'subscribed_at' => isset($item['subscribe_time']) ? $item['subscribe_time'] : null,
- 'user_created_at' => isset($item['user_createtime']) ? $item['user_createtime'] : null,
- 'product' => null,
- 'price' => $item['money'] * 100,
- 'status' => $status,
- 'agent_uid' => null,
- 'from_novel' => $item['book_name'],
- 'referral_link_id' => null,
- 'from_novel_id' => (int)$item['book_id'],
- 'platform_created_at' => $item['create_time'],
- 'paid_at' => isset($item['finish_time']) ? $item['finish_time'] : null,
- ];
- # 创建或更新
- Order::updateOrCreate(
- ['platform_id'=>$this->platform, 'order_id'=>$insertData['order_id'], 'channel_id'=>$insertData['channel_id']], $insertData
- );
- }
- if (!feof($handle)) {
- Log::logError('订单数据读取失败', $taskObj->toArray(), 'SunOrderList');
- }
- }
- $this->info('共有数据'.$total.'条');
- # 更新任务状态为已完成
- $result = SunnyTask::taskFinish($taskObj);
- if(!$result) {
- Log::logError('任务状态变更失败', $taskObj->toArray(), 'SunOrderList');
- }
- } catch (\Exception $e) {
- Log::logError('阳光账号信息获取失败', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'task' => $taskObj->toArray()
- ], 'SunOrderList');
- return false;
- }
- return true;
- }
- public function getFansDayCollect($taskObj)
- {
- try{
- $this->info($taskObj->path);
- $handle = @fopen($taskObj->path, "r");
- $total = 0;
- # 获取账号AppId和渠道号
- $accountData = Account::accountInfoByPlatform($this->platform);
- if ($handle) {
- while (($buffer = fgets($handle, 4096)) !== false) {
- $total++;
- $item = json_decode($buffer, true);
- $appId = isset($accountData[$item['channel_id']]['app_id']) ? $accountData[$item['channel_id']]['app_id'] : null;
- $date = isset($item['date']) ? $item['date'] : null;
- if(is_null($appId) || is_null($date)) {
- Log::logError('基础数据获取失败', $item, 'SunFansDayCollect');
- continue;
- }
- $date = date('Y-m-d', strtotime($date));
- $insertData = [
- 'app_id' => $appId,
- 'ref_date' => $date,
- 'cumulate_user' => null,
- 'new_user' => $item['increase'],
- 'cancel_user' => $item['unfollow_num'],
- 'net_increase' => $item['net_follow_num'],
- 'cancel_percent' => null,
- ];
- # 创建或更新
- FansData::updateOrCreate(
- ['app_id'=>$appId, 'ref_date'=>$date], $insertData
- );
- }
- if (!feof($handle)) {
- Log::logError('粉丝日数据读取失败', $taskObj->toArray(), 'SunFansDayCollect');
- }
- }
- $this->info('共有数据'.$total.'条');
- # 更新任务状态为已完成
- $result = SunnyTask::taskFinish($taskObj);
- if(!$result) {
- Log::logError('粉丝日数据任务变更失败', $taskObj->toArray(), 'SunFansDayCollect');
- }
- } catch (\Exception $e) {
- Log::logError('粉丝日数据获取失败', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'task' => $taskObj->toArray()
- ], 'SunFansDayCollect');
- return false;
- }
- return true;
- }
- }
|