123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /*
- * This file is part of the ZhMead/laravel-logger.
- *
- * (c) Mead <751066209@qql.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs\Base;
- use App\Repositories\Enums\Manage\DealStatusEnum;
- use App\Repositories\Enums\Manage\ImportStatusEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\Base\Dict;
- use App\Repositories\Models\Base\Resource;
- use App\Repositories\Models\Base\SourceMatters;
- use App\Repositories\Models\Manage\Category;
- use App\Repositories\Models\Manage\ImportRecord;
- use App\Repositories\Models\Manage\Message;
- use App\Repositories\Models\Manage\PreMessage;
- use Carbon\Carbon;
- use Fukuball\Jieba\Finalseg;
- use Fukuball\Jieba\Jieba;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- class ImportDepartmentJob implements ShouldQueue
- {
- use InteractsWithQueue;
- use Queueable;
- public $tries = 1;
- public $timeout = 1440;
- private $resourceId;
- private $admin;
- /**
- * Create a new job instance.
- */
- public function __construct($resourceId, $admin)
- {
- $this->resourceId = $resourceId;
- $this->admin = $admin;
- }
- /**
- * 确定任务应该超时的时间
- *
- * @return \DateTime
- */
- public function retryUntil()
- {
- return Carbon::now()->addHours(8);
- }
- /**
- * Execute the job.
- */
- public function handle()
- {
- $this->importTemplate1($this->resourceId);
- }
- /**
- * 导入数据
- * @return false|void
- */
- public function importTemplate1($id)
- {
- ini_set('memory_limit', '1024M');
- $path = Storage::disk('public')->path(Resource::byIdGetPath($id));
- // $path = base_path('./1.xlsx');
- // $path = base_path('./2.xlsx');
- if (!file_exists($path)) {
- Log::error('*************找不到文件***************');
- Log::error($path);
- return false;
- }
- try {
- $config = ['path' => ''];
- $excel = new \Vtiful\Kernel\Excel($config);
- $excel->openFile($path)
- ->openSheet()
- ->setSkipRows(2);
- //处理数据
- $data = [];
- Log::error('*************开始读取数据*****************');
- $total = 0;
- $r_total = 0;
- $w_total = 0;
- while (($row = $excel->nextRow()) !== NULL) {
- $nums = count($row);
- if ($nums >= 3) {
- $total++;
- list(
- $name,
- $type_name,
- $similar_name,
- ) = $row;
- if (empty($name)) continue;
- $type = 0;
- if ($type_name == '乡镇') $type = 1;
- $data[] = [
- 'name' => $name,
- 'type' => $type,
- 'similar_name' => $similar_name,
- ];
- }
- }
- Log::error("数据读取完成");
- $n = count($data);
- $r_total = $n;
- Log::error("开始写入数据:共{$n}条");
- //数据写入
- DB::beginTransaction();
- foreach ($data as $i => $item) {
- //教室
- Department::query()->updateOrCreate([
- 'name' => $item['name'],
- ], [
- 'similar_name' => str2arr(str_replace(',', ',', $item['similar_name'])),
- 'type' => $item['type']
- ]);
- Log::error("正在创建:第{$i}条数据已完成");
- }
- DB::commit();
- Log::error("-----------完成---------------");
- } catch (\Exception $exception) {
- Log::error($exception);
- DB::rollBack();
- }
- }
- }
|