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.

370 lines
16 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\Helpers\ResponseCode;
use App\Jobs\SendCourseSms;
use App\Models\Course;
use App\Models\CourseAppointmentTotal;
use App\Models\CustomForm;
use App\Models\User;
use App\Notifications\CourseContentNotify;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
class CourseController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
parent::__construct(new Course());
}
/**
* @OA\Get(
* path="/api/admin/courses/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="filter", in="query", @OA\Schema(type="string"), required=false, description="查询条件。数组"),
* @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组包括teachercourseSettingscoursePeriods"),
* @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="排序字段名字。当值是sign_status是按照课程状态排序"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="has_course_forms", in="query", @OA\Schema(type="string"), required=true, description="是否有自定义表单0否1是"),
* @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 = $this->model->with(underlineToHump($all['show_relation'] ?? []))
->withCount(['courseSigns' => function ($query) {
$query->whereNotIn('status', [4, 5]);
}])
->withCount(['courseSigns as sign_pass_total' => function ($query) {
$query->where('status', 1)->whereHas('user');
}])->withCount(['courseSigns as sign_wait_total' => function ($query) {
$query->where('status', 0)->whereHas('user');
}])->withCount(['courseSigns as sign_fault_total' => function ($query) {
$query->where('status', 2)->whereHas('user');
}])->withCount(['courseSigns as sign_prepare_total' => function ($query) {
$query->where('status', 3)->whereHas('user');
}])->withCount(['courseSigns as sign_cancel_total' => function ($query) {
$query->where('status', 4)->whereHas('user');
}])->withCount(['courseSigns as sign_give_up_total' => function ($query) {
$query->where('status', 5)->whereHas('user');
}])->withCount(['courseSigns as sign_black_total' => function ($query) {
$query->where('status', 6)->whereHas('user');
}])->where(function ($query) use ($all) {
if (isset($all['has_course_forms']) && !empty($all['has_course_forms'])) {
$query->whereHas('courseForms');
}
if (isset($all['filter']) && !empty($all['filter'])) {
foreach ($all['filter'] as $condition) {
$key = $condition['key'] ?? null;
$op = $condition['op'] ?? null;
$value = $condition['value'] ?? null;
if (!isset($key) || !isset($op) || !isset($value)) {
continue;
}
// 等于
if ($op == 'eq') {
$query->where($key, $value);
}
// 不等于
if ($op == 'neq') {
$query->where($key, '!=', $value);
}
// 大于
if ($op == 'gt') {
$query->where($key, '>', $value);
}
// 大于等于
if ($op == 'egt') {
$query->where($key, '>=', $value);
}
// 小于
if ($op == 'lt') {
$query->where($key, '<', $value);
}
// 小于等于
if ($op == 'elt') {
$query->where($key, '<=', $value);
}
// 模糊搜索
if ($op == 'like') {
$query->where($key, 'like', '%' . $value . '%');
}
// 否定模糊搜索
if ($op == 'notlike') {
$query->where($key, 'not like', '%' . $value . '%');
}
// 范围搜索
if ($op == 'range') {
list($from, $to) = explode(',', $value);
if (empty($from) || empty($to)) {
continue;
}
$query->whereBetween($key, [$from, $to]);
}
}
}
});
$list = $list->orderBy($all['sort_name'] ?? 'sign_status', $all['sort_type'] ?? 'asc');
if (isset($all['is_export']) && !empty($all['is_export'])) {
$list = $list->get()->toArray();
$export_fields = $all['export_fields'] ?? [];
// 导出文件名字
$tableName = $this->model->getTable();
$filename = (new CustomForm())->getTableComment($tableName);
return Excel::download(new BaseExport($export_fields, $list, $tableName), $filename . date('YmdHis') . '.xlsx');
} else {
// 输出
$list = $list->paginate($all['page_size'] ?? 20);
}
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/admin/courses/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->withCount('courseSigns')->with(underlineToHump($all['show_relation'] ?? []))->find($all['id']);
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/admin/courses/save",
* tags={"课程管理"},
* summary="更新或新增课程信息",
* description="根据传入的id决定是更新现有课程还是新增新的课程信息。",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="课程ID存在则更新不存在则新增"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="验证token"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), description="课程名称"),
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string", format="date"), description="开课日期"),
* @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string", format="date"), description="结课日期"),
* @OA\Parameter(name="sign_start_date", in="query", @OA\Schema(type="string", format="date"), description="报名开始日期"),
* @OA\Parameter(name="sign_end_date", in="query", @OA\Schema(type="string", format="date"), description="报名结束日期"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="integer"), description="课程类型:接口获取"),
* @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), description="课程内容"),
* @OA\Parameter(name="total", in="query", @OA\Schema(type="integer"), description="开课人数"),
* @OA\Parameter(name="class", in="query", @OA\Schema(type="string"), description="所在班级"),
* @OA\Parameter(name="price", in="query", @OA\Schema(type="string"), description="价格"),
* @OA\Parameter(name="is_arrange", in="query", @OA\Schema(type="integer"), description="是否排课-0否1是"),
* @OA\Parameter(name="is_fee", in="query", @OA\Schema(type="integer"), description="是否缴费-0否1是"),
* @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), description="课程状态0待发布, 1已发布"),
* @OA\Parameter(name="teacher_id", in="query", @OA\Schema(type="integer"), description="班主任ID,多个英文逗号分隔"),
* @OA\Parameter(name="publicize_content", in="query", @OA\Schema(type="integer"), description="宣传内容"),
* @OA\Parameter(name="publicize_ids", in="query", @OA\Schema(type="integer"), description="宣传内容附件id数组"),
* @OA\Parameter(name="sign_total", in="query", @OA\Schema(type="integer"), description="报名人数默认0不限制"),
* @OA\Parameter(name="image_id", in="query", @OA\Schema(type="integer"), description="图片id"),
* @OA\Parameter(name="qun_image_id", in="query", @OA\Schema(type="integer"), description="群图片id"),
* @OA\Parameter(name="is_virtual", in="query", @OA\Schema(type="integer"), description="是否虚拟课程0否1是"),
* @OA\Parameter(name="course_content_status", in="query", @OA\Schema(type="integer"), description="课表状态0未发布, 1已发布"),
* @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();
DB::commit();
// 更改状态
$model = Course::updateSignStatus($model->id);
$model = Course::updateStatus($model->id);
if ($model->status == 1 && $model->start_date) {
CourseAppointmentTotal::addByCourse($model->id);
}
// 记录日志
$this->saveLogs($original, $model);
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/courses/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()
{
return parent::destroy();
}
/**
* @OA\Post(
* path="/api/admin/courses/excel-show",
* tags={"课程管理"},
* summary="导入预览",
* description="",
* @OA\Parameter(name="file", in="query", @OA\Schema(type="string"), required=true, description="文件"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function excelShow()
{
return parent::excelShow();
}
/**
* @OA\Post(
* path="/api/admin/courses/import",
* tags={"课程管理"},
* summary="导入",
* description="",
* @OA\Parameter(name="data", in="query", @OA\Schema(type="string"), required=true, description="导入分析获取到的二维数组"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function import()
{
return parent::import();
}
/**
* @OA\Get(
* path="/api/admin/courses/qrcode",
* 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 qrcode()
{
$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())]);
}
$url = (new Course())->getCourseQrcode($all['id']);
return $this->success($url);
}
/**
* @OA\Get(
* path="/api/admin/courses/send-sms",
* 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 sendSms()
{
$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->find($all['id']);
// 获取学员列表
$users = User::whereHas('courseSigns', function ($query) use ($detail) {
$query->where('status', 1)->where('course_id', $detail->id);
})->whereNotNull('mobile')->get();
foreach ($users as $user) {
$data = ['course_id' => $detail->id];
Notification::send($user, new CourseContentNotify($data));
}
return $this->success("发送成功");
}
}