OrderPackageReceivedSyncService.php 13 KB

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