123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- declare(strict_types=1);
- namespace app\controller;
- use app\model\SymptomDictionary;
- use think\App;
- use think\facade\Log;
- use think\facade\Request;
- use think\helper\Arr;
- use think\response\Json;
- use app\model\PatientAuxiliaryDiagnosis;
- use app\controller\Cdss;
- use think\facade\Db;
- /**
- * 患者辅助诊疗控制器
- * Class PatientAuxiliary
- * @package app\controller
- */
- class PatientAuxiliary extends CommonTwoController
- {
- /**
- * CDSS控制器实例
- * @var Cdss
- */
- protected $cdss;
- /**
- * 构造函数
- * @param App $app 应用实例
- */
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->cdss = new Cdss($app);
- }
- /**
- * 保存患者辅助诊疗信息
- * @return Json
- */
- public function savePatientAuxiliaryInfo(): Json
- {
- // 获取请求参数
- $params = $this->request->param();
-
- // 设置默认值
- $params = array_merge([
- 'ZYH' => '',
- 'ZSZZ' => '',
- 'name' => '',
- 'tag' => '',
- 'XB' => '',
- 'NL' => 0,
- 'document' => '',
- 'docter' => '',
- 'YGDM' => '',
- 'KSDM' => '',
- 'YZXX' => ''
- ], $params);
- // 参数验证
- $validate = validate([
- 'ZYH' => 'require|number', // 只验证住院号必填
- ]);
- if (!$validate->check($params)) {
- return $this->_json_error($validate->getError());
- }
- if($params['YZXX']){
- $medicineProduction = $inspection = '';
- if(isset($params['YZXX']['MedicineProduction']) && !empty($params['YZXX']['MedicineProduction'])){
- $medicineProduction = implode(',',$params['YZXX']['MedicineProduction']);
- }
- if(isset($params['YZXX']['Inspection']) && !empty($params['YZXX']['Inspection'])){
- $inspection = implode(',',$params['YZXX']['Inspection']);
- }
- }
- // 准备基础数据
- $baseData = [
- 'name' => $params['name'],
- 'tag' => $params['tag'],
- 'zszz' => $params['ZSZZ'],
- 'xb' => $params['XB'],
- 'nl' => intval($params['NL']),
- 'document' => $params['document'],
- 'docter' => $params['docter'],
- 'ygdm' => $params['YGDM'],
- 'ksdm' => $params['KSDM']
- ];
- if($params['YZXX']) {
- $baseData['name'] = $medicineProduction;
- $baseData['name'] .= $medicineProduction ? ($inspection ? ",{$inspection}" : "") : "{$inspection}";
- }
- try {
- // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
- if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
- try {
- // 从主诉症状中提取关键词
- // $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
- $dictionary = Db::table('jm_cdss_symptom_keywords')->field('keyword')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", keyword, "%")')->select()->toArray();
- // $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
- $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'keyword')) : '';
- // 如果提取到了症状关键词,才调用辅助诊疗
- if (!empty($mainSymptoms)) {
- $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
- $mainSymptoms, // 使用提取的症状关键词
- $params['NL'],
- $params['XB'] == '男' ? 2 : 1
- );
-
- if (!empty($auxiliaryResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
- $data
- );
- }
- }
- } catch (\Exception $e) {
- return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
- }
- }
- // 2. 知识库 - 需要name和tag
- if ($params['name'] && $params['tag']) {
- try {
- // 调用detail方法
- $response = $this->cdss->detail([
- 'name' => $params['name'],
- 'tag' => $params['tag'],
- 'is_web' => 1
- ]);
-
- // 从Json响应中获取数据
- $knowledgeResult = $response->getData();
-
- if (!empty($knowledgeResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
- $data
- );
- }
- } catch (\Exception $e) {
- return $this->_json_error('知识库调用失败: ' . $e->getMessage());
- }
- }
- if((isset($medicineProduction) && isset($inspection)) && $medicineProduction || $inspection){
- try {
- if($medicineProduction){
- // 调用detail方法
- $medicineResult = $this->cdss->detail([
- 'name' => $medicineProduction,
- 'tag' => 'MedicineProduction',
- 'is_web' => 1
- ]);
- // 从Json响应中获取数据
- $medicine = $medicineResult->getData();
- $knowledgeResult = $medicine;
- }
- if($inspection){
- // 调用detail方法
- $inspectionResult = $this->cdss->detail([
- 'name' => $inspection,
- 'tag' => 'Inspection',
- ]);
- // 从Json响应中获取数据
- $inspections = $inspectionResult->getData();
- if(isset($knowledgeResult)){
- $knowledgeResult['data'] = array_merge($knowledgeResult['data'],$inspections['data']);
- }else {
- $knowledgeResult = $inspections;
- }
- }
- if (!empty($knowledgeResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
- $data
- );
- }
- } catch (\Exception $e) {
- return $this->_json_error('知识库调用失败: ' . $e->getMessage());
- }
- }
- // 3. 医嘱审核 - 需要name和性别
- $hasReviewResult = false;
- if (((isset($inspection) && $inspection) || $params['name']) && $params['XB']) {
- $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
- try {
- $reviewResult = $this->cdss->yzEamine([
- 'name' => $params['name'] ?: $inspection,
- 'gender' => $gender
- ]);
-
- if (!empty($reviewResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_REVIEW,
- $data
- );
- $hasReviewResult = true;
- }
- } catch (\Exception $e) {
- return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
- }
- }
-
- // 检查辅助诊疗结果
- $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
-
- // 检查知识库结果 - detail()返回的是Json对象
-
- $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
-
- // 返回结果 - 任何一个匹配就设置req=1
- return $this->_json_succ(
- [
- // 'req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0
- 'req' => $hasReviewResult ? 1 : 0
- ],
- 0,
- '保存成功'
- );
- } catch (\Exception $e) {
- return $this->_json_error('保存失败: ' . $e->getMessage());
- }
- }
- /**
- * 获取患者辅助诊疗信息
- * @return Json
- */
- public function getPatientAuxiliaryInfo(): Json
- {
- // 获取住院号
- $zyh = $this->request->param('zyh');
- \think\facade\Log::info(__FUNCTION__.': zyh='.$zyh);
- if (!$zyh) {
- return $this->_json_error('住院号不能为空');
- }
- try {
- // 获取该住院号的所有记录
- $records = PatientAuxiliaryDiagnosis::where('zyh', $zyh)->select()->toArray();
-
- // 整理返回数据
- $result = [];
- foreach ($records as $record) {
- $result[] = [
- 'tag' => $record['tag'], // auxiliary/knowledge/review
- 'name' => $record['name'],
- 'ZSZZ' => $record['ZSZZ'],
- 'content' => json_decode($record['content'], true), // 解码JSON字符串为数组
- 'xb' => $record['xb'],
- 'nl' => $record['nl'],
- 'document' => $record['document'],
- 'docter' => $record['docter'],
- 'ygdm' => $record['ygdm'],
- 'ksdm' => $record['ksdm'],
- 'create_time' => $record['create_time'],
- 'update_time' => $record['update_time']
- ];
- }
- return $this->_json_succ($result);
- } catch (\Exception $e) {
- return $this->_json_error('获取失败: ' . $e->getMessage());
- }
- }
- /**
- * 保存患者辅助诊疗信息
- * @return Json
- */
- public function savePatientAuxiliaryInfoInterfece($params): Json
- {
- // 设置默认值
- $params = array_merge([
- 'ZYH' => '',
- 'ZSZZ' => '',
- 'name' => '',
- 'tag' => '',
- 'XB' => '',
- 'NL' => 0,
- 'document' => '',
- 'docter' => '',
- 'YGDM' => '',
- 'KSDM' => ''
- ], $params);
- // 参数验证
- $validate = validate([
- 'ZYH' => 'require|number', // 只验证住院号必填
- ]);
- if (!$validate->check($params)) {
- return $this->_json_error($validate->getError());
- }
- // 准备基础数据
- $baseData = [
- 'name' => $params['name'],
- 'tag' => $params['tag'],
- 'zszz' => $params['ZSZZ'],
- 'xb' => $params['XB'],
- 'nl' => intval($params['NL']),
- 'document' => $params['document'],
- 'docter' => $params['docter'],
- 'ygdm' => $params['YGDM'],
- 'ksdm' => $params['KSDM']
- ];
- try {
- // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
- if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
- try {
- // 从主诉症状中提取关键词
- $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
- // $dictionary = SymptomDictionary::field('keyword')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", keyword, "%")')->select()->toArray();
- $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
- // 如果提取到了症状关键词,才调用辅助诊疗
- if (!empty($mainSymptoms)) {
- $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
- $mainSymptoms, // 使用提取的症状关键词
- $params['NL'],
- stripos($params['XB'], '男') !== false ? 1 : 2
- );
- if (!empty($auxiliaryResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
- $data
- );
- }
- }
- } catch (\Exception $e) {
- return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
- }
- }
- // 2. 知识库 - 需要name和tag
- if ($params['name'] && $params['tag']) {
- try {
- // 调用detail方法
- $response = $this->cdss->detail([
- 'name' => $params['name'],
- 'tag' => $params['tag']
- ]);
- // 从Json响应中获取数据
- $knowledgeResult = $response->getData();
- if (!empty($knowledgeResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
- $data
- );
- }
- } catch (\Exception $e) {
- return $this->_json_error('知识库调用失败: ' . $e->getMessage());
- }
- }
- // 3. 医嘱审核 - 需要name和性别
- $hasReviewResult = false;
- if ($params['name'] && $params['XB']) {
- $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
- try {
- $reviewResult = $this->cdss->yzEamine([
- 'name' => $params['name'],
- 'gender' => $gender
- ]);
- if (!empty($reviewResult)) {
- $data = array_merge($baseData, [
- 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
- ]);
- PatientAuxiliaryDiagnosis::updateOrInsert(
- (string)$params['ZYH'],
- PatientAuxiliaryDiagnosis::TAG_REVIEW,
- $data
- );
- $hasReviewResult = true;
- }
- } catch (\Exception $e) {
- return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
- }
- }
- // 检查辅助诊疗结果
- $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
- // 检查知识库结果 - detail()返回的是Json对象
- $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
- // 返回结果 - 任何一个匹配就设置req=1
- return $this->_json_succ(
- ['req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0],
- 0,
- '保存成功'
- );
- } catch (\Exception $e) {
- return $this->_json_error('保存失败: ' . $e->getMessage());
- }
- }
- }
|