You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Admin;
use App\Models\Study;
use App\Models\StudyAsk;
use App\Models\StudyLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ResponseCode;
/**
* 学习资料管理
*/
class StudyController extends CommonController
{
/**
* @OA\Get(
* path="/api/admin/study/index",
* tags={"学习资料管理"},
* summary="列表",
* description="",
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
$all = request()->all();
$list = Study::where(function ($query) use ($all) {
if (isset($all['keyword'])) {
$query->where('name', 'like', '%' . $all['keyword'] . '%');
}
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
->paginate($all['page_size'] ?? 20);
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/admin/study/study-log",
* tags={"学习资料管理"},
* summary="学习记录",
* description="",
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function studyLog()
{
$all = request()->all();
$list = StudyLog::with('user')->where(function ($query) use ($all) {
if (isset($all['keyword'])) {
$query->where('content', 'like', '%' . $all['keyword'] . '%');
}
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
->paginate($all['page_size'] ?? 20);
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/admin/study/show",
* tags={"学习资料管理"},
* summary="详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function show()
{
$all = \request()->all();
$messages = [
'id.required' => 'Id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$detail = Study::with('asks')->find($all['id']);
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/admin/study/save",
* tags={"学习资料管理"},
* summary="更新",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"),
* @OA\Parameter(name="admin_id", in="query", @OA\Schema(type="string"), required=false, description="不用填"),
* @OA\Parameter(name="department_id", in="query", @OA\Schema(type="string"), required=false, description="不用填"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名称"),
* @OA\Parameter(name="expire_day", in="query", @OA\Schema(type="string"), required=false, description="有效天数"),
* @OA\Parameter(name="rate", in="query", @OA\Schema(type="string"), required=false, description="通过正确率"),
* @OA\Parameter(name="minute", in="query", @OA\Schema(type="string"), required=false, description="最低学习分钟数"),
* @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), required=false, description="内容"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="类型1访客2施工3车辆"),
* @OA\Parameter(name="file", in="query", @OA\Schema(type="string"), required=false, description="文件id数组"),
* @OA\Parameter(name="ask_list", in="query", @OA\Schema(type="string"), required=false, description="问题数组。包括type类型1单选2多选title问题内容answer回答选项二维数组包括content答案内容result是否正确0错误1正确"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function save()
{
$all = \request()->all();
$messages = [
'name.required' => '名称必填'
];
$validator = Validator::make($all, [
'name' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
DB::beginTransaction();
try {
if (isset($all['id'])) {
$model = Study::find($all['id']);
if (isset($all['ask_list'])) {
$model->asks()->delete();
}
} else {
$model = new Study();
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
}
$model->fill($all);
$model->save();
if (isset($all['ask_list']) && !empty($all['ask_list'])) {
$model->asks()->createMany($all['ask_list']);
}
DB::commit();
return $this->success('更新成功');
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/study/destroy",
* tags={"学习资料管理"},
* summary="删除",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function destroy()
{
$all = \request()->all();
$messages = [
'id.required' => 'Id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
Study::where('id', $all['id'])->delete();
StudyAsk::where('study_id', $all['id'])->delete();
return $this->success('删除成功');
}
}