interviewId = $interviewId; } /** * 获取面试ID * @return int */ public function getInterviewId(): int { return $this->interviewId; } /** * 添加搜索条件 * @param string $field * @param string $op * @param mixed $value */ public function addSearchWhere(string $field, string $op, $value) { $this->searchWhere[] = [$field, $op, $value]; } /** * @notes 设置搜索条件 * @return array */ public function setSearch(): array { return [ '=' => ['type'], '%like%' => ['question', 'answer'], ]; } /** * @notes 获取列表 * @return array */ public function lists(): array { // 获取分页参数 $this->page = (int)$this->request->param('page', 1); if ($this->page < 1) { $this->page = 1; } // 获取每页数量,设置默认值和最大值 $this->limit = (int)$this->request->param('limit', self::DEFAULT_LIMIT); if ($this->limit < 1) { $this->limit = self::DEFAULT_LIMIT; } if ($this->limit > self::MAX_LIMIT) { $this->limit = self::MAX_LIMIT; } // 计算偏移量 $this->offset = ($this->page - 1) * $this->limit; // 添加未删除条件 $this->searchWhere[] = ['delete_time', 'null', null]; // 确保添加了面试ID条件 if ($this->interviewId > 0) { // 检查是否已经添加了interview_id条件 $hasInterviewIdCondition = false; foreach ($this->searchWhere as $condition) { if ($condition[0] === 'interview_id') { $hasInterviewIdCondition = true; break; } } // 如果还没有添加,则添加 if (!$hasInterviewIdCondition) { $this->searchWhere[] = ['interview_id', '=', $this->interviewId]; } } // 调试输出 // dump($this->searchWhere); return InterviewDialog::field('id, interview_id, type, question, answer, question_url, answer_url, question_duration, create_time, update_time') ->where($this->searchWhere) ->order('id', 'desc') ->limit($this->offset, $this->limit) ->select() ->each(function ($item) { // 使用模型方法获取类型文本 $item['type_text'] = InterviewDialog::getTypeText($item['type']); // 处理时间显示 $item['create_time_text'] = $item['create_time']; // 处理内容截取 $item['question_short'] = mb_strlen($item['question']) > 30 ? mb_substr($item['question'], 0, 30) . '...' : $item['question']; $item['answer_short'] = mb_strlen($item['answer']) > 30 ? mb_substr($item['answer'], 0, 30) . '...' : $item['answer']; }) ->toArray(); } /** * @notes 获取数量 * @return int */ public function count(): int { // 确保添加了面试ID条件 if ($this->interviewId > 0) { // 检查是否已经添加了interview_id条件 $hasInterviewIdCondition = false; foreach ($this->searchWhere as $condition) { if ($condition[0] === 'interview_id') { $hasInterviewIdCondition = true; break; } } // 如果还没有添加,则添加 if (!$hasInterviewIdCondition) { $this->searchWhere[] = ['interview_id', '=', $this->interviewId]; } } $this->searchWhere[] = ['delete_time', 'null', null]; return InterviewDialog::where($this->searchWhere)->count(); } }