LogisticZopSync.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ($response->status) {
  39. $zopResult[] = [
  40. 'routes' => $response->result,
  41. 'logisticNum' => $this->logistic_number,
  42. ];
  43. }
  44. $result = $this->transformRoutes($zopResult);
  45. /* @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService */
  46. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  47. !empty($result) && $orderPackageReceivedSyncService->update($result);
  48. }
  49. /**
  50. * 转换快递路由信息
  51. * @param array $routs 快递路由
  52. * @return array
  53. */
  54. public function transformRoutes(array $routs): array
  55. {
  56. $result = [];
  57. foreach ($routs as $route) {
  58. $result = $this->transformRouteItem($route, $result);
  59. }
  60. return $result;
  61. }
  62. /**
  63. * @param $route
  64. * @param array $result
  65. * @return array
  66. */
  67. private function transformRouteItem($route, array $result): array
  68. {
  69. $resultItem = [];
  70. $resultItem['logistic_number'] = $route['logisticNum'];
  71. $itemRoutes = $route['routes'];
  72. $lastRoute = null;
  73. if (!empty($itemRoutes)) {
  74. $lastRoute = $itemRoutes[count($itemRoutes) - 1];
  75. $resultItem = $this->getNormalStatusAndReceivedAt($lastRoute, $resultItem);
  76. $resultItem['transfer_status'] = $this->getTransferStatus($itemRoutes);
  77. } else {
  78. $resultItem['status'] = null;
  79. $resultItem['transfer_status'] = [];
  80. }
  81. if (!array_key_exists('status', $resultItem)) {
  82. $resultItem['status'] = null;
  83. $resultItem['transfer_status'] = [];
  84. }
  85. try {
  86. $resultItem = $this->setExceptionType($resultItem, $lastRoute ? $lastRoute->scanDate / 1000 : null);
  87. } catch (\Exception $e) {
  88. }
  89. if ($resultItem['status'] == null) {
  90. unset($resultItem['status']);
  91. unset($resultItem['transfer_status']);
  92. }
  93. //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
  94. if (!array_key_exists('exception', $resultItem)
  95. && !array_key_exists('exception_type', $resultItem)
  96. && array_key_exists('transfer_status', $resultItem)
  97. ) {
  98. $resultItem['exception_type'] = '无';
  99. $resultItem['exception'] = '否';
  100. }
  101. $result[] = $resultItem;
  102. return $result;
  103. }
  104. /**
  105. * @param array $data
  106. * @param $lastRouteDate
  107. * @return array
  108. */
  109. private function setExceptionType(array $data, $lastRouteDate = null): array
  110. {
  111. $logistic_number = $data['logistic_number'];
  112. /** @var OrderPackage $orderPackage */
  113. $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
  114. $delivered_duration = now()->diffInHours(Carbon::parse($orderPackage['sent_at']));
  115. $last_routed_duration = now()->diffInHours(Carbon::parse($lastRouteDate));
  116. $VALID_HOURS = 4;
  117. $SHORT_RESPONSE_HOURS = 24;
  118. $LONG_RESPONSE_HOURS = (function ($province) {
  119. switch ($province) {
  120. case '浙江省':
  121. case '江苏省':
  122. case '上海':
  123. case '安徽省':
  124. return 72;
  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 120;
  147. case '青海省':
  148. case '宁夏回族自治区':
  149. case '甘肃省':
  150. case '内蒙古自治区':
  151. case '新疆维吾尔自治区':
  152. case '西藏自治区':
  153. return 168;
  154. default:
  155. return 72;
  156. }
  157. })($orderPackage->order->province);
  158. $SENDING_RESPONSE_HOURS = 48;
  159. $IS_ROUTED = 1; //0000 0001 有路由信息
  160. $IS_IN_VALID_TIME = 2; //0000 0010 大于4小时
  161. $IS_WEIGHED = 4; //0000 0100 称重过
  162. $IS_RECEIVED = 8; //0000 1000 已经收货
  163. $IS_SENDING = 16; //0001 0000 正在派送
  164. $IS_SHORT_NO_RESPONSE = 32; //0010 0000 中转异常
  165. $IS_LONG_NO_RESPONSE = 64; //0010 0000 疑似丢件
  166. $IS_SENDING_NO_RESPONSE = 128; //0010 0000 派送异常
  167. $conclusion = (function () use (
  168. $data, $delivered_duration, $last_routed_duration,
  169. $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,
  170. $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS,
  171. $orderPackage
  172. ) {
  173. $conclusion = 0;
  174. $conclusion |= !empty($data['transfer_status']) ? $IS_ROUTED : 0;
  175. $conclusion |= ($delivered_duration > $VALID_HOURS) ? $IS_IN_VALID_TIME : 0;
  176. $conclusion |= ($orderPackage->weighed_at) ? $IS_WEIGHED : 0;
  177. $conclusion |= ($data['status'] == '已收件') ? $IS_RECEIVED : 0;
  178. $conclusion |= ($data['status'] == '派送中') ? $IS_SENDING : 0;//
  179. $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
  180. $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
  181. $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
  182. return $conclusion;
  183. })();
  184. switch ($conclusion) {
  185. case $IS_IN_VALID_TIME:
  186. $data['exception_type'] = '疑似库内丢件';
  187. break;
  188. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  189. $data['exception_type'] = '揽件异常';
  190. break;
  191. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE:
  192. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE | $IS_WEIGHED:
  193. $data['exception_type'] = '中转异常';
  194. break;
  195. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE:
  196. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE | $IS_WEIGHED:
  197. $data['exception_type'] = '疑似丢件';
  198. break;
  199. default:
  200. break;
  201. }
  202. if($conclusion
  203. ==($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)){
  204. $data['exception_type'] = '派件异常';
  205. }
  206. switch ($conclusion) {
  207. case $IS_IN_VALID_TIME:
  208. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  209. case $IS_ROUTED | $IS_SHORT_NO_RESPONSE:
  210. case $IS_LONG_NO_RESPONSE:
  211. $data['exception'] = '是';
  212. break;
  213. default:
  214. break;
  215. }
  216. return $data;
  217. }
  218. /**
  219. * 正常的状态与签收时间
  220. * @param $lastRoute
  221. * @param array $resultItem
  222. * @return array
  223. */
  224. private function getNormalStatusAndReceivedAt($lastRoute, array $resultItem): array
  225. {
  226. switch ($lastRoute->scanType) {
  227. case '收件':
  228. $resultItem['status'] = '已揽收';
  229. break;
  230. case '到件':
  231. case '发件':
  232. $resultItem['status'] = '在途';
  233. break;
  234. case 'ARRIVAL':
  235. case '派件':
  236. $resultItem['status'] = '派送中';
  237. break;
  238. case 'SIGNED':
  239. case '签收':
  240. $resultItem['status'] = '已收件';
  241. $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  242. break;
  243. default:
  244. $resultItem['status'] = '无';
  245. break;
  246. }
  247. return $resultItem;
  248. }
  249. /**
  250. * @param $itemRoutes
  251. * @return array
  252. */
  253. private function getTransferStatus($itemRoutes): array
  254. {
  255. $transfer_status = [];
  256. foreach ($itemRoutes as $item) {
  257. $data = [];
  258. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
  259. $scanSite = $item->scanSite;
  260. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  261. $data['remark'] = $item->scanType;
  262. $transfer_status[] = $data;
  263. }
  264. return $transfer_status;
  265. }
  266. /**
  267. * 发送请求到中通
  268. * @return mixed
  269. */
  270. private function sentRequestToZT()
  271. {
  272. $url = config('api_logistic.ZTO.url');
  273. $xAppKey = config('api_logistic.ZTO.x-appKey');
  274. $appSecret = config('api_logistic.ZTO.appSecret');
  275. $properties = new ZopProperties($xAppKey, $appSecret);
  276. $client = new ZopClient($properties);
  277. $request = new ZopRequest();
  278. $request->setUrl($url);
  279. $request->setBody(json_encode([
  280. 'billCode' => $this->logistic_number,
  281. ]));
  282. return json_decode($client->execute($request));
  283. }
  284. }