DeliveryService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Services;
  3. use App\OracleDocOrderDeliveryInfo;
  4. use App\OracleDOCOrderHeader;
  5. use Illuminate\Support\Carbon;
  6. use Illuminate\Support\Facades\Cache;
  7. class DeliveryService
  8. {
  9. public function getDelivery($printStr): array
  10. {
  11. list($batchCodes, $orderCodes, $logisticNumbers) = $this->conversionPrintData($printStr);
  12. if ($batchCodes) {
  13. $orderHeaders = OracleDOCOrderHeader::query()->selectRaw('orderno')->whereIn('WaveNo', $batchCodes)->get()->toArray();
  14. $orderCodes = array_unique(array_merge($orderCodes, array_column($orderHeaders, 'orderno')));
  15. }
  16. if ($orderCodes) {
  17. $deliveryInfo = OracleDocOrderDeliveryInfo::query()->selectRaw('trackingNo')->whereIn('orderno', $orderCodes)->get()->toArray();
  18. $deliveryNos = OracleDOCOrderHeader::query()->selectRaw('deliveryno')->whereIn('orderno', $orderCodes)->get()->toArray();
  19. $logisticNumbers = array_unique(array_merge($logisticNumbers, array_column($deliveryInfo, 'trackingno'), array_column($deliveryNos, 'deliveryno')));
  20. }
  21. $OracleDocOrderDeliveryInfos = OracleDocOrderDeliveryInfo::query()->with('docOrderHeader.docOrderDeliveryInfo')->whereIn('trackingNo', $logisticNumbers)->get();
  22. $tbParams = app(TBDeliveryService::class)->getDeliveryInfo($OracleDocOrderDeliveryInfos);
  23. $pddParams = app(PDDDeliveryService::class)->getDeliveryInfo($OracleDocOrderDeliveryInfos);
  24. $sfParams = app(SFDeliveryService::class)->getDeliveryInfo($OracleDocOrderDeliveryInfos);
  25. $jdParams = app(JDDeliveryService::class)->getDeliveryInfo($logisticNumbers);
  26. $sfQhdParams = app(SFQHDDeliveryService::class)->getDeliveryInfo($logisticNumbers);
  27. return array_merge($tbParams, $pddParams, $sfParams, $jdParams, $sfQhdParams);
  28. }
  29. public function conversionPrintData($printStr): array
  30. {
  31. preg_match_all('/[\w]+/', $printStr, $nos);
  32. foreach ($nos[0] as $no) {
  33. if (strstr($no, 'SO') || strstr($no, 'so')) $orderCodes[] = $no;
  34. elseif (strstr($no, 'W') || strstr($no, 'w')) $batchesCodes[] = $no;
  35. else $logisticNumbers[] = $no;
  36. }
  37. return [$batchesCodes ?? [], $orderCodes ?? [], $logisticNumbers ?? []];
  38. }
  39. /**
  40. * 快递面单填充自定义区域内容 或 自制面单
  41. * @param array $items
  42. * @return array
  43. */
  44. public function customProcessing(array $items): array
  45. {
  46. foreach ($items as $key => &$item) {
  47. if ($item['is_process'] == true) continue;
  48. switch ($item['component_type']) {
  49. case 'TB':
  50. $items[$key] = app(TBDeliveryService::class)->processing($item);
  51. break;
  52. case 'PDD':
  53. $items[$key] = app(PDDDeliveryService::class)->processing($item);
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. return $items;
  60. }
  61. /**
  62. * 根据缓存删除十分钟前的文件
  63. */
  64. public function destroyFileOfCache()
  65. {
  66. if (Cache::has('print-template-file-list')) {
  67. $arr = Cache::get('print-template-file-list');
  68. $now = Carbon::now();
  69. foreach ($arr as $key => $item) {
  70. $time = (new Carbon($item['time']))->addMilliseconds(10);
  71. if ($now->lt($time)) {
  72. if (file_exists($item['path'])) {
  73. unlink($item['path']);
  74. }
  75. unset($arr[$key]);
  76. }
  77. }
  78. Cache::put('print-template-file-list', $arr);
  79. }
  80. }
  81. public function pushClearCache($path)
  82. {
  83. if (!Cache::has("print-template-file-list")){
  84. Cache::put('print-template-file-list',[$path]);
  85. return;
  86. }
  87. $arr = Cache::get("print-template-file-list");
  88. $arr[] = $path;
  89. Cache::put('print-template-file-list',$arr);
  90. }
  91. }