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.

236 lines
10 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\Exports\BaseExport;
use App\Exports\CommonExport;
use App\Helpers\ResponseCode;
use App\Models\AppointmentType;
use App\Models\Book;
use App\Models\Calendar;
use App\Models\CourseContentEvaluationAsk;
use App\Models\CourseContentEvaluationForm;
use App\Models\CustomForm;
use App\Models\CustomFormField;
use App\Models\HistoryCourse;
use App\Models\SupplyDemand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Rap2hpoutre\FastExcel\FastExcel;
class CalendarsController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
parent::__construct(new Calendar());
}
/**
* @OA\Get(
* path="/api/admin/calendars/index",
* tags={"日历管理"},
* summary="列表",
* description="",
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是"),
* @OA\Parameter(name="export_fields", in="query", @OA\Schema(type="string"), required=false, description="需要导出的字段数组"),
* @OA\Parameter(name="file_name", in="query", @OA\Schema(type="string"), required=false, description="导出文件名"),
* @OA\Parameter(name="month", in="query", @OA\Schema(type="string"), required=true, description="月份"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
$all = \request()->all();
$list = Calendar::with('course', 'courseContent', 'historyCourses')
->where(function ($query) use ($all) {
if (isset($all['month'])) {
$query->where('start_time', 'like', $all['month'] . '%');
}
})->orderBy('date')
->get();
if (isset($all['is_export']) && $all['is_export'] == 1) {
$list = $list->toArray();
return Excel::download(new CommonExport($list, $all['export_fields'] ?? ''), ($all['file_name'] ?? '') . date('YmdHis') . '.xlsx');
}
// 本月日历天数
$monthDayCalendar = Calendar::where(function ($query) use ($all) {
if (isset($all['month'])) {
$query->where('start_time', 'like', $all['month'] . '%');
}
})->where('is_count_days', 1)->sum('days');
// 本年日历天数
$yearDayCalendar = Calendar::where(function ($query) use ($all) {
if (isset($all['month'])) {
// 获取$all['month']的年份部分
$year = date('Y', strtotime($all['month']));
$query->where('start_time', 'like', $year . '%');
}
})->where('is_count_days', 1)->sum('days');
// 本月开课场次
$monthCourseCount = Calendar::where(function ($query) use ($all) {
if (isset($all['month'])) {
$query->where('start_time', 'like', $all['month'] . '%');
}
})->count();
// 本年开课场次
$yearCourseCount = Calendar::where(function ($query) use ($all) {
if (isset($all['month'])) {
// 获取$all['month']的年份部分
$year = date('Y', strtotime($all['month']));
$query->where('start_time', 'like', $year . '%');
}
})->count();
return $this->success(compact('list', 'monthDayCalendar', 'yearDayCalendar', 'monthCourseCount', 'yearCourseCount'));
}
/**
* @OA\Get(
* path="/api/admin/calendars/show",
* tags={"日历管理"},
* summary="详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="show_relation", 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 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 = $this->model->with(['courseContent', 'historyCourses'])->find($all['id']);
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/admin/calendars/save",
* tags={"日历管理"},
* summary="保存",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer", format="int64"), required=true, description="ID存在则更新不存在则新增"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="integer"), required=false, description="类型1课程2课堂3事件"),
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="integer"), required=false, description="课程ID"),
* @OA\Parameter(name="course_content_id", in="query", @OA\Schema(type="integer"), required=false, description="课程课堂ID"),
* @OA\Parameter(name="date", in="query", @OA\Schema(type="string", format="date"), required=false, description="日期YYYY-MM-DD"),
* @OA\Parameter(name="title", in="query", @OA\Schema(type="string", maxLength=255), required=false, description="标题"),
* @OA\Parameter(name="url", in="query", @OA\Schema(type="string", maxLength=255), required=false, description="url"),
* @OA\Parameter(name="content", in="query", @OA\Schema(type="string", format="mediumtext"), required=false, description="内容"),
* @OA\Parameter(name="start_time", in="query", @OA\Schema(type="string", format="date-time"), required=false, description="开始时间YYYY-MM-DD HH:MM:SS"),
* @OA\Parameter(name="end_time", in="query", @OA\Schema(type="string", format="date-time"), required=false, description="结束时间YYYY-MM-DD HH:MM:SS"),
* @OA\Parameter(name="is_publish", in="query", @OA\Schema(type="string"), required=true, description="是否向用户发布0否1是"),
* @OA\Parameter(name="address", in="query", @OA\Schema(type="string"), required=true, description="地址"),
* @OA\Parameter(name="days", in="query", @OA\Schema(type="string"), required=true, description="天数"),
* @OA\Parameter(name="history_courses", in="query", @OA\Schema(type="array", @OA\Items(type="object")), required=false, description="历史课程数组每项包含type(课程体系ID), course_name(课程名称), course_type_signs_pass(培养人数未去重), course_type_signs_pass_unique(培养人数去重), course_signs_pass(课程培养人数), start_time(开始时间), end_time(结束时间)"),
* @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();
DB::beginTransaction();
try {
if (isset($all['id'])) {
$model = $this->model->find($all['id']);
if (empty($model)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
}
} else {
$model = $this->model;
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
}
$original = $model->getOriginal();
$model->fill($all);
$model->save();
// 处理历史课程数据
if (isset($all['history_courses']) && is_array($all['history_courses'])) {
// 删除原有的历史课程数据
$model->historyCourses()->delete();
$model->historyCourses()->createMany($all['history_courses']);
}
DB::commit();
// 记录日志
$this->saveLogs($original, $model);
// 返回带有历史课程的数据
$model->load('historyCourses');
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/calendars/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())]);
}
DB::beginTransaction();
try {
$model = $this->model->find($all['id']);
if (empty($model)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
}
// 删除关联的历史课程数据
$model->historyCourses()->delete();
// 删除日历
$model->delete();
DB::commit();
return $this->success([]);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
}