QuestionController.php 5.8 KB

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