ExternalChainLibraryImport.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\Business;
  4. use App\Http\Models\Link;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. class ExternalChainLibraryImport implements ToCollection
  9. {
  10. use Importable;
  11. const TYPE = [
  12. '否' => 0,
  13. '是' => 1,
  14. ];
  15. public function collection(Collection $collections)
  16. {
  17. $linkType = array_flip(Link::TYPES);
  18. $business = array_flip(Business::query()->pluck('title', 'id')->toArray());
  19. $list = [];
  20. foreach ($collections as $key => $collection) {
  21. if ($key < 1) {
  22. continue;
  23. }
  24. $data = [
  25. 'type' => $linkType[$collection[0]],//对不上直接抛异常
  26. 'tag' => $collection[1] ?? '',
  27. 'url' => $collection[2] ?? '',
  28. 'country' => $collection[5] ?? '',
  29. 'big_ball' => self::TYPE[$collection[6]] ?? 0,
  30. 'quantitative_restrictions' => self::TYPE[$collection[7]] ?? 0,
  31. 'phone_verification' => self::TYPE[$collection[8]] ?? 0,
  32. 'timed_release' => self::TYPE[$collection[9]] ?? 0,
  33. 'authority_score' => $collection[10] ?? '',
  34. 'spam_score' => $collection[11] ?? '',
  35. 'home_page' => self::TYPE[$collection[12]] ?? 0,
  36. 'remark' => $collection[13] ?? '',
  37. 'created_at' => date('Y-m-d H:i:s')
  38. ];
  39. $cost = explode('/', $collection[3]);
  40. if (!empty($cost)) {
  41. if (!empty($cost[1])) {
  42. $data['cost'] = "1,2";
  43. } else {
  44. $data['cost'] = "1";
  45. }
  46. }
  47. $businessIdsList = [];
  48. $businessIds = explode('/', $collection[4]);
  49. if (!empty($businessIds)) {
  50. foreach ($businessIds as $kk => $vv) {
  51. $businessIdsList[] = $business[$vv] ?? 0;
  52. }
  53. }
  54. if (!empty($businessIdsList)) {
  55. $businessIdsList = array_unique($businessIdsList);
  56. $businessIdsList = implode(',', $businessIdsList);
  57. $data['business_ids'] = $businessIdsList;
  58. }
  59. $list[] = $data;
  60. }
  61. Link::query()->insert($list);
  62. }
  63. }