SyncCarrier.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Logistic;
  4. use App\Services\common\BatchUpdateService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. class SyncCarrier extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'sync:carrier';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Sync carriers every hour';
  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 void
  34. */
  35. public function handle()
  36. {
  37. $carriers = DB::connection("oracle")->select(DB::raw("SELECT CUSTOMERID,CUSTOMER_TYPE,DESCR_C,CASE DESCR_E WHEN 'WL' THEN '物流' ELSE '快递' END AS TYPE FROM BAS_CUSTOMER WHERE CUSTOMER_TYPE = 'CA'"));
  38. $logistics = Logistic::query()->whereIn("code",array_column($carriers,"customerid"))->get();
  39. $changes = diff($carriers,$logistics,"code",["code"=>"customerid","name"=>"descr_c","type"=>"type"]);
  40. if ($changes['attached'])Logistic::query()->insert($changes['attached']);
  41. if ($changes['updated']){
  42. array_unshift($changes['updated'],["code","name","type"]);
  43. app(BatchUpdateService::class)->batchUpdate("logistics",$changes['updated']);
  44. }
  45. if ($changes["detached"])Logistic::query()->whereIn("code",$changes["detached"])->delete();
  46. }
  47. }