268 lines
7.9 KiB
PHP
268 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
use app\common\model\StudentParent;
|
|
use app\common\model\StudentSchedule;
|
|
use app\constant\ResponseCode;
|
|
use support\Request;
|
|
use support\Response;
|
|
use plugin\admin\app\model\Student;
|
|
use plugin\admin\app\controller\Crud;
|
|
use support\exception\BusinessException;
|
|
use think\Exception;
|
|
|
|
/**
|
|
* 学生管理
|
|
*/
|
|
class StudentController extends Crud
|
|
{
|
|
|
|
/**
|
|
* @var Student
|
|
*/
|
|
protected $model = null;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->model = new Student;
|
|
}
|
|
|
|
/**
|
|
* 浏览
|
|
* @return Response
|
|
*/
|
|
public function index(): Response
|
|
{
|
|
return view('student/index');
|
|
}
|
|
|
|
public function select(Request $request): Response
|
|
{
|
|
try {
|
|
|
|
$data = $request->get();
|
|
|
|
$student = \app\common\model\Student::order('id asc');
|
|
if (isset($data['id']) && $data['id']) {
|
|
$student->where(['id' => $data['id']]);
|
|
}
|
|
|
|
$limit = (int)$request->get('limit', 10);
|
|
$limit = $limit <= 0 ? 10 : $limit;
|
|
$page = (int)$request->get('page');
|
|
$page = $page > 0 ? $page : 1;
|
|
|
|
$total = $student->count();
|
|
$list = $student->page($page, $limit)->with(['parent'])->select();
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'count' => $total,
|
|
'data' => $list,
|
|
'msg' => 'success'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 插入
|
|
* @param Request $request
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function insert(Request $request): Response
|
|
{
|
|
if ($request->method() === 'POST') {
|
|
|
|
$request_data = $request->post();
|
|
$salt = random_str(16);
|
|
if (empty($request_data['password'])) {
|
|
$password = trim(explode(' ', $request_data['account'])[0]) . '001';
|
|
$password = md5($password . $salt);
|
|
} else {
|
|
$password = md5($request_data['password'] . $salt);
|
|
}
|
|
|
|
$res = \app\common\model\Student::create([
|
|
'student_name' => $request_data['student_name'],
|
|
'account' => $request_data['account'],
|
|
'password' => $password,
|
|
'salt' => $salt,
|
|
'avatar' => $request_data['avatar'],
|
|
'parent_id' => $request_data['parent_id'],
|
|
]);
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'msg' => '添加成功'
|
|
]);
|
|
// return parent::insert($request);
|
|
}
|
|
|
|
$parent = StudentParent::order('id asc')->field('id,parent_name,account')->select()->toArray();
|
|
return view('student/insert', ['parent' => $parent]);
|
|
}
|
|
|
|
/**
|
|
* 更新
|
|
* @param Request $request
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function update(Request $request): Response
|
|
{
|
|
if ($request->method() === 'POST') {
|
|
try {
|
|
$request_data = $request->post();
|
|
|
|
$student = \app\common\model\Student::where(['id' => $request_data['id']])->findOrEmpty();
|
|
if ($student->isEmpty()) {
|
|
throw new Exception('未找到学生信息,操作失败');
|
|
}
|
|
|
|
if (empty($request_data['password'])) {
|
|
$update = [
|
|
'student_name' => $request_data['student_name'],
|
|
'account' => $request_data['account'],
|
|
'avatar' => $request_data['avatar'],
|
|
'parent_id' => $request_data['parent_id'],
|
|
];
|
|
} else {
|
|
$salt = random_str(16);
|
|
$password = md5($request_data['password'] . $salt);
|
|
$update = [
|
|
'student_name' => $request_data['student_name'],
|
|
'account' => $request_data['account'],
|
|
'password' => $password,
|
|
'salt' => $salt,
|
|
'avatar' => $request_data['avatar'],
|
|
'parent_id' => $request_data['parent_id'],
|
|
];
|
|
}
|
|
|
|
$student->save($update);
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'msg' => 'success'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
|
|
// return parent::update($request);
|
|
}
|
|
|
|
$parent = StudentParent::order('id asc')->field('id,parent_name,account')->select()->toArray();
|
|
return view('student/update', ['parent' => $parent]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 查看课表
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function checkSchedule(Request $request)
|
|
{
|
|
$data = $request->get();
|
|
$student = \app\common\model\Student::where(['id' => $data['student_id']])->findOrEmpty();
|
|
return view('student/check_schedule', ['student_id' => $data['student_id']]);
|
|
}
|
|
|
|
/**
|
|
* @desc 获取学生排课月份总览
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function getStudentScheduleMonth(Request $request)
|
|
{
|
|
try {
|
|
$data = $request->get();
|
|
$schedule_time = StudentSchedule::where(['student_id' => $data['student_id']])
|
|
->field('
|
|
student_id,
|
|
month,
|
|
COUNT(id) AS total_courses,
|
|
SUM(hour) AS total_hours,
|
|
|
|
SUM(CASE WHEN is_publish = 1 THEN 1 ELSE 0 END) AS published_courses,
|
|
SUM(CASE WHEN is_publish = 0 THEN 1 ELSE 0 END) AS unpublished_courses
|
|
')
|
|
->group('month')
|
|
->with(['student']);
|
|
|
|
$limit = (int)$request->get('limit', 10);
|
|
$limit = $limit <= 0 ? 10 : $limit;
|
|
$page = (int)$request->get('page');
|
|
$page = $page > 0 ? $page : 1;
|
|
|
|
$total = $schedule_time->count();
|
|
$list = $schedule_time->page($page, $limit)->select();
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'data' => $list,
|
|
'count' => $total,
|
|
'msg' => 'success',
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @desc 重置密码
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function resetPassword(Request $request)
|
|
{
|
|
try {
|
|
$request_data = $request->post();
|
|
$student = \app\common\model\Student::where(['id' => $request_data['id']])->findOrEmpty();
|
|
$new_password = trim(explode(' ', $student->account)[0]) . '001';
|
|
$salt = random_str(16);
|
|
$password = md5($new_password . $salt);
|
|
|
|
$res = $student->save([
|
|
'salt' => $salt,
|
|
'password' => $password
|
|
]);
|
|
if (!$res) {
|
|
throw new Exception('重置失败');
|
|
}
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'msg' => '重置成功'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
}
|