OrderIssueImport.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Imports;
  3. use App\OracleDOCOrderHeader;
  4. use App\Order;
  5. use App\OrderIssue;
  6. use App\OrderIssueProcessLog;
  7. use App\OrderIssueType;
  8. use Doctrine\DBAL\Connection;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Cache;
  12. use Maatwebsite\Excel\Concerns\ToCollection;
  13. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  14. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  15. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  16. HeadingRowFormatter::default('none');
  17. class OrderIssueImport implements ToCollection, WithHeadingRow,WithMultipleSheets
  18. {
  19. public function Collection(Collection $collection)
  20. {
  21. $endIS = false;
  22. if (!isset($collection->toArray()[0]['原始运单号']) || !isset($collection->toArray()[0]['情况说明']) || !isset($collection->toArray()[0]['问题类别']))
  23. Cache::put('error', '请检查您第一行标题是否存在原始运单号,情况说明,问题类别');
  24. else $endIS = true;
  25. $exception = [];
  26. $sum = 2;
  27. $orderService = app('orderService');
  28. $orderIssueService = app('orderIssueService');
  29. $orderIssues = [];
  30. if ($endIS) {
  31. foreach ($collection as $row) {
  32. if (!$row['原始运单号']) {
  33. array_push($exception, ['第' . $sum . '行数据运单号为空!']);
  34. $sum++;
  35. continue;
  36. }
  37. if (!$row['情况说明']) {
  38. array_push($exception, ['第' . $sum . '行问题说明为空!']);
  39. $sum++;
  40. continue;
  41. }
  42. if (!$row['问题类别']) {
  43. array_push($exception, ['第' . $sum . '行问题类别为空!']);
  44. $sum++;
  45. continue;
  46. }
  47. if(OracleDOCOrderHeader::where('orderNo',$row['原始运单号'])->count() == 0){
  48. array_push($exception, ['订单' . $row['原始运单号'] . '运单号不存在']);
  49. continue;
  50. }
  51. $order = Order::with('issue')->where('code', $row['原始运单号'])->first();
  52. $orderHeader =OracleDOCOrderHeader::where('orderNo',$row['原始运单号'])->first();
  53. if (!$row['问题类别']) {
  54. array_push($exception, ['订单' . $row['原始运单号'] . '没有对应的问题类别']);
  55. } else if (!$orderHeader) {
  56. array_push($exception, ['订单' . $row['原始运单号'] . '对应的WMS运单不存在']);
  57. } else if (isset($order->issue) ) {
  58. array_push($exception, ['订单' . $row['原始运单号'] . '对应的问题件已存在']);
  59. } else {
  60. $type = OrderIssueType::where('name',$row['问题类别'])->first();
  61. if ($type == null) {
  62. array_push($exception, ['订单' . $row['原始运单号'] . '没有对应的问题类别']);
  63. continue;
  64. }
  65. $order = $orderService->createOrderByWMSOrderNo($row['原始运单号']);
  66. if (!$order) {
  67. $orderIssue = $orderIssueService->createOrFind($row['原始运单号']);
  68. $orderIssue->order_issue_type_id = $type->id;
  69. $orderIssue->save();
  70. if (!$orderIssue) {
  71. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
  72. }
  73. OrderIssueProcessLog::create(['order_issue_id'=>$orderIssue['id'],'user_id'=>Auth::user()['id'],'content'=>'创建订单问题件','type'=>'创建']);
  74. array_push($orderIssues, $orderIssue);
  75. }
  76. }
  77. $sum++;
  78. }
  79. }
  80. Cache::put('exception', $exception, 86400);
  81. }
  82. /**
  83. * 该方法是实现上传文件只选中 第一个表
  84. * ExcelImprot 默认是有多少个分表就执行多少次的分表
  85. * @return OrderIssueImport[]|array
  86. */
  87. public function sheets():array{
  88. return [0=>new OrderIssueImport()];
  89. }
  90. }