You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							427 lines
						
					
					
						
							13 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							427 lines
						
					
					
						
							13 KiB
						
					
					
				| <?php | |
| /** | |
|  * | |
|  */ | |
| include_once(SERVER_ROOT . "/model/mBase.php"); | |
| include_once(SERVER_ROOT . "/vendor/mpdf/mpdf/mpdf.php"); | |
| 
 | |
| class mUserCase extends mBase { | |
|     private $obj; | |
|     private $tbl; | |
| 
 | |
|     public function __construct() { | |
|         $this->obj = new dUserCase(); | |
|         $this->tbl = 'tcm_user_case'; | |
|     } | |
| 
 | |
|     public function insertUserCase($info) { | |
|         $id = $this->obj->insert($this->tbl, $info); | |
|         if (empty($id)) { | |
|             $this->writeLog('user_case', 'insert_error_log', '添加医案失败|' . json_encode($info, JSON_UNESCAPED_UNICODE)); | |
|             return false; | |
|         } | |
| 
 | |
|         return $id; | |
|     } | |
| 
 | |
|     public function updateUserCase($id, $info) { | |
|         $res = $this->obj->update($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id))); | |
|         if (!$res) { | |
|             $this->writeLog('user_case', 'update_error_log', '更新医案失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE)); | |
|             return false; | |
|         } | |
| 
 | |
|         return true; | |
|     } | |
| 
 | |
|     public function getUserCaseById($uid, $id) { | |
|         return $this->obj->select($this->tbl, array('sql' => '`id`=? and `uid`=?', 'vals' => array($id, $uid))); | |
|     } | |
| 
 | |
|     public function saveUserCase($uid, $id, $data) { | |
|         if (empty($data['name'])) { | |
|             $this->setError('药方名称不能为空'); | |
|             return false; | |
|         } | |
|         if (empty($data['feedback'])) { | |
|             $this->setError('用药反馈不能为空'); | |
|             return false; | |
|         } | |
| 
 | |
|         $user_case = $this->getUserCaseById($uid, $id); | |
|         if (empty($user_case)) { | |
|             $this->setError('抱歉,未找到匹配的医案'); | |
|             return false; | |
|         } | |
| 
 | |
|         $res = $this->updateUserCase($id, $data); | |
|         if (!$res) { | |
|             $this->setError('更新失败'); | |
|             return false; | |
|         } | |
| 
 | |
|         return true; | |
|     } | |
| 
 | |
|     public function createUserCase($uid, $formula_id, $data) { | |
|         if (empty($data['patient_name'])) { | |
|             $this->setError('请填写患者姓名'); | |
|             return false; | |
|         } | |
|         if ($data['patient_age'] <= 0 || $data['patient_age'] > 127) { | |
|             $this->setError('患者年龄不正确'); | |
|             return false; | |
|         } | |
|         if ($data['sex'] < 0) { | |
|             $this->setError('患者性别不正确'); | |
|             return false; | |
|         } | |
|         if ($data['prescribe_num'] <= 0) { | |
|             $this->setError('开药数量不正确'); | |
|             return false; | |
|         } | |
|         if (empty($data['prescribe_herb'])) { | |
|             $this->setError('开药详情不能为空'); | |
|             return false; | |
|         } | |
|         if (empty($data['patient_say'])) { | |
|             $this->setError('请填写主诉'); | |
|             return false; | |
|         } | |
|         if (empty($data['first_diagnosis'])) { | |
|             $this->setError('请填写舌诊脉诊'); | |
|             return false; | |
|         } | |
|         if (empty($data['diagnosis'])) { | |
|             $this->setError('请填写诊断'); | |
|             return false; | |
|         } | |
| 
 | |
|         //录医案 医案名称默认为药方名称 | |
|         if ($formula_id > 0) { | |
|             $mformula = new mFormula(); | |
|             $formula = $mformula->getFormulaById($formula_id); | |
|             if (!$formula) { | |
|                 $this->setError('抱歉,未找到匹配的药方'); | |
|                 return false; | |
|             } | |
| 
 | |
|             if ($formula['uid'] > 0 && $uid != $formula['uid']) { | |
|                 $this->setError('抱歉,这不是你的药方'); | |
|                 return false; | |
|             } | |
| 
 | |
|             $data['name'] = $formula['name']; | |
|         } | |
| 
 | |
|         //手工录医案 生成随机医案名称 | |
|         if ($formula_id == 0) { | |
|             $data['name'] = "手工录入医案" . date("mdHi"); | |
|         } | |
| 
 | |
|         //格式化医案开药详情 | |
|         $mformula = new mFormula(); | |
|         $prescribe_herb = array(); | |
| 
 | |
|         $prescribe_herb_data = json_decode($data['prescribe_herb'], true); | |
|         foreach ($prescribe_herb_data as $item) { | |
|             $temp = array( | |
|                 'num' => $item['num'], | |
|             ); | |
| 
 | |
|             if (isset($item['herb_id'])) { | |
|                 $herb = $mformula->getHerbById($item['herb_id']); | |
|                 if (!$herb) { | |
|                     $this->setError('药材不存在|' . $item['herb_id']); | |
|                     return false; | |
|                 } | |
| 
 | |
|                 $temp['herb_id'] = $herb['id']; | |
|                 $prescribe_herb[] = $temp; | |
|                 continue; | |
|             } | |
| 
 | |
|             if (isset($item['name'])) { | |
|                 $herb = $mformula->getHerb($item['name']); | |
|                 if ($herb) { | |
|                     $temp['herb_id'] = $herb['id']; | |
|                     $prescribe_herb[] = $temp; | |
|                     continue; | |
|                 } | |
| 
 | |
|                 $herb_id = $mformula->insertHerb(array('uid' => $uid, 'name' => $item['name'])); | |
|                 if (!$herb_id) { | |
|                     $this->setError('添加药材失败'); | |
|                     return false; | |
|                 } | |
| 
 | |
|                 $temp['herb_id'] = $herb_id; | |
|                 $prescribe_herb[] = $temp; | |
|             } | |
|         } | |
| 
 | |
|         if (!$prescribe_herb) { | |
|             $this->setError('医案开药详情不正确'); | |
|             return false; | |
|         } | |
| 
 | |
|         $data['prescribe_herb'] = json_encode($prescribe_herb); | |
|         $data['formula_id'] = $formula_id; | |
|         $data['uid'] = $uid; | |
| 
 | |
|         $id = $this->insertUserCase($data); | |
|         if (!$id) { | |
|             $this->setError('添加医案失败'); | |
|             return false; | |
|         } | |
| 
 | |
|         if ($formula_id > 0) { | |
|             //更新用户药方使用次数 | |
|             $res = $mformula->updateFormulaUseLog($uid, $formula_id); | |
|             if (!$res) { | |
|                 $this->setError('药方使用次数更新失败'); | |
|                 return false; | |
|             } | |
|         } | |
| 
 | |
|         return $id; | |
|     } | |
| 
 | |
|     public function getUserCaseInfo($uid, $id) { | |
|         $user_case = $this->getUserCaseById($uid, $id); | |
|         if (empty($user_case)) { | |
|             $this->setError('抱歉,未找到匹配的医案'); | |
|             return false; | |
|         } | |
| 
 | |
|         $mformula = new mFormula(); | |
| 
 | |
|         if ($user_case['formula_id'] > 0) { | |
|             $formula = $mformula->getFormulaInfo($user_case['formula_id'], $uid); | |
|             if (empty($formula)) { | |
|                 $this->setError('抱歉,未找到匹配的药方'); | |
|                 return false; | |
|             } | |
| 
 | |
|             $user_case['formula'] = $formula; | |
|         } | |
| 
 | |
|         //格式化医案开药详情 | |
|         $prescribe_herb_data = json_decode($user_case['prescribe_herb'], true); | |
| 
 | |
|         $herbids = array_column($prescribe_herb_data, 'herb_id'); | |
|         $herbs = $mformula->getHerbs($herbids); | |
|         $herb_arr = array_column($herbs, null, 'id'); | |
| 
 | |
|         $prescribe_herb = array(); | |
|         foreach ($prescribe_herb_data as &$item) { | |
|             $temp = array( | |
|                 'id' => $item['id'], | |
|                 'name' => $herb_arr[$item['herb_id']]['name'], | |
|                 'num' => $item['num'], | |
|                 'desc' => $herb_arr[$item['herb_id']]['desc'], | |
|             ); | |
| 
 | |
|             $prescribe_herb[] = $temp; | |
|         } | |
| 
 | |
|         $user_case['prescribe_herb'] = $prescribe_herb; | |
| 
 | |
|         return $user_case; | |
|     } | |
| 
 | |
|     public function getUserCaseList($uid, $page_num, $page_size, $start_date = '', $end_date = '') { | |
|         $offset = ($page_num - 1) * $page_size; | |
| 
 | |
|         $where = 'uid = ?'; | |
|         $vals = array($uid); | |
|         if ($start_date) { | |
|             $where .= ' and case_time > ?'; | |
|             array_push($vals, $start_date); | |
|         } | |
| 
 | |
|         if ($end_date) { | |
|             $where .= ' and case_time < ?'; | |
|             array_push($vals, $end_date); | |
|         } | |
| 
 | |
|         return $this->obj->selectAll($this->tbl, array('sql' => $where, 'vals' => $vals), 'case_time desc ', array($offset, $page_size)); | |
|     } | |
| 
 | |
|     public function getUserCaseListCount($uid) { | |
|         return $this->obj->count($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid))); | |
|     } | |
| 
 | |
|     public function getUserCaseListPdfInfo($uid, $page_num, $page_size, $start_date, $end_date) { | |
|         $data = $this->getUserCaseList($uid, $page_num, $page_size, $start_date, $end_date); | |
| 
 | |
|         $formula_ids = array(); | |
|         $herb_ids = array(); | |
|         foreach ($data as &$value) { | |
|             $value['prescribe_herb'] = json_decode($value['prescribe_herb'], true); | |
| 
 | |
|             $herb_ids = array_merge($herb_ids, array_column($value['prescribe_herb'], 'herb_id')); | |
| 
 | |
|             $formula_ids[] = $value['formula_id']; | |
|         } | |
| 
 | |
|         $mformula = new mFormula(); | |
| 
 | |
|         //查询药方索引 | |
|         if (!empty($formula_ids)) { | |
|             $formula_data = $mformula->getFormulaByIds($formula_ids); | |
|             $formula_arr = array_column($formula_data, null, 'id'); | |
|         } | |
| 
 | |
|         $herbs = $mformula->getHerbs($herb_ids); | |
|         $herb_arr = array_column($herbs, null, 'id'); | |
| 
 | |
|         //格式化pdf数据 | |
|         foreach ($data as &$value) { | |
|             $value['patient_sex'] = $value['patient_sex'] == 0 ? '男' : '女'; | |
|             $value['case_time'] = date('Y年m月d日', strtotime($value['case_time'])); | |
| 
 | |
|             //格式化开药详情 | |
|             $herb_str_arr = array(); | |
|             foreach ($value['prescribe_herb'] as $va) { | |
|                 $herb_name = $herb_arr[$va['herb_id']]['name']; | |
|                 $herb_num = $va['num']; | |
|                 $herb_desc = $herb_arr[$va['herb_id']]['desc']; | |
| 
 | |
|                 $herb_str_arr[] = $herb_name . $herb_desc . $herb_num . "克"; | |
|             } | |
|             $value['prescribe_herb_info'] = implode('、', $herb_str_arr); | |
| 
 | |
|             $value['formula_info'] = ''; | |
|             if (isset($formula_arr[$value['formula_id']])) { | |
|                 $formula_info = $formula_arr[$value['formula_id']]; | |
| 
 | |
|                 $value['formula_info'] .= "选 "; | |
| 
 | |
|                 if (!empty($formula_info['source'])) { | |
|                     $value['formula_info'] .= $formula_info['source'] . " "; | |
|                 } | |
| 
 | |
|                 if (!empty($formula_info['name'])) { | |
|                     $value['formula_info'] .= $formula_info['name'] . ":"; | |
|                 } | |
|             } | |
| 
 | |
|             unset($value['id']); | |
|             unset($value['uid']); | |
|             unset($value['formula_id']); | |
|             unset($value['prescribe_herb']); | |
|             unset($value['create_time']); | |
|         } | |
| 
 | |
|         return $data; | |
|     } | |
| 
 | |
|     public function getUserCasePdfInfo($uid, $id) { | |
|         $data = $this->getUserCaseInfo($uid, $id); | |
| 
 | |
|         $data['patient_sex'] = $data['patient_sex'] == 0 ? '男' : '女'; | |
|         $data['case_time'] = date('Y年m月d日', strtotime($data['case_time'])); | |
| 
 | |
|         //格式化pdf数据 | |
|         $herb_arr = array(); | |
|         foreach ($data['prescribe_herb'] as $herb) { | |
|             $herb_arr[] = $herb['name'] . $herb['num'] . "克"; | |
|         } | |
|         $herb_str = implode('、', $herb_arr); | |
|         $data['prescribe_herb_info'] = $herb_str; | |
| 
 | |
|         //查询药方索引 | |
|         $data['formula_info'] = ''; | |
|         if (!empty($data['formula'])) { | |
|             $data['formula_info'] .= "选 "; | |
| 
 | |
|             if (!empty($data['formula']['source'])) { | |
|                 $data['formula_info'] .= $data['formula']['source'] . " "; | |
|             } | |
| 
 | |
|             if (!empty($data['formula']['name'])) { | |
|                 $data['formula_info'] .= $data['formula']['name'] . ":"; | |
|             } | |
|         } | |
| 
 | |
|         unset($data['id']); | |
|         unset($data['uid']); | |
|         unset($data['index_id']); | |
|         unset($data['prescribe_herb']); | |
|         unset($data['create_time']); | |
|         unset($data['formula']); | |
| 
 | |
|         return $data; | |
|     } | |
| 
 | |
|     public function getPdf($uid, $id, $start_date = '', $end_date = '') { | |
|         $pdf_name = md5($uid . $id . $start_date . $end_date) . ".pdf"; | |
|         $temp_dir = sprintf(USER_CASE_PDF_PATH, $uid); | |
|         $temp_url = sprintf(USER_CASE_PDF_URL, $uid); | |
| 
 | |
|         if (file_exists($temp_dir . $pdf_name)) { | |
|             return array( | |
|                 'pdf_path' => $temp_dir . $pdf_name, | |
|                 'pdf_url' => $temp_url . $pdf_name, | |
|             ); | |
|         } | |
| 
 | |
|         //导出单个医案 | |
|         if ($id > 0) { | |
|             $post_url = USER_CASE_HTML_URL; | |
|             $data = array( | |
|                 "uid" => $uid, | |
|                 "id" => $id, | |
|             ); | |
|         } else { | |
|             $post_url = USER_CASE_LIST_HTML_URL; | |
|             $data = array( | |
|                 "uid" => $uid, | |
|                 "start_date" => $start_date, | |
|                 "end_date" => $end_date, | |
|             ); | |
|         } | |
| 
 | |
|         $return = $this->postCUrl($post_url, $data); | |
|         $res = json_decode($return, 1); | |
|         if (isset($res['info'])) { | |
|             $this->setError('抱歉,未找到可导出的医案记录'); | |
|             return false; | |
|         } | |
| 
 | |
|         $this->createPdf($temp_dir, $pdf_name, $return); | |
| 
 | |
|         return array( | |
|             'pdf_path' => $temp_dir . $pdf_name, | |
|             'pdf_url' => $temp_url . $pdf_name, | |
|         ); | |
|     } | |
| 
 | |
|     public function createPdf($temp_dir, $pdf_name, $pdf_html) { | |
|         if (!is_dir($temp_dir)) { | |
|             mkdir($temp_dir, 0755, true); | |
|             chown($temp_dir, 'nobody'); | |
|             chgrp($temp_dir, 'nobody'); | |
|         } | |
| 
 | |
|         $mpdf = new mPDF(); | |
| 
 | |
|         $mpdf->WriteHTML($pdf_html); | |
| 
 | |
|         return $mpdf->Output($temp_dir . $pdf_name, 'F');  // D表示下载,I表示在浏览器中查看 | |
|     } | |
| 
 | |
|     function sendPdf($email, $pdf_path) { | |
|         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
|             $this->setError('请填写合法的邮箱地址'); | |
|             return false; | |
|         } | |
|         $res = $this->sendMail(array($email), date('Y年m月d日', time()) . '-医案导出', '', $pdf_path); | |
|         if (!$res) { | |
|             $this->setError('发送失败'); | |
|             return false; | |
|         } | |
|         return true; | |
|     } | |
| } |