DemoLinkImport.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\Link;
  4. use App\Http\Models\LinkTaskDetail;
  5. use App\Http\Models\Site;
  6. use Illuminate\Support\Collection;
  7. use Maatwebsite\Excel\Concerns\Importable;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9. class DemoLinkImport implements ToCollection
  10. {
  11. use Importable;
  12. protected $siteId;
  13. public function __construct()
  14. {
  15. }
  16. const FIELD_MAP = [
  17. 0 => 'domain',
  18. 4 => 'type',
  19. 5 => 'type_domain',
  20. 6 => 'username',
  21. 7 => 'account',
  22. 8 => 'password',
  23. 9 => 'page',
  24. 10 => 'create_time',
  25. 11 => 'upload_time'
  26. ];
  27. public function collection(Collection $collections)
  28. {
  29. $allSites = Site::query()->select(['id', 'domain'])->get();
  30. $allLinks = Link::all();
  31. $all = collect([]);
  32. $noSiteList = [];
  33. $noLink = [];
  34. foreach ($collections as $key => $collection) {
  35. if ($key < 1) {
  36. continue;
  37. }
  38. $temp = $this->fieldMap($collection);
  39. $hasSite = $allSites->firstWhere('domain', $temp['domain']);
  40. if ($hasSite) {
  41. $hasLink = $allLinks->firstWhere('url', $temp['type_domain']);
  42. if (!$hasLink) {
  43. $noLink[] = $temp;
  44. }
  45. $createdAt = $temp['create_time'] ? date('Y-m-d H:i:s', $temp['create_time']) : null;
  46. $inserted = [
  47. 'link_type' => $temp['type'],
  48. 'link_url' => $temp['type_domain'],
  49. 'link_id' => $hasLink->id ?? 0,
  50. 'redundant_site_id' => $hasSite->id,
  51. 'username' => $temp['username'],
  52. 'email' => $temp['account'],
  53. 'password' => $temp['password'],
  54. 'url' => $temp['page'],
  55. 'created_at' => $createdAt,
  56. 'enable' => 1,
  57. 'status' => 5
  58. ];
  59. $all->push(collect($inserted));
  60. } else {
  61. $noSiteList[] = $temp;
  62. }
  63. }
  64. $allItems = $all->chunk(5000)->toArray();
  65. foreach ($allItems as $item) {
  66. LinkTaskDetail::query()->insert($item);
  67. }
  68. }
  69. public function collection2(Collection $collections)
  70. {
  71. $allLinks = Link::all();
  72. $all = collect([]);
  73. foreach ($collections as $key => $collection) {
  74. if ($key < 1) {
  75. continue;
  76. }
  77. $temp = $this->fieldMap($collection);
  78. $linkTypes = Link::TYPES;
  79. array_walk($linkTypes, function ($item) {
  80. return strtolower($item);
  81. });
  82. $typeIndex = array_search(strtolower($temp['type']), $linkTypes);
  83. $noTypeList = [];
  84. if ($typeIndex === false) {
  85. // continue;
  86. $noTypeList[] = $temp['type'];
  87. }
  88. dd($noTypeList);
  89. $hasLink = $allLinks->where('url', $temp['type_domain'])->firstWhere('type', $typeIndex);
  90. if (!$hasLink) {
  91. $inserted = [
  92. 'is_export' => 1,
  93. 'type' => $typeIndex,
  94. 'url' => $temp['type_domain'],
  95. 'created_at' => date('Y-m-d H:i:s')
  96. ];
  97. $all->push(collect($inserted));
  98. }
  99. }
  100. $allItems = $all->chunk(5000)->toArray();
  101. // foreach ($allItems as $item) {
  102. // Link::query()->insert($item);
  103. // }
  104. }
  105. public function fieldMap($collect): array
  106. {
  107. $result = [];
  108. foreach ($collect as $inx => $val) {
  109. if (empty(self::FIELD_MAP[$inx])) continue;
  110. $result[self::FIELD_MAP[$inx]] = $val;
  111. }
  112. return $result;
  113. }
  114. }