course/plugin/admin/app/controller/StudentController.php

445 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace plugin\admin\app\controller;
2024-07-16 23:51:41 +08:00
use app\common\model\StudentParent;
2024-07-17 11:36:07 +08:00
use app\common\model\StudentSchedule;
use app\constant\ResponseCode;
2024-08-03 23:25:22 +08:00
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
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
{
2024-08-03 23:25:22 +08:00
// 导入模板文件
$import_example = 'https://lxpk.lingruikj.com/files/xlsx/202408/2024080322474292.xlsx';
$example_name = '学生家长导入模板数据.xlsx';
return view('student/index', ['import_example' => $import_example, 'example_name' => $example_name]);
}
2024-07-16 23:51:41 +08:00
public function select(Request $request): Response
{
try {
$data = $request->get();
2024-08-03 23:25:22 +08:00
$student = \app\common\model\Student::order('id desc');
2024-07-16 23:51:41 +08:00
if (isset($data['id']) && $data['id']) {
$student->where(['id' => $data['id']]);
}
if (isset($data['account']) && $data['account']) {
$student->where('account', 'like', '%' . $data['account'] . '%');
2024-08-03 23:25:22 +08:00
}
if (isset($data['student_name']) && $data['student_name']) {
$student->where('student_name', 'like', '%' . $data['student_name'] . '%');
2024-08-03 23:25:22 +08:00
}
2024-07-16 23:51:41 +08:00
$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 (check_chinese_chars($request_data['account'])){
throw new Exception('账号中不能包含中文');
}
if (empty($request_data['password'])) {
2024-07-16 18:02:04 +08:00
$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'],
2024-07-16 23:51:41 +08:00
'parent_id' => $request_data['parent_id'],
]);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => '添加成功'
]);
// return parent::insert($request);
}
2024-07-16 23:51:41 +08:00
$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 (check_chinese_chars($request_data['account'])){
throw new Exception('账号中不能包含中文');
2025-01-18 15:04:13 +08:00
}
if (empty($request_data['password'])) {
$update = [
'student_name' => $request_data['student_name'],
'account' => $request_data['account'],
'avatar' => $request_data['avatar'],
2024-07-16 23:51:41 +08:00
'parent_id' => $request_data['parent_id'],
'show_schedule' => isset($request_data['show_schedule']) ? $request_data['show_schedule'] : 0,
'show_homework' => isset($request_data['show_homework']) ? $request_data['show_homework'] : 0,
'show_feedback' => isset($request_data['show_feedback']) ? $request_data['show_feedback'] : 0,
];
} 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'],
2024-07-16 23:51:41 +08:00
'parent_id' => $request_data['parent_id'],
'show_schedule' => isset($request_data['show_schedule']) ? $request_data['show_schedule'] : 0,
'show_homework' => isset($request_data['show_homework']) ? $request_data['show_homework'] : 0,
'show_feedback' => isset($request_data['show_feedback']) ? $request_data['show_feedback'] : 0,
];
}
$student->save($update);
if ($request_data['student_name']) {
//更改其他表中所有学生名称
StudentSchedule::where(['student_id' => $request_data['id']])->save(['student_name' => $request_data['student_name']]);
}
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);
}
2024-07-16 23:51:41 +08:00
2024-08-08 15:42:41 +08:00
$parent = StudentParent::order('id desc')->field('id,parent_name,account')->select()->toArray();
2024-07-16 23:51:41 +08:00
return view('student/update', ['parent' => $parent]);
}
2024-07-17 11:36:07 +08:00
/**
* @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()
]);
}
}
2024-09-19 10:24:34 +08:00
/**
2024-09-19 10:37:24 +08:00
* @desc 重置openid
2024-09-19 10:24:34 +08:00
* @param Request $request
* @return Response
*/
public function resetOpenid(Request $request)
{
try {
$request_data = $request->post();
$student = \app\common\model\Student::where(['id' => $request_data['id']])->findOrEmpty();
if ($student->isEmpty()) {
2024-09-19 10:24:34 +08:00
throw new Exception('未找到学生信息');
}
$res = $student->save([
2024-09-19 15:56:17 +08:00
'openid' => '',
'avatar' => '',
2024-09-19 10:24:34 +08:00
]);
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()
]);
}
}
/**
* @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
]);
2024-07-16 23:51:41 +08:00
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()
]);
}
}
2024-08-03 23:25:22 +08:00
/**
* @desc 导入教师数据
* @param Request $request
* @return Response
*/
public function importStudentAccount(Request $request)
{
try {
ini_set('memory_limit', '256M');
$file = $request->file('file');
if ($file && $file->isValid()) {
$ext = $file->getUploadExtension();
if (!in_array($ext, ['xlsx', 'xls', 'csv'])) {
throw new Exception('上传文件格式错误');
}
$uploadDir = '/files/xlsx/' . date('Ym') . '/';
$filename = date('YmdHis') . rand(10, 99);
$uploadPath = $uploadDir . $filename . '.' . $ext;
$rootUploadDir = public_path($uploadPath);
$file->move($rootUploadDir);
//读取表格数据,插入数据库
$objRead = IOFactory::createReader('Xlsx');
if (!$objRead->canRead($rootUploadDir)) {
/** @var Xls $objRead */
$objRead = IOFactory::createReader('Xls');
if (!$objRead->canRead($rootUploadDir)) {
throw new Exception('只支持导入Excel文件');
}
}
/* 如果不需要获取特殊操作则只读内容可以大幅度提升读取Excel效率 */
$objRead->setReadDataOnly(true);
/* 建立excel对象 */
$obj = $objRead->load($rootUploadDir);
/* 获取指定的sheet表 */
$currSheet = $obj->getSheet(0);
/* 取得最大的列号 */
$columnH = $currSheet->getHighestColumn();
/* 兼容原逻辑,循环时使用的是小于等于 */
$columnCnt = Coordinate::columnIndexFromString($columnH);
/* 获取总行数 */
$rowCnt = $currSheet->getHighestRow();
if ($rowCnt <= 1) {
throw new Exception('不能上传空数据');
}
$success_count = 0;
$error_count = 0;
$msg = '';
2025-01-18 15:04:13 +08:00
$check_msg = '';
2024-08-03 23:25:22 +08:00
// 遍历每一行
for ($row = 2; $row <= $rowCnt; $row++) {
// 遍历每一列
$account = trim($currSheet->getCell('A' . $row)->getValue() ?: '');
$student_name = trim($currSheet->getCell('B' . $row)->getValue() ?: '');
2025-01-18 15:04:13 +08:00
if(check_chinese_chars($account)){
$check_msg .= '【'+ $account +'】';
2025-01-18 15:04:13 +08:00
}
if ($account && $student_name) {
2024-08-03 23:25:22 +08:00
$password = trim($currSheet->getCell('C' . $row)->getValue() ?: '');
$salt = random_str(16);
if (empty($password)) {
$password = trim(explode(' ', $account)[0]) . '001';
}
//查找教师数据是否存在
$parent = \app\common\model\Student::where(['account' => $account, 'student_name' => $student_name])->findOrEmpty();
if ($parent->isEmpty()) {
$password = md5($password . $salt);
\app\common\model\Student::create([
'account' => $account,
'student_name' => $student_name,
'password' => $password,
'salt' => $salt,
]);
$success_count++;
} else {
$error_count++;
$msg .= '【' . $student_name . '】';
}
}
}
}
$return_msg = '成功【' . $success_count . '】条,' . '失败【' . $error_count . '】条';
if ($error_count) {
$return_msg .= ',失败数据' . $msg . '已存在';
}
2025-01-18 15:04:13 +08:00
if($check_msg){
$return_msg .= ',包含中文字符数据' . $check_msg;
}
2024-08-03 23:25:22 +08:00
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'data' => [],
'msg' => $return_msg
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
}
}