12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Imports;
- use App\Http\Models\Business;
- use App\Http\Models\Link;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToCollection;
- class ExternalChainLibraryImport implements ToCollection
- {
- use Importable;
- const TYPE = [
- '否' => 0,
- '是' => 1,
- ];
- public function collection(Collection $collections)
- {
- $linkType = array_flip(Link::TYPES);
- $business = array_flip(Business::query()->pluck('title', 'id')->toArray());
- $list = [];
- foreach ($collections as $key => $collection) {
- if ($key < 1) {
- continue;
- }
- $data = [
- 'type' => $linkType[$collection[0]],//对不上直接抛异常
- 'tag' => $collection[1] ?? '',
- 'url' => $collection[2] ?? '',
- 'country' => $collection[5] ?? '',
- 'big_ball' => self::TYPE[$collection[6]] ?? 0,
- 'quantitative_restrictions' => self::TYPE[$collection[7]] ?? 0,
- 'phone_verification' => self::TYPE[$collection[8]] ?? 0,
- 'timed_release' => self::TYPE[$collection[9]] ?? 0,
- 'authority_score' => $collection[10] ?? '',
- 'spam_score' => $collection[11] ?? '',
- 'home_page' => self::TYPE[$collection[12]] ?? 0,
- 'remark' => $collection[13] ?? '',
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $cost = explode('/', $collection[3]);
- if (!empty($cost)) {
- if (!empty($cost[1])) {
- $data['cost'] = "1,2";
- } else {
- $data['cost'] = "1";
- }
- }
- $businessIdsList = [];
- $businessIds = explode('/', $collection[4]);
- if (!empty($businessIds)) {
- foreach ($businessIds as $kk => $vv) {
- $businessIdsList[] = $business[$vv] ?? 0;
- }
- }
- if (!empty($businessIdsList)) {
- $businessIdsList = array_unique($businessIdsList);
- $businessIdsList = implode(',', $businessIdsList);
- $data['business_ids'] = $businessIdsList;
- }
- $list[] = $data;
- }
- Link::query()->insert($list);
- }
- }
|