PatientsSheet.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Imports\User;
  3. use App\Models\Patient;
  4. use App\Models\User;
  5. use Maatwebsite\Excel\Concerns\ToModel;
  6. use Illuminate\Support\Collection;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. use phpDocumentor\Reflection\DocBlock;
  9. class PatientsSheet implements ToCollection
  10. {
  11. /**
  12. * @param Collection $collection
  13. */
  14. public function collection(Collection $collection)
  15. {
  16. $patients_info = [];
  17. foreach ($collection as $row) {
  18. if ($row[0]=="患者姓名")
  19. {
  20. continue;
  21. }
  22. //姓名
  23. if ($row[2] == null)
  24. {
  25. $user_name = '默认用户';
  26. $nickname = '默认用户';
  27. }else{
  28. $user_name = $row[2] ;
  29. $nickname = $row[2] ;
  30. }
  31. if ($row[1])
  32. {
  33. $has_user = User::where('phone',$row[1])->count();
  34. if ($has_user == 0)
  35. {
  36. $new_user = User::create([
  37. 'nickname' =>$nickname,
  38. 'user_name' => $user_name,
  39. 'phone' => $row[1],
  40. 'created_at' => date('Y-m-d H:i:s',time()),
  41. 'balance' => 0
  42. ]);
  43. }else{
  44. $new_user['id'] = User::where('phone',$row[1])->value('id');
  45. }
  46. }
  47. if ($new_user['id'] != null)
  48. {
  49. $patients_info['user_id'] = $new_user['id'];
  50. }else{
  51. $patients_info['user_id'] = 0;
  52. }
  53. if ($row[4] == null)
  54. {
  55. $patients_info['sex'] = 0;
  56. }else{
  57. if ($row[4] == "男")
  58. {
  59. $patients_info['sex'] = 1;
  60. }else{
  61. $patients_info['sex'] = 2;
  62. }
  63. }
  64. if ($row[0]==null){
  65. $patients_info['name'] = '默认用户';
  66. }else{
  67. $patients_info['name'] = $row[0];
  68. }
  69. if ($row[1] == null){
  70. $patients_info['phone'] = 0;
  71. }else{
  72. $patients_info['phone'] = $row[1];
  73. }
  74. //todo 患者生日没有导入
  75. Patient::create($patients_info);
  76. }
  77. }
  78. }