DrugsImport.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Imports;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\TCM\Drug;
  5. use App\Repositories\Models\TCM\Prescription;
  6. use Maatwebsite\Excel\Concerns\OnEachRow;
  7. use Maatwebsite\Excel\Row;
  8. use Overtrue\LaravelPinyin\Facades\Pinyin;
  9. class DrugsImport implements OnEachRow
  10. {
  11. public function onRow(Row $row)
  12. {
  13. if ($row->getIndex() <= 2) return false;
  14. if (empty($row[1])) return false;
  15. $type = Prescription::DRUG_TYPE_YP;
  16. switch ($row[2]) {
  17. case '颗粒':
  18. $type = Prescription::DRUG_TYPE_KL;
  19. break;
  20. }
  21. $letter = Pinyin::abbr($row[1]);
  22. $initial = substr($letter, 1);
  23. $data = [
  24. 'name' => $row[1],
  25. 'type' => $type,
  26. 'area' => $row[3],
  27. 'unit' => $row[4],
  28. 'letter' => $letter,
  29. 'initial' => $initial,
  30. 'admin_id' => login_admin_id(),
  31. 'status' => ModelStatusEnum::OK,
  32. ];
  33. if (Drug::query()->where('name', $row[1])->exists()) return false;
  34. Drug::create($data);
  35. return true;
  36. }
  37. }