MedicalCalculatorApiController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use think\response\Json;
  5. use app\model\MedicalCalculator;
  6. use app\model\MedicalQuestion;
  7. use app\model\MedicalOption;
  8. class MedicalCalculatorApiController extends CommonTwoController
  9. {
  10. /**
  11. * 获取计算器列表
  12. */
  13. public function index(): Json
  14. {
  15. $page = $this->request->param('page/d', 1);
  16. $limit = $this->request->param('limit/d', 20);
  17. $calculators = MedicalCalculator::withoutField(['formula', 'results'])
  18. ->page($page, $limit)
  19. ->select();
  20. $total = MedicalCalculator::count();
  21. return $this->_json_succ([
  22. 'list' => $calculators,
  23. 'total' => $total,
  24. 'page' => $page,
  25. 'limit' => $limit
  26. ]);
  27. }
  28. /**
  29. * 创建计算器
  30. */
  31. public function create(): Json
  32. {
  33. $data = $this->request->param();
  34. try {
  35. $calculator = new MedicalCalculator;
  36. $calculator->save($data);
  37. // 保存问题和选项
  38. if (!empty($data['questions'])) {
  39. $this->saveQuestions($calculator, $data['questions']);
  40. }
  41. return $this->_json_succ($calculator);
  42. } catch (\Exception $e) {
  43. return $this->_json_error('创建失败: ' . $e->getMessage());
  44. }
  45. }
  46. /**
  47. * 获取计算器详情
  48. */
  49. public function show(string $id): Json
  50. {
  51. $calculator = MedicalCalculator::with([
  52. 'questions' => function($query) {
  53. $query->with(['options' => function($query) {
  54. $query->order('id', 'asc');
  55. }]);
  56. }
  57. ])->where('id', $id)->find();
  58. // 调试输出 - 使用 json_encode 转换为字符串
  59. trace('Calculator data: ' . json_encode($calculator, JSON_UNESCAPED_UNICODE));
  60. if ($calculator) {
  61. trace('Questions count: ' . $calculator->questions->count());
  62. if (!$calculator->questions->isEmpty()) {
  63. trace('First question options: ' . json_encode($calculator->questions[0]->options, JSON_UNESCAPED_UNICODE));
  64. }
  65. }
  66. if (empty($calculator)) {
  67. return $this->_json_error('计算器不存在');
  68. }
  69. return $this->_json_succ($calculator);
  70. }
  71. /**
  72. * 更新计算器
  73. */
  74. public function update(string $id): Json
  75. {
  76. $calculator = MedicalCalculator::find($id);
  77. if (!$calculator) {
  78. return $this->_json_error('计算器不存在');
  79. }
  80. $data = $this->request->param();
  81. try {
  82. $calculator->save($data);
  83. // 更新问题和选项
  84. if (isset($data['questions'])) {
  85. $this->saveQuestions($calculator, $data['questions']);
  86. }
  87. return $this->_json_succ($calculator);
  88. } catch (\Exception $e) {
  89. return $this->_json_error('更新失败: ' . $e->getMessage());
  90. }
  91. }
  92. /**
  93. * 删除计算器
  94. */
  95. public function destroy(string $id): Json
  96. {
  97. $calculator = MedicalCalculator::find($id);
  98. if (!$calculator) {
  99. return $this->_json_error('计算器不存在');
  100. }
  101. try {
  102. // 开启事务
  103. \think\facade\Db::startTrans();
  104. // 删除关联的选项
  105. MedicalOption::whereIn('medical_question_id', function($query) use ($id) {
  106. $query->name('medical_questions')->where('medical_calculator_id', $id)->field('id');
  107. })->delete();
  108. // 删除关联的问题
  109. MedicalQuestion::where('medical_calculator_id', $id)->delete();
  110. // 删除计算器
  111. $calculator->delete();
  112. \think\facade\Db::commit();
  113. return $this->_json_succ(null, '删除成功');
  114. } catch (\Exception $e) {
  115. \think\facade\Db::rollback();
  116. return $this->_json_error('删除失败: ' . $e->getMessage());
  117. }
  118. }
  119. /**
  120. * 获取计算器问题
  121. */
  122. public function getQuestions(string $id): Json
  123. {
  124. $questions = MedicalQuestion::with(['options'])
  125. ->where('medical_calculator_id', $id)
  126. ->select();
  127. if ($questions->isEmpty()) {
  128. return $this->_json_error('未找到相关问题');
  129. }
  130. return $this->_json_succ($questions);
  131. }
  132. /**
  133. * 计算
  134. */
  135. public function calculate(string $id): Json
  136. {
  137. $calculator = MedicalCalculator::find($id);
  138. if (!$calculator) {
  139. return $this->_json_error('计算器不存在');
  140. }
  141. $inputs = $this->request->param();
  142. try {
  143. $result = $calculator->calculateResult($inputs);
  144. $interpretation = $calculator->getResultInterpretation($result);
  145. return $this->_json_succ([
  146. 'value' => $result,
  147. 'interpretation' => $interpretation
  148. ]);
  149. } catch (\Exception $e) {
  150. return $this->_json_error('计算失败: ' . $e->getMessage());
  151. }
  152. }
  153. /**
  154. * 保存问题和选项
  155. */
  156. protected function saveQuestions(MedicalCalculator $calculator, array $questions)
  157. {
  158. foreach ($questions as $questionData) {
  159. if (empty($questionData['question']) || empty($questionData['type'])) {
  160. continue;
  161. }
  162. $question = new MedicalQuestion([
  163. 'medical_calculator_id' => $calculator->id,
  164. 'question' => $questionData['question'],
  165. 'variable_name' => $questionData['variable_name'] ?? '',
  166. 'type' => $questionData['type'],
  167. 'score' => $questionData['score'] ?? 0,
  168. ]);
  169. $question->save();
  170. // 保存选项
  171. if (!empty($questionData['options']) && is_array($questionData['options'])) {
  172. foreach ($questionData['options'] as $optionData) {
  173. if (empty($optionData['content'])) {
  174. continue;
  175. }
  176. $option = new MedicalOption([
  177. 'medical_question_id' => $question->id,
  178. 'content' => $optionData['content'],
  179. 'score' => $optionData['score'] ?? 0,
  180. ]);
  181. $option->save();
  182. }
  183. }
  184. }
  185. }
  186. }