LinkHallWorker.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\LinkTaskDetail;
  4. use App\Http\Models\LinkTaskUrl;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. class LinkHallWorker implements ToCollection
  9. {
  10. use Importable;
  11. protected $taskId;
  12. public function __construct($taskId)
  13. {
  14. $this->taskId = $taskId;
  15. }
  16. const FIELD_MAP = [
  17. 3 => 'username',
  18. 4 => 'email',
  19. 5 => 'password',
  20. 6 => 'url',
  21. 7 => 'show_url',
  22. 8 => 'remark',
  23. ];
  24. public function collection(Collection $collections)
  25. {
  26. $results = [];
  27. foreach ($collections as $key => $collection) {
  28. if ($key < 1) {
  29. continue;
  30. }
  31. $temp = $this->fieldMap($collection);
  32. $results[] = $temp;
  33. }
  34. $details = LinkTaskDetail::query()->where(['task_id' => $this->taskId])->get();
  35. foreach ($details as $inx => $detail) {
  36. if (!empty($results[$inx])) {
  37. $detail->update([
  38. 'username' => $results[$inx]['username'],
  39. 'email' => $results[$inx]['email'],
  40. 'password' => $results[$inx]['password'],
  41. 'url' => $results[$inx]['url'],
  42. 'remark' => $results[$inx]['remark'],
  43. ]);
  44. $showUrls = $results[$inx]['show_url'] ?? [];
  45. $insetData = [];
  46. foreach ($showUrls as $url) {
  47. $insetData[] = [
  48. 'link_tasks_detail_id' => $detail->id,
  49. 'url' => $url,
  50. 'status' => 1,//默认,
  51. 'valid_status' => 3,//待检测
  52. 'created_at' => date('Y-m-d H:i:s'),
  53. 'updated_at' => date('Y-m-d H:i:s')
  54. ];
  55. }
  56. LinkTaskUrl::query()->insert($insetData);
  57. }
  58. }
  59. }
  60. public function fieldMap($collect): array
  61. {
  62. foreach ($collect as $inx => $val) {
  63. if (empty(self::FIELD_MAP[$inx])) continue;
  64. if (self::FIELD_MAP[$inx] == 'show_url') {
  65. $result[self::FIELD_MAP[$inx]] = trim($val) ?
  66. explode("\n", trim($val)) :
  67. [];
  68. } else {
  69. $result[self::FIELD_MAP[$inx]] = trim($val);
  70. }
  71. }
  72. return $result ?? [];
  73. }
  74. }