| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?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;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class WeightService
- {
- public $weight = ''; // 重量
- public $length = ''; // 长
- public $width = ''; // 宽
- public $height = ''; // 高
- public $code = ''; // 快递单号
- public $weight_at = ''; // 称重时间
- public $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、更新包裹信息
- try {
- $bool = $this->updateOrderPackage($orderPackage, $params, $measuringMachine);
- } catch (\Exception $e) {
- $result = DB::select('select * from information_schema.innodb_trx');
- Log::warning("包裹称重",["message"=>json_encode($result),"param"=>json_encode($result)]);
- return $this->getWeightMessage($orderPackage, $e->getMessage());
- }
- // 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);
- }
- return $this->getSuccessMessage($params, $orderPackage);
- }
- // 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
- }
|