LogisticZopSync.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $resultItem = $this->setExceptionType($resultItem, $lastRoute?$lastRoute->scanDate / 1000:null);
  82. if ($resultItem['status'] == null) {
  83. unset($resultItem['status']);
  84. unset($resultItem['transfer_status']);
  85. }
  86. $result[] = $resultItem;
  87. return $result;
  88. }
  89. /**
  90. * @param array $data
  91. * @param $lastRouteDate
  92. * @return array
  93. */
  94. private function setExceptionType(array $data, $lastRouteDate = null): array
  95. {
  96. $logistic_number = $data['logistic_number'];
  97. /** @var OrderPackage $orderPackage */
  98. $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
  99. $delivered_duration = now()->diffInHours(Carbon::parse($orderPackage['sent_at']));
  100. $last_routed_duration = now()->diffInHours(Carbon::parse($lastRouteDate));
  101. $VALID_HOURS = 4;
  102. $SHORT_RESPONSE_HOURS = 24;
  103. $LONG_RESPONSE_HOURS = (function ($province) {
  104. switch ($province) {
  105. case '浙江省':
  106. case '江苏省':
  107. case '上海':
  108. case '安徽省':
  109. return 72;
  110. case '北京':
  111. case '天津':
  112. case '江西省':
  113. case '湖北省':
  114. case '湖南省':
  115. case '广东省':
  116. case '福建省':
  117. case '山东省':
  118. case '河北省':
  119. case '河南省':
  120. case '山西省':
  121. case '四川省':
  122. case '陕西省':
  123. case '重庆':
  124. case '广西壮族自治区':
  125. case '贵州省':
  126. case '云南省':
  127. case '海南省':
  128. case '吉林省':
  129. case '黑龙江省':
  130. case '辽宁省':
  131. return 120;
  132. case '青海省':
  133. case '宁夏回族自治区':
  134. case '甘肃省':
  135. case '内蒙古自治区':
  136. case '新疆维吾尔自治区':
  137. case '西藏自治区':
  138. return 168;
  139. default:
  140. break;
  141. }
  142. })($orderPackage->order->province);
  143. $SENDING_RESPONSE_HOURS = 48;
  144. $IS_ROUTED = 1; //0000 0001 有路由信息
  145. $IS_IN_VALID_TIME = 2; //0000 0010 大于4小时
  146. $IS_WEIGHED = 4; //0000 0100 称重过
  147. $IS_RECEIVED = 8; //0000 1000 已经收货
  148. $IS_SENDING = 16; //0001 0000 正在派送
  149. $IS_SHORT_NO_RESPONSE = 32; //0010 0000 中转异常
  150. $IS_LONG_NO_RESPONSE = 64; //0010 0000 疑似丢件
  151. $IS_SENDING_NO_RESPONSE = 128; //0010 0000 派送异常
  152. $conclusion = (function () use (
  153. $data, $delivered_duration, $last_routed_duration,
  154. $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,
  155. $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS,
  156. $orderPackage
  157. ) {
  158. $conclusion = 0;
  159. $conclusion |= !empty($data['transfer_status']) ? $IS_ROUTED : 0;
  160. $conclusion |= ($delivered_duration > $VALID_HOURS) ? $IS_IN_VALID_TIME : 0;
  161. $conclusion |= ($orderPackage->weighed_at) ? $IS_WEIGHED : 0;
  162. $conclusion |= ($data['status'] == '已收件') ? $IS_RECEIVED : 0;
  163. $conclusion |= ($data['status'] == '派送中') ? $IS_SENDING : 0;//
  164. $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
  165. $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
  166. $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
  167. return $conclusion;
  168. })();
  169. switch ($conclusion) {
  170. case $IS_IN_VALID_TIME:
  171. $data['exception_type'] = '疑似库内丢件';
  172. break;
  173. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  174. $data['exception_type'] = '揽件异常';
  175. break;
  176. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE:
  177. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE | $IS_WEIGHED:
  178. $data['exception_type'] = '中转异常';
  179. break;
  180. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE:
  181. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE | $IS_WEIGHED:
  182. $data['exception_type'] = '疑似丢件';
  183. break;
  184. default:
  185. break;
  186. }
  187. if($conclusion
  188. ==($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)){
  189. $data['exception_type'] = '派件异常';
  190. }
  191. switch ($conclusion) {
  192. case $IS_IN_VALID_TIME:
  193. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  194. case $IS_ROUTED | $IS_SHORT_NO_RESPONSE:
  195. case $IS_LONG_NO_RESPONSE:
  196. $data['exception'] = '是';
  197. break;
  198. default:
  199. break;
  200. }
  201. return $data;
  202. }
  203. /**
  204. * 正常的状态与签收时间
  205. * @param $lastRoute
  206. * @param array $resultItem
  207. * @return array
  208. */
  209. private function getNormalStatusAndReceivedAt($lastRoute, array $resultItem): array
  210. {
  211. switch ($lastRoute->scanType) {
  212. case '收件':
  213. $resultItem['status'] = '已揽收';
  214. break;
  215. case '到件':
  216. case '发件':
  217. $resultItem['status'] = '在途';
  218. break;
  219. case 'ARRIVAL':
  220. case '派件':
  221. $resultItem['status'] = '派送中';
  222. break;
  223. case 'SIGNED':
  224. case '签收':
  225. $resultItem['status'] = '已收件';
  226. $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  227. break;
  228. default:
  229. $resultItem['status'] = '无';
  230. break;
  231. }
  232. return $resultItem;
  233. }
  234. /**
  235. * @param $itemRoutes
  236. * @return array
  237. */
  238. private function getTransferStatus($itemRoutes): array
  239. {
  240. $transfer_status = [];
  241. foreach ($itemRoutes as $item) {
  242. $data = [];
  243. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
  244. $scanSite = $item->scanSite;
  245. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  246. $data['remark'] = $item->scanType;
  247. $transfer_status[] = $data;
  248. }
  249. return $transfer_status;
  250. }
  251. /**
  252. * 发送请求到中通
  253. * @return mixed
  254. */
  255. private function sentRequestToZT()
  256. {
  257. $url = config('api_logistic.ZTO.url');
  258. $xAppKey = config('api_logistic.ZTO.x-appKey');
  259. $appSecret = config('api_logistic.ZTO.appSecret');
  260. $properties = new ZopProperties($xAppKey, $appSecret);
  261. $client = new ZopClient($properties);
  262. $request = new ZopRequest();
  263. $request->setUrl($url);
  264. $request->setBody(json_encode([
  265. 'billCode' => $this->logistic_number,
  266. ]));
  267. return json_decode($client->execute($request));
  268. }
  269. }