SyncWmsCommoditiesInformation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\CommodityService;
  4. use App\ValueStore;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Facades\Cache;
  8. class SyncWmsCommoditiesInformation extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'SyncWmsCommoditiesInformation';
  16. private $task_start_at;
  17. private $task_end_at;
  18. private $restart;
  19. private $is_enabled;
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '同步WMS的商品信息';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. public function init()
  36. {
  37. $this->task_start_at = config('sync.commodity_sync.task_start_at');
  38. $this->task_end_at = config('sync.commodity_sync.task_end_at');
  39. $this->restart = config('sync.commodity_sync.restart');
  40. $this->is_enabled= config('sync.commodity_sync.enabled');
  41. }
  42. /**
  43. * Execute the console command.
  44. */
  45. public function handle()
  46. {
  47. $this->init();
  48. if($this->is_enabled==false)return;
  49. sleep(rand(5,20));
  50. $start_time = Cache::remember($this->task_start_at,null,function (){
  51. return ValueStore::query()->firstOrCreate(['name'=>$this->task_start_at])->first()->value;
  52. });
  53. $end_time = Cache::remember($this->task_end_at,null,function (){
  54. return ValueStore::query()->firstOrCreate(['name'=>$this->task_end_at])->first()->value;
  55. });
  56. $start = Carbon::now();
  57. // 判断上一次任务异常了
  58. //1.第一次就异常了
  59. if(isset($start_time) && empty($end_time) && $start->diffInMinutes(Carbon::parse($start_time)) < $this->restart)return;
  60. //2.中间某次异常了
  61. if(isset($start_time) && isset($end_time)
  62. && Carbon::parse($end_time)->lt(Carbon::parse($start_time))
  63. && $start->diffInMinutes(Carbon::parse($start_time)) < $this->restart)
  64. return;
  65. $start = (string)$start;
  66. Cache::put($this->task_start_at,$start);
  67. ValueStore::query()->where('name',$this->task_start_at)->update(['value'=>$start]);
  68. $this->SyncWmsCommoditiesInformation();
  69. $end = (string)Carbon::now();
  70. Cache::put($this->task_end_at,$end);
  71. ValueStore::query()->where('name',$this->task_end_at)->update(['value'=>$end]);
  72. }
  73. public function SyncWmsCommoditiesInformation(){
  74. /** @var CommodityService $commodityService */
  75. $commodityService = app(CommodityService::class);
  76. $commodityService->syncCommodityCreated();
  77. $commodityService->syncCommodityUpdated();
  78. }
  79. }