PaperBoxesImport.php 5.1 KB

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