ImportDepartmentJob.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of the ZhMead/laravel-logger.
  4. *
  5. * (c) Mead <751066209@qql.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs\Base;
  11. use App\Repositories\Enums\Manage\DealStatusEnum;
  12. use App\Repositories\Enums\Manage\ImportStatusEnum;
  13. use App\Repositories\Enums\ModelStatusEnum;
  14. use App\Repositories\Models\Base\Admin;
  15. use App\Repositories\Models\Base\Department;
  16. use App\Repositories\Models\Base\Dict;
  17. use App\Repositories\Models\Base\Resource;
  18. use App\Repositories\Models\Base\SourceMatters;
  19. use App\Repositories\Models\Manage\Category;
  20. use App\Repositories\Models\Manage\ImportRecord;
  21. use App\Repositories\Models\Manage\Message;
  22. use App\Repositories\Models\Manage\PreMessage;
  23. use Carbon\Carbon;
  24. use Fukuball\Jieba\Finalseg;
  25. use Fukuball\Jieba\Jieba;
  26. use Illuminate\Bus\Queueable;
  27. use Illuminate\Contracts\Queue\ShouldQueue;
  28. use Illuminate\Queue\InteractsWithQueue;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Log;
  31. use Illuminate\Support\Facades\Storage;
  32. use Illuminate\Support\Str;
  33. class ImportDepartmentJob implements ShouldQueue
  34. {
  35. use InteractsWithQueue;
  36. use Queueable;
  37. public $tries = 1;
  38. public $timeout = 1440;
  39. private $resourceId;
  40. private $admin;
  41. /**
  42. * Create a new job instance.
  43. */
  44. public function __construct($resourceId, $admin)
  45. {
  46. $this->resourceId = $resourceId;
  47. $this->admin = $admin;
  48. }
  49. /**
  50. * 确定任务应该超时的时间
  51. *
  52. * @return \DateTime
  53. */
  54. public function retryUntil()
  55. {
  56. return Carbon::now()->addHours(8);
  57. }
  58. /**
  59. * Execute the job.
  60. */
  61. public function handle()
  62. {
  63. $this->importTemplate1($this->resourceId);
  64. }
  65. /**
  66. * 导入数据
  67. * @return false|void
  68. */
  69. public function importTemplate1($id)
  70. {
  71. ini_set('memory_limit', '1024M');
  72. $path = Storage::disk('public')->path(Resource::byIdGetPath($id));
  73. // $path = base_path('./1.xlsx');
  74. // $path = base_path('./2.xlsx');
  75. if (!file_exists($path)) {
  76. Log::error('*************找不到文件***************');
  77. Log::error($path);
  78. return false;
  79. }
  80. try {
  81. $config = ['path' => ''];
  82. $excel = new \Vtiful\Kernel\Excel($config);
  83. $excel->openFile($path)
  84. ->openSheet()
  85. ->setSkipRows(2);
  86. //处理数据
  87. $data = [];
  88. Log::error('*************开始读取数据*****************');
  89. $total = 0;
  90. $r_total = 0;
  91. $w_total = 0;
  92. while (($row = $excel->nextRow()) !== NULL) {
  93. $nums = count($row);
  94. if ($nums >= 3) {
  95. $total++;
  96. list(
  97. $name,
  98. $type_name,
  99. $similar_name,
  100. ) = $row;
  101. if (empty($name)) continue;
  102. $type = 0;
  103. if ($type_name == '乡镇') $type = 1;
  104. $data[] = [
  105. 'name' => $name,
  106. 'type' => $type,
  107. 'similar_name' => $similar_name,
  108. ];
  109. }
  110. }
  111. Log::error("数据读取完成");
  112. $n = count($data);
  113. $r_total = $n;
  114. Log::error("开始写入数据:共{$n}条");
  115. //数据写入
  116. DB::beginTransaction();
  117. foreach ($data as $i => $item) {
  118. //教室
  119. Department::query()->updateOrCreate([
  120. 'name' => $item['name'],
  121. ], [
  122. 'similar_name' => str2arr(str_replace(',', ',', $item['similar_name'])),
  123. 'type' => $item['type']
  124. ]);
  125. Log::error("正在创建:第{$i}条数据已完成");
  126. }
  127. DB::commit();
  128. Log::error("-----------完成---------------");
  129. } catch (\Exception $exception) {
  130. Log::error($exception);
  131. DB::rollBack();
  132. }
  133. }
  134. }