Kaynağa Gözat

区域表增量补充,逻辑添加至运输管理

Zhouzhendong 5 yıl önce
ebeveyn
işleme
83ba8b60c2

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

@@ -164,17 +164,8 @@ sql;
 
     public function zzd()
     {
-        $item = [
-            "订单编号" => "SO210120009746",
-            "商品条码" => "3046920028004",
-        ];
-        $detail = OracleDOCOrderDetail::query()->select("DOC_ORDER_DETAILS.customerid","DOC_ORDER_DETAILS.sku","DOC_ORDER_DETAILS.orderno","DOC_ORDER_DETAILS.orderlineno","sostatus")
-            ->where("DOC_ORDER_DETAILS.orderno",$item["订单编号"])
-            ->whereHas("sku",function ($query)use($item){
-                /** @var Builder $query */
-                $query->where("alternate_sku1",$item["商品条码"]);
-            })->leftJoin("DOC_ORDER_HEADER","DOC_ORDER_DETAILS.ORDERNO","=","DOC_ORDER_HEADER.ORDERNO")->first();
-        dd($detail);
+        return app("RegionService")->getCity("哈达克力扣州");
+
     }
 
     public function mergeCarrier()

+ 2 - 0
app/Providers/AppServiceProvider.php

@@ -77,6 +77,7 @@ use App\Services\WarehouseService;
 use App\Services\WaybillFinancialService;
 use App\Services\WeighExceptedService;
 use App\Services\OrderFreezeService;
+use App\Services\RegionService;
 use Illuminate\Queue\Events\JobFailed;
 use Illuminate\Support\Facades\Queue;
 use Illuminate\Support\Facades\Schema;
@@ -184,6 +185,7 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('ProcessStatisticService',ProcessStatisticService::class);
         app()->singleton('ProcessesContentService',ProcessesContentService::class);
         app()->singleton('RealtimePendingOrdersService',RealtimePendingOrdersService::class);
+        app()->singleton('RegionService',RegionService::class);
         app()->singleton('RejectedBillItemService',RejectedBillItemService::class);
         app()->singleton('RejectedBillService',RejectedBillService::class);
         app()->singleton('RejectedService',RejectedService::class);

+ 106 - 0
app/Services/RegionService.php

@@ -0,0 +1,106 @@
+<?php 
+
+namespace App\Services;
+
+use App\Region;
+use App\Traits\ServiceAppAop;
+
+class RegionService
+{
+    use ServiceAppAop;
+
+    /**
+     * 根据省份获取ID
+     *
+     * @param string $province
+     *
+     * @return int
+     */
+    public function getProvince(string $province):int
+    {
+        $pool = ["省","自治区","市","特别行政"];
+        $province = $this->extractKeyword($province,$pool);
+        $region = Region::withTrashed()->where("name","like",$province."%")
+            ->where("type",1)->first();
+        if (!$region)$region = Region::query()->create([
+            "name"      => $province,
+            "type"      => 2,
+        ]);
+        return $region->id;
+    }
+
+    /**
+     * 根据城市获取ID
+     *
+     * @param string $city
+     * @param string|int|null $parent
+     *
+     * @return int
+     */
+    public function getCity(string $city, $parent = null):int
+    {
+        $pool = ["市","区","自治州","州","盟"];
+        $city = $this->extractKeyword($city,$pool);
+        $region = Region::withTrashed()->where("name","like",$city."%")
+            ->where("type",2)->first();
+        if (!$region){
+            $region = [
+                "name"      => $city,
+                "type"      => 2,
+            ];
+            if ($parent){
+                if (is_int($parent))$region["parent_id"] = $parent;
+                else $region["parent_id"] = $this->getProvince($parent);
+            }
+            $region = Region::query()->create($region);
+        }
+        return $region->id;
+    }
+
+    /**
+     * 根据区县获取ID
+     *
+     * @param string $district
+     * @param string|int|null $parent
+     *
+     * @return int
+     */
+    public function getDistrict(string $district, $parent = null):int
+    {
+        $pool = ["市","区","自治县","县","自治旗","旗","特区","林区"];
+        $district = $this->extractKeyword($district,$pool);
+        $region = Region::withTrashed()->where("name","like",$district."%")
+            ->where("type",3)->first();
+        if (!$region){
+            $region = [
+                "name"      => $district,
+                "type"      => 3,
+            ];
+            if ($parent){
+                if (is_int($parent))$region["parent_id"] = $parent;
+                else $region["parent_id"] = $this->getCity($parent);
+            }
+            $region = Region::query()->create($region);
+        }
+        return $region->id;
+    }
+
+    /**
+     * 根据字典池提取关键字
+     *
+     * @param string $name
+     * @param array $pool
+     *
+     * @return string
+     */
+    private function extractKeyword(string $name,array $pool)
+    {
+        foreach ($pool as $keyword){
+            $result = mb_strpos($name,$keyword);
+            if ($result!==false){
+                return mb_substr($name,0,$result);
+            }
+        }
+        return $name;
+    }
+}

+ 7 - 0
app/Services/WaybillService.php

@@ -81,6 +81,13 @@ Class WaybillService
         return DB::transaction(function ()use($request){
             $waybill=new Waybill();
             $inputs = $request->all();
+            if ($inputs["wms_bill_number"]){
+                $order = app("OrderService")->first(["code"=>$inputs["wms_bill_number"]]);
+                if ($order){
+                    $inputs["destination_city_id"] = app("RegionService")->getCity($order->city,$order->province);
+                    $inputs["district_id"] = app("RegionService")->getDistrict($order->district,$order->city);
+                }
+            }
             $inputs['status']='未审核';
             $inputs['waybill_number']=Uuid::uuid1();
             $waybill->fill($inputs);

+ 6 - 1
app/Waybill.php

@@ -21,9 +21,14 @@ class Waybill extends Model
         'status','type','waybill_number','owner_id','wms_bill_number','origination','destination','recipient','recipient_mobile','charge','ordering_remark',
         'logistic_id','carrier_bill','origination_city_id','destination_city_id','warehouse_weight','warehouse_weight_unit_id','carrier_weight','carrier_weight_unit_id','carType_id',
         'car_owner_info','fee','pick_up_fee','other_fee','collect_fee','dispatch_remark','waybill_price_model_id','warehouse_weight_other','warehouse_weight_unit_id_other'
-        ,'carrier_weight_other','carrier_weight_unit_id_other','source_bill','mileage','amount','inquire_tel','amount_unit_id','other_charge','other_charge_remark','deliver_at'
+        ,'carrier_weight_other','carrier_weight_unit_id_other','source_bill','mileage','amount','inquire_tel','amount_unit_id','other_charge','other_charge_remark','deliver_at',
+        "district_id"
     ];
 
+    public function district()
+    {   //区
+        return $this->belongsTo(Region::class,"district_id","id")->where("type",3);
+    }
     public function owner(){
         return $this->hasOne('App\Owner','id','owner_id');
     }

+ 19 - 0
database/migrations/2021_01_20_104314_add_column_deleted_at_table_region.php

@@ -6,6 +6,13 @@ use Illuminate\Support\Facades\Schema;
 
 class AddColumnDeletedAtTableRegion extends Migration
 {
+    protected $name = [
+        "地域",
+        "地域-查询",
+        "地域-录入",
+        "地域-编辑",
+        "地域-删除",
+    ];
     /**
      * Run the migrations.
      *
@@ -15,7 +22,15 @@ class AddColumnDeletedAtTableRegion extends Migration
     {
         Schema::table('regions', function (Blueprint $table) {
             $table->softDeletes();
+            $table->index("type");
         });
+
+        foreach ($this->name as $name){
+            \App\Authority::query()->firstOrCreate([
+                "name"=>$name,
+                "alias_name"=>$name
+            ]);
+        }
     }
 
     /**
@@ -27,6 +42,10 @@ class AddColumnDeletedAtTableRegion extends Migration
     {
         Schema::table('regions', function (Blueprint $table) {
             $table->dropSoftDeletes();
+            $table->dropIndex(["type"]);
         });
+        foreach ($this->name as $name){
+            \App\Authority::query()->where("name",$name)->delete();
+        }
     }
 }

+ 32 - 0
database/migrations/2021_01_20_165449_add_column_district_id_table_waybills.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddColumnDistrictIdTableWaybills extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('waybills', function (Blueprint $table) {
+            $table->bigInteger("district_id")->nullable()->index()->comment("外键区");
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('waybills', function (Blueprint $table) {
+            $table->dropColumn("district_id");
+        });
+    }
+}