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

147 lines
4.6 KiB
PHP

<?php
namespace app\admin\controller;
use app\model\QaCitys;
use app\model\QaQuestions;
use app\model\Qas;
use support\Log;
use support\Request;
use think\facade\Db;
class QaController extends base
{
public function getQaList(Request $request)
{
$city_id = $request->get('city_id');
$keyword = $request->get('keyword');
$list = Qas::with('qaQuestions')->order('id decs');
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));
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');
$list = Qas::with(['qaCitys','qaQuestions'])->order('create_time desc');
if (!empty($title)){
$list = $list->where('title','like','%'.$title.'%');
}
if (!empty($status)){
$list = $list->where('status',$status);
}
$list = $list->paginate($request->get('limit',10));
return $this->success($list);
}
public function getQaDetail(Request $request)
{
$id = $request->get('id');
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
$data = Qas::with(['qaCitys','qaQuestions'])->where('status',1)->find();
return $this->success($data);
}
public function addQa(Request $request)
{
$post = $request->post();
if (empty($post['city_id'])) return $this->error(2001, 'city_id data cannot be empty!');
if (empty($post['title'])) return $this->error(2001, 'title data cannot be empty!');
$qaQuestion = $post['qaQuestions'];
unset($post['qaQuestions']);
try {
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;
}
});
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
return $this->success(null);
}
public function editQa(Request $request)
{
$post = $request->post();
if (empty($post['id'])) return $this->error(2001, 'id data cannot be empty!');
$qaQuestion = $post['qaQuestions'];
unset($post['create_time']);
unset($post['update_time']);
unset($post['qaQuestions']);
try {
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;
}
});
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
return $this->success(null);
}
public function delQa(Request $request)
{
$id = $request->post('id');
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
try {
Db::transaction(function () use ($id){
Qas::destroy($id);
QaQuestions::where('qa_id',$id)->delete();
});
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
return $this->success(null);
}
public function getQaCityList()
{
$list = QaCitys::order('city_id desc')->select();
return $this->success($list);
}
}