QuestionController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\service\sys_manage\FieldService;
  4. use app\admin\validate\sys_manage\ContentValidate;
  5. use app\controller\Curd;
  6. use app\model\Content;
  7. use app\model\SysField;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. class QuestionController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new Content();
  16. $this->validate = true;
  17. $this->validateClass = new ContentValidate();
  18. }
  19. /** 列表
  20. * @Desc
  21. * @Author Gorden
  22. * @Date 2024/3/5 10:00
  23. *
  24. * @param Request $request
  25. * @return Response
  26. * @throws \support\exception\BusinessException
  27. */
  28. public function select(Request $request): Response
  29. {
  30. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  31. $where['content_category'] = 'WELLNESSQUESTION';
  32. $order = $request->get('order', 'desc');
  33. $field = $field ?? 'content_sort';
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  38. {
  39. $model = $this->model->with([
  40. 'category' => function ($query) {
  41. $query->select('category_id', 'category_name');
  42. }
  43. ]);
  44. foreach ($where as $column => $value) {
  45. if (is_array($value)) {
  46. if ($value[0] === 'like' || $value[0] === 'not like') {
  47. $model = $model->where($column, $value[0], "%$value[1]%");
  48. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  49. $model = $model->where($column, $value[0], $value[1]);
  50. } elseif ($value[0] == 'in' && !empty($value[1])) {
  51. $valArr = $value[1];
  52. if (is_string($value[1])) {
  53. $valArr = explode(",", trim($value[1]));
  54. }
  55. $model = $model->whereIn($column, $valArr);
  56. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  57. $valArr = $value[1];
  58. if (is_string($value[1])) {
  59. $valArr = explode(",", trim($value[1]));
  60. }
  61. $model = $model->whereNotIn($column, $valArr);
  62. } elseif ($value[0] == 'null') {
  63. $model = $model->whereNull($column);
  64. } elseif ($value[0] == 'not null') {
  65. $model = $model->whereNotNull($column);
  66. } elseif ($value[0] !== '' || $value[1] !== '') {
  67. $model = $model->whereBetween($column, $value);
  68. }
  69. } else {
  70. $model = $model->where($column, $value);
  71. }
  72. }
  73. if ($field) {
  74. $model = $model->orderBy($field, $order)->orderBy('content_addtimes','DESC');
  75. }
  76. return $model;
  77. }
  78. public function afterQuery($items)
  79. {
  80. foreach ($items as &$item) {
  81. if (!empty($item->content_config_json)) {
  82. $contentConfigJson = json_decode($item->content_config_json, true);
  83. // $questionArray = [];
  84. // foreach ($contentConfigJson as $question) {
  85. // $questionArray[] = [
  86. // [
  87. // 'label' => '选项',
  88. // 'value' => $question['field'],
  89. // ],
  90. // [
  91. // 'label' => '题目',
  92. // 'value' => $question['question'],
  93. // ]
  94. // ];
  95. // }
  96. $item->content_config_json = array_values($contentConfigJson);
  97. } else {
  98. $item->content_config_json = [];
  99. }
  100. }
  101. return $items;
  102. }
  103. /**
  104. * @Desc 问卷详情
  105. * @Author Gorden
  106. * @Date 2024/12/2 10:09
  107. *
  108. * @param Request $request
  109. * @return Response
  110. */
  111. public function info(Request $request): Response
  112. {
  113. $contentId = $request->get('content_id');
  114. $question = Content::where('content_id', $contentId)->first();
  115. $questionContent = [];
  116. if (!empty($question->content_config_json)) {
  117. $contentConfigJson = json_decode($question->content_config_json, true);
  118. foreach ($contentConfigJson as $item) {
  119. $fields = SysField::whereIn('field_id', $item['field'])->select('field_name', 'field_form_type')->get()->toArray();
  120. $fieldName = '';
  121. foreach ($fields as $field) {
  122. $formType = FieldService::$formType[$field['field_form_type']] ?? '';
  123. $fieldName .= $field['field_name'] . '(' . $formType . ');';
  124. }
  125. $questionContent[] = [
  126. 'question' => $item['question'],
  127. 'fieldStr' => rtrim($fieldName, ';')
  128. ];
  129. }
  130. $question->content_question_content = $questionContent;
  131. }
  132. dump($question);
  133. return json_success('success', $question);
  134. }
  135. /**
  136. * @Desc insert 处理
  137. * @Author Gorden
  138. * @Date 2024/3/27 10:24
  139. *
  140. * @param Request $request
  141. * @return array
  142. * @throws \support\exception\BusinessException
  143. */
  144. protected function insertInput(Request $request): array
  145. {
  146. $data = $this->inputFilter($request->post());
  147. $data['content_category'] = 'WELLNESSQUESTION';
  148. $data['content_config_json'] = json_encode($request->post('question'));
  149. return $data;
  150. }
  151. /**
  152. * @Desc update 处理
  153. * @Author Gorden
  154. * @Date 2024/12/2 9:22
  155. *
  156. * @param Request $request
  157. * @return array
  158. * @throws BusinessException
  159. */
  160. protected function updateInput(Request $request): array
  161. {
  162. $primary_key = $this->model->getKeyName();
  163. $id = $request->post($primary_key);
  164. $data = $this->inputFilter($request->post());
  165. $model = $this->model->find($id);
  166. if (!$model) {
  167. throw new BusinessException('记录不存在', 2);
  168. }
  169. $data['content_config_json'] = json_encode($request->post('question'));
  170. unset($data[$primary_key]);
  171. return [$id, $data];
  172. }
  173. }