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.

220 lines
9.4 KiB

5 months ago
<?php
namespace App\Http\Controllers\Admin;
use App\Exports\BaseExport;
1 month ago
use App\Exports\CommonExport;
5 months ago
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;
2 weeks ago
use App\Models\HistoryCourse;
5 months ago
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="",
1 month ago
* @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="导出文件名"),
5 months ago
* @OA\Parameter(name="month", in="query", @OA\Schema(type="string"), required=true, description="月份"),
5 months ago
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
5 months ago
$all = \request()->all();
2 weeks ago
$list = Calendar::with('course', 'courseContent', 'historyCourses')
2 weeks ago
->where(function ($query) use ($all) {
if (isset($all['month'])) {
$query->where('start_time', 'like', $all['month'] . '%');
}
})->orderBy('date')
->get();
1 month ago
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');
}
2 weeks ago
// 本月日历天数
$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'])) {
$query->where('start_time', 'like', $all['month'] . '%');
}
})->where('is_count_days', 1)->sum('days');
return $this->success(compact('list', 'monthDayCalendar', 'yearDayCalendar'));
5 months ago
}
/**
* @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())]);
}
2 weeks ago
$detail = $this->model->with(['courseContent', 'historyCourses'])->find($all['id']);
5 months ago
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/admin/calendars/save",
* tags={"日历管理"},
* summary="保存",
* description="",
5 months ago
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer", format="int64"), required=true, description="ID存在则更新不存在则新增"),
5 months ago
* @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"),
5 months ago
* @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="标题"),
5 months ago
* @OA\Parameter(name="url", in="query", @OA\Schema(type="string", maxLength=255), required=false, description="url"),
5 months ago
* @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"),
4 months ago
* @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="地址"),
3 weeks ago
* @OA\Parameter(name="days", in="query", @OA\Schema(type="string"), required=true, description="天数"),
2 weeks ago
* @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(结束时间)"),
5 months ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="认证token"),
* @OA\Response(
* response="200",
* description="操作成功"
* )
* )
*/
public function save()
{
2 weeks ago
$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()]);
}
5 months ago
}
/**
* @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()
{
2 weeks ago
$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()]);
}
5 months ago
}
}