Hospital.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use think\Db;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Shared\Date;
  7. class Hospital extends Controller
  8. {
  9. /**
  10. * 绑定数据表
  11. * @var string
  12. */
  13. protected $table = 'store_hospital';
  14. /**
  15. * 系统消息
  16. * @auth true
  17. * @menu true
  18. * @throws \think\Exception
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. * @throws \think\exception\PDOException
  23. */
  24. public function index()
  25. {
  26. $this->title = '超级医疗平台列表';
  27. $query = $this->_query($this->table)->like('name');
  28. $query->order('id desc')->page();
  29. }
  30. /**
  31. * 数据列表处理
  32. * @param array $data
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. protected function _index_page_filter(&$data)
  38. {
  39. foreach ($data as &$vo) {
  40. $vo['sheng'] =Db::name('q_area')->where('id',$vo['sheng'])->value('shortname');
  41. $vo['shi'] =Db::name('q_area')->where('id',$vo['shi'])->value('shortname');
  42. $vo['qu'] =Db::name('q_area')->where('id',$vo['qu'])->value('shortname');
  43. }
  44. }
  45. /**
  46. * 添加消息
  47. * @auth true
  48. * @throws \think\Exception
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. * @throws \think\exception\PDOException
  53. */
  54. public function add()
  55. {
  56. $this->_form($this->table, 'form');
  57. }
  58. /**
  59. * 编辑消息
  60. * @auth true
  61. * @throws \think\Exception
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. * @throws \think\exception\PDOException
  66. */
  67. public function edit()
  68. {
  69. $this->_form($this->table, 'form');
  70. }
  71. protected function _form_filter(&$data)
  72. {
  73. $data['sheng']=1;
  74. $data['shi']=2;
  75. $data['qu']=3;
  76. }
  77. /**
  78. * 删除消息
  79. * @auth true
  80. * @throws \think\Exception
  81. * @throws \think\exception\PDOException
  82. */
  83. public function del()
  84. {
  85. $this->_save($this->table, ['is_deleted' => '1']);
  86. }
  87. function get_address($id){
  88. $add = db('q_area')->where('pid', $id)->select();
  89. return $add;
  90. }
  91. public function import(){
  92. set_time_limit(60);//限制页面执行时间60s
  93. $file = $_FILES['file']['tmp_name'];//临时文件路径或者文件url
  94. try{
  95. $file = iconv("utf-8", "gb2312", $file);//转码
  96. if (empty($file) OR !file_exists($file)) {
  97. throw new \Exception('文件不存在!');
  98. }
  99. $objReader = IOFactory::createReader('Xlsx');
  100. if (!$objReader->canRead($file)) {
  101. $objReader = IOFactory::createReader('Xls');
  102. if (!$objReader->canRead($file)) {
  103. throw new \Exception('只支持导入Excel文件!');
  104. }
  105. }
  106. $objReader->setReadDataOnly(true);//忽略任何格式的信息
  107. $objPHPExcel = $objReader->load($file);
  108. $worksheet = $objPHPExcel->getActiveSheet();
  109. $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
  110. $highestRow = $sheet->getHighestRow(); // 取得总行数
  111. // $highestColumn = $sheet->getHighestColumn(); // 取得总列数
  112. /**
  113. * 必填: 姓名 手机号 性别 报读院校 报读专业 报读层次 收件信息
  114. * 非必填: 民族 政治面貌 户口所在地(市区) 考试县区 身份证号码 考前学历 毕业院校 毕业时间(年月) 毕业证书编号 毕业专业
  115. */
  116. $genders = [ 0 => '保密', 1 => '男', 2 => '女'];
  117. $data = [];
  118. $msg = '';
  119. //$keys = ['name', 'phone', 'gender','end_date'];
  120. for ($j = 2; $j <= $highestRow; $j++) {
  121. //foreach ($keys as $k => $v) {
  122. // $$v = $worksheet->getCellByColumnAndRow($k + 1, $j)->getValue();
  123. //}
  124. $name = $worksheet->getCellByColumnAndRow(1, $j)->getValue();
  125. $phone = $worksheet->getCellByColumnAndRow(2, $j)->getValue();
  126. $gender= $worksheet->getCellByColumnAndRow(3, $j)->getValue();
  127. $gender = array_search($gender, $genders);
  128. $end_date = $worksheet->getCellByColumnAndRow(4, $j)->getValue();
  129. //$end_date = date('Y-m-d', ($value - 25569) * 24 * 3600)
  130. $end_date = date('Y-m-d', Date::excelToTimestamp($end_date));//转化为时间类型
  131. //$name = $worksheet->getCell("A" . $j)->getValue();
  132. //$phone = $worksheet->getCell("B" . $j)->getValue();
  133. //$gender = $worksheet->getCell("C" . $j)->getValue();
  134. if (empty($name)) {
  135. $msg .= '<p style="color:red">第'.($j-1).'条数据,姓名没有填写,插入失败</p>';
  136. } else {
  137. $data[] = [
  138. 'name' => $name,
  139. 'phone' => $phone,
  140. 'gender' => $gender,
  141. 'end_date ' => $end_date ,
  142. ];
  143. }
  144. }
  145. $res = Db::name('User')->data($data)->limit(100)->insertAll();// 分批插入数据
  146. $msg .= '<p style="color:blue">导入完成</p><p style="color:blue">总共'.($highestRow-1).'条数据</p><p style="color:blue">成功'.count($data).'条数据</p><p style="color:red">失败'.($highestRow-count($data)-1).'条数据</p>';
  147. return json(['code'=>1, 'msg'=>$msg]);
  148. } catch (\Exception $e) {
  149. return json(['code'=>0, 'msg'=>$e->getMessage()]);
  150. }
  151. }
  152. }