ExportTraces.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Good;
  4. use App\Trace;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Storage;
  7. class ExportTraces extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'export:traces {gid} {--queue}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '导出code';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->line('开始导出码');
  38. $id = $this->argument('gid');
  39. $good = Good::where('id', $id)->first();
  40. $name = $good->id . '.txt';
  41. if (Storage::disk('public')->exists($name)) {
  42. Storage::disk('public')->delete($name);
  43. }
  44. Trace::where('gid', $id)->select('id', 'key', 'gid', 'slug')->chunk(2000, function ($trace) use ($name, $good) {
  45. $trace = $trace->map(function ($v) use ($good) {
  46. $data[] = $v['key'];
  47. $data[] = config('app.url') . '/key/' . $v['slug'];
  48. return implode(',', $data);
  49. });
  50. $data = implode($trace->toArray(), "\r\n");
  51. Storage::disk('public')->append($name, $data);
  52. });
  53. $good->update([
  54. 'is_download' => 'T'
  55. ]);
  56. $this->line('导出完成');
  57. }
  58. }