WeightBaseController.php 14 KB

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