WeightBaseController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\weight;
  3. use App\Events\WeighedEvent;
  4. use App\Jobs\WeightUpdateInstantBill;
  5. use App\MeasuringMachine;
  6. use App\OracleActAllocationDetails;
  7. use App\OracleDOCOrderHeader;
  8. use App\OrderPackage;
  9. use App\Services\OrderService;
  10. use Illuminate\Support\Carbon;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Http\Request;
  13. class WeightBaseController
  14. {
  15. protected $weight = ''; // 重量
  16. protected $length = ''; // 长
  17. protected $width = ''; // 宽
  18. protected $height = ''; // 高
  19. protected $code = ''; // 快递单号
  20. protected $weight_at = ''; // 称重时间
  21. protected $hid = ''; // 称重设备id
  22. protected $name = ''; // 名称
  23. public function new(Request $request)
  24. {
  25. // app('LogService')->log(__METHOD__, $this->name, "记录上传日志:" . json_encode($request->all()) . '||' , null);
  26. $errors = $this->validator($request);
  27. if(count($errors)){
  28. app('LogService')->log(__METHOD__, $this->name, "上传校验Error:" . json_encode($request->all()) , null);
  29. return $this->validatorErrors($errors);
  30. }
  31. return $this->weightOrderPackage($request);
  32. }
  33. public function weightOrderPackage(Request $request)
  34. {
  35. /**
  36. * @var OrderPackage $orderPackage
  37. * @var MeasuringMachine $measuringMachine
  38. */
  39. // 1、转化数据
  40. $params = $this->conversionRequest($request);
  41. // 2.获取快递单号
  42. $logistic_number = $this->getCodeValue($params);
  43. // 3、获取称重设备
  44. $measuringMachine = $this->getMeasuringMachine($params);
  45. // 4、快递单号对应的OrderPackage
  46. $orderPackage = $this->getOrderPackageByCode($logistic_number);
  47. if (is_null($orderPackage)) {
  48. /** @var OracleDOCOrderHeader $orderHeader */
  49. $orderHeader = $this->findOrderHeaderByLogisticNumber($logistic_number);
  50. if (is_null($orderHeader)) {
  51. app('LogService')->log(__METHOD__, $this->name, 'WMSOrderHeaderNotFind (Error)',$logistic_number , null);
  52. return $this->getNotFindOrderHeaderMessage($params, $orderPackage);
  53. }
  54. try {
  55. $order = $this->createOrderByOrderHeader($orderHeader);
  56. $orderPackage = $this->createOrderPackage($params, $measuringMachine, $order);
  57. } catch (\Exception $e) {
  58. app('LogService')->log(__METHOD__, $this->name, '写入WAS失败! (Error)',$logistic_number , null);
  59. return json_encode(["success" => false, "message" => "写入WAS失败!"], JSON_UNESCAPED_UNICODE);
  60. }
  61. }
  62. // 5、更新包裹信息
  63. $bool = $this->updateOrderPackage($orderPackage, $params, $measuringMachine);
  64. // 6、称重时间
  65. if($bool)$this->afterApply($orderPackage);
  66. else {
  67. app('LogService')->log(__METHOD__, $this->name, '写入WAS失败! (Error)',$logistic_number , null);
  68. return $this->getUpdatePackageMessage($orderPackage);
  69. }
  70. // 7、处理波次信息
  71. try {
  72. if ($orderPackage->isActivityBatch()) {
  73. $this->activityWaveNoProcessing($orderPackage);
  74. }
  75. } catch (\Exception $e) {
  76. app('LogService')->log(__METHOD__, $this->name, 'weightApi (Error)', json_encode($orderPackage) . '||' . json_encode($e), null);
  77. return $this->getWeightMessage($orderPackage, $e);
  78. }
  79. $response = $this->getSuccessMessage($params, $orderPackage);
  80. app('LogService')->log(__METHOD__, $this->name, "下发写入包裹成功:" . json_encode($request->getContent()) . '||' . json_encode($response), null);
  81. return json_encode($response, JSON_UNESCAPED_UNICODE);
  82. }
  83. // region ---数据转化
  84. public function conversionRequest(Request $request)
  85. {
  86. // 1、转化数据
  87. $params = [];
  88. foreach ($request->input() as $key => $item) {
  89. $params[strtolower($key)] = $item;
  90. }
  91. return $params;
  92. }
  93. // endregion
  94. // region ---称重完成之后的操作
  95. // 后续操作
  96. public function afterApply(OrderPackage $orderPackage)
  97. {
  98. $orderPackage->loadMissing(['order' => function ($query) {
  99. $query->with('owner', 'logistic');
  100. }, 'measuringMachine', 'paperBox']);
  101. event(new WeighedEvent($orderPackage));
  102. dispatch(new WeightUpdateInstantBill($orderPackage));
  103. }
  104. // endregion
  105. // region ---消息返回
  106. // 返回称重成功信息
  107. public function getSuccessMessage($params, $orderPackage): array
  108. {
  109. return ['success' => true, 'message' => '称重成功'];
  110. }
  111. // 返回包裹未找到异常
  112. public function getNotFindOrderPackageMessage($params, $orderPackage): string
  113. {
  114. return json_encode(['success' => false, 'message' => '未找打包裹信息', JSON_UNESCAPED_UNICODE]);
  115. }
  116. // 返回富勒信息未找到异常
  117. public function getNotFindOrderHeaderMessage($params, $orderPackage): string
  118. {
  119. return json_encode(['success' => false, 'message' => '富勒信息未找到'], JSON_UNESCAPED_UNICODE);
  120. }
  121. // 返回称重下发错误
  122. public function getWeightMessage($orderPackage, $e)
  123. {
  124. return json_encode(['success' => false, 'message' => $e->getMessage], JSON_UNESCAPED_UNICODE);
  125. }
  126. public function getUpdatePackageMessage($orderPackage)
  127. {
  128. return json_encode(['success' => false, 'message' => '更新包裹信息出现异常'], JSON_UNESCAPED_UNICODE);
  129. }
  130. // endregion
  131. // region ---参数校验
  132. public function validator(Request $request): array
  133. {
  134. return [];
  135. }
  136. public function validatorErrors($errors)
  137. {
  138. return json_encode(['success' => false, 'message' => '更新包裹信息出现异常'.json_encode($errors)],JSON_UNESCAPED_UNICODE);
  139. }
  140. // endregion
  141. // region ---参数获取
  142. // 重量
  143. public function getWeightValue($params)
  144. {
  145. return $this->getValue($this->weight, $params);
  146. }
  147. // 高
  148. public function getHeightValue($params)
  149. {
  150. return $this->getValue($this->height, $params);
  151. }
  152. // 长
  153. public function getLengthValue($params)
  154. {
  155. return $this->getValue($this->length, $params);
  156. }
  157. // 宽
  158. public function getWidthValue($params)
  159. {
  160. return $this->getValue($this->width, $params);
  161. }
  162. // 快递单号
  163. public function getCodeValue($params)
  164. {
  165. return $this->getValue($this->code, $params);
  166. }
  167. // 称重时间
  168. public function getWeightAtValue($params)
  169. {
  170. return $this->getValue($this->weight_at, $params);
  171. }
  172. // 获取参数
  173. public function getValue($name, $param)
  174. {
  175. $names = explode(',', $name);
  176. $value = array_reduce($names, function ($data, $key) {
  177. if (isset($data[$key])) $data = $data[$key];
  178. else $data = [];
  179. return $data;
  180. }, $param);
  181. if (is_array($value) && count($value) == 0) return null;
  182. return $value;
  183. }
  184. // 排序参数
  185. public function getEdges($params): array
  186. {
  187. $length = $this->getLengthValue($params);
  188. $height = $this->getHeightValue($params);
  189. $width = $this->getWidthValue($params);
  190. $edges = [$length ?? 0, $width ?? 0, $height ?? 0];
  191. rsort($edges);
  192. return $edges;
  193. }
  194. // endregion
  195. // region ---包裹
  196. // 获取包裹
  197. public function getOrderPackageByCode($code)
  198. {
  199. return OrderPackage::query()
  200. ->with(['order' => function ($query) {
  201. /** @var Builder $query */
  202. $query->with('owner', 'logistic');
  203. }])->where('logistic_number', $code)->first();
  204. }
  205. // 更新包裹
  206. public function updateOrderPackage(OrderPackage $orderPackage, $params, $measuringMachine): bool
  207. {
  208. $edges = $this->getEdges($params);
  209. $req_date = Carbon::now()->toDateTimeString();
  210. $orderPackage['weight'] = $this->getWeightValue($params);
  211. $orderPackage['measuring_machine_id'] = $measuringMachine['id'];
  212. $orderPackage['length'] = $edges[0];
  213. $orderPackage['width'] = $edges[1];
  214. $orderPackage['height'] = $edges[2];
  215. $orderPackage['weighed_at'] = $req_date;
  216. $orderPackage['bulk'] = $edges[0] * $edges[1] * $edges[2] / 1000;
  217. return $orderPackage->save();
  218. }
  219. // 创建包裹信息
  220. public function createOrderPackage($params, $measuringMachine, $order)
  221. {
  222. $weighed_at = Carbon::now();
  223. $edges = $this->getEdges($params);
  224. OrderPackage::query()->create([
  225. 'order_id' => $order->id,
  226. 'logistic_number' => $this->getCodeValue($params),
  227. 'measuring_machine_id' => $measuringMachine->id,
  228. 'weight' => $this->getWeightValue($params),
  229. 'length' => $edges[0],
  230. 'width' => $edges[1],
  231. 'height' => $edges[2],
  232. 'bulk' => $edges[0] * $edges[1] * $edges[2],
  233. 'weighed_at' => $weighed_at,
  234. 'status' => "无",
  235. ]);
  236. return $this->getOrderPackageByCode($this->getCodeValue($params));
  237. }
  238. // endregion
  239. // region ---称重设备
  240. // 获取称重设备
  241. public function getMeasuringMachine($params): MeasuringMachine
  242. {
  243. $hid = $this->getValue($this->hid, $params);
  244. /** @var MeasuringMachine $measuringMachine */
  245. $measuringMachine = MeasuringMachine::query()->firstOrCreate(['code' => $hid]); // 称重设备
  246. $measuringMachine->turnOn();
  247. $measuringMachine->turnOffInMinutes(30);
  248. return $measuringMachine;
  249. }
  250. // endregion
  251. // region ---wms操作
  252. // 获取orderHeader
  253. public function findOrderHeaderByLogisticNumber($code)
  254. {
  255. $query = OracleActAllocationDetails::query()->select('OrderNO')->where('PickToTraceId', $code);
  256. $orderHeader = OracleDOCOrderHeader::query()->with('actAllocationDetails', 'oracleBASCode')->whereIn('OrderNO', $query)->first();
  257. if($orderHeader == null){
  258. $orderHeader = OracleDOCOrderHeader::query()->with('actAllocationDetails', 'oracleBASCode')->where('SOReference5', $code)->first();
  259. }
  260. return $orderHeader;
  261. }
  262. // 根据WMS订单信息创建订单信息
  263. public function createOrderByOrderHeader($orderHeader)
  264. {
  265. /** @var OrderService $orderService */
  266. $orderService = app('OrderService');
  267. $order_create_params = $orderService->getParamByOrderHeader($orderHeader);
  268. $order = $orderService->first(['code' => $orderHeader->orderno]);
  269. if ($order) return $order;
  270. $order = $orderService->createOrder($order_create_params);
  271. app('LogService')->log(__METHOD__, $this->name, ' 创建Order', json_encode($order) . " || " . $orderHeader);
  272. return $order;
  273. }
  274. //处理活动波次
  275. public function activityWaveNoProcessing(&$orderPackage)
  276. {
  277. $fluxController = new \App\Http\Controllers\api\thirdPart\flux\PackageController();
  278. if ($orderPackage->isActivityBatch()) {
  279. app('LogService')->log(__METHOD__, $this->name . " 依波次号同步所有包裹:", json_encode($orderPackage), null);
  280. OrderPackage::query()->where('batch_number', $orderPackage['batch_number'])->update([
  281. 'weight' => $orderPackage['weight'] ?? null,
  282. 'length' => $orderPackage['length'] ?? null,
  283. 'width' => $orderPackage['width'] ?? null,
  284. 'height' => $orderPackage['height'] ?? null,
  285. 'bulk' => $orderPackage['bulk'] ?? null,
  286. 'measuring_machine_id' => $orderPackage['measuring_machine_id'] ?? null,
  287. 'weighed_at' => $orderPackage['weighed_at'] ?? null,
  288. 'paper_box_id' => $orderPackage['paper_box_id'] ?? null,
  289. ]);
  290. $result = $fluxController->markWMSOnBatch($orderPackage['batch_number'], $orderPackage['weight']);
  291. if (!$result['result']) {
  292. $orderPackage->uploaded_to_wms = "异常";
  293. }
  294. } else {
  295. app('LogService')->log(__METHOD__, $this->name . " 写入包裹至WMS异常:", json_encode($orderPackage), null);
  296. try {
  297. $result = $fluxController->accomplishToWMS($orderPackage);
  298. if ($result['result'] == 'success') $orderPackage->uploaded_to_wms = "是";
  299. else $orderPackage->uploaded_to_wms = "异常";
  300. } catch (\Exception $e) {
  301. $orderPackage->uploaded_to_wms = "否";
  302. }
  303. }
  304. $orderPackage->save();
  305. }
  306. // endregion
  307. // region ---上传快递单号处理
  308. /**
  309. * 快递单号处理
  310. *
  311. * @param $code
  312. * @return string
  313. */
  314. public function processCode($code): string
  315. {
  316. /** 如果是$code 是数组处理 */
  317. if(is_array($code)){
  318. return $this->processCodeArr($code);
  319. }
  320. return $this->processCodeStr($code);
  321. }
  322. /**
  323. * 快递单号 array 处理
  324. *
  325. * @param array $code
  326. * @return mixed|string
  327. */
  328. public function processCodeArr(array $code): string
  329. {
  330. usort($code,function($codeA,$codeB){
  331. if(strlen($codeA) == strlen($codeB))return 0;
  332. return strlen($codeA) > strlen($codeB) ? 1 : -1;
  333. });
  334. return $code[0];
  335. }
  336. /**
  337. * 快递单号 string 处理
  338. *
  339. * @param $code
  340. * @return string
  341. */
  342. public function processCodeStr($code): string
  343. {
  344. /** 双条码处理 */
  345. $codes = preg_split('/[,,@ ]+/is',$code);
  346. if(count($codes)>0){
  347. foreach ($codes as $item) {
  348. $item = trim($item,"'");
  349. if(mb_strpos($item,'-'))continue;
  350. if(strlen(trim($item,' ')) < 5)continue;
  351. return $item;
  352. }
  353. }
  354. $arr = [];
  355. preg_match('/[A-Z|a-z|\d]+/',$code,$arr);
  356. return $arr[0];
  357. }
  358. // endregion
  359. }