XyCdssService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\service;
  4. use app\controller\Cdss;
  5. use app\controller\PatientAuxiliary;
  6. use app\model\CdssDisease;
  7. use app\model\CdssInspection;
  8. use app\model\CdssXyDisease;
  9. use app\model\DiseasesKeyword;
  10. use app\model\KnowledgeRule;
  11. use app\model\Symptom;
  12. use app\model\SymptomDictionary;
  13. use think\helper\Arr;
  14. require_once app()->getRootPath() . 'vendor/autoload.php';
  15. class XyCdssService
  16. {
  17. private static $instance;
  18. public static function getInstance(){
  19. if(!self::$instance instanceof self){
  20. self::$instance = new self();
  21. }
  22. return self::$instance;
  23. }
  24. public function getResult($params){
  25. $chief = Arr::get($params,'data.chief',''); //主诉
  26. $presentHistory = Arr::get($params,'data.presentHistory',''); //现病史
  27. $patientBirthday = Arr::get($params,'patientMes.patientBirthday',''); //出生年月
  28. $sex = Arr::get($params,'patientMes.patientGender','');
  29. $age = Arr::get($params,'patientMes.patientAge');
  30. // if($sex == '男性'){
  31. // $sex = 1;
  32. // }elseif($sex == '女性'){
  33. // $sex = 2;
  34. // }else{
  35. // $sex = 0;
  36. // }
  37. $age = $age ?: ($patientBirthday ? date("Y") - explode('-',$patientBirthday)[0] : 0) ;
  38. $result = [];
  39. // if($chief){
  40. // $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$chief."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
  41. // $chief = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
  42. // }
  43. $name = '';
  44. if(isset($params['lisAdvices']) || isset($params['pacsAdvices'])){
  45. $name = Arr::get($params,'lisAdvices.0.itemName','') ?: Arr::get($params,'pacsAdvices.0.itemName','');
  46. }if(isset($params['lisCol']) || isset($params['pacsCol'])){
  47. $name = Arr::get($params,'lisCol.0.lisName','') ?: Arr::get($params,'pacsCol.0.examName','');
  48. }
  49. return (new PatientAuxiliary(app()))->savePatientAuxiliaryInfoInterfece([
  50. 'ZYH' => Arr::get($params,'patientMes.treatmentCode',''),
  51. 'ZSZZ' => $chief,
  52. 'name' => $name,
  53. 'tag' => Arr::get($params,'patientMes.patientType','Inspection'),
  54. 'XB' => $sex,
  55. 'NL' => $age,
  56. ]);
  57. /**
  58. * 废弃 ——————————————————————————————————————————————————————————————————————————————————————————
  59. */
  60. /**
  61. * 疑似诊断
  62. */
  63. if($chief){
  64. $res = $this->getCdssXyDisease($chief);
  65. if(empty($res)) $res = $this->getSymptom($chief);
  66. $this->getSexFilter($res,$sex);//根据性别筛选
  67. $res = $this->getAgeFilter($res,$age); //根据年龄筛选
  68. $this->getAboutDiseasesByKeywordCount($res,$chief);
  69. foreach ($res as $k => $v){
  70. //获取鉴别诊断
  71. $res[$k]['antidiastole'] = CdssDisease::where('name' , $v['name'])->value('antidiastole');
  72. //获取检查检验
  73. $disease = CdssXyDisease::where('name',$v['name'])->find();
  74. $inspection = $disease->inspection;
  75. $inspectionList = explode(',', $inspection);
  76. foreach ($inspectionList as $key => $value) {
  77. $res[$k]['inspection_list'][$key]['name'] = $value;
  78. }
  79. //获取治疗方案
  80. $res[$k]['treatment'] = $disease->inspection;
  81. }
  82. $result['suspectedDiagnosis'] = $res;
  83. }
  84. /**
  85. * 鉴别诊断、推荐检查检验、治疗方案
  86. */
  87. if(isset($params['diagnoses'])){
  88. $result['diagnoses'] = $this->diagnoses(Arr::get($params,'diagnoses',[]));
  89. }
  90. /**
  91. * 检查检验合理性判断、
  92. */
  93. if(isset($params['lisAdvices']) || isset($params['pacsAdvices'])){
  94. $result['RationalityCheck'] = $this->pacsLisRationalityCheck(Arr::get($params,'lisAdvices',[]),Arr::get($params,'pacsAdvices',[]),$sex);
  95. }
  96. /**
  97. * 检查检验报告解读、检验检查异常值
  98. */
  99. if(isset($params['lisCol']) || isset($params['pacsCol'])){
  100. $result['ReportInterpretation'] = $this->pacsLisReportInterpretation(Arr::get($params,'lisCol',[]),Arr::get($params,'pacsCol',[]));
  101. }
  102. return $result;
  103. }
  104. /**
  105. * 获取鉴别诊断、推荐检查检验、推荐评估表、治疗方案
  106. * @param $aboutDiseases
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. private function diagnoses($aboutDiseases){
  112. foreach ($aboutDiseases as $k => $v){
  113. //获取鉴别诊断
  114. $cdssDisease = CdssDisease::where('name' , $v['name'])->value('antidiastole');
  115. if($cdssDisease){
  116. $aboutDiseases[$k]['antidiastole'] = $cdssDisease;
  117. }else{
  118. $aboutDiseases[$k]['antidiastole'] = '';
  119. }
  120. //获取检查检验
  121. $disease = CdssXyDisease::where('name',$v['name'])->find();
  122. if($disease){
  123. $inspection = $disease->inspection;
  124. $inspectionList = explode(',', $inspection);
  125. foreach ($inspectionList as $key => $value) {
  126. $aboutDiseases[$k]['inspection_list'][$key]['name'] = $value;
  127. }
  128. //获取治疗方案
  129. $aboutDiseases[$k]['treatment'] = $disease->inspection;
  130. }else{
  131. $aboutDiseases[$k]['inspection_list'] = [];
  132. $aboutDiseases[$k]['treatment'] = '';
  133. }
  134. }
  135. return $aboutDiseases;
  136. }
  137. private function pacsLisRationalityCheck($lisAdvices,$pacsAdvices,$sex){
  138. $res['lis_rationality_check'] = $res['pace_rationality_check'] = [];
  139. foreach ($lisAdvices as $val){
  140. $lis_rationality_check = KnowledgeRule::field('name,rule_level,rule_tip')->whereLike('name',"%{$val['itemName']}%")->where('gender',$sex)->find();
  141. if($lis_rationality_check) $res['lis_rationality_check'][] = $lis_rationality_check;
  142. }
  143. foreach ($pacsAdvices as $val){
  144. $pacs_rationality_check = KnowledgeRule::field('name,rule_level,rule_tip')->whereLike('name',"%{$val['itemName']}%")->where('gender',$sex)->find();
  145. if($pacs_rationality_check) $res['pace_rationality_check'][] = $pacs_rationality_check;
  146. }
  147. return $res;
  148. }
  149. private function pacsLisReportInterpretation($lisCol,$pacsCol){
  150. $res['lis_report_interpretation'] = $res['pacs_report_interpretation'] = [];
  151. $field = 'name,overview,principle,reagent,operation,clinicalSignificance,normalValue,annotation,precautions,donghua_name,zhonglian_name,process,related_symptoms,related_diseases,haoze_name,sex';
  152. foreach ($lisCol as $key => $val){
  153. $res['lis_report_interpretation'][$key]['name'] = $val['lisName'];
  154. foreach ($val['lisItems'] as $v) {
  155. $lis_report_interpretation = CdssInspection::field($field)->whereLike('name', "%{$v['itemName']}%")->find();
  156. if ($lis_report_interpretation) {
  157. $res['lis_report_interpretation'][$key]['list'][] = $lis_report_interpretation;
  158. }
  159. }
  160. }
  161. foreach ($pacsCol as $v){
  162. $pacs_report_interpretation = CdssInspection::field($field)->whereLike('name',"%{$v['examName']}%")->find();
  163. if($pacs_report_interpretation){
  164. $res['pacs_report_interpretation'][] = $pacs_report_interpretation;
  165. }
  166. }
  167. return $res;
  168. }
  169. /**
  170. * 疑似诊断
  171. * @param $values
  172. * @return array
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. private function getCdssXyDisease($values){
  178. /**
  179. * 从疾病的临床表现属性匹配症状
  180. * 单个或多条件匹配
  181. */
  182. $query = CdssXyDisease::field('name,pathogenesis,clinicalFeature')->where('1=1');
  183. if (stripos($values, ',') !== false) {
  184. $symptoms = explode(',', $values);
  185. // 使用AND连接多个症状条件
  186. foreach ($symptoms as $symptom) {
  187. $query = $query->where('clinicalFeature', 'like', '%' . $symptom . '%');
  188. }
  189. } else {
  190. $query->whereLike('clinicalFeature', '%' . $values . '%');
  191. }
  192. $aboutDiseases = $query->limit(200)->select()->toArray();
  193. return $aboutDiseases;
  194. }
  195. private function getSymptom($values){
  196. $aboutDiseases = [];
  197. if (stripos($values, ',') !== false) {
  198. $symptoms = explode(',', $values);
  199. foreach ($symptoms as $key => $symptom) {
  200. $diseases[] = Symptom::field('disease')->where('name', 'like', $symptom . '%')->select()->toArray();
  201. }
  202. } else {
  203. $diseases = Symptom::field('disease')->whereLike('name', $values . '%')->select()->toArray();
  204. }
  205. if (empty($diseases)) return [];
  206. $diseases = array_column($diseases, 'disease');
  207. foreach ($diseases as $k => $disease) {
  208. $diseases[$k] = str_replace('||', ',', $disease);
  209. }
  210. $diseases = implode(',', $diseases);
  211. $diseasesArr = explode(',', $diseases);
  212. foreach ($diseasesArr as $key => $val){
  213. $aboutDiseases[$key]['name'] = $diseasesArr[$val];
  214. $aboutDiseases[$key]['pathogenesis'] = '';
  215. $aboutDiseases[$key]['clinicalFeature'] = '';
  216. }
  217. return $aboutDiseases ?? [];
  218. }
  219. public function getSexFilter(&$aboutDiseases,$sex){
  220. switch ($sex){
  221. case 2:
  222. $tag = 4;
  223. break;
  224. case 1:
  225. default:
  226. $tag = 3;
  227. break;
  228. }
  229. $sexDiseasesKeywords = DiseasesKeyword::where('tag', $tag)->value('keyword');
  230. if ($sexDiseasesKeywords) {
  231. $sexDiseasesKeywords = explode(',', $sexDiseasesKeywords);
  232. foreach ($aboutDiseases as $k => $v) {
  233. foreach ($sexDiseasesKeywords as $sexDiseasesKeyword) {
  234. if (stripos($v['name'], $sexDiseasesKeyword) !== false) {
  235. unset($aboutDiseases[$k]);
  236. }
  237. }
  238. }
  239. }
  240. }
  241. public function getAgeFilter($aboutDiseases,$age){
  242. $age = (int) $age;
  243. $childDiseasesKeyword = DiseasesKeyword::where('tag', DiseasesKeyword::CHILDTAG)->find();
  244. $oldDiseasesKeyword = DiseasesKeyword::where('tag', DiseasesKeyword::OLDTAG)->find();
  245. $childAboutDiseasesKeyword = $childDiseasesKeyword['keyword'];
  246. $oldAboutDiseasesKeyword = $oldDiseasesKeyword['keyword'];
  247. $ageAboutDiseases = [];
  248. foreach ($aboutDiseases as $k => $v) {
  249. if($age > 14 && $age < 60){
  250. $merge = array_merge($childAboutDiseasesKeyword, $oldAboutDiseasesKeyword);
  251. for ($i = 0; $i < count($merge); $i++) {
  252. if (stripos($v['name'], $merge[$i]) !== false) {
  253. unset($aboutDiseases[$k]);
  254. }
  255. }
  256. $ageAboutDiseases = array_values($aboutDiseases);
  257. }else{
  258. $diseasesKeyword = ($age < 60) ? $childAboutDiseasesKeyword : $oldAboutDiseasesKeyword;
  259. for ($i = 0; $i < count($diseasesKeyword); $i++) {
  260. if (stripos($v['name'], $diseasesKeyword[$i]) !== false) {
  261. $ageAboutDiseases[] = $v;
  262. }
  263. }
  264. }
  265. }
  266. return $ageAboutDiseases;
  267. }
  268. private function getAboutDiseasesByKeywordCount(&$aboutDiseases, $keyword)
  269. {
  270. if (stripos($keyword, ',') !== false) {
  271. foreach ($aboutDiseases as $key => $value) {
  272. $aboutDiseases[$key]['keyword_count'] = $this->getKeywordTotalCount($value['clinicalFeature'], $keyword);
  273. }
  274. } else {
  275. foreach ($aboutDiseases as $key => $value) {
  276. $aboutDiseases[$key]['keyword_count'] = substr_count($value['clinicalFeature'], $keyword);
  277. }
  278. }
  279. if (count($aboutDiseases) > 1) {
  280. $keyword_count = array_column($aboutDiseases, 'keyword_count');
  281. array_multisort($keyword_count, SORT_DESC, $aboutDiseases);
  282. }
  283. }
  284. private function getKeywordTotalCount($clinicalFeature, $keyword): int
  285. {
  286. $keywordArray = array_values(array_filter(explode(',', $keyword)));
  287. $keywordArrayCount = count($keywordArray);
  288. $totalCount = 0;
  289. for ($i = 0; $i < $keywordArrayCount; $i++) {
  290. if ($clinicalFeature) {
  291. $totalCount += substr_count($clinicalFeature, $keywordArray[$i]);
  292. }
  293. }
  294. return $totalCount;
  295. }
  296. }