SyncBatchTask.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\BatchService;
  4. use App\Services\common\BatchUpdateService;
  5. use App\Services\DocWaveHeaderService;
  6. use App\Services\LogService;
  7. use Illuminate\Console\Command;
  8. class SyncBatchTask extends Command
  9. {
  10. protected $signature = 'sync:batch';
  11. protected $description = 'sync wms batch task';
  12. /** @var DocWaveHeaderService $service */
  13. private $service;
  14. /** @var BatchService $batchService */
  15. private $batchService;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->service = app(DocWaveHeaderService::class);
  20. $this->batchService = app(BatchService::class);
  21. }
  22. public function handle()
  23. {
  24. $this->dispose();
  25. }
  26. private function dispose()
  27. {
  28. //获取更新时间与WMS数据
  29. $date = $this->service->getSyncDate();
  30. $waves = $this->service->get(["edittime"=>$date],["edittime"=>"gtOrEqual"]);
  31. if (!$waves)return;
  32. //获取本地数据对比差异
  33. $codes = array_column($waves->toArray(),"waveno");
  34. $map = [];
  35. $batches = $this->batchService->get(["code"=>$codes]);
  36. if ($batches){
  37. foreach ($batches as $batch)$map[$batch->code] = $batch->id;
  38. }
  39. $update = [["id","status","remark","updated_at"]];
  40. $insert = [];
  41. foreach ($waves as $wave){
  42. $status = $wave->wavestatus == '40' ? "未处理" : ($wave->wavestatus == '90' ? '取消' : '已处理');
  43. if (isset($map[$wave->waveno])){
  44. $update[] = [
  45. "id" => $map[$wave->waveno],
  46. "status" => $status,
  47. "remark"=>$wave->descr,
  48. "updated_at"=>$wave->edittime,
  49. ];
  50. continue;
  51. }
  52. $insert[] = [
  53. "code" => $wave->waveno,
  54. "status" => $status,
  55. "remark"=>$wave->descr,
  56. "created_at"=>$wave->addtime,
  57. "updated_at"=>$wave->edittime,
  58. ];
  59. }
  60. //存在则更新
  61. if (count($update)>1){
  62. $bool = app(BatchUpdateService::class)->batchUpdate("batches",$update);
  63. if ($bool)LogService::log(__METHOD__,"SUCCESS-同步更新波次成功",json_encode($update));
  64. else{
  65. LogService::log(__METHOD__,"ERROR-同步更新波次失败",json_encode($update));
  66. return;
  67. }
  68. }
  69. //不存在则录入
  70. if ($insert){
  71. $this->batchService->insert($insert);
  72. LogService::log(__METHOD__,"SUCCESS-同步插入波次成功",json_encode($insert));
  73. }
  74. $lastDate = $waves[0]->edittime;
  75. $this->service->setSyncDate($lastDate);
  76. }
  77. }