SyncBatchTask.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $owner = app("OwnerService")->codeGetOwner($wave->customerid);
  53. $insert[] = [
  54. "code" => $wave->waveno,
  55. "status" => $status,
  56. "remark"=>$wave->descr,
  57. "created_at"=>$wave->addtime,
  58. "updated_at"=>$wave->edittime,
  59. "owner_id"=>$owner->id,
  60. ];
  61. }
  62. //存在则更新
  63. if (count($update)>1){
  64. $bool = app(BatchUpdateService::class)->batchUpdate("batches",$update);
  65. if ($bool)LogService::log(__METHOD__,"SUCCESS-同步更新波次成功",json_encode($update));
  66. else{
  67. LogService::log(__METHOD__,"ERROR-同步更新波次失败",json_encode($update));
  68. return;
  69. }
  70. }
  71. //不存在则录入
  72. if ($insert){
  73. $this->batchService->insert($insert);
  74. LogService::log(__METHOD__,"SUCCESS-同步插入波次成功",json_encode($insert));
  75. }
  76. $lastDate = $waves[0]->edittime;
  77. $this->service->setSyncDate($lastDate);
  78. }
  79. }