小说推广数据系统

NovelOrderBindAccount.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\Order;
  6. use Illuminate\Console\Command;
  7. class NovelOrderBindAccount extends Command
  8. {
  9. protected $signature = 'NovelOrderBindAccount';
  10. protected $description = '小说订单数据绑定AppId';
  11. protected $page = 1;
  12. protected $pageSize = 500;
  13. protected $id = 0;
  14. public function handle()
  15. {
  16. \DB::connection()->disableQueryLog();
  17. $this->info(date('m-d H:i:s') . ' 开始整理');
  18. $this->deal();
  19. $this->info(date('m-d H:i:s') . ' 整理结束');
  20. }
  21. private function deal()
  22. {
  23. try {
  24. # 获取公众号数据
  25. $channelList = Account::select(['channel_id', 'app_id', 'platform_id'])
  26. ->get();
  27. do{
  28. $this->info('当前页码数【'.$this->page.'】');
  29. $this->info('本次最大的记录ID【'.$this->id.'】');
  30. $orderList = Order::select(['id', 'channel_id', 'platform_id'])
  31. ->whereNull('app_id')->orderBy('id')
  32. ->where('id', '>', $this->id)
  33. ->limit($this->pageSize)->get();
  34. $this->id = $orderList->max('id');
  35. # 处理数据
  36. foreach ($orderList as $order) {
  37. $platformId = isset($order->platform_id) ? $order->platform_id : null;
  38. $channelId = isset($order->channel_id) ? $order->channel_id : null;
  39. $id = isset($order->id) ? $order->id : null;
  40. if(empty($platformId) || empty($channelId) || empty($id)) {
  41. Log::logError('(小说)订单数据绑定AppId订单数据异常', $order, 'NovelOrderBindAccount');
  42. continue;
  43. }
  44. $channelInfo = $channelList->where('platform_id', $platformId)->where('channel_id', $channelId)
  45. ->first();
  46. $appId = isset($channelInfo->app_id) ? $channelInfo->app_id : null;
  47. if(empty($appId)) {
  48. Log::logError('(小说)订单数据绑定AppId未找到', $order, 'NovelOrderBindAccount');
  49. continue;
  50. }
  51. # 更新AppId
  52. $result = Order::where('id', $id)->update(['app_id' => $appId]);
  53. if(!$result) {
  54. $this->info('更新失败,ID【'.$id.'】');
  55. }
  56. }
  57. $count = $orderList->count();
  58. $this->page++;
  59. } while($count == $this->pageSize);
  60. } catch(\Exception $e) {
  61. Log::logError('(小说)订单数据绑定AppId过程发生异常', [
  62. 'line' => $e->getLine(),
  63. 'msg' => $e->getMessage()
  64. ], 'NovelOrderBindAccount');
  65. }
  66. }
  67. }