Selaa lähdekoodia

称重service统一继承修改

ajun 4 vuotta sitten
vanhempi
commit
98f482e02c

+ 42 - 0
app/Services/weight/GoodScanWeightService.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Services\weight;
+
+
+class GoodScanWeightService extends WeightService
+{
+    protected $weight = 'weight';     // 重量
+    protected $length = 'l';     // 长
+    protected $width = 'w';      // 宽
+    protected $height = 'h';     // 高
+    protected $code = 'code';       // 快递单号
+    protected $weight_at = '';  // 称重时间
+    protected $hid = 'hid';        // 称重设备id
+    public $name = 'GoodScan';       // 名称
+
+    public function getSuccessMessage($params, $orderPackage): array
+    {
+        return ["code" => 0, 'error' => 'upload success'];
+    }
+
+    public function getNotFindOrderPackageMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '未找打包裹信息'];
+    }
+
+    public function getNotFindOrderHeaderMessage($params, $orderPackage): array
+    {
+        return ['code' => 500, 'error' => '保存时发生错误(未在WMS中找到订单)!'];
+    }
+
+    public function getWeightMessage($orderPackage, $e): array
+    {
+        return ['success' => false, 'message' => $e->getMessage];
+    }
+
+    public function getWriteWasFailMessage($params, $orderPackage): array
+    {
+        return ["code"=>500,"error"=>"写入WMS失败!"];
+    }
+
+}

+ 49 - 0
app/Services/weight/HaoChuangWeightService.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Services\weight;
+
+
+use Illuminate\Support\Carbon;
+
+class HaoChuangWeightService extends WeightService
+{
+    protected $weight = 'weight';     // 重量
+    protected $length = 'length';     // 长
+    protected $width = 'width';      // 宽
+    protected $height = 'height';     // 高
+    protected $code = 'barcode';       // 快递单号
+    protected $weight_at = '';  // 称重时间
+    protected $hid = 'id';        // 称重设备id
+    public $name = 'HaoChuang';       // 名称
+
+    public function getSuccessMessage($params, $orderPackage): array
+    {
+        return ['code' => true, 'msg' => '保存成功', 'data' => true, 'serverMsg' => null,
+            "requestor" => [
+                "requestor" => "1",
+                "eventCode" => "0",
+                "reqDate" => $orderPackage->weighed_at ?? Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT),
+                "resDate" => Carbon::now()
+            ]];
+    }
+
+    public function getNotFindOrderPackageMessage($params, $orderPackage): array
+    {
+        return ['code' => 500, 'msg' => '未找打包裹信息', 'data' => null];
+    }
+
+    public function getNotFindOrderHeaderMessage($params, $orderPackage): array
+    {
+        return ['code' => 500, 'msg' => '富勒信息未找到', 'data' => null];
+    }
+
+    public function getWeightMessage($orderPackage, $e): array
+    {
+        return ['code' => 500, 'msg' => '称重下发修改时发生错误'.json_encode($e), 'data' => null];
+    }
+
+    public function getWriteWasFailMessage($params,$orderPackage): array
+    {
+        return ['code' => 500 ,'msg' => '写入Was包裹失败!', 'data' => null];
+    }
+}

+ 45 - 0
app/Services/weight/HengLiWeightService.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Services\weight;
+
+
+
+class HengLiWeightService extends WeightService
+{
+    // 参数
+    protected $weight = 'weight';     // 重量
+    protected $length = 'length';     // 长
+    protected $width = 'width';      // 宽
+    protected $height = 'height';     // 高
+    protected $code = 'code';       // 快递单号
+    protected $weight_at = 'weight_at';  // 称重时间
+    protected $hid = 'hid';        // 称重设备id
+    public $name = 'HengLi';       // 名称
+
+    public function getWeightValue($params)
+    {
+        $value = $this->getValue($this->weight, $params);
+        return str_replace('_', '.', $value);
+    }
+
+    public function getSuccessMessage($params, $orderPackage): array
+    {
+        return ['success' => true, 'message' => '称重成功'];
+    }
+
+    public function getNotFindOrderPackageMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '未找打包裹信息'];
+    }
+
+    public function getNotFindOrderHeaderMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '富勒信息未找到'];
+    }
+
+    public function getWeightMessage($orderPackage, $e): array
+    {
+        return ['success' => false, 'message' => $e->getMessage];
+    }
+
+}

+ 504 - 0
app/Services/weight/WeightService.php

@@ -0,0 +1,504 @@
+<?php
+
+namespace App\Services\weight;
+
+
+use App\Events\WeighedEvent;
+use App\Http\Controllers\api\thirdPart\flux\PackageController;
+use App\Jobs\WeightUpdateInstantBill;
+use App\MeasuringMachine;
+use App\OracleActAllocationDetails;
+use App\OracleDOCOrderHeader;
+use App\OrderPackage;
+use App\Services\OrderService;
+use App\Waybill;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Carbon;
+
+class WeightService
+{
+    protected $weight = '';     // 重量
+    protected $length = '';     // 长
+    protected $width = '';      // 宽
+    protected $height = '';     // 高
+    protected $code = '';       // 快递单号
+    protected $weight_at = '';  // 称重时间
+    protected $hid = '';        // 称重设备id
+    public $name = '';       // 名称
+
+    public function new(array $params): array
+    {
+        $params = $this->conversionParams($params);
+        return $this->weightOrderPackage($params);
+    }
+
+    /**
+     * 参数转换
+     * @param array $params
+     * @return array
+     */
+    public function conversionParams(array $params): array
+    {
+        return $params;
+    }
+
+    /**
+     * 包裹称重
+     * @param array $params
+     * @return array
+     */
+    public function weightOrderPackage(array $params): array
+    {
+
+        // 2.获取快递单号
+        $logistic_number = $this->getCodeValue($params);
+
+        if (!$logistic_number) return $this->getLogisticNumberIsNullMessage($params);
+        // 3、获取称重设备
+        $measuringMachine = $this->getMeasuringMachine($params);
+        /** @var OrderPackage $orderPackage */
+        $orderPackage = $this->getOrderPackageByCode($logistic_number);
+
+        // 4、快递单号对应的OrderPackage
+        if (is_null($orderPackage)) {
+            /** @var OracleDOCOrderHeader $orderHeader */
+            $orderHeader = $this->findOrderHeaderByLogisticNumber($logistic_number);
+            if (is_null($orderHeader)) {
+                return $this->getNotFindOrderHeaderMessage($params, $orderPackage);
+            }
+            try {
+                $order = $this->createOrderByOrderHeader($orderHeader);
+                $orderPackage = $this->createOrderPackage($params, $measuringMachine, $order);
+            } catch (\Exception $e) {
+                return $this->getWriteWasFailMessage($params, $orderPackage);
+            }
+        }
+
+
+        // 5、更新包裹信息
+        $bool = $this->updateOrderPackage($orderPackage, $params, $measuringMachine);
+        // 6、称重时间
+        if ($bool) $this->afterApply($orderPackage);
+        else {
+            app('LogService')->log(__METHOD__, $this->name, '写入WAS失败! (Error)', $logistic_number, null);
+            return $this->getUpdatePackageMessage($orderPackage);
+        }
+
+        // 7、处理波次信息 推送至WMS称重信息
+        try {
+            $this->activityWaveNoProcessing($orderPackage);
+        } catch (\Exception $e) {
+            return $this->getWeightMessage($orderPackage, $e);
+        }
+
+        $result = $this->getSuccessMessage($params, $orderPackage);
+        return $result;
+    }
+
+
+// region ---消息返回
+
+    /**
+     * 称重成功
+     * @param $params
+     * @param $orderPackage
+     * @return array
+     */
+    public function getSuccessMessage($params, $orderPackage): array
+    {
+        return ['success' => true, 'message' => '称重成功'];
+    }
+
+    /**
+     * 没有找到对应的包裹信息
+     * @param $params
+     * @param $orderPackage
+     * @return array
+     */
+    public function getNotFindOrderPackageMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '未找打包裹信息'];
+    }
+
+    /**
+     * 富勒信息对应快递单号错误
+     * @param $params
+     * @param $orderPackage
+     * @return array
+     */
+    public function getNotFindOrderHeaderMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '富勒信息未找到'];
+    }
+
+    /**
+     * 称重下发修改错误
+     * @param $orderPackage
+     * @param $e
+     * @return array
+     */
+    public function getWeightMessage($orderPackage, $e): array
+    {
+        return ['success' => false, 'message' => $e->getMessage];
+    }
+
+    /**
+     * 更新包裹信息异常错误返回
+     * @param $orderPackage
+     * @return array
+     */
+    public function getUpdatePackageMessage($orderPackage): array
+    {
+        return ['success' => false, 'message' => '更新包裹信息出现异常'];
+    }
+
+    /**
+     * 快递单号过滤后为空
+     * @param $params
+     * @return array
+     */
+    public function getLogisticNumberIsNullMessage($params): array
+    {
+        return ['success' => false, 'message' => '快递单号过滤后为空'];
+    }
+
+    /**
+     * 写入Was失败信息
+     * @param $params
+     * @param $orderPackage
+     * @return array
+     */
+    public function getWriteWasFailMessage($params, $orderPackage): array
+    {
+        return ['success' => false, 'message' => '写入was失败!'];
+    }
+
+// endregion
+
+//  region ---称重完成之后的操作
+
+    /**
+     * 称重完成后的后续操作
+     * @param OrderPackage $orderPackage
+     */
+    public function afterApply(OrderPackage $orderPackage)
+    {
+        $orderPackage->loadMissing(['order' => function ($query) {
+            $query->with('owner', 'logistic');
+        }, 'measuringMachine', 'paperBox']);
+        event(new WeighedEvent($orderPackage));                 // 称重信息播报
+        dispatch(new WeightUpdateInstantBill($orderPackage));   // 及时订单
+        if(!empty($orderPackage->order)){
+            Waybill::setWeightByOrderCode($orderPackage->order->code,$orderPackage['weight']);
+        }
+    }
+
+// endregion
+
+// region  ---参数获取
+    /**
+     * 重量
+     * @param $params
+     * @return mixed|null
+     */
+    public function getWeightValue($params)
+    {
+        return $this->getValue($this->weight, $params);
+    }
+
+    /**
+     * 高
+     * @param $params
+     * @return mixed|null
+     */
+    public function getHeightValue($params)
+    {
+        return $this->getValue($this->height, $params);
+    }
+
+    /**
+     * 长
+     * @param $params
+     * @return mixed|null
+     */
+    public function getLengthValue($params)
+    {
+        return $this->getValue($this->length, $params);
+    }
+
+    /**
+     * 宽
+     * @param $params
+     * @return mixed|null
+     */
+    public function getWidthValue($params)
+    {
+        return $this->getValue($this->width, $params);
+    }
+
+    /**
+     * 获取快递单号
+     * @param $params
+     * @return mixed|null
+     */
+    public function getCodeValue($params)
+    {
+        return $this->getValue($this->code, $params);
+    }
+
+    /**
+     * 称重时间
+     * @param $params
+     * @return mixed|null
+     */
+    public function getWeightAtValue($params)
+    {
+        return $this->getValue($this->weight_at, $params);
+    }
+
+    /**
+     * 获取参数
+     * @param $name
+     * @param $param
+     * @return mixed|null
+     */
+    public function getValue($name, $param)
+    {
+        $names = explode(',', $name);
+        $value = array_reduce($names, function ($data, $key) {
+            if (isset($data[$key])) $data = $data[$key];
+            else $data = [];
+            return $data;
+        }, $param);
+        if (is_array($value) && count($value) == 0) return null;
+        return $value;
+    }
+
+    /**
+     * 体积参数排序
+     * @param $params
+     * @return array
+     */
+    public function getEdges($params): array
+    {
+        $length = $this->getLengthValue($params);
+        $height = $this->getHeightValue($params);
+        $width = $this->getWidthValue($params);
+        $edges = [$length ?? 0, $width ?? 0, $height ?? 0];
+        rsort($edges);
+        return $edges;
+    }
+
+// endregion
+
+//  region ---包裹
+
+    /**
+     * 获取单号对应的包裹
+     * @param $code
+     * @return Builder|Model|object|null
+     */
+    public function getOrderPackageByCode($code)
+    {
+        return OrderPackage::query()
+            ->with(['order' => function ($query) {
+                /** @var Builder $query */
+                $query->with('owner', 'logistic');
+            }])->where('logistic_number', $code)->first();
+    }
+
+    /**
+     * 更新包裹信息
+     * @param OrderPackage $orderPackage
+     * @param $params
+     * @param $measuringMachine
+     * @return bool
+     */
+    public function updateOrderPackage(OrderPackage $orderPackage, $params, $measuringMachine): bool
+    {
+        $edges = $this->getEdges($params);
+        $req_date = Carbon::now()->toDateTimeString();
+        $orderPackage['weight'] = $this->getWeightValue($params);
+        $orderPackage['measuring_machine_id'] = $measuringMachine['id'];
+        $orderPackage['length'] = $edges[0];
+        $orderPackage['width'] = $edges[1];
+        $orderPackage['height'] = $edges[2];
+        $orderPackage['weighed_at'] = $req_date;
+        $orderPackage['bulk'] = $edges[0] * $edges[1] * $edges[2];
+        return $orderPackage->save();
+    }
+
+    /**
+     * 创建包裹信息
+     * @param $params
+     * @param $measuringMachine
+     * @param $order
+     * @return Builder|Model|object|null
+     */
+    public function createOrderPackage($params, $measuringMachine, $order)
+    {
+        $weighed_at = Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT);
+        $edges = $this->getEdges($params);
+        OrderPackage::query()->create([
+            'order_id' => $order->id,
+            'logistic_number' => $this->getCodeValue($params),
+            'measuring_machine_id' => $measuringMachine->id,
+            'weight' => $this->getWeightValue($params),
+            'length' => $edges[0],
+            'width' => $edges[1],
+            'height' => $edges[2],
+            'bulk' => $edges[0] * $edges[1] * $edges[2],
+            'weighed_at' => $weighed_at,
+            'status' => "无",
+        ]);
+        return $this->getOrderPackageByCode($this->getCodeValue($params));
+    }
+
+//  endregion
+
+//  region ---称重设备
+
+    /**
+     * 称重机获取
+     * @param $params
+     * @return MeasuringMachine
+     */
+    public function getMeasuringMachine($params): MeasuringMachine
+    {
+        $hid = $this->getValue($this->hid, $params);
+        /** @var MeasuringMachine $measuringMachine */
+        $measuringMachine = MeasuringMachine::query()->firstOrCreate(['code' => $hid], ['name' => $hid]); // 称重设备
+        $measuringMachine->turnOn();
+        $measuringMachine->turnOffInMinutes(30);
+        return $measuringMachine;
+    }
+//  endregion
+
+// region ---wms操作
+
+    /**
+     * 根据快递单号找到对应的WMS订单信息
+     * @param $code
+     * @return Builder|Model|object|null
+     */
+    public function findOrderHeaderByLogisticNumber($code)
+    {
+        $query = OracleActAllocationDetails::query()->select('OrderNO')->where('PickToTraceId', $code);
+        $orderHeader = OracleDOCOrderHeader::query()->with('actAllocationDetails', 'oracleBASCode')->whereIn('OrderNO', $query)->first();
+        if ($orderHeader == null) {
+            $orderHeader = OracleDOCOrderHeader::query()->with('actAllocationDetails', 'oracleBASCode')->where('SOReference5', $code)->first();
+        }
+        return $orderHeader;
+    }
+
+    /**
+     * 同步wms订单信息
+     * @param $orderHeader
+     * @return Builder|Model|object
+     */
+    public function createOrderByOrderHeader($orderHeader)
+    {
+        /** @var OrderService $orderService */
+        $orderService = app(OrderService::class);
+        $order_create_params = $orderService->getParamByOrderHeader($orderHeader);
+        $order = $orderService->first(['code' => $orderHeader->orderno]);
+        if ($order) return $order;
+        $order = $orderService->createOrder($order_create_params);
+        app('LogService')->log(__METHOD__, $this->name, ' 创建Order', json_encode($order) . " || " . $orderHeader);
+        return $order;
+    }
+
+    /**
+     * 活动波次处理
+     * @param $orderPackage
+     */
+    public function activityWaveNoProcessing(&$orderPackage)
+    {
+        $fluxController = new PackageController();
+        if ($orderPackage->isActivityBatch()) {
+            app('LogService')->log(__METHOD__, $this->name . " 依波次号同步所有包裹", json_encode($orderPackage), null);
+            OrderPackage::query()->where('batch_number', $orderPackage['batch_number'])->update([
+                'weight' => $orderPackage['weight'] ?? null,
+                'length' => $orderPackage['length'] ?? null,
+                'width' => $orderPackage['width'] ?? null,
+                'height' => $orderPackage['height'] ?? null,
+                'bulk' => $orderPackage['bulk'] ?? null,
+                'measuring_machine_id' => $orderPackage['measuring_machine_id'] ?? null,
+                'weighed_at' => $orderPackage['weighed_at'] ?? null,
+                'paper_box_id' => $orderPackage['paper_box_id'] ?? null,
+            ]);
+            $result = $fluxController->markWMSOnBatch($orderPackage['batch_number'], $orderPackage['weight']);
+            if (!$result['result']) {
+                $orderPackage->uploaded_to_wms = "异常";
+            }
+        } else {
+            app('LogService')->log(__METHOD__, $this->name . " 写入包裹至WMS异常", json_encode($orderPackage), null);
+            try {
+                $result = $fluxController->accomplishToWMS($orderPackage);
+                if ($result['result'] == 'success') $orderPackage->uploaded_to_wms = "是";
+                else $orderPackage->uploaded_to_wms = "异常";
+            } catch (\Exception $e) {
+                $orderPackage->uploaded_to_wms = "否";
+            }
+        }
+        $orderPackage->save();
+    }
+// endregion
+
+// region ---上传快递单号处理
+
+    /**
+     * 快递单号处理
+     *
+     * @param $code
+     * @return string
+     */
+    public function processCode($code): string
+    {
+
+        /** 如果是$code 是数组处理 */
+        if (is_array($code)) {
+            return $this->processCodeArr($code);
+        }
+        return $this->processCodeStr($code);
+    }
+
+    /**
+     *  快递单号 array 处理
+     *
+     * @param array $code
+     * @return mixed|string
+     */
+    public function processCodeArr(array $code): string
+    {
+        usort($code, function ($codeA, $codeB) {
+            if (strlen($codeA) == strlen($codeB)) return 0;
+            return strlen($codeA) > strlen($codeB) ? 1 : -1;
+        });
+        return $code[0];
+    }
+
+    /**
+     * 快递单号 string 处理
+     *
+     * @param $code
+     * @return string|null
+     */
+    public function processCodeStr($code): ?string
+    {
+        $codes = [];
+        preg_match_all('/[\w]+/', $code, $codes);
+        if (count($codes) == 0) return $code;
+        $codes = array_unique(array_filter(array_shift($codes), function ($item) {
+            return strlen($item) > 8;
+        }));
+        usort($codes, function ($a, $b) {
+            if (strlen($a) == strlen($b)) return 0;
+            return strlen($a) < strlen($b) ? 1 : -1;
+        });
+        return $codes[0] ?? null;
+    }
+
+// endregion
+}