LinkService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\api\servies\link;
  3. use app\api\dao\link\LinkDao;
  4. use app\api\servies\ZskkDefaultService;
  5. use think\facade\Config;
  6. use think\facade\Log;
  7. use think\Db;
  8. use think\db\exception;
  9. /**
  10. * 后台控制器基类
  11. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  12. */
  13. class LinkService extends ZskkDefaultService {
  14. protected $logName = "LinkService";
  15. private $linkDao = null;
  16. public function __construct(LinkDao $linkDao) {
  17. parent::__construct();
  18. $this->linkDao = $linkDao;
  19. }
  20. public function getPacsInstitutionExam()
  21. {
  22. $institution = $this->linkDao->getAllIns();
  23. $allIns = [];
  24. foreach ($institution as $k=>$v)
  25. {
  26. $allIns[$v['id']] = $v['name'];
  27. }
  28. $exam = $this->linkDao->getinsExam();
  29. rsort($exam);
  30. $arr = [];
  31. foreach ($exam as $k=>$v)
  32. {
  33. if(empty($allIns[$v['institution_id']] ?? ''))
  34. {
  35. continue;
  36. }
  37. $arr[] = ['name'=>$allIns[$v['institution_id']] ,'num'=> $v['num']];
  38. }
  39. return $arr;
  40. }
  41. public function getPacsAgeExam()
  42. {
  43. $data = $this->linkDao->getPacsAgeExam();
  44. $arr = $this->statisticsAge($data);
  45. return $arr;
  46. }
  47. public function getPacsDateExam()
  48. {
  49. $data = $this->linkDao->getPacsDateExam();
  50. return $data;
  51. }
  52. /**
  53. * 统计年龄分布
  54. */
  55. function statisticsAge($data) {
  56. $ageStats = [];
  57. foreach ($data as $row) {
  58. $age = $this->normalizeAge($row['age']);
  59. $count = (int)$row['row_cnt'];
  60. // 跳过无效年龄
  61. if ($age === null) {
  62. continue;
  63. }
  64. // 累加相同年龄的数量
  65. if (!isset($ageStats[$age])) {
  66. $ageStats[$age] = 0;
  67. }
  68. $ageStats[$age] += $count;
  69. }
  70. // 转换为所需格式并按年龄排序
  71. $result = [];
  72. ksort($ageStats);
  73. foreach ($ageStats as $age => $count) {
  74. $result[] = [
  75. 'age' => $age,
  76. 'count' => $count
  77. ];
  78. }
  79. return $result;
  80. }
  81. function normalizeAge($ageStr) {
  82. if (empty($ageStr) || $ageStr === 'NULL' || $ageStr === '不详') {
  83. return null;
  84. }
  85. $ageStr = trim($ageStr);
  86. // 处理 Y 结尾的年龄(如 27Y, 83Y)
  87. if (preg_match('/^(\d+)Y$/i', $ageStr, $matches)) {
  88. return (int)$matches[1];
  89. }
  90. // 处理"岁"结尾的年龄(如 31岁, 72岁)
  91. if (preg_match('/^(\d+)岁/', $ageStr, $matches)) {
  92. return (int)$matches[1];
  93. }
  94. // 处理小数格式(如 74.00, 64.00)
  95. if (preg_match('/^(\d+)\.?\d*$/', $ageStr, $matches)) {
  96. return (int)$matches[1];
  97. }
  98. // 处理纯数字(如 48, 179)
  99. if (is_numeric($ageStr)) {
  100. return (int)$ageStr;
  101. }
  102. // 处理月份(如 3月28天, 10M)- 转换为 0 岁
  103. if (preg_match('/月|M$/i', $ageStr)) {
  104. return 0;
  105. }
  106. // 处理天数或小时(如 19天, 0小时19分钟)- 转换为 0 岁
  107. if (preg_match('/天|小时|分钟|D$/i', $ageStr)) {
  108. return 0;
  109. }
  110. // 其他无法识别的格式返回 null
  111. return null;
  112. }
  113. public function getPacsCount()
  114. {
  115. $info = Db::query("SELECT TRUNC(createdAt,'MM') AS month_begin,COUNT(*) AS cnt FROM pacs.query_study_logs GROUP BY TRUNC(createdAt,'MM') ORDER BY month_begin");
  116. $all = 0;
  117. foreach ($info as $k=>$v)
  118. {
  119. $info[$k]['month_begin'] = (explode(' ',$v['month_begin'])[0] ?? '');
  120. $all += $v['cnt'];
  121. }
  122. $project = $this->linkDao->countExam();
  123. $data = ['image'=>$all,'project'=>$project,'list'=>$info];
  124. return $data;
  125. }
  126. public function getPacsResult()
  127. {
  128. $arr = $this->linkDao->getReportResult();
  129. $data = ['阴性'=>0,'阳性'=>0,'其他'=>0];
  130. foreach ($arr as $k=>$v)
  131. {
  132. if($v['report_result'] == '1')
  133. {
  134. $data['阴性'] += $v['cnt'];
  135. }elseif ($v['report_result'] == '2')
  136. {
  137. $data['阳性'] += $v['cnt'];
  138. }else{
  139. $data['其他'] += $v['cnt'];
  140. }
  141. }
  142. $return = $this->makeData($data);
  143. return $return;
  144. }
  145. public function makeData($arr)
  146. {
  147. $data = [];
  148. foreach ($arr as $k=>$v)
  149. {
  150. $data[] = ['name'=>$k,'value'=>$v];
  151. }
  152. return $data;
  153. }
  154. public function getPacsSex()
  155. {
  156. $arr = $this->linkDao->getSexData();
  157. $data = ['男'=>0,'女'=>0,'其他'=>0];
  158. foreach ($arr as $k=>$v)
  159. {
  160. if($v['sex'] == 'M' || $v['sex'] == '男' )
  161. {
  162. $data['男'] += $v['cnt'];
  163. }elseif ($v['sex'] == 'F' || $v['sex'] == '女')
  164. {
  165. $data['女'] += $v['cnt'];
  166. }else{
  167. $data['其他'] += $v['cnt'];
  168. }
  169. }
  170. $return = $this->makeData($data);
  171. return $return;
  172. }
  173. public function getMiddleProjectList()
  174. {
  175. $list = $this->linkDao->getMiddleProjectList();
  176. return $list;
  177. }
  178. public function getPacsMapExam()
  179. {
  180. $institution = $this->linkDao->getAllIns();
  181. $allIns = [];
  182. foreach ($institution as $k=>$v)
  183. {
  184. $allIns[$v['id']] = $v;
  185. }
  186. $exam = $this->linkDao->getInsExam();
  187. rsort($exam);
  188. $arr = [];
  189. foreach ($exam as $k=>$v)
  190. {
  191. if(empty($allIns[$v['institution_id']] ?? ''))
  192. {
  193. continue;
  194. }
  195. $arr[] = ['num'=>$v['num'],'lng'=>$allIns[$v['institution_id']]['lng'],'lat'=>$allIns[$v['institution_id']]['lat'],'NAME'=>$allIns[$v['institution_id']]['name']];
  196. }
  197. return $arr;
  198. }
  199. }