PatientsSheet.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. \Log::info('新增用户'.$new_user['id']);
  44. }else{
  45. $new_user['id'] = User::where('phone',$row[1])->value('id');
  46. }
  47. }
  48. if ($new_user['id'] != null)
  49. {
  50. $patients_info['user_id'] = $new_user['id'];
  51. }else{
  52. $patients_info['user_id'] = 0;
  53. }
  54. if ($row[4] == null)
  55. {
  56. $patients_info['sex'] = 0;
  57. }else{
  58. if ($row[5] == "男")
  59. {
  60. $patients_info['sex'] = 1;
  61. }else{
  62. $patients_info['sex'] = 2;
  63. }
  64. }
  65. if ($row[0]==null){
  66. $patients_info['name'] = '默认用户';
  67. }else{
  68. $patients_info['name'] = $row[0];
  69. }
  70. if ($row[1] == null){
  71. $patients_info['phone'] = 0;
  72. }else{
  73. $patients_info['phone'] = $row[1];
  74. }
  75. if ($row[3])
  76. {
  77. $patients_info['birthday'] = self::excelTime($row[3]);
  78. }else{
  79. $patients_info['birthday'] = self::excelTime(0);
  80. }
  81. Patient::create($patients_info);
  82. }
  83. }
  84. static function excelTime($date, $time = false) {
  85. if(function_exists('GregorianToJD')){
  86. if (is_numeric( $date )) {
  87. $jd = GregorianToJD( 1, 1, 1970 );
  88. $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
  89. $date = explode( '/', $gregorian );
  90. $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
  91. ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
  92. ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
  93. . ($time ? " 00:00:00" : '');
  94. return $date_str;
  95. }
  96. }else{
  97. $date=$date>25568?$date+1:25569;
  98. /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
  99. $ofs=(70 * 365 + 17+2) * 86400;
  100. $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
  101. }
  102. // dd($date);
  103. return $date;
  104. }
  105. }