SocialImport.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\SocialPublish;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\DB;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. class SocialImport implements ToCollection
  9. {
  10. use Importable;
  11. protected $siteId;
  12. public function __construct($siteId)
  13. {
  14. $this->siteId = $siteId;
  15. }
  16. public function collection(Collection $collections)
  17. {
  18. $list = [];
  19. $delList = [];
  20. foreach ($collections as $key => $collection) {
  21. if ($key < 1) {
  22. continue;
  23. }
  24. if (trim($collection[1]) == 1) {
  25. $delList[] = trim($collection[0]);
  26. } else {
  27. $update = [
  28. 'site_id' => $this->siteId,
  29. 'label' => trim($collection[0]),
  30. ];
  31. $list[] = $update;
  32. }
  33. }
  34. if (!empty($list)) {
  35. DB::table('social_label')->insert($list);
  36. }
  37. if (!empty($delList)) {
  38. DB::table('social_label')
  39. ->where('site_id', $this->siteId)
  40. ->whereIn('label', $delList)
  41. ->delete();
  42. }
  43. }
  44. }