Selaa lähdekoodia

Merge branch 'zzd' of ssh://was.baoshi56.com:10022/var/git/bswas

 Conflicts:
	.gitignore
	app/Http/Controllers/TestController.php
LD 5 vuotta sitten
vanhempi
commit
28f5a6a881

+ 1 - 0
.gitignore

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

+ 5 - 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","owner_id",
     ];
     public function orders(){
         return $this->hasMany('App\Order','batch_id','id');
@@ -33,5 +33,9 @@ class Batch extends Model
         });
         return parent::delete(); // TODO: Change the autogenerated stub
     }
+    public function owner()
+    {
+        return $this->hasOne(Owner::class,"id","owner_id");
+    }
 
 }

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

@@ -2,42 +2,143 @@
 
 namespace App\Console\Commands;
 
+use App\Services\BatchService;
+use App\Services\CacheService;
+use App\Services\common\BatchUpdateService;
 use App\Services\DocWaveHeaderService;
+use App\Services\LogService;
+use App\ValueStore;
+use Carbon\Carbon;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
 
 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->disposeHeader();
+        $this->disposeDetail();
+    }
+
+    private function disposeHeader()
+    {
+        //获取更新时间与WMS数据
+        $date = $this->service->getSyncDate();
+        $waves = $this->service->get(["edittime"=>$date],["edittime"=>"gtOrEqual"]);
+        if (count($waves) < 1)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;
+            }
+            $owner = app("OwnerService")->codeGetOwner($wave->customerid);
+            $insert[] = [
+                "code" => $wave->waveno,
+                "status" => $status,
+                "remark"=>$wave->descr,
+                "created_at"=>$wave->addtime,
+                "updated_at"=>$wave->edittime,
+                "owner_id"=>$owner->id,
+            ];
+        }
 
+        //存在则更新
+        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);
+    }
+
+    public function disposeDetail()
+    {
+        $date = app(CacheService::class)->getOrExecute("wave_detail_last_sync_date",function (){
+            $valueStore = ValueStore::query()->where("name","wave_detail_last_sync_date")->first();
+            return $valueStore->value ?? Carbon::now()->subSeconds(65)->toDateTimeString();
+        });
+        $sql = "SELECT WM_CONCAT(ORDERNO) orderno,WAVENO FROM DOC_WAVE_DETAILS WHERE EDITTIME > TO_DATE(?,'yyyy-mm-dd hh24:mi:ss')  GROUP BY WAVENO";
+        $details = DB::connection("oracle")->select(DB::raw($sql),[$date]);
+        if (count($details) < 1)return;
+        $map = [];
+        $nos = [];
+        foreach ($details as $detail){
+            $map[$detail->waveno] = explode(",",$detail->orderno);
+            $nos[] = $detail->waveno;
+        }
+        $batches = $this->batchService->get(["code"=>$nos]);
+        foreach ($batches as $batch){
+            app("OrderService")->update(["code"=>$map[$batch->code]],["batch_id"=>$batch->id]);
+            unset($map[$batch->code]);
+        }
+        if ($map){
+            $waveCodes = array_keys($map);
+            $waves = $this->service->get(["waveno"=>$waveCodes],["waveno"=>"in"]);
+            $insert = [];
+            foreach ($waves as $wave){
+                $status = $wave->wavestatus == '40' ? "未处理" : ($wave->wavestatus == '90' ? '取消' : '已处理');
+                $owner = app("OwnerService")->codeGetOwner($wave->customerid);
+                $insert[] = [
+                    "code" => $wave->waveno,
+                    "status" => $status,
+                    "remark"=>$wave->descr,
+                    "created_at"=>$wave->addtime,
+                    "updated_at"=>$wave->edittime,
+                    "owner_id"=>$owner->id,
+                ];
+            }
+            if ($insert){
+                $this->batchService->insert($insert);
+                LogService::log(__METHOD__,"SUCCESS-同步插入波次成功",json_encode($insert));
+                $batches = $this->batchService->get(["code"=>$waveCodes]);
+                foreach ($batches as $batch){
+                    app("OrderService")->update(["code"=>$map[$batch->code]],["batch_id"=>$batch->id]);
+                }
+            }
+        }
 
+        ValueStore::query()->where("name","wave_last_sync_date")->update(["value"=>Carbon::now()->subSeconds(1)->toDateTimeString()]);
     }
 }

+ 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('sync:batch')->everyMinute();
     }
 
     /**

+ 2 - 0
app/Http/Controllers/TestController.php

@@ -46,10 +46,12 @@ use App\Services\OrderService;
 use App\Services\OrderTrackingOwnerService;
 use App\Services\OrderTrackingService;
 use App\Services\OwnerService;
+use App\Services\ProvinceService;
 use App\Services\StoreService;
 use App\Services\WarehouseService;
 use App\StoreCheckingReceiveItem;
 use App\User;
+use App\ValueStore;
 use App\Warehouse;
 use App\Waybill;
 use App\WaybillPriceModel;

+ 2 - 0
app/OwnerFeeDetail.php

@@ -24,6 +24,8 @@ class OwnerFeeDetail extends Model
         "work_fee",         //作业费
         "logistic_fee",     //物流费
         "created_at",       //创建时间
+        "outer_id",         //关联表ID
+        "outer_table_name", //关联表名
     ];
     public $timestamps = false;
 

+ 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);
+    }
+
+}

+ 15 - 0
app/Services/DocWaveHeaderService.php

@@ -26,6 +26,15 @@ Class DocWaveHeaderService
             case "gtOrEqual":
                 $query->where($column,">=",$value);
                 break;
+            case "gt":
+                $query->where($column,">",$value);
+                break;
+            case "raw":
+                $query->whereRaw($value);
+                break;
+            case "in":
+                $query->whereIn($column,$value);
+                break;
         }
         return $query;
     }
@@ -36,4 +45,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]);
+    }
 }

+ 41 - 0
app/Services/OrderService.php

@@ -9,6 +9,8 @@ use App\OracleDOCOrderHeader;
 use App\Order;
 use App\OrderIssue;
 use App\Owner;
+use App\OwnerPriceExpress;
+use App\OwnerPriceLogistic;
 use App\RejectedBill;
 use App\Services\common\BatchUpdateService;
 use App\Services\common\DataHandlerService;
@@ -1109,4 +1111,43 @@ class OrderService
             $orderService->setOrderSyncAt($renewal,$renewal_order->addTime,count($orderHeaders)>0);   // 更新时间
         }
     }
+
+    public function update(array $params, array $values)
+    {
+        $query = Order::query();
+        foreach ($params as $column=>$param){
+            if (is_array($param))$query->whereIn($column,$param);
+            else $query->where($column,$param);
+        }
+        return $query->update($values);
+    }
+
+    public function createInstantBill(Order $order):bool
+    {
+        //检查订单对象
+        if (!$order || $order->status != "订单完成")return false;
+        if (!$order->packages)$order->load("packages");
+
+        /** @var OwnerPriceExpressService $service */
+        $service = app("OwnerPriceExpressService");
+        $logistic_fee = 0;
+        foreach ($order->packages as $package){
+            $provinceName = $order->province;
+            $province = app(CacheService::class)->getOrExecute("province_".$provinceName,function ()use($provinceName){
+                return app("ProvinceService")->first(["name"=>$provinceName]);
+            },86400);
+            if (!$province){$logistic_fee = null;break;}
+
+            $fee = $service->matching($package->weight, $order->owner_id, $order->logistic_id, $province->id);
+            if ($fee<0){$logistic_fee = null;break;}
+
+            $logistic_fee += $fee;
+        }
+
+        $work_fee = 0;
+        /** @var OwnerPriceOperationService $service */
+        $service = app("OwnerPriceOperationService");
+        $service->matchRule($order,[]);
+
+    }
 }

+ 16 - 11
app/Services/OwnerPriceExpressService.php

@@ -101,7 +101,7 @@ Class OwnerPriceExpressService
     }
 
     /**
-     * CODE: -1:未找到计费模型
+     * CODE: -1:未找到计费模型 -2:重量无效
      *
      * @param double $weight
      * @param integer $owner_id
@@ -111,16 +111,21 @@ Class OwnerPriceExpressService
      */
     public function matching($weight, $owner_id, $logistic_id, $province_id)
     {
-        $model = OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
-            /** @var Builder $query */
-            $query->where("province_id",$province_id);
-        }])->whereHas("owners",function ($query)use($owner_id){
-            /** @var Builder $query */
-           $query->where("id",$owner_id);
-        })->whereHas("logistics",function ($query)use($logistic_id){
-            /** @var Builder $query */
-            $query->where("id",$logistic_id);
-        })->first();
+        if (!$weight)return -2;
+
+        $key = "price_express_".$province_id."_".$logistic_id."_".$owner_id;
+        $model = app(CacheService::class)->getOrExecute($key,function ()use($owner_id, $logistic_id, $province_id){
+            return OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
+                /** @var Builder $query */
+                $query->where("province_id",$province_id);
+            }])->whereHas("owners",function ($query)use($owner_id){
+                /** @var Builder $query */
+                $query->where("id",$owner_id);
+            })->whereHas("logistics",function ($query)use($logistic_id){
+                /** @var Builder $query */
+                $query->where("id",$logistic_id);
+            })->first();
+        });
         if (!$model || !$model->details)return -1;
         if ($weight < $model->initial_weight)$weight = $model->initial_weight;
         $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;

+ 7 - 0
app/Services/OwnerService.php

@@ -216,4 +216,11 @@ Class OwnerService
         return $builder;
     }
 
+    public function codeGetOwner($code)
+    {
+        return app(CacheService::class)->getOrExecute("owner_".$code,function ()use($code){
+            return Owner::query()->firstOrCreate(["code"=>$code],["code"=>$code,"name"=>$code]);
+        });
+    }
+
 }

+ 9 - 0
app/Services/ProvinceService.php

@@ -29,4 +29,13 @@ Class ProvinceService
         }
         return null;
     }
+
+    public function first(array $params)
+    {
+        $query = Province::query();
+        foreach ($params as $column=>$param){
+            $query->where($column,$param);
+        }
+        return $query->first();
+    }
 }

+ 9 - 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,10 @@ 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("中文描述");
+            $table->string("owner_id")->index()->nullable()->comment("外键货主");
+        });
     }
 
     /**
@@ -24,5 +28,9 @@ 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");
+            $table->dropColumn("owner_id");
+        });
     }
 }

+ 36 - 0
database/migrations/2020_11_30_153512_add_column_outer_table_owner_fee_details.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddColumnOuterTableOwnerFeeDetails extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        \App\ValueStore::query()->firstOrCreate(["name"=>"wave_detail_last_sync_date"]);
+        Schema::table('owner_fee_details', function (Blueprint $table) {
+            $table->bigInteger("outer_id")->nullable()->index()->comment("关联表ID");
+            $table->string("outer_table_name")->nullable()->comment("关联表名称");
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        \App\ValueStore::query()->where("name","wave_detail_last_sync_date")->delete();
+        Schema::table('owner_fee_details', function (Blueprint $table) {
+            $table->dropColumn("outer_id");
+            $table->dropColumn("outer_table_name");
+        });
+    }
+}