PaperBoxesImport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Imports;
  3. use App\Owner;
  4. use App\PaperBox;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  9. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  10. HeadingRowFormatter::default('none');
  11. class PaperBoxesImport implements ToCollection,WithHeadingRow
  12. {
  13. protected $isOverride;
  14. public function __construct($isOverride)
  15. {
  16. if ($isOverride==1){
  17. $this->isOverride=true;
  18. }
  19. }
  20. /**
  21. * @param Collection $collection
  22. */
  23. public function Collection(Collection $collection)
  24. {
  25. $endIs=false;
  26. $cityIs=true;
  27. if (isset($collection->toArray()[0]['货主'])&&isset($collection->toArray()[0]['纸箱名称'])&&isset($collection->toArray()[0]['长'])
  28. &&isset($collection->toArray()[0]['宽'])&&isset($collection->toArray()[0]['高'])){
  29. $endIs=true;
  30. }else{
  31. Cache::put('error','请检查您第一行标题是否存在货主,纸箱名称,长,宽,高',86400);
  32. $endIs=false;
  33. }
  34. $exception=[];
  35. $sum=2;
  36. if ($endIs) {
  37. foreach ($collection as $row) {
  38. if (!$row['货主']) {
  39. array_push($exception, ['第' . $sum . '行数据货主为空!']);
  40. $sum++;
  41. continue;
  42. }
  43. if (!$row['纸箱名称']) {
  44. array_push($exception, ['第' . $sum . '行数据纸箱名称为空!']);
  45. $sum++;
  46. continue;
  47. }
  48. if (!$row['长'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['长']) == 0) {
  49. array_push($exception, ['第' . $sum . '行数据长为空或不为数字!']);
  50. $sum++;
  51. continue;
  52. }
  53. if (!$row['宽'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['宽']) == 0) {
  54. array_push($exception, ['第' . $sum . '行数据宽为空或不为数字!']);
  55. $sum++;
  56. continue;
  57. }
  58. if (!$row['高'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['高']) == 0) {
  59. array_push($exception, ['第' . $sum . '行数据高为空或不为数字!']);
  60. $sum++;
  61. continue;
  62. }
  63. $owner = Owner::where("name", $row['货主'])->first();
  64. if (!$owner) {
  65. array_push($exception, ['第' . $sum . '行数据货主在系统中未找到!']);
  66. $sum++;
  67. continue;
  68. }
  69. $length = $row['长'];
  70. $width = $row['宽'];
  71. $height = $row['高'];
  72. $max = ($length >= ($width >= $height ? $width : $height) ? $length : ($width >= $height ? $width : $height));
  73. if ($max == $length) {
  74. $centre = $width >= $height ? $width : $height;
  75. $min = $width < $height ? $width : $height;
  76. } elseif ($max == $width) {
  77. $centre = $length >= $height ? $length : $height;
  78. $min = $length < $height ? $length : $height;
  79. } else {
  80. $centre = $width >= $length ? $width : $length;
  81. $min = $width < $length ? $width : $length;
  82. }
  83. $paperBox = PaperBox::with('owners')->where('model', $row['纸箱名称'])->first();
  84. if ($paperBox) {
  85. if ($this->isOverride) {
  86. $paperBox->length = $max;
  87. $paperBox->width = $centre;
  88. $paperBox->height = $min;
  89. $paperBox->save();
  90. array_push($exception, ['第' . $sum . '行数据已覆盖!']);
  91. } else {
  92. array_push($exception, ['第' . $sum . '行数据已存在!']);
  93. }
  94. if ($paperBox->owners){
  95. $isOwner = true;
  96. foreach ($paperBox->owners as $o){
  97. if ($o->id == $owner->id){
  98. $isOwner = false;
  99. break;
  100. }
  101. }
  102. if ($isOwner) $paperBox->owners()->syncWithoutDetaching($owner->id);
  103. }
  104. } else {
  105. $paperBox = new PaperBox([
  106. 'model' => $row['纸箱名称'],
  107. 'length' => $max,
  108. 'width' => $centre,
  109. 'height' => $min,
  110. ]);
  111. $paperBox->save();
  112. $paperBox->owners()->sync($owner->id);
  113. }
  114. $sum++;
  115. }
  116. Cache::put('exception', $exception, 86400);
  117. }
  118. }
  119. }