ResultService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\service\qc;
  3. use DateTime;
  4. use think\Db;
  5. class ResultService
  6. {
  7. protected $model = null;
  8. public function _initialize()
  9. {
  10. }
  11. public function getStandardIdByExamId($examId) {
  12. $institution_id = Db::table('exams')->where('id', $examId)->value('institution_id');
  13. $standardId = Db::table('qc_standards')->where('institution_id', $institution_id)->where('type', 2)->where('status', 1)->value('id');
  14. return $standardId;
  15. }
  16. public function getAiResultsDataByStandardId($result, $standard_id) {
  17. $results = $result['results'];
  18. $basis = $result['basis'];
  19. $suggests = $result['suggests'];
  20. $summary = $result['summary'];
  21. $data = [];
  22. $dimensions = Db::table('qc_dimension')->where('standard_id', $standard_id)->order('sort', 'asc')->select();
  23. foreach($dimensions as $key => $dimension) {
  24. $optionInd = ['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4][$results[$key]];
  25. $options = Db::table('qc_options')->where('dimension_id', $dimension['id'])->order('sort', 'asc')->select();
  26. // halt($options);
  27. $data[] = [
  28. 'dimension' => $dimension['title'],
  29. 'option' => $options[$optionInd]['title'],
  30. 'score' => $options[$optionInd]['score'],
  31. 'weight' => $dimension['weight'],
  32. 'basis' => $basis[$key],
  33. 'suggests' => $suggests[$key],
  34. 'standard_id' => $standard_id,
  35. ];
  36. }
  37. return [$data, $summary];
  38. }
  39. public function analysis($exam_id) {
  40. $exam = Db::table('exams')->where('id', $exam_id)->find();
  41. $report = Db::table('report')->where('exam_id', $exam_id)->find();
  42. $study = Db::table('studies')->where('id', $exam['study_id'])->find();
  43. $studyDay = $exam['exam_datetime'];
  44. $studyTime = $study['studytime'];
  45. $studyDate = $this->getStudyDate($studyDay, $studyTime);
  46. // 判断报告医生和审核医生是否同一人;
  47. // 判断检查时间和审核时间是否超过2个小时;
  48. // 判断是否存在身份证号、手机号、检查日期、报告日期、审核日期为空,
  49. $doctor = $this->analysisDoctor($report['report_doctor_id'], $report['review_doctor_id'], $report['report_doctor_name'], $report['review_doctor_name']);
  50. $time = $this->analysisTime($studyDate, $report['review_datetime']);
  51. $idcrad = $this->analysisEmptyIdcrad($exam['card_num']);
  52. $phone = $this->analysisEmptyPhone($exam['phone']);
  53. $studyDate = $this->analysisEmptyStudyDate($studyDate);
  54. $reportDate = $this->analysisEmptyReportDate($report['report_datetime']);
  55. $reviewDate = $this->analysisEmptyReviewDate($report['review_datetime']);
  56. $arr = [
  57. $doctor,
  58. $time,
  59. $idcrad,
  60. $phone,
  61. $studyDate,
  62. $reportDate,
  63. $reviewDate,
  64. ];
  65. return $arr;
  66. }
  67. public function analysisDoctor($report_doctor_id, $review_doctor_id, $report_doctor_name, $review_doctor_name) {
  68. $data = [
  69. 'title' => '报告医生和审核医生是否同一人',
  70. 'style' => 'success'
  71. ];
  72. if($report_doctor_id === $review_doctor_id) {
  73. $data['style'] = 'error';
  74. $data['desc'] = '是(报告和审核医生都是'.$report_doctor_name.')';
  75. return $data;
  76. }
  77. $data['desc'] = '否(报告医生是'.$report_doctor_name.'审核医生是'.$review_doctor_name.')';
  78. return $data;
  79. }
  80. public function analysisTime($studyDate, $reviewDate, $dhours = 2) {
  81. $data = [
  82. 'title' => '检查时间和审核时间是否超过'.$dhours.'个小时',
  83. 'style' => 'success'
  84. ];
  85. $studyDateTime = new DateTime($studyDate);
  86. $reviewDateTime = new DateTime($reviewDate);
  87. $interval = $studyDateTime->diff($reviewDateTime);
  88. // 获取差异的小时数
  89. $hoursDiff = $interval->h + ($interval->days * 24);
  90. if($hoursDiff > $dhours) {
  91. $data['style'] = 'error';
  92. $data['desc'] = '是(检查时间和审核时间'.$hoursDiff.'个小时'.')';
  93. return $data;
  94. }
  95. $data['desc'] = '否(检查时间和审核时间'.$hoursDiff.'个小时'.')';
  96. return $data;;
  97. }
  98. public function analysisEmptyIdcrad($idcrad) {
  99. $data = [
  100. 'title' => '是否存在身份证号',
  101. 'style' => 'success',
  102. 'desc' => '是'
  103. ];
  104. if(empty($idcrad)) {
  105. $data['style'] = 'error';
  106. $data['desc'] = '否';
  107. return $data;
  108. }
  109. return $data;
  110. }
  111. public function analysisEmptyPhone($phone) {
  112. $data = [
  113. 'title' => '是否存在手机号',
  114. 'style' => 'success',
  115. 'desc' => '是'
  116. ];
  117. if(empty($phone)) {
  118. $data['style'] = 'error';
  119. $data['desc'] = '否';
  120. return $data;
  121. }
  122. return $data;
  123. }
  124. public function analysisEmptyStudyDate($studyDate) {
  125. $data = [
  126. 'title' => '是否存在检查日期',
  127. 'style' => 'success',
  128. 'desc' => '是'
  129. ];
  130. if(empty($studyDate)) {
  131. $data['style'] = 'error';
  132. $data['desc'] = '否';
  133. return $data;
  134. }
  135. return $data;
  136. }
  137. public function analysisEmptyReportDate($reportDate) {
  138. $data = [
  139. 'title' => '是否存在报告日期',
  140. 'style' => 'success',
  141. 'desc' => '是'
  142. ];
  143. if(empty($reportDate)) {
  144. $data['style'] = 'error';
  145. $data['desc'] = '否';
  146. return $data;
  147. }
  148. return $data;
  149. }
  150. public function analysisEmptyReviewDate($reviewDate) {
  151. $data = [
  152. 'title' => '是否存在审核日期',
  153. 'style' => 'success',
  154. 'desc' => '是'
  155. ];
  156. if(empty($reviewDate)) {
  157. $data['style'] = 'error';
  158. $data['desc'] = '否';
  159. return $data;
  160. }
  161. return $data;
  162. }
  163. public function getStudyDate($studyDay, $studyTime) {
  164. if(!$studyDay) {
  165. return null;
  166. }
  167. if(!$studyTime) {
  168. return null;
  169. }
  170. $studyTime = explode('.',$studyTime)[0];
  171. $formattedDateTime = date('Y-m-d H:i:s', strtotime($studyDay . ' ' . $studyTime));
  172. return $formattedDateTime;
  173. }
  174. public function calculateSummaryResults($data, $standard_id) {
  175. $score = $this->calculateWeightedAverage($data);
  176. $desc = $this->calculateResultByScore($score, $standard_id);
  177. return [
  178. 'score' => $score,
  179. 'title' => $desc['title'] ?? '',
  180. 'desc' => $desc['desc'] ?? ''
  181. ];
  182. }
  183. public function calculateResultByScore($score, $standard_id) {
  184. $scoreDescriptions = $this->getScoreDescriptionsByStandardId($standard_id);
  185. foreach($scoreDescriptions as $scoreDescription) {
  186. $start_score = $scoreDescription['start_score'];
  187. $finish_score = $scoreDescription['finish_score'];
  188. if($score >= $start_score && $score <= $finish_score) {
  189. return $scoreDescription;
  190. }
  191. }
  192. }
  193. public function getScoreDescriptionsByStandardId($standard_id) {
  194. // todo 缓存
  195. $scoreDescriptions = Db::table('qc_score_description')->where('standard_id', $standard_id)->order('sort', 'desc')->select();
  196. return $scoreDescriptions;
  197. }
  198. public function calculateWeightedAverage($data) {
  199. $totalScore = 0; // 用于累加分数与权重的乘积
  200. $totalWeight = 0; // 用于累加权重总和
  201. foreach ($data as $v) {
  202. $totalScore += $v['score'] * $v['weight']; // 累加分数与权重的乘积
  203. $totalWeight += $v['weight']; // 累加权重
  204. }
  205. if ($totalWeight == 0) {
  206. return 0; // 如果权重总和为0,避免除以0的错误
  207. }
  208. return number_format($totalScore / $totalWeight, 2, '.', ''); // 保留两位小数
  209. }
  210. }