WeightBaseController.php 14 KB

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