QuestionController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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', 'ASC');
  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. public function selectList()
  104. {
  105. $questions = Content::where('content_category', 'WELLNESSQUESTION')
  106. ->select('content_title', 'content_config_json')
  107. ->orderBy('content_sort', 'asc')
  108. ->get()
  109. ->toArray();
  110. foreach ($questions as &$question) {
  111. if (!empty($question['content_config_json'])) {
  112. $contentConfigJson = json_decode($question['content_config_json'], true);
  113. foreach ($contentConfigJson as $item) {
  114. $fields = SysField::whereIn('field_id', $item['field'])
  115. ->select('field_id', 'field_form_key', 'field_form_type', 'field_refer_json')
  116. ->orderByDesc('field_sort')
  117. ->get()
  118. ->toArray();
  119. $fieldData = [];
  120. foreach ($fields as $field) {
  121. if (!empty($field['field_refer_json'])) {
  122. $fieldData = [
  123. 'form_type' => $field['field_form_type'],
  124. 'form_key' => $field['field_form_key'],
  125. 'field' => json_decode($field['field_refer_json'], true)
  126. ];
  127. }
  128. }
  129. $question['list'][] = [
  130. 'question' => $item['question'],
  131. 'fields' => $fieldData
  132. ];
  133. }
  134. }
  135. unset($question['content_config_json']);
  136. }
  137. return json_success('success', $questions);
  138. }
  139. /**
  140. * @Desc 问卷详情
  141. * @Author Gorden
  142. * @Date 2024/12/2 10:09
  143. *
  144. * @param Request $request
  145. * @return Response
  146. */
  147. public function info(Request $request): Response
  148. {
  149. $contentId = $request->get('content_id');
  150. $question = Content::where('content_id', $contentId)->first();
  151. $questionContent = [];
  152. if (!empty($question->content_config_json)) {
  153. $contentConfigJson = json_decode($question->content_config_json, true);
  154. foreach ($contentConfigJson as $item) {
  155. $fields = SysField::whereIn('field_id', $item['field'])->select('field_name', 'field_form_type')->get()->toArray();
  156. $fieldName = '';
  157. foreach ($fields as $field) {
  158. $formType = FieldService::$formType[$field['field_form_type']] ?? '';
  159. $fieldName .= $field['field_name'] . '(' . $formType . ');';
  160. }
  161. $questionContent[] = [
  162. 'question' => $item['question'],
  163. 'fieldStr' => rtrim($fieldName, ';')
  164. ];
  165. }
  166. $question->content_question_content = $questionContent;
  167. }
  168. dump($question);
  169. return json_success('success', $question);
  170. }
  171. /**
  172. * @Desc insert 处理
  173. * @Author Gorden
  174. * @Date 2024/3/27 10:24
  175. *
  176. * @param Request $request
  177. * @return array
  178. * @throws \support\exception\BusinessException
  179. */
  180. protected function insertInput(Request $request): array
  181. {
  182. $data = $this->inputFilter($request->post());
  183. $data['content_category'] = 'WELLNESSQUESTION';
  184. $data['content_config_json'] = json_encode($request->post('question'));
  185. return $data;
  186. }
  187. /**
  188. * @Desc update 处理
  189. * @Author Gorden
  190. * @Date 2024/12/2 9:22
  191. *
  192. * @param Request $request
  193. * @return array
  194. * @throws BusinessException
  195. */
  196. protected function updateInput(Request $request): array
  197. {
  198. $primary_key = $this->model->getKeyName();
  199. $id = $request->post($primary_key);
  200. $data = $this->inputFilter($request->post());
  201. $model = $this->model->find($id);
  202. if (!$model) {
  203. throw new BusinessException('记录不存在', 2);
  204. }
  205. $data['content_config_json'] = json_encode($request->post('question'));
  206. unset($data[$primary_key]);
  207. return [$id, $data];
  208. }
  209. }