Jelajahi Sumber

同步波次

Zhouzhendong 5 tahun lalu
induk
melakukan
c9bf6a5ddf

+ 1 - 0
.gitignore

@@ -26,3 +26,4 @@ yarn-error.log
 /.gitignore
 /public/icon/img404-thumbnail.jpg
 /public/phpMyAdmin4.8.5/
+/phpunit.xml

+ 1 - 1
app/Batch.php

@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model;
 class Batch extends Model
 {
     protected $fillable = [
-        'id','code','type', 'wms_type', 'status', 'wms_status', 'wms_created_at',
+        'id','code','type', 'wms_type', 'status', 'wms_status', 'wms_created_at',"remark",
     ];
     public function orders(){
         return $this->hasMany('App\Order','batch_id','id');

+ 65 - 19
app/Console/Commands/SyncBatchTask.php

@@ -2,42 +2,88 @@
 
 namespace App\Console\Commands;
 
+use App\Services\BatchService;
+use App\Services\common\BatchUpdateService;
 use App\Services\DocWaveHeaderService;
+use App\Services\LogService;
 use Illuminate\Console\Command;
 
 class SyncBatchTask extends Command
 {
-    /**
-     * @var string
-     */
     protected $signature = 'sync:batch';
 
-    /**
-     * @var string
-     */
     protected $description = 'sync wms batch task';
 
-    /**
-     * @return void
-     */
+    /** @var DocWaveHeaderService $service */
+    private $service;
+    /** @var BatchService $batchService */
+    private $batchService;
+
     public function __construct()
     {
         parent::__construct();
+        $this->service = app(DocWaveHeaderService::class);
+        $this->batchService = app(BatchService::class);
     }
 
-    /**
-     * Execute the console command.
-     *
-     * @return void
-     */
     public function handle()
     {
-        /** @var DocWaveHeaderService $service */
-        $service = app(DocWaveHeaderService::class);
-        $date = $service->getSyncDate();
-        $flux = $service->get(["edittime"=>$date],["edittime"=>"gtOrEqual"]);
-        if (!$flux)return;
+        $this->dispose();
+    }
+
+    private function dispose()
+    {
+        //获取更新时间与WMS数据
+        $date = $this->service->getSyncDate();
+        $waves = $this->service->get(["edittime"=>$date],["edittime"=>"gtOrEqual"]);
+        if (!$waves)return;
+
+        //获取本地数据对比差异
+        $codes = array_column($waves->toArray(),"waveno");
+        $map = [];
+        $batches = $this->batchService->get(["code"=>$codes]);
+        if ($batches){
+            foreach ($batches as $batch)$map[$batch->code] = $batch->id;
+        }
+        $update = [["id","status","remark","updated_at"]];
+        $insert = [];
+        foreach ($waves as $wave){
+            $status = $wave->wavestatus == '40' ? "未处理" : ($wave->wavestatus == '90' ? '取消' : '已处理');
+            if (isset($map[$wave->waveno])){
+                $update[] = [
+                    "id" => $map[$wave->waveno],
+                    "status" => $status,
+                    "remark"=>$wave->descr,
+                    "updated_at"=>$wave->edittime,
+                ];
+                continue;
+            }
+            $insert[] = [
+                "code" => $wave->waveno,
+                "status" => $status,
+                "remark"=>$wave->descr,
+                "created_at"=>$wave->addtime,
+                "updated_at"=>$wave->edittime,
+            ];
+        }
+
+        //存在则更新
+        if (count($update)>1){
+            $bool = app(BatchUpdateService::class)->batchUpdate("batches",$update);
+            if ($bool)LogService::log(__METHOD__,"SUCCESS-同步更新波次成功",json_encode($update));
+            else{
+                LogService::log(__METHOD__,"ERROR-同步更新波次失败",json_encode($update));
+                return;
+            }
+        }
 
+        //不存在则录入
+        if ($insert){
+            $this->batchService->insert($insert);
+            LogService::log(__METHOD__,"SUCCESS-同步插入波次成功",json_encode($insert));
+        }
 
+        $lastDate = $waves[0]->edittime;
+        $this->service->setSyncDate($lastDate);
     }
 }

+ 3 - 1
app/Console/Kernel.php

@@ -2,6 +2,7 @@
 
 namespace App\Console;
 
+use App\Console\Commands\SyncBatchTask;
 use Illuminate\Console\Scheduling\Schedule;
 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 
@@ -21,6 +22,7 @@ class Kernel extends ConsoleKernel
         \App\Console\Commands\SyncLogCacheTask::class,
         \App\Console\Commands\SyncUserVisitMenuLogsCacheTask::class,
         \App\Console\Commands\TestTemp::class,
+        SyncBatchTask::class,
     ];
 
     /**
@@ -34,12 +36,12 @@ class Kernel extends ConsoleKernel
         $schedule->command('LogExpireDelete')->dailyAt('00:01');
         $schedule->command('InventoryDailyLoggingOwner')->dailyAt('08:00');
         $schedule->command('FluxOrderFix')->hourlyAt(1);
-//        $schedule->command('FluxOrderFix')->cron('* * * * *');
         $schedule->command('WASSyncWMSOrderInformation')->everyMinute();
         $schedule->command('syncLogCacheTask')->everyMinute();
         $schedule->command('createOwnerReport')->monthlyOn(1);
         $schedule->command('createOwnerBillReport')->monthlyOn(1);
         $schedule->command('createOwnerAreaReport')->monthlyOn(25);
+        $schedule->command('syncBatchTask')->everyMinute();
     }
 
     /**

+ 3 - 1
app/Http/Controllers/TestController.php

@@ -48,6 +48,7 @@ use App\Services\StoreService;
 use App\Services\WarehouseService;
 use App\Unit;
 use App\User;
+use App\ValueStore;
 use App\Warehouse;
 use Carbon\Carbon;
 use Illuminate\Database\Eloquent\Builder;
@@ -96,7 +97,8 @@ class TestController extends Controller
         }
     }
     public function test4(){
-        dd(app(DocWaveHeaderService::class)->getSyncDate());
+        \Redis::set("test1","123");
+       dd(\Redis::get("test1"));
     }
     public function t($a)
     {

+ 24 - 0
app/Services/BatchService.php

@@ -0,0 +1,24 @@
+<?php 
+
+namespace App\Services; 
+
+use App\Batch;
+
+Class BatchService
+{ 
+    public function get(array $params)
+    {
+        $query = Batch::query();
+        foreach ($params as $column=>$param){
+            if (is_array($param))$query->whereIn($column,$param);
+            else $query->where($column,$param);
+        }
+        return $query->get();
+    }
+
+    public function insert(array $insert)
+    {
+        return Batch::query()->insert($insert);
+    }
+
+}

+ 12 - 0
app/Services/DocWaveHeaderService.php

@@ -26,6 +26,12 @@ Class DocWaveHeaderService
             case "gtOrEqual":
                 $query->where($column,">=",$value);
                 break;
+            case "gt":
+                $query->where($column,">",$value);
+                break;
+            case "raw":
+                $query->whereRaw($value);
+                break;
         }
         return $query;
     }
@@ -36,4 +42,10 @@ Class DocWaveHeaderService
         $valueStore = ValueStore::query()->where("name","wave_last_sync_date")->first();
         return $valueStore->value ?? Carbon::now()->subSeconds(65)->toDateTimeString();
     }
+
+    public function setSyncDate(string $value)
+    {
+        $valueStore = ValueStore::query()->where("name","wave_last_sync_date");
+        return $valueStore->update(["value"=>$value]);
+    }
 }

+ 7 - 1
database/migrations/2020_11_27_133112_save_value_store_data.php → database/migrations/2020_11_30_133112_save_value_store_data_date.php

@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
-class SaveValueStoreData extends Migration
+class SaveValueStoreDataDate extends Migration
 {
     /**
      * Run the migrations.
@@ -14,6 +14,9 @@ class SaveValueStoreData extends Migration
     public function up()
     {
         \App\ValueStore::query()->firstOrCreate(["name"=>"wave_last_sync_date"]);
+        Schema::table("batches",function (Blueprint $table){
+            $table->string("remark")->nullable()->comment("中文描述");
+        });
     }
 
     /**
@@ -24,5 +27,8 @@ class SaveValueStoreData extends Migration
     public function down()
     {
         \App\ValueStore::query()->where("name","wave_last_sync_date")->delete();
+        Schema::table("batches",function (Blueprint $table){
+            $table->dropColumn("remark");
+        });
     }
 }