OrderPackageReceivedSyncService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace App\Services;
  3. use App\Jobs\LogisticSFSync;
  4. use App\Jobs\LogisticYDSync;
  5. use App\Jobs\LogisticYTOSync;
  6. use App\Jobs\LogisticZopSync;
  7. use App\OrderPackage;
  8. use Carbon\Carbon;
  9. use Exception;
  10. use Illuminate\Database\Eloquent\Collection;
  11. class OrderPackageReceivedSyncService
  12. {
  13. protected $logisticSFService;
  14. protected $logisticZopService;
  15. /**
  16. * 同步快递信息
  17. * 1 如果当前时间大于初始化时间 每日执行一次,更新order_packages中创建时间大于初始化时间,没有异常,用户未收货的全部订单的快递路由状态
  18. * 2 如果当前时间小于等于初始化时间,执行初始化脚本,将数据库中全部小于等于初始化时间的数据更新
  19. * @throws Exception
  20. */
  21. public function syncLogisticRoute()
  22. {
  23. LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法", '');
  24. ini_set('max_execution_time', 60 * 60);
  25. //初始化时间 2020-12-31 23:59:59
  26. $initDate = Carbon::parse(config('api_logistic.init_date'));
  27. $query = OrderPackage::query()
  28. ->select(['logistic_number', 'order_id'])
  29. ->with(['order' => function ($query) {
  30. return $query->with('logistic');
  31. }]);
  32. if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间
  33. //初始化查询一个月的数据,exception为否
  34. $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
  35. ->whereNull('received_at');
  36. } else {//查询20天以内的数据
  37. $query = $query->where('sent_at', '>=', now()->subDays(20))
  38. ->whereNull('received_at');
  39. }
  40. $query->chunk(2000, function ($orderPackages) {
  41. $logisticNumbers = $this->buildData($orderPackages);
  42. //sf
  43. if (array_key_exists('SF', $logisticNumbers)) {
  44. $SFLogisticNumbers = $logisticNumbers['SF'];
  45. LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", '');
  46. foreach ($SFLogisticNumbers as $logisticNumber) {
  47. LogisticSFSync::dispatch($logisticNumber);
  48. }
  49. }
  50. //更新中通
  51. if (array_key_exists('ZTO', $logisticNumbers)) {
  52. $ZTOLogisticNumbers = $logisticNumbers['ZTO'];
  53. LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-ZTO", '');
  54. foreach ($ZTOLogisticNumbers as $logisticNumber) {
  55. LogisticZopSync::dispatch($logisticNumber);
  56. }
  57. }
  58. //更新韵达
  59. if (array_key_exists('YUNDA', $logisticNumbers)) {
  60. $YDLogisticNumbers = $logisticNumbers['YUNDA'];
  61. LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YUNDA", '');
  62. foreach ($YDLogisticNumbers as $logistic_number) {
  63. LogisticYDSync::dispatch($logistic_number);
  64. }
  65. }
  66. //更新圆通
  67. if (array_key_exists('YTO', $logisticNumbers)) {
  68. $YTOLogisticNumbers = $logisticNumbers['YTO'];
  69. LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YTO", '');
  70. foreach ($YTOLogisticNumbers as $logistic_number) {
  71. LogisticYTOSync::dispatch($logistic_number);
  72. }
  73. }
  74. });
  75. }
  76. /**
  77. * 根据传递的承运商与快递单号更新快递信息
  78. * @param array $logisticNumbers 快递单号
  79. * example: ['SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],'ZT'=>['75424148714142','548464120822', '75424147834290']....]
  80. * @throws Exception 快递接口调用或者返回的信息有误,无法更新指定的快递路由信息
  81. */
  82. public function syncLogisticRouteApi(array $logisticNumbers)
  83. {
  84. $this->update($this->getLogisticRoutes($logisticNumbers));
  85. }
  86. /**
  87. * 获取快件揽收信息
  88. * @param array $request [
  89. * 'SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],
  90. * 'ZT'=>['75424148714142','548464120822', '75424147834290']
  91. * ]
  92. * @return array
  93. * @throws Exception
  94. */
  95. public function getLogisticRoutes(array $request): array
  96. {
  97. $this->logisticSFService = new LogisticSFService();
  98. $resultSF = [];
  99. $resultYD = [];
  100. $resultYT = [];
  101. $resultOther = [];
  102. foreach ($request as $key => $logisticNums) {
  103. switch ($key) {
  104. case "SF":
  105. $resultSF = $this->logisticSFService->get($logisticNums);
  106. break;
  107. case "YD":
  108. $resultYD = [];
  109. break;
  110. case "YT":
  111. $resultYT = [];
  112. break;
  113. default:
  114. $resultOther = [];
  115. break;
  116. }
  117. }
  118. return array_merge($resultSF, $resultYD, $resultYT, $resultOther);
  119. }
  120. /**
  121. * 根据快递单号更新状态
  122. * @param array $orderPackages
  123. */
  124. public function update(array $orderPackages)
  125. {
  126. foreach ($orderPackages as $data) {
  127. if (empty($data)) continue;
  128. $orderPackage = OrderPackage::query()->where('logistic_number', $data['logistic_number'])->first();
  129. try {
  130. if ($orderPackage->order && $orderPackage->order->issue) {
  131. unset($data['exception_type']);
  132. unset($data['exception']);
  133. }
  134. } catch (Exception $e) {
  135. LogService::log(OrderPackageReceivedSyncService::class, "标记问题件不需要更新异常状态失败", $data['logistic_number'].'-'.json_encode($e));
  136. }
  137. if (isset($data['status'])) $orderPackage->status = $data['status'];
  138. if (isset($data['received_at'])) $orderPackage->received_at = $data['received_at'];
  139. if (isset($data['exception'])) $orderPackage->exception = $data['exception'];
  140. if (isset($data['transfer_status'])&&!empty($data['transfer_status'])) $orderPackage->transfer_status = $data['transfer_status'];
  141. if (isset($data['exception_type'])) $orderPackage->exception_type = $data['exception_type'];
  142. $orderPackage->save();
  143. }
  144. }
  145. /**
  146. * 将orderPackage集合分类并摘取指定数据
  147. * @param Collection $orderPackages
  148. * @return array
  149. */
  150. private function buildData(Collection $orderPackages): array
  151. {
  152. $data = [];
  153. foreach ($orderPackages as $orderPackage) {
  154. try {
  155. $logisticCode = $orderPackage->order->logistic->code;
  156. } catch (Exception $e) {
  157. continue;
  158. }
  159. $key = config('api_logistic.logistic.' . $logisticCode);
  160. if (!isset($data[$key])) {
  161. $data[$key] = [];
  162. }
  163. $data[$key][] = $orderPackage->logistic_number;
  164. }
  165. return $data;
  166. }
  167. /**
  168. * @param array $data
  169. * @param $lastRouteDate
  170. * @return array
  171. */
  172. public function setExceptionType(array $data, $lastRouteDate): array
  173. {
  174. $logistic_number = $data['logistic_number'];
  175. /** @var OrderPackage $orderPackage */
  176. $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
  177. $delivered_duration = now()->diffInHours(Carbon::parse($orderPackage['sent_at']));
  178. $last_routed_duration = now()->diffInHours(Carbon::parse($lastRouteDate));
  179. $VALID_HOURS = 4;
  180. $SHORT_RESPONSE_HOURS = (function ($province) {
  181. switch ($province) {
  182. case '浙江省':
  183. case '江苏省':
  184. case '上海':
  185. case '安徽省':
  186. return 24;
  187. case '北京':
  188. case '天津':
  189. case '江西省':
  190. case '湖北省':
  191. case '湖南省':
  192. case '广东省':
  193. case '福建省':
  194. case '山东省':
  195. case '河北省':
  196. case '河南省':
  197. case '山西省':
  198. case '四川省':
  199. case '陕西省':
  200. case '重庆':
  201. case '广西壮族自治区':
  202. case '贵州省':
  203. case '云南省':
  204. case '海南省':
  205. case '吉林省':
  206. case '黑龙江省':
  207. case '辽宁省':
  208. return 72;
  209. case '青海省':
  210. case '宁夏回族自治区':
  211. case '甘肃省':
  212. case '内蒙古自治区':
  213. case '新疆维吾尔自治区':
  214. case '西藏自治区':
  215. return 120;
  216. default:
  217. return 24;
  218. }
  219. })($orderPackage->order->province);
  220. $LONG_RESPONSE_HOURS = (function ($province) {
  221. switch ($province) {
  222. case '浙江省':
  223. case '江苏省':
  224. case '上海':
  225. case '安徽省':
  226. return 72;
  227. case '北京':
  228. case '天津':
  229. case '江西省':
  230. case '湖北省':
  231. case '湖南省':
  232. case '广东省':
  233. case '福建省':
  234. case '山东省':
  235. case '河北省':
  236. case '河南省':
  237. case '山西省':
  238. case '四川省':
  239. case '陕西省':
  240. case '重庆':
  241. case '广西壮族自治区':
  242. case '贵州省':
  243. case '云南省':
  244. case '海南省':
  245. case '吉林省':
  246. case '黑龙江省':
  247. case '辽宁省':
  248. return 120;
  249. case '青海省':
  250. case '宁夏回族自治区':
  251. case '甘肃省':
  252. case '内蒙古自治区':
  253. case '新疆维吾尔自治区':
  254. case '西藏自治区':
  255. return 168;
  256. default:
  257. return 72;
  258. }
  259. })($orderPackage->order->province);
  260. $SENDING_RESPONSE_HOURS = 48;
  261. $HAVEN_SECOND_GOT_HOURS = 24;
  262. $IS_ROUTED = 1; //0000 0001 有路由信息
  263. $IS_IN_VALID_TIME = 2; //0000 0010 大于4小时
  264. $IS_WEIGHED = 4; //0000 0100 称重过
  265. $IS_RECEIVED = 8; //0000 1000 已经收货
  266. $IS_SENDING = 16; //0001 0000 正在派送
  267. $IS_SHORT_NO_RESPONSE = 32; //0010 0000 中转异常
  268. $IS_LONG_NO_RESPONSE = 64; //0010 0000 疑似丢件
  269. $IS_SENDING_NO_RESPONSE = 128; //0010 0000 派送异常
  270. $IS_SECOND_ROUTE_HAVE = 256; //0100 0000 揽件异常
  271. $conclusion = (function () use (
  272. $data, $delivered_duration, $last_routed_duration,
  273. $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,
  274. $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS, $HAVEN_SECOND_GOT_HOURS, $IS_SECOND_ROUTE_HAVE,
  275. $orderPackage
  276. ) {
  277. $conclusion = 0;
  278. $conclusion |= !empty($data['transfer_status']) ? $IS_ROUTED : 0;
  279. $conclusion |= ($delivered_duration > $VALID_HOURS) ? $IS_IN_VALID_TIME : 0;
  280. $conclusion |= ($orderPackage->weighed_at) ? $IS_WEIGHED : 0;
  281. $conclusion |= ($data['status'] == '已收件') ? $IS_RECEIVED : 0;
  282. $conclusion |= ($data['status'] == '派送中') ? $IS_SENDING : 0;//
  283. $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
  284. $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
  285. $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
  286. $conclusion |= ($delivered_duration > $HAVEN_SECOND_GOT_HOURS && $data['routes_length'] < 3) ? $IS_SECOND_ROUTE_HAVE : 0;//超过指定时间,路由信息小于两条
  287. return $conclusion;
  288. })();
  289. switch ($conclusion) {
  290. case $IS_IN_VALID_TIME:
  291. $data['exception_type'] = '疑似库内丢件';
  292. break;
  293. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  294. $data['exception_type'] = '揽件异常';
  295. break;
  296. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE:
  297. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE | $IS_WEIGHED:
  298. $data['exception_type'] = '中转异常';
  299. break;
  300. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE:
  301. case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE | $IS_WEIGHED:
  302. $data['exception_type'] = '疑似丢件';
  303. break;
  304. default:
  305. break;
  306. }
  307. if ($conclusion
  308. == ($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)) {
  309. $data['exception_type'] = '派件异常';
  310. }
  311. if ($conclusion
  312. == ($conclusion | $IS_SECOND_ROUTE_HAVE)) {
  313. $data['exception_type'] = '派件异常';
  314. $data['exception'] = '是';
  315. }
  316. switch ($conclusion) {
  317. case $IS_IN_VALID_TIME:
  318. case $IS_IN_VALID_TIME | $IS_WEIGHED:
  319. case $IS_ROUTED | $IS_SHORT_NO_RESPONSE:
  320. case $IS_LONG_NO_RESPONSE:
  321. $data['exception'] = '是';
  322. break;
  323. default:
  324. break;
  325. }
  326. return [
  327. 'exception_type' => array_key_exists('exception_type', $data) ? $data['exception_type'] : null,
  328. 'exception' => array_key_exists('exception', $data) ? $data['exception'] : null,
  329. ];
  330. }
  331. }