LogisticZopSync.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace App\Jobs;
  3. use App\library\zop\ZopClient;
  4. use App\library\zop\ZopProperties;
  5. use App\library\zop\ZopRequest;
  6. use App\OrderPackage;
  7. use App\Services\OrderPackageReceivedSyncService;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Carbon;
  14. class LogisticZopSync implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $logistic_number;
  18. /**
  19. * Create a new job instance.
  20. *
  21. * @param $logistic_number
  22. */
  23. public function __construct($logistic_number)
  24. {
  25. $this->logistic_number = $logistic_number;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. //
  35. ini_set('max_execution_time', 10);
  36. $zopResult = [];
  37. $response = $this->sentRequestToZT();
  38. if(is_null($response)) return;
  39. if ($response->status) {
  40. $zopResult[] = [
  41. 'routes' => $response->result,
  42. 'logisticNum' => $this->logistic_number,
  43. ];
  44. }
  45. $result = $this->transformRoutes($zopResult);
  46. /* @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService */
  47. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  48. !empty($result) && $orderPackageReceivedSyncService->update($result);
  49. }
  50. /**
  51. * 转换快递路由信息
  52. * @param array $routs 快递路由
  53. * @return array
  54. */
  55. public function transformRoutes(array $routs): array
  56. {
  57. $result = [];
  58. foreach ($routs as $route) {
  59. $result = $this->transformRouteItem($route, $result);
  60. }
  61. return $result;
  62. }
  63. /**
  64. * @param $route
  65. * @param array $result
  66. * @return array
  67. */
  68. private function transformRouteItem($route, array $result): array
  69. {
  70. $resultItem = [];
  71. $resultItem['logistic_number'] = $route['logisticNum'];
  72. $itemRoutes = $route['routes'];
  73. $lastRoute = null;
  74. if (!empty($itemRoutes)) {
  75. $lastRoute = $itemRoutes[count($itemRoutes) - 1];
  76. $resultItem = $this->getNormalStatusAndReceivedAt($lastRoute, $resultItem);
  77. $resultItem['transfer_status'] = $this->getTransferStatus($itemRoutes);
  78. } else {
  79. $resultItem['status'] = null;
  80. $resultItem['transfer_status'] = [];
  81. }
  82. if (!array_key_exists('status', $resultItem)) {
  83. $resultItem['status'] = null;
  84. $resultItem['transfer_status'] = [];
  85. }
  86. try {
  87. $resultItem = $this->setExceptionType($resultItem, $lastRoute ? $lastRoute->scanDate / 1000 : null);
  88. } catch (\Exception $e) {
  89. }
  90. if ($resultItem['status'] == null) {
  91. unset($resultItem['status']);
  92. unset($resultItem['transfer_status']);
  93. }
  94. //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
  95. if (!array_key_exists('exception', $resultItem)
  96. && !array_key_exists('exception_type', $resultItem)
  97. && array_key_exists('transfer_status', $resultItem)
  98. ) {
  99. $resultItem['exception_type'] = '无';
  100. $resultItem['exception'] = '否';
  101. }
  102. $result[] = $resultItem;
  103. return $result;
  104. }
  105. /**
  106. * @param array $data
  107. * @param $lastRouteDate
  108. * @return array
  109. */
  110. private function setExceptionType(array $data, $lastRouteDate = null): array
  111. {
  112. $logistic_number = $data['logistic_number'];
  113. /** @var OrderPackage $orderPackage */
  114. $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
  115. $delivered_duration = now()->diffInHours(Carbon::parse($orderPackage['sent_at']));
  116. $last_routed_duration = now()->diffInHours(Carbon::parse($lastRouteDate));
  117. $VALID_HOURS = 4;
  118. $SHORT_RESPONSE_HOURS = (function ($province) {
  119. switch ($province) {
  120. case '浙江省':
  121. case '江苏省':
  122. case '上海':
  123. case '安徽省':
  124. return 24;
  125. case '北京':
  126. case '天津':
  127. case '江西省':
  128. case '湖北省':
  129. case '湖南省':
  130. case '广东省':
  131. case '福建省':
  132. case '山东省':
  133. case '河北省':
  134. case '河南省':
  135. case '山西省':
  136. case '四川省':
  137. case '陕西省':
  138. case '重庆':
  139. case '广西壮族自治区':
  140. case '贵州省':
  141. case '云南省':
  142. case '海南省':
  143. case '吉林省':
  144. case '黑龙江省':
  145. case '辽宁省':
  146. return 72;
  147. case '青海省':
  148. case '宁夏回族自治区':
  149. case '甘肃省':
  150. case '内蒙古自治区':
  151. case '新疆维吾尔自治区':
  152. case '西藏自治区':
  153. return 120;
  154. default:
  155. return 24;
  156. }
  157. })($orderPackage->order->province);
  158. $LONG_RESPONSE_HOURS = (function ($province) {
  159. switch ($province) {
  160. case '浙江省':
  161. case '江苏省':
  162. case '上海':
  163. case '安徽省':
  164. return 72;
  165. case '北京':
  166. case '天津':
  167. case '江西省':
  168. case '湖北省':
  169. case '湖南省':
  170. case '广东省':
  171. case '福建省':
  172. case '山东省':
  173. case '河北省':
  174. case '河南省':
  175. case '山西省':
  176. case '四川省':
  177. case '陕西省':
  178. case '重庆':
  179. case '广西壮族自治区':
  180. case '贵州省':
  181. case '云南省':
  182. case '海南省':
  183. case '吉林省':
  184. case '黑龙江省':
  185. case '辽宁省':
  186. return 120;
  187. case '青海省':
  188. case '宁夏回族自治区':
  189. case '甘肃省':
  190. case '内蒙古自治区':
  191. case '新疆维吾尔自治区':
  192. case '西藏自治区':
  193. return 168;
  194. default:
  195. return 72;
  196. }
  197. })($orderPackage->order->province);
  198. $SENDING_RESPONSE_HOURS = 48;
  199. $IS_ROUTED = 1; //0000 0001 有路由信息
  200. $IS_IN_VALID_TIME = 2; //0000 0010 大于4小时
  201. $IS_WEIGHED = 4; //0000 0100 称重过
  202. $IS_RECEIVED = 8; //0000 1000 已经收货
  203. $IS_SENDING = 16; //0001 0000 正在派送
  204. $IS_SHORT_NO_RESPONSE = 32; //0010 0000 中转异常
  205. $IS_LONG_NO_RESPONSE = 64; //0010 0000 疑似丢件
  206. $IS_SENDING_NO_RESPONSE = 128; //0010 0000 派送异常
  207. $conclusion = (function () use (
  208. $data, $delivered_duration, $last_routed_duration,
  209. $VALID_HOURS, $IS_ROUTED, $IS_IN_VALID_TIME, $IS_WEIGHED, $IS_RECEIVED, $IS_SENDING, $IS_SHORT_NO_RESPONSE, $IS_LONG_NO_RESPONSE, $IS_SENDING_NO_RESPONSE,
  210. $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS,
  211. $orderPackage
  212. ) {
  213. $conclusion = 0;
  214. $conclusion |= !empty($data['transfer_status']) ? $IS_ROUTED : 0;
  215. $conclusion |= ($delivered_duration > $VALID_HOURS) ? $IS_IN_VALID_TIME : 0;
  216. $conclusion |= ($orderPackage->weighed_at) ? $IS_WEIGHED : 0;
  217. $conclusion |= ($data['status'] == '已收件') ? $IS_RECEIVED : 0;
  218. $conclusion |= ($data['status'] == '派送中') ? $IS_SENDING : 0;//
  219. $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
  220. $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
  221. $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
  222. return $conclusion;
  223. })();
  224. switch ($conclusion) {
  225. case $IS_IN_VALID_TIME:
  226. $data['exception_type'] = '疑似库内丢件';
  227. break;
  228. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  229. $data['exception_type'] = '揽件异常';
  230. break;
  231. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE:
  232. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE | $IS_WEIGHED:
  233. $data['exception_type'] = '中转异常';
  234. break;
  235. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE:
  236. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE | $IS_WEIGHED:
  237. $data['exception_type'] = '疑似丢件';
  238. break;
  239. default:
  240. break;
  241. }
  242. if ($conclusion
  243. == ($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)) {
  244. $data['exception_type'] = '派件异常';
  245. }
  246. switch ($conclusion) {
  247. case $IS_IN_VALID_TIME:
  248. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  249. case $IS_ROUTED | $IS_SHORT_NO_RESPONSE:
  250. case $IS_LONG_NO_RESPONSE:
  251. $data['exception'] = '是';
  252. break;
  253. default:
  254. break;
  255. }
  256. return $data;
  257. }
  258. /**
  259. * 正常的状态与签收时间
  260. * @param $lastRoute
  261. * @param array $resultItem
  262. * @return array
  263. */
  264. private function getNormalStatusAndReceivedAt($lastRoute, array $resultItem): array
  265. {
  266. switch ($lastRoute->scanType) {
  267. case '收件':
  268. $resultItem['status'] = '已揽收';
  269. break;
  270. case '到件':
  271. case '发件':
  272. $resultItem['status'] = '在途';
  273. break;
  274. case 'ARRIVAL':
  275. case '派件':
  276. $resultItem['status'] = '派送中';
  277. break;
  278. case 'SIGNED':
  279. case '签收':
  280. $resultItem['status'] = '已收件';
  281. $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  282. break;
  283. default:
  284. $resultItem['status'] = '无';
  285. break;
  286. }
  287. return $resultItem;
  288. }
  289. /**
  290. * @param $itemRoutes
  291. * @return array
  292. */
  293. private function getTransferStatus($itemRoutes): array
  294. {
  295. $transfer_status = [];
  296. foreach ($itemRoutes as $item) {
  297. $data = [];
  298. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
  299. $scanSite = $item->scanSite;
  300. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  301. $data['remark'] = $item->scanType;
  302. $transfer_status[] = $data;
  303. }
  304. return $transfer_status;
  305. }
  306. /**
  307. * 发送请求到中通
  308. * @return mixed
  309. */
  310. private function sentRequestToZT()
  311. {
  312. $url = config('api_logistic.ZTO.url');
  313. $xAppKey = config('api_logistic.ZTO.x-appKey');
  314. $appSecret = config('api_logistic.ZTO.appSecret');
  315. $properties = new ZopProperties($xAppKey, $appSecret);
  316. $client = new ZopClient($properties);
  317. $request = new ZopRequest();
  318. $request->setUrl($url);
  319. $request->setBody(json_encode([
  320. 'billCode' => $this->logistic_number,
  321. ],JSON_UNESCAPED_UNICODE));
  322. return json_decode($client->execute($request));
  323. }
  324. }