536 lines
18 KiB
PHP
536 lines
18 KiB
PHP
<?php
|
||
|
||
namespace plugin\admin\app\controller;
|
||
|
||
use app\common\model\Student;
|
||
use app\common\model\StudentSchedule;
|
||
use app\common\model\Subject;
|
||
use app\common\model\TeacherScheduleTime;
|
||
use app\common\model\TimeZone;
|
||
use app\common\service\SendMsgCronJobService;
|
||
use app\constant\ResponseCode;
|
||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||
use support\Request;
|
||
use support\Response;
|
||
use plugin\admin\app\model\Teacher;
|
||
use plugin\admin\app\controller\Crud;
|
||
use support\exception\BusinessException;
|
||
use think\Exception;
|
||
|
||
/**
|
||
* 教师管理
|
||
*/
|
||
class TeacherController extends Crud
|
||
{
|
||
|
||
/**
|
||
* @var Teacher
|
||
*/
|
||
protected $model = null;
|
||
|
||
/**
|
||
* 构造函数
|
||
* @return void
|
||
*/
|
||
public function __construct()
|
||
{
|
||
$this->model = new Teacher;
|
||
}
|
||
|
||
/**
|
||
* 浏览
|
||
* @return Response
|
||
*/
|
||
public function index(): Response
|
||
{
|
||
// 导入模板文件
|
||
$import_example = 'https://lxpk.lingruikj.com/files/xlsx/202408/2024080322192257.xlsx';
|
||
$example_name = '教师导入模板数据.xlsx';
|
||
return view('teacher/index', ['import_example' => $import_example, 'example_name' => $example_name]);
|
||
}
|
||
|
||
/**
|
||
* 插入
|
||
* @param Request $request
|
||
* @return Response
|
||
* @throws BusinessException
|
||
*/
|
||
public function insert(Request $request): Response
|
||
{
|
||
if ($request->method() === 'POST') {
|
||
|
||
try {
|
||
$request_data = $request->post();
|
||
|
||
|
||
$salt = random_str(16);
|
||
if (empty($request_data['password'])) {
|
||
$password = 'YD' . $request_data['account'] . '123';
|
||
} else {
|
||
$password = $request_data['password'];
|
||
}
|
||
$password = md5($password . $salt);
|
||
|
||
$timezone = TimeZone::where(['id' => $request_data['time_zone_id']])->findOrEmpty();
|
||
|
||
$data = [
|
||
'account' => $request_data['account'],
|
||
'password' => $password,
|
||
'slat' => $salt,
|
||
'teacher_name' => $request_data['teacher_name'],
|
||
'time_zone_id' => $timezone->id,
|
||
'time_zone_name' => $timezone->name,
|
||
'time_zone_abbr' => $timezone->abbr,
|
||
'time_zone_offset' => $timezone->offset,
|
||
];
|
||
$res = \app\common\model\Teacher::create($data);
|
||
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()
|
||
]);
|
||
}
|
||
|
||
return parent::insert($data);
|
||
}
|
||
|
||
$timezone = TimeZone::order('id asc')->select()->toArray();
|
||
|
||
return view('teacher/insert', ['timezone' => $timezone]);
|
||
}
|
||
|
||
/**
|
||
* 更新
|
||
* @param Request $request
|
||
* @return Response
|
||
* @throws BusinessException
|
||
*/
|
||
public function update(Request $request): Response
|
||
{
|
||
if ($request->method() === 'POST') {
|
||
|
||
try {
|
||
$request_data = $request->post();
|
||
$teacher = \app\common\model\Teacher::where(['id' => $request_data['id']])->findOrEmpty();
|
||
|
||
$time_zone = TimeZone::where(['id' => $request_data['time_zone_id']])->findOrEmpty();
|
||
if (empty($request_data['password'])) {
|
||
$update = [
|
||
'account' => $request_data['account'],
|
||
'teacher_name' => $request_data['teacher_name'],
|
||
'openid' => $request_data['openid'],
|
||
'time_zone_id' => $time_zone->id,
|
||
'time_zone_name' => $time_zone->name,
|
||
'time_zone_offset' => $time_zone->offset,
|
||
];
|
||
} else {
|
||
$salt = random_str(16);
|
||
$password = md5($request_data['password'] . $salt);
|
||
$update = [
|
||
'account' => $request_data['account'],
|
||
'password' => $password,
|
||
'salt' => $salt,
|
||
'teacher_name' => $request_data['teacher_name'],
|
||
'openid' => $request_data['openid'],
|
||
'time_zone_id' => $time_zone->id,
|
||
'time_zone_name' => $time_zone->name,
|
||
'time_zone_offset' => $time_zone->offset,
|
||
];
|
||
}
|
||
|
||
$res = $teacher->save($update);
|
||
if (!$res) {
|
||
throw new Exception('操作失败');
|
||
}
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_SUCCESS,
|
||
'msg' => 'success'
|
||
]);
|
||
|
||
} catch (Exception $e) {
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_FAIL,
|
||
'msg' => $e->getMessage()
|
||
]);
|
||
}
|
||
|
||
$request_data = $request->post();
|
||
|
||
|
||
return parent::update($request);
|
||
}
|
||
|
||
$data = $request->get();
|
||
$teacher = \app\common\model\Teacher::where(['id' => $data['id']])->findOrEmpty();
|
||
//时区
|
||
$time_zone = TimeZone::order('sort desc,id asc')->field('id,name,offset')->select()->toArray();
|
||
return view('teacher/update', ['time_zone' => $time_zone, 'teacher' => $teacher]);
|
||
}
|
||
|
||
|
||
/**
|
||
* @desc 教师空闲时间
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function getTeacherTimeZone(Request $request)
|
||
{
|
||
try {
|
||
$teacher = \app\common\model\Teacher::where(['id' => $request->post('id')])->findOrEmpty();
|
||
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_SUCCESS,
|
||
'data' => $teacher,
|
||
'msg' => 'success',
|
||
]);
|
||
} catch (Exception $e) {
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_FAIL,
|
||
'msg' => $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @desc 查看教师日程安排
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function check_free_time(Request $request)
|
||
{
|
||
|
||
$data = $request->get();
|
||
|
||
return view('teacher/check_free_time', ['teacher_id' => $data['id']]);
|
||
}
|
||
|
||
/**
|
||
* @desc 查看教师日程安排
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function check_schedule(Request $request)
|
||
{
|
||
|
||
$data = $request->get();
|
||
|
||
return view('teacher/check_free_time', ['teacher_id' => $data['id']]);
|
||
}
|
||
|
||
|
||
/**
|
||
* @desc 获取教师课程安排统计
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function getTeacherScheduleMonth(Request $request)
|
||
{
|
||
try {
|
||
$data = $request->get();
|
||
$schedule_time = TeacherScheduleTime::where(['teacher_id' => $data['teacher_id']])
|
||
->field('
|
||
teacher_id,
|
||
month,
|
||
COUNT(id) AS total_courses,
|
||
SUM(hour) AS total_hours,
|
||
SUM(CASE WHEN subject_id > 0 THEN 1 ELSE 0 END) AS has_subject,
|
||
SUM(CASE WHEN subject_id = 0 THEN 1 ELSE 0 END) AS has_no_subject,
|
||
SUM(CASE WHEN subject_id = 0 THEN hour ELSE 0 END) AS total_nonscheduled_hours,
|
||
SUM(CASE WHEN subject_id != 0 THEN hour ELSE 0 END) AS total_scheduled_hours,
|
||
SUM(CASE WHEN subject_id != 0 AND is_publish = 1 THEN hour ELSE 0 END) AS total_published_scheduled_hours,
|
||
SUM(CASE WHEN subject_id != 0 AND is_publish = 0 THEN hour ELSE 0 END) AS total_unpublished_scheduled_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(['teacher']);
|
||
|
||
$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' => 'ok'
|
||
]);
|
||
} catch (Exception $e) {
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_FAIL,
|
||
'msg' => $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @desc 教师月份课程安排日历
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function teacherScheduleTime(Request $request)
|
||
{
|
||
|
||
$data = $request->get();
|
||
$teacher_id = $data['teacher_id'];
|
||
$month = $data['month'];
|
||
$teacher = \app\common\model\Teacher::where(['id' => $teacher_id])->findOrEmpty();
|
||
//获取该老师当前分配学生
|
||
// $student_schedule = StudentSchedule::where([''])
|
||
|
||
return view('teacher/teacher_schedule_time', ['teacher' => $teacher, 'month' => $month]);
|
||
}
|
||
|
||
|
||
/**
|
||
* 更新
|
||
* @param Request $request
|
||
* @return Response
|
||
* @throws BusinessException
|
||
*/
|
||
public function scheduleTimeSet(Request $request): Response
|
||
{
|
||
if ($request->method() === 'POST') {
|
||
|
||
if ($request->post('is_publish')) {
|
||
$request->post('is_publish', 0);
|
||
}
|
||
// parent::update($request);
|
||
|
||
return parent::update($request);
|
||
}
|
||
$free_time_id = $request->get('free_time_id');
|
||
$schedule_time = \app\common\model\TeacherScheduleTime::where(['id' => $free_time_id])->with(['teacher'])->findOrEmpty()->toArray();
|
||
//课程
|
||
$project = Subject::order('sort desc, id asc')->select()->toArray();
|
||
//所有学生
|
||
$student = Student::order('id asc')->field('id,account,student_name')->select()->toArray();
|
||
$student_schedule = StudentSchedule::where(['teacher_schedule_time_id' => $free_time_id])->select()->toArray();
|
||
$student_schedule_id = [];
|
||
if ($student_schedule) {
|
||
$student_schedule_id = array_column($student_schedule, 'student_id');
|
||
}
|
||
|
||
|
||
return view('teacher/schedule_time_setting', [
|
||
'schedule_time' => $schedule_time,
|
||
'project' => $project,
|
||
'student' => $student,
|
||
'student_schedule_id' => $student_schedule_id]);
|
||
}
|
||
|
||
/**
|
||
* @desc 重置openid
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function resetOpenid(Request $request)
|
||
{
|
||
try {
|
||
$request_data = $request->post();
|
||
$teacher = \app\common\model\Teacher::where(['id' => $request_data['id']])->findOrEmpty();
|
||
|
||
if ($teacher->isEmpty()) {
|
||
throw new Exception('未找到教师信息');
|
||
}
|
||
|
||
$res = $teacher->save(['openid' => '']);
|
||
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();
|
||
$teacher = \app\common\model\Teacher::where(['id' => $request_data['id']])->findOrEmpty();
|
||
$new_password = 'YD' . $teacher->account . '123';
|
||
$salt = random_str(16);
|
||
$password = md5($new_password . $salt);
|
||
|
||
$res = $teacher->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()
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @desc 导入教师数据
|
||
* @param Request $request
|
||
* @return Response
|
||
*/
|
||
public function importTeacherAccount(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 = '';
|
||
// 遍历每一行
|
||
for ($row = 2; $row <= $rowCnt; $row++) {
|
||
// 遍历每一列
|
||
|
||
$account = trim($currSheet->getCell('A' . $row)->getValue() ?: '');
|
||
$teacher_name = trim($currSheet->getCell('B' . $row)->getValue() ?: '');
|
||
|
||
if ($account && $teacher_name) {
|
||
|
||
$password = trim($currSheet->getCell('C' . $row)->getValue() ?: '');
|
||
$salt = random_str(16);
|
||
if (empty($password)) {
|
||
$password = 'YD' . $account . '123';
|
||
}
|
||
//查找教师数据是否存在
|
||
$teacher = \app\common\model\Teacher::where(['account' => $account, 'teacher_name' => $teacher_name])->findOrEmpty();
|
||
if ($teacher->isEmpty()) {
|
||
$password = md5($password . $salt);
|
||
\app\common\model\Teacher::create([
|
||
'account' => $account,
|
||
'teacher_name' => $teacher_name,
|
||
'password' => $password,
|
||
'salt' => $salt,
|
||
]);
|
||
$success_count++;
|
||
} else {
|
||
$error_count++;
|
||
$msg .= '【' . $teacher_name . '】';
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
$return_msg = '成功【' . $success_count . '】条,' . '失败【' . $error_count . '】条';
|
||
if ($error_count) {
|
||
$return_msg .= ',失败数据' . $msg . '已存在';
|
||
}
|
||
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_SUCCESS,
|
||
'data' => [],
|
||
'msg' => $return_msg
|
||
]);
|
||
} catch (Exception $e) {
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_FAIL,
|
||
'msg' => $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
public function alterSubmitFreeTime(Request $request)
|
||
{
|
||
try {
|
||
|
||
$res = (new SendMsgCronJobService())->alertTeacherSubmitFreeTime($request->post('id'));
|
||
|
||
if ($res['code'] == ResponseCode::FAIL) {
|
||
throw new Exception($res['msg']);
|
||
}
|
||
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_SUCCESS,
|
||
'msg' => '提醒一发送,请到【消息任务结果】查看状态'
|
||
]);
|
||
} catch (Exception $e) {
|
||
return json([
|
||
'code' => ResponseCode::WEB_API_FAIL,
|
||
'msg' => $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
}
|