123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace app\admin\controller\sys_manage;
- use app\admin\validate\sys_manage\AdvValidate;
- use app\admin\validate\sys_manage\ArticleValidate;
- use app\admin\validate\sys_manage\ContentValidate;
- use app\controller\Curd;
- use app\model\Adv;
- use app\model\Article;
- use app\model\Content;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class QuestionController extends Curd
- {
- public function __construct()
- {
- $this->model = new Content();
- $this->validate = true;
- $this->validateClass = new ContentValidate();
- }
- /** 列表
- * @Desc
- * @Author Gorden
- * @Date 2024/3/5 10:00
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $where['content_category'] = 'WELLNESSQUESTION';
- $order = $request->get('order', 'desc');
- $field = $field ?? 'content_addtimes';
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- }
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- if (!empty($item->content_config_json)) {
- $contentConfigJson = json_decode($item->content_config_json, true);
- $questionArray = [];
- foreach ($contentConfigJson as $question) {
- $questionArray[] = [
- [
- 'label' => '选项',
- 'value' => $question['field'],
- ],
- [
- 'label' => '题目',
- 'value' => $question['question'],
- ]
- ];
- }
- $item->content_config_json = $questionArray;
- } else {
- $item->content_config_json = [];
- }
- }
- return $items;
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/27 10:24
- *
- * @param Request $request
- * @return array
- * @throws \support\exception\BusinessException
- */
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- $data['content_category'] = 'WELLNESSQUESTION';
- $contentConfigJson = [];
- if (!empty($request->post('question'))) {
- $questions = $request->post('question');
- foreach ($questions as $question) {
- if (!empty($question[1]['value'])) {
- $contentConfigJson[] = [
- 'field' => $question[0]['value'],
- 'question' => $question[1]['value'],
- ];
- }
- }
- $contentConfigJson = new \ArrayObject($contentConfigJson);
- $data['content_config_json'] = json_encode($contentConfigJson);
- }
- return $data;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- $contentConfigJson = [];
- if (!empty($request->post('question'))) {
- $questions = $request->post('question');
- foreach ($questions as $question) {
- $contentConfigJson[] = [
- 'question' => $question[1]['value'],
- 'field' => $question[0]['value'],
- ];
- }
- $contentConfigJson = new \ArrayObject($contentConfigJson);
- $data['content_config_json'] = json_encode($contentConfigJson);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|