123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- namespace app\admin\service\qc;
- use DateTime;
- use think\Db;
- class ResultService
- {
- protected $model = null;
- public function _initialize()
- {
- }
-
- public function getStandardIdByExamId($examId) {
- $institution_id = Db::table('exams')->where('id', $examId)->value('institution_id');
- $standardId = Db::table('qc_standards')->where('institution_id', $institution_id)->where('type', 2)->where('status', 1)->value('id');
- return $standardId;
- }
- public function getAiResultsDataByStandardId($result, $standard_id) {
- $results = $result['results'];
- $basis = $result['basis'];
- $suggests = $result['suggests'];
- $summary = $result['summary'];
- $data = [];
- $dimensions = Db::table('qc_dimension')->where('standard_id', $standard_id)->order('sort', 'asc')->select();
- foreach($dimensions as $key => $dimension) {
- $optionInd = ['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4][$results[$key]];
- $options = Db::table('qc_options')->where('dimension_id', $dimension['id'])->order('sort', 'asc')->select();
- // halt($options);
- $data[] = [
- 'dimension' => $dimension['title'],
- 'option' => $options[$optionInd]['title'],
- 'score' => $options[$optionInd]['score'],
- 'weight' => $dimension['weight'],
- 'basis' => $basis[$key],
- 'suggests' => $suggests[$key],
- 'standard_id' => $standard_id,
- ];
- }
- return [$data, $summary];
- }
- public function analysis($exam_id) {
- $exam = Db::table('exams')->where('id', $exam_id)->find();
- $report = Db::table('report')->where('exam_id', $exam_id)->find();
- $study = Db::table('studies')->where('id', $exam['study_id'])->find();
- $studyDay = $exam['exam_datetime'];
- $studyTime = $study['studytime'];
- $studyDate = $this->getStudyDate($studyDay, $studyTime);
- // 判断报告医生和审核医生是否同一人;
- // 判断检查时间和审核时间是否超过2个小时;
- // 判断是否存在身份证号、手机号、检查日期、报告日期、审核日期为空,
- $doctor = $this->analysisDoctor($report['report_doctor_id'], $report['review_doctor_id'], $report['report_doctor_name'], $report['review_doctor_name']);
- $time = $this->analysisTime($studyDate, $report['review_datetime']);
- $idcrad = $this->analysisEmptyIdcrad($exam['card_num']);
- $phone = $this->analysisEmptyPhone($exam['phone']);
- $studyDate = $this->analysisEmptyStudyDate($studyDate);
- $reportDate = $this->analysisEmptyReportDate($report['report_datetime']);
- $reviewDate = $this->analysisEmptyReviewDate($report['review_datetime']);
-
- $arr = [
- $doctor,
- $time,
- $idcrad,
- $phone,
- $studyDate,
- $reportDate,
- $reviewDate,
- ];
- return $arr;
- }
- public function analysisDoctor($report_doctor_id, $review_doctor_id, $report_doctor_name, $review_doctor_name) {
- $data = [
- 'title' => '报告医生和审核医生是否同一人',
- 'style' => 'success'
- ];
- if($report_doctor_id === $review_doctor_id) {
- $data['style'] = 'error';
- $data['desc'] = '是(报告和审核医生都是'.$report_doctor_name.')';
- return $data;
- }
- $data['desc'] = '否(报告医生是'.$report_doctor_name.'审核医生是'.$review_doctor_name.')';
- return $data;
- }
- public function analysisTime($studyDate, $reviewDate, $dhours = 2) {
- $data = [
- 'title' => '检查时间和审核时间是否超过'.$dhours.'个小时',
- 'style' => 'success'
- ];
- $studyDateTime = new DateTime($studyDate);
- $reviewDateTime = new DateTime($reviewDate);
- $interval = $studyDateTime->diff($reviewDateTime);
- // 获取差异的小时数
- $hoursDiff = $interval->h + ($interval->days * 24);
- if($hoursDiff > $dhours) {
- $data['style'] = 'error';
- $data['desc'] = '是(检查时间和审核时间'.$hoursDiff.'个小时'.')';
- return $data;
- }
- $data['desc'] = '否(检查时间和审核时间'.$hoursDiff.'个小时'.')';
- return $data;;
- }
- public function analysisEmptyIdcrad($idcrad) {
- $data = [
- 'title' => '是否存在身份证号',
- 'style' => 'success',
- 'desc' => '是'
- ];
- if(empty($idcrad)) {
- $data['style'] = 'error';
- $data['desc'] = '否';
- return $data;
- }
- return $data;
- }
- public function analysisEmptyPhone($phone) {
- $data = [
- 'title' => '是否存在手机号',
- 'style' => 'success',
- 'desc' => '是'
- ];
- if(empty($phone)) {
- $data['style'] = 'error';
- $data['desc'] = '否';
- return $data;
- }
- return $data;
- }
- public function analysisEmptyStudyDate($studyDate) {
- $data = [
- 'title' => '是否存在检查日期',
- 'style' => 'success',
- 'desc' => '是'
- ];
- if(empty($studyDate)) {
- $data['style'] = 'error';
- $data['desc'] = '否';
- return $data;
- }
- return $data;
- }
- public function analysisEmptyReportDate($reportDate) {
- $data = [
- 'title' => '是否存在报告日期',
- 'style' => 'success',
- 'desc' => '是'
- ];
- if(empty($reportDate)) {
- $data['style'] = 'error';
- $data['desc'] = '否';
- return $data;
- }
- return $data;
- }
- public function analysisEmptyReviewDate($reviewDate) {
- $data = [
- 'title' => '是否存在审核日期',
- 'style' => 'success',
- 'desc' => '是'
- ];
- if(empty($reviewDate)) {
- $data['style'] = 'error';
- $data['desc'] = '否';
- return $data;
- }
- return $data;
- }
- public function getStudyDate($studyDay, $studyTime) {
- if(!$studyDay) {
- return null;
- }
- if(!$studyTime) {
- return null;
- }
- $studyTime = explode('.',$studyTime)[0];
- $formattedDateTime = date('Y-m-d H:i:s', strtotime($studyDay . ' ' . $studyTime));
- return $formattedDateTime;
- }
- public function calculateSummaryResults($data, $standard_id) {
- $score = $this->calculateWeightedAverage($data);
- $desc = $this->calculateResultByScore($score, $standard_id);
- return [
- 'score' => $score,
- 'title' => $desc['title'] ?? '',
- 'desc' => $desc['desc'] ?? ''
- ];
- }
- public function calculateResultByScore($score, $standard_id) {
- $scoreDescriptions = $this->getScoreDescriptionsByStandardId($standard_id);
- foreach($scoreDescriptions as $scoreDescription) {
- $start_score = $scoreDescription['start_score'];
- $finish_score = $scoreDescription['finish_score'];
- if($score >= $start_score && $score <= $finish_score) {
- return $scoreDescription;
- }
- }
- }
- public function getScoreDescriptionsByStandardId($standard_id) {
- // todo 缓存
- $scoreDescriptions = Db::table('qc_score_description')->where('standard_id', $standard_id)->order('sort', 'desc')->select();
- return $scoreDescriptions;
- }
- public function calculateWeightedAverage($data) {
- $totalScore = 0; // 用于累加分数与权重的乘积
- $totalWeight = 0; // 用于累加权重总和
-
- foreach ($data as $v) {
- $totalScore += $v['score'] * $v['weight']; // 累加分数与权重的乘积
- $totalWeight += $v['weight']; // 累加权重
- }
-
- if ($totalWeight == 0) {
- return 0; // 如果权重总和为0,避免除以0的错误
- }
-
- return number_format($totalScore / $totalWeight, 2, '.', ''); // 保留两位小数
- }
- }
|