Exams.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\admin\service\doctor\DoctorService;
  4. use app\admin\service\qc\ResultService;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Exception;
  8. use think\Session;
  9. /**
  10. *
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Exams extends Backend
  15. {
  16. /**
  17. * Exams模型对象
  18. * @var \app\admin\model\manage\Exams
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['aiReport', 'lookReport','report','film','completetaskstatus','lookresult','reportselectlist','filmselectlist','analysis','lookAiResult'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\manage\Exams;
  26. }
  27. public function aiReport() {
  28. $ids = $_GET['exam'];
  29. $service = new DoctorService();
  30. $data = $service->getPrintData($ids);
  31. // halt(json_encode(['message'=>$data]));
  32. $res = $this->curl_post('http://172.30.254.2:5001',['message'=>$data]);
  33. $aiQuality = Db::table('quality_ai')->where('exam_id',$ids)->find();
  34. if(empty($aiQuality))
  35. {
  36. Db::table('quality_ai')->insert(['ai_quality'=>$res,'exam_id'=>$ids]);
  37. }else{
  38. Db::table('quality_ai')->where('id',$aiQuality['id'])->update(['ai_quality'=>$res]);
  39. }
  40. $this->success();
  41. }
  42. public function curl_post($url, $data)
  43. {
  44. $ch = curl_init ();
  45. curl_setopt ( $ch, CURLOPT_URL, $url );
  46. curl_setopt ( $ch, CURLOPT_POST, 1 );
  47. curl_setopt ( $ch, CURLOPT_HEADER, 0 );
  48. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  49. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  50. $return = curl_exec ( $ch );
  51. curl_close ( $ch );
  52. return $return;
  53. }
  54. /**
  55. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  56. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  57. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  58. */
  59. public function lookReport($ids)
  60. {
  61. $report = Db::table('report')->where('exam_id',$ids)->where('type',1)->find();
  62. $this->view->assign('row',$report);
  63. return $this->view->fetch();
  64. }
  65. public function report($ids)
  66. {
  67. // var_dump(makeNew16Uid());
  68. // var_dump($ids);
  69. if ($this->request->isPost()) {
  70. $params = $this->request->post("report");
  71. var_dump($params);die;
  72. }
  73. return $this->view->fetch();
  74. }
  75. public function film($type,$ids,$task=1,$tid='')
  76. {
  77. $ins = Db::table('exams')->where('id',$ids)->value('institution_id');
  78. $row = Db::table('qc_standards')->where('type',$type)
  79. ->where('institution_id',$ins)
  80. ->where('status',1)->find();
  81. $dimension = Db::table('qc_dimension')->where('standard_id',$row['id'])->where('status',1)->order('sort')->select();
  82. if(!empty($dimension))
  83. {
  84. foreach ($dimension as $k=>$v)
  85. {
  86. $dimension[$k]['option'] = Db::table('qc_options')->where('dimension_id',$v['id'])->column('id,title');
  87. $dimension[$k]['option_id'] = Db::table('qc_results')->where('dimension_id',$v['id'])->where('exam_id',$ids)->where('standard_id',$row['id'])->value('option_id');
  88. }
  89. if ($this->request->isPost()) {
  90. $params = $this->request->post();
  91. $arr = [
  92. 'qc_type'=>$type, //质控类型 1 技术质控 2诊断质控
  93. 'task_type'=>1, //任务类型 1全量质控 2任务质控
  94. 'standard_id'=>$params['standard'],
  95. 'exam_id'=>$ids
  96. ];
  97. if($task == 1)
  98. {
  99. Db::table('qc_results')->where('qc_type',$type)->where('exam_id',$ids)->where('task_type',$task)->delete();
  100. }elseif ($task == 2)
  101. {
  102. Db::table('qc_results')->where('qc_type',$type)->where('exam_id',$ids)->delete();
  103. }
  104. unset($params['standard']);
  105. $newArr = [];
  106. foreach ($params as $k=>$v)
  107. {
  108. $arr['dimension_id'] = $k;
  109. foreach ($dimension as $key=>$value)
  110. {
  111. if($value['id'] == $k)
  112. {
  113. $arr['dimension_name'] = $value['title'];
  114. $arr['weight'] = $value['weight'];
  115. }
  116. }
  117. $arr['option_id'] = $v;
  118. $option = Db::table('qc_options')->where('id',$v)->find();
  119. $arr['option_title'] = $option['title'];
  120. $arr['score'] = $option['score'];
  121. $newArr[] = $arr;
  122. }
  123. Db::table('qc_results')->insertAll($newArr);
  124. $resultService = new ResultService();
  125. $summaryResult = $resultService->calculateSummaryResults($newArr, $row['id']);
  126. $tUpdate = [];
  127. if($type == 1)
  128. {
  129. //1技术质控
  130. $tUpdate['study_score'] = $summaryResult['score'];
  131. $tUpdate['study_result'] = $summaryResult['title'];
  132. }elseif ($type == 2)
  133. {
  134. //2诊断质控
  135. $tUpdate['report_score'] = $summaryResult['score'];
  136. $tUpdate['report_result'] = $summaryResult['title'];
  137. }
  138. // 获取全量质控的任务数据
  139. $oldTask = Db::table('qc_task_exams')->where('exam_id',$ids)->where('task_type',1)->find();
  140. if(empty($oldTask))
  141. {
  142. $newTask = ['exam_id'=>$ids,'task_type'=>1,'type'=>3];
  143. $newId = Db::table('qc_task_exams')->insertGetId($newTask);
  144. $newTask['id'] = $newId;
  145. $oldTask = $newTask;
  146. }
  147. $oldTask = array_merge($oldTask,$tUpdate);
  148. Db::table('qc_task_exams')->where('exam_id',$ids)->where('task_type',1)->update($tUpdate);
  149. $this->completeTaskStatus($oldTask,['id'=>$oldTask['id']]);
  150. if($task == 2)
  151. {
  152. foreach ($newArr as $k=>$v)
  153. {
  154. $newArr[$k]['task_type'] = 2;
  155. }
  156. Db::table('qc_results')->insertAll($newArr);
  157. if(!empty($tUpdate))
  158. {
  159. if(!empty($tid))
  160. {
  161. Db::table('qc_task_exams')->where('id',$tid)->update($tUpdate);
  162. }
  163. }
  164. $taskData = Db::table('qc_task_exams')->where('id',$tid)->find();
  165. $this->completeTaskStatus($taskData,['id'=>$tid]);
  166. }
  167. $recordArr = [
  168. 'qc_type'=>$type, //质控类型 1 技术质控 2诊断质控
  169. 'task_type'=>$task, //任务类型 1全量质控 2任务质控
  170. 'exam_id'=>$ids,
  171. 'createdBy'=>Session::get('admin')['nickname'],
  172. 'results'=>json_encode($params),
  173. 'result_title'=>$summaryResult['title'],
  174. 'result_score'=>$summaryResult['score'],
  175. 'result_desc'=>$summaryResult['desc'],
  176. ];
  177. if($task == 2)
  178. {
  179. $recordArr['task_id'] = $taskData['task_id'];
  180. $qcNum = Db::table('qc_task_exams')->where('task_id',$taskData['task_id'])->where('complete',1)->count();
  181. Db::table('qc_tasks')->where('id',$recordArr['task_id'])->update(['qc_num'=>$qcNum]);
  182. }
  183. Db::table('qc_records')->insert($recordArr);
  184. $this->success('评价成功');
  185. }
  186. }
  187. $this->view->assign('dimension',$dimension);
  188. $this->view->assign('standard',$row['id']);
  189. return $this->view->fetch();
  190. }
  191. public function completeTaskStatus($taskData,$where)
  192. {
  193. $complete = 0;
  194. switch ($taskData['type'])
  195. {
  196. case 1:
  197. if(!empty($taskData['study_score']))
  198. {
  199. $complete = 1;
  200. }
  201. break;
  202. case 2:
  203. if(!empty($taskData['report_score']))
  204. {
  205. $complete = 1;
  206. }
  207. break;
  208. case 3:
  209. if(!empty($taskData['report_score']) && !empty($taskData['study_score']))
  210. {
  211. $complete = 1;
  212. }
  213. break;
  214. }
  215. if($complete == 1){Db::table('qc_task_exams')->where($where)->update(['complete'=>1]);}
  216. }
  217. public function lookResult($type, $ids,$task=1)
  218. {
  219. $resultService = new ResultService();
  220. $result = Db::table('qc_results')->where('qc_type', $type)->where('task_type', $task)->where('exam_id', $ids)->select();
  221. $newArr = [];
  222. $standard_id = false;
  223. // halt($result);
  224. foreach ($result as $k=>$v)
  225. {
  226. $arr['dimension'] = $v['dimension_name'];
  227. $arr['option'] = $v['option_title'];
  228. $arr['score'] = $v['score'];
  229. $arr['weight'] = $v['weight'];
  230. $standard_id = $v['standard_id'];
  231. $newArr[] = $arr;
  232. }
  233. $summaryResult = $resultService->calculateSummaryResults($newArr, $standard_id);
  234. // $weightedAverageScore = $resultService->calculateWeightedAverage($newArr);
  235. // $scoreDescObj = $resultService->getScoreDescObjFromAverageScore($weightedAverageScore, );
  236. $this->view->assign('data',$newArr);
  237. $this->view->assign('result',$summaryResult);
  238. // $this->view->assign('score',$weightedAverageScore);
  239. return $this->view->fetch();
  240. }
  241. public function lookAiResult($ids)
  242. {
  243. $resultService = new ResultService();
  244. $result = Db::table('quality_ai')->where('exam_id', $ids)->value('ai_quality');
  245. if(empty($result))
  246. {
  247. $this->error('没有生成ai质控');
  248. }
  249. $result = str_replace(['\n', '\r'], '', $result);
  250. $result = json_decode($result,true);
  251. // ["results"]=>array(6) { [0]=> string(1) "e" [1]=> string(1) "e" [2]=> string(1) "e" [3]=> string(1) "c" [4]=> string(1) "b" [5]=> string(1) "c"}
  252. // ["basis"]=>array(6) { [0]=> string(57) "缺失医院地址、科室名称、检查设备名称。" [1]=> string(24) "患者病案号缺失。" [2]=> string(21) "检查参数缺失。" [3]=> string(72) "征象描述基本完整,但存在少量错别字或不规范缩写。" [4]=> string(60) "诊断意见基本明确,但可能有少量语言冗余。" [5]=> string(80) "检查时间记录不完整,仅记录日期 `20250206`,未精确到分钟。"}
  253. // ["suggests"]=>array(6) { [0]=> string(63) "补充完整医院地址、科室名称、检查设备名称。" [1]=> string(24) "补充病案号信息。" [2]=> string(27) "补充检查参数信息。" [3]=> string(57) "优化征象描述,减少错别字和不规范缩写。" [4]=> string(63) "优化诊断意见,减少语言冗余,确保格式规范。" [5]=> string(61) "检查时间应精确到分钟(如 `2025-02-06 14:30`)。"}
  254. // ["summary"]=>string(216) "1. 补充缺失信息:医院地址、科室、设备名称、病案号、检查参数;2. 优化征象描述和诊断意见,减少错别字、不规范缩写和语言冗余;3. 检查时间应精确到分钟。"
  255. // halt($result);
  256. // $newArr = [];
  257. // $standard_id = false;
  258. // foreach ($result as $k=>$v)
  259. // {
  260. // $arr['dimension'] = $v['dimension_name'];
  261. // $arr['option'] = $v['option_title'];
  262. // $arr['score'] = $v['score'];
  263. // $arr['weight'] = $v['weight'];
  264. // $standard_id = $v['standard_id'];
  265. // $newArr[] = $arr;
  266. // }
  267. $standard_id = $resultService->getStandardIdByExamId($ids);
  268. $res = $resultService->getAiResultsDataByStandardId($result, $standard_id);
  269. $newArr = $res[0];
  270. $summary = $res[1];
  271. $summaryResult = $resultService->calculateSummaryResults($newArr, $standard_id);
  272. // $weightedAverageScore = $resultService->calculateWeightedAverage($newArr);
  273. // $scoreDescObj = $resultService->getScoreDescObjFromAverageScore($weightedAverageScore, );
  274. $this->view->assign('data',$newArr);
  275. $this->view->assign('result',$summaryResult);
  276. $this->view->assign('summary',$summary);
  277. // $this->view->assign('score',$weightedAverageScore);
  278. return $this->view->fetch();
  279. }
  280. public function reportSelectList()
  281. {
  282. try{
  283. // 获取列表
  284. $total = Db::table('quality_factor')
  285. ->where('type',2)
  286. ->count();
  287. $list = Db::table('quality_factor')
  288. ->where('type',2)
  289. ->field('id,description as name')
  290. ->select();
  291. // 格式化
  292. return json(array("total" => $total, "rows" => $list));
  293. } catch ( Exception $exception){
  294. $this->error($exception->getMessage());
  295. }
  296. }
  297. public function filmSelectList()
  298. {
  299. try{
  300. // 获取列表
  301. $total = Db::table('quality_factor')
  302. ->where('type',1)
  303. ->count();
  304. $list = Db::table('quality_factor')
  305. ->where('type',1)
  306. ->field('id,description as name')
  307. ->select();
  308. // 格式化
  309. return json(array("total" => $total, "rows" => $list));
  310. } catch ( Exception $exception){
  311. $this->error($exception->getMessage());
  312. }
  313. }
  314. public function analysis($ids)
  315. {
  316. // ids 是exam_id
  317. $resultService = new ResultService();
  318. $newArr = $resultService->analysis($ids);
  319. $this->view->assign('data',$newArr);
  320. return $this->view->fetch();
  321. }
  322. }