OrderPackageReceivedSyncService.php 17 KB

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