QuestionController.php 8.1 KB

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