123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\Order;
- use Illuminate\Console\Command;
- class NovelOrderBindAccount extends Command
- {
- protected $signature = 'NovelOrderBindAccount';
- protected $description = '小说订单数据绑定AppId';
- protected $page = 1;
- protected $pageSize = 500;
- protected $id = 0;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->info(date('m-d H:i:s') . ' 开始整理');
- $this->deal();
- $this->info(date('m-d H:i:s') . ' 整理结束');
- }
- private function deal()
- {
- try {
- # 获取公众号数据
- $channelList = Account::select(['channel_id', 'app_id', 'platform_id'])
- ->get();
- do{
- $this->info('当前页码数【'.$this->page.'】');
- $this->info('本次最大的记录ID【'.$this->id.'】');
- $orderList = Order::select(['id', 'channel_id', 'platform_id'])
- ->whereNull('app_id')->orderBy('id')
- ->where('id', '>', $this->id)
- ->limit($this->pageSize)->get();
- $this->id = $orderList->max('id');
- # 处理数据
- foreach ($orderList as $order) {
- $platformId = isset($order->platform_id) ? $order->platform_id : null;
- $channelId = isset($order->channel_id) ? $order->channel_id : null;
- $id = isset($order->id) ? $order->id : null;
- if(empty($platformId) || empty($channelId) || empty($id)) {
- Log::logError('(小说)订单数据绑定AppId订单数据异常', $order, 'NovelOrderBindAccount');
- continue;
- }
- $channelInfo = $channelList->where('platform_id', $platformId)->where('channel_id', $channelId)
- ->first();
- $appId = isset($channelInfo->app_id) ? $channelInfo->app_id : null;
- if(empty($appId)) {
- Log::logError('(小说)订单数据绑定AppId未找到', $order, 'NovelOrderBindAccount');
- continue;
- }
- # 更新AppId
- $result = Order::where('id', $id)->update(['app_id' => $appId]);
- if(!$result) {
- $this->info('更新失败,ID【'.$id.'】');
- }
- }
- $count = $orderList->count();
- $this->page++;
- } while($count == $this->pageSize);
- } catch(\Exception $e) {
- Log::logError('(小说)订单数据绑定AppId过程发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'NovelOrderBindAccount');
- }
- }
- }
|