| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 | <?phpnamespace App\Imports;use App\Http\Logics\Admin\SiteSyncLogic;use App\Http\Models\Article;use App\Http\Models\Server;use App\Http\Models\Site;use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Log;use Illuminate\Validation\Rule;use Maatwebsite\Excel\Concerns\Importable;use Maatwebsite\Excel\Concerns\ToCollection;use Maatwebsite\Excel\Concerns\WithMappedCells;use Maatwebsite\Excel\Concerns\WithValidation;class ArticleImport implements ToCollection{    use Importable;    public function __construct()    {    }    const oldStatusMap = [        0 => '待翻译',        1 => '翻译中',        2 => '待审核',        3 => '审核失败',        4 => '审核通过'    ];    const oldMap = [        'success_time' => '审核时间',        'sync_time' => '同步时间',        'submit_time' => '提交时间',        'expire_time' => '过期时间',        'create_time' => '创建时间',        'word_num' => '已翻词数',    ];    const FIELD_MAP = [        0 => 'domain',        6 => 'title',        7 => 'content',        8 => 'new_title',        9 => 'new_content',        10 => 'status',        11 => 'tags',        14 => 'word_num',        15 => 'sync_time',        16 => 'success_time',        17 => 'submit_time',        18 => 'expire_time',        19 => 'create_time'    ];    const STATUS_MAP = [        0 => 1,        1 => 2,        2 => 3,        3 => 4,        4 => 5    ];    public function collection(Collection $collections)    {//        $allSites = Site::query()->select(['id', 'domain'])->get();//        $all = collect([]);        $noSiteList = [];        foreach ($collections as $key => $collection) {            if ($key < 1) {                continue;            }            $temp = $this->fieldMap($collection);            if (empty($temp['sync_time'])) {                continue;            }            $hasSite = $allSites->firstWhere('domain', $temp['domain']);            if ($hasSite) {                $createdAt = $temp['create_time'] ? date('Y-m-d H:i:s', $temp['create_time']) : null;                $syncAt = $temp['sync_time'] ? date('Y-m-d H:i:s', $temp['sync_time']) : null;                $auditAt = $temp['success_time'] ? date('Y-m-d H:i:s', $temp['success_time']) : null;                //[{"anchor": null, "cn_keyword": "膜分离", "en_keyword": null}]                $inserted = [                    'is_export' => 1,                    'translator_id' => 8,//杜佳                    'site_id' => $hasSite->id,                    'group' => json_encode([                        [                            'anchor' => null,                            'cn_keyword' => null,                            'en_keyword' => $temp['tags']                        ]                    ]),                    'title' => $temp['title'],                    'content' => $temp['content'],                    'translate_title' => $temp['new_title'],                    'translate_content' => $temp['new_content'],                    'translate_words' => $temp['word_num'],                    'sync_at' => $syncAt,                    'audit_at' => $auditAt,                    'created_at' => $createdAt,                    'status' => self::STATUS_MAP[$temp['status']] ?? 99 //已结算,                ];                $all->push(collect($inserted));            } else {                $noSiteList[] = $temp['domain'];            }        }        pre_dump($noSiteList);        exit('dsd');        $allItems = $all->chunk(2000)->toArray();        foreach ($allItems as $item) {//            dd($item);            Article::query()->insert($item);        }    }    public function fieldMap($collect): array    {        $result = [];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            if (in_array(self::FIELD_MAP[$inx], ['status', 'word_num', 'sync_time', 'success_time', 'submit_time', 'expire_time', 'create_time'])) {                $result[self::FIELD_MAP[$inx]] = intval($val);            } else {                $result[self::FIELD_MAP[$inx]] = $val;            }        }        return $result;    }}
 |