Ver Fonte

波次同步时修改订单波次信息

Zhouzhendong há 5 anos atrás
pai
commit
38c07675ca

+ 57 - 4
app/Console/Commands/SyncBatchTask.php

@@ -3,10 +3,14 @@
 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
 {
@@ -28,15 +32,16 @@ class SyncBatchTask extends Command
 
     public function handle()
     {
-        $this->dispose();
+        $this->disposeHeader();
+        $this->disposeDetail();
     }
 
-    private function dispose()
+    private function disposeHeader()
     {
         //获取更新时间与WMS数据
         $date = $this->service->getSyncDate();
         $waves = $this->service->get(["edittime"=>$date],["edittime"=>"gtOrEqual"]);
-        if (!$waves)return;
+        if (count($waves) < 1)return;
 
         //获取本地数据对比差异
         $codes = array_column($waves->toArray(),"waveno");
@@ -84,8 +89,56 @@ class SyncBatchTask extends Command
             $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()]);
+    }
 }

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

@@ -44,6 +44,7 @@ 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\Unit;
@@ -97,8 +98,9 @@ class TestController extends Controller
         }
     }
     public function test4(){
-        \Redis::set("test1","123");
-       dd(\Redis::get("test1"));
+        $a = [];
+        if (!$a)dd(2);
+        dd(1);
     }
     public function t($a)
     {

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

+ 3 - 0
app/Services/DocWaveHeaderService.php

@@ -32,6 +32,9 @@ Class DocWaveHeaderService
             case "raw":
                 $query->whereRaw($value);
                 break;
+            case "in":
+                $query->whereIn($column,$value);
+                break;
         }
         return $query;
     }

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

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

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