travel/service/app/admin/controller/QaController.php

153 lines
4.9 KiB
PHP
Raw Normal View History

2024-06-24 20:58:23 +08:00
<?php
namespace app\admin\controller;
2024-06-27 17:34:53 +08:00
use app\model\QaCitys;
2024-06-28 17:30:56 +08:00
use app\model\QaQuestions;
2024-06-24 20:58:23 +08:00
use app\model\Qas;
2024-06-27 19:53:53 +08:00
use support\Log;
2024-06-24 20:58:23 +08:00
use support\Request;
2024-06-28 17:30:56 +08:00
use think\facade\Db;
2024-06-24 20:58:23 +08:00
class QaController extends base
{
public function getQaList(Request $request)
{
2024-06-27 17:34:53 +08:00
$city_id = $request->get('city_id');
2024-06-27 19:53:53 +08:00
$keyword = $request->get('keyword');
2024-06-27 17:34:53 +08:00
2024-07-01 11:25:55 +08:00
$list = Qas::with(['qaQuestions'=>function ($query) {
$query->order('sort desc');
}])->order('id decs');
2024-06-27 19:53:53 +08:00
if (!empty($city_id)){
$list = $list->where(['status' => 1, 'city_id' => $city_id]);
}
if (!empty($keyword)){
$list = $list->whereRaw("title LIKE ? OR content LIKE ?", ['%'.$keyword.'%', '%'.$keyword.'%']);
}
$list = $list->paginate($request->get('limit',10));
2024-06-27 17:34:53 +08:00
return $this->success($list);
}
/**
* cms list
* @param Request $request
* @return \support\Response
* @throws \think\db\exception\DbException
*/
public function getQa(Request $request)
{
$title = $request->get('title');
$status = $request->get('status');
2024-07-01 11:25:55 +08:00
$list = Qas::with(['qaCitys','qaQuestions'=>function ($query) {
$query->order('sort asc');
}])->order('create_time desc');
2024-06-27 17:34:53 +08:00
if (!empty($title)){
$list = $list->where('title','like','%'.$title.'%');
}
if (!empty($status)){
$list = $list->where('status',$status);
}
$list = $list->paginate($request->get('limit',10));
2024-06-24 20:58:23 +08:00
return $this->success($list);
}
public function getQaDetail(Request $request)
{
2024-06-26 15:38:38 +08:00
$id = $request->get('id');
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
2024-06-24 20:58:23 +08:00
2024-07-01 11:25:55 +08:00
$data = Qas::with(['qaCitys','qaQuestions'=>function ($query) {
$query->order('sort asc');
}])->where('status',1)->find();
2024-06-24 20:58:23 +08:00
return $this->success($data);
}
public function addQa(Request $request)
{
$post = $request->post();
2024-06-27 19:53:53 +08:00
if (empty($post['city_id'])) return $this->error(2001, 'city_id data cannot be empty!');
2024-06-24 20:58:23 +08:00
if (empty($post['title'])) return $this->error(2001, 'title data cannot be empty!');
2024-06-28 17:30:56 +08:00
2024-07-01 16:05:56 +08:00
$qaQuestion = $post['qaQuestions'] ?? null;
2024-06-28 17:30:56 +08:00
unset($post['qaQuestions']);
2024-06-24 20:58:23 +08:00
try {
2024-06-28 17:30:56 +08:00
Db::transaction(function () use ($post,$qaQuestion){
$data = Qas::create($post);
if (!empty($qaQuestion)){
foreach ($qaQuestion as $k => $v){
$qaQuestion = new QaQuestions();
$qaQuestion->qa_id = $data->id;
$qaQuestion->title = $v['title'];
$qaQuestion->content = $v['content'];
$qaQuestion->sort = $v['sort'];
$qaQuestion->save();
}
$data->qaQuestions = $qaQuestion;
}
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 17:30:56 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
public function editQa(Request $request)
{
$post = $request->post();
if (empty($post['id'])) return $this->error(2001, 'id data cannot be empty!');
2024-07-01 16:03:54 +08:00
$qaQuestion = $post['qaQuestions'] ?? null;
2024-06-27 17:34:53 +08:00
unset($post['create_time']);
unset($post['update_time']);
2024-06-28 17:30:56 +08:00
unset($post['qaQuestions']);
2024-06-24 20:58:23 +08:00
try {
2024-06-28 17:30:56 +08:00
Db::transaction(function () use ($post,$qaQuestion){
$data = Qas::update($post);
if (!empty($qaQuestion)){
QaQuestions::where('qa_id',$post['id'])->delete();
foreach ($qaQuestion as $k => $v){
$qaQuestion = new QaQuestions();
$qaQuestion->qa_id = $data->id;
$qaQuestion->title = $v['title'];
$qaQuestion->content = $v['content'];
$qaQuestion->sort = $v['sort'];
$qaQuestion->save();
}
$data->qa_question = $qaQuestion;
}
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 17:30:56 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
public function delQa(Request $request)
{
2024-06-27 17:34:53 +08:00
$id = $request->post('id');
2024-06-24 20:58:23 +08:00
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
try {
2024-06-28 18:33:51 +08:00
Db::transaction(function () use ($id){
Qas::destroy($id);
QaQuestions::where('qa_id',$id)->delete();
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 18:33:51 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
2024-06-27 17:34:53 +08:00
public function getQaCityList()
{
2024-07-02 16:35:41 +08:00
$list = QaCitys::order('sort desc')->select();
2024-06-27 17:34:53 +08:00
return $this->success($list);
}
2024-06-24 20:58:23 +08:00
}