WeightBaseController.php 14 KB

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