OrderPackageReceivedSyncService.php 13 KB

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