|
|
|
@ -9,6 +9,8 @@ use app\model\customer_resources\CustomerResources; |
|
|
|
use app\model\student\Student; |
|
|
|
use app\model\member\Member; |
|
|
|
use app\model\student_label\StudentLabel; |
|
|
|
use app\model\sys\SysUser; |
|
|
|
use think\facade\Cache; |
|
|
|
use think\facade\Db; |
|
|
|
use core\base\BaseService; |
|
|
|
use core\exception\CommonException; |
|
|
|
@ -26,7 +28,7 @@ class StudentService extends BaseService |
|
|
|
{ |
|
|
|
// 获取当前登录用户ID |
|
|
|
$customerId = $this->getUserId(); |
|
|
|
|
|
|
|
|
|
|
|
// 通过客户资源表获取user_id |
|
|
|
$customerResource = (new CustomerResources())->where('id', $customerId)->find(); |
|
|
|
if (!$customerResource) { |
|
|
|
@ -46,10 +48,10 @@ class StudentService extends BaseService |
|
|
|
$student['age'] = $this->calculateAge($student['birthday']); |
|
|
|
$student['gender_text'] = $student['gender'] == 1 ? '男' : '女'; |
|
|
|
$student['headimg'] = $student['headimg'] ? get_image_url($student['headimg']) : ''; |
|
|
|
|
|
|
|
|
|
|
|
// 添加student_id字段,确保前端能正确获取学员ID |
|
|
|
$student['student_id'] = $student['id']; |
|
|
|
|
|
|
|
|
|
|
|
// 获取学员课时统计信息 |
|
|
|
$courseStats = $this->getStudentCourseStats($student['id']); |
|
|
|
$student['course_stats'] = $courseStats; |
|
|
|
@ -85,11 +87,11 @@ class StudentService extends BaseService |
|
|
|
|
|
|
|
// 获取课程统计信息 |
|
|
|
$courseStats = $this->getStudentCourseStats($studentId); |
|
|
|
|
|
|
|
|
|
|
|
// 获取预约资格信息 |
|
|
|
$bookingService = new \app\service\api\student\CourseBookingService(); |
|
|
|
$qualification = $bookingService->checkStudentQualification($studentId); |
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
'student_id' => $student['id'], |
|
|
|
'name' => $student['name'], |
|
|
|
@ -134,10 +136,10 @@ class StudentService extends BaseService |
|
|
|
} |
|
|
|
|
|
|
|
$studentData = $student->toArray(); |
|
|
|
|
|
|
|
|
|
|
|
// 处理图片URL |
|
|
|
$studentData['headimg'] = $studentData['headimg'] ? get_image_url($studentData['headimg']) : ''; |
|
|
|
|
|
|
|
|
|
|
|
// 计算年龄 |
|
|
|
$studentData['age'] = $this->calculateAge($studentData['birthday']); |
|
|
|
$studentData['gender_text'] = $studentData['gender'] == 1 ? '男' : '女'; |
|
|
|
@ -208,7 +210,7 @@ class StudentService extends BaseService |
|
|
|
public function updateStudentInfo($data) |
|
|
|
{ |
|
|
|
$studentId = $data['student_id']; |
|
|
|
|
|
|
|
|
|
|
|
// 验证学员权限 |
|
|
|
$this->checkStudentPermission($studentId); |
|
|
|
|
|
|
|
@ -217,7 +219,7 @@ class StudentService extends BaseService |
|
|
|
->where('id', $studentId) |
|
|
|
->where('deleted_at', 0) |
|
|
|
->find(); |
|
|
|
|
|
|
|
|
|
|
|
if (!$student) { |
|
|
|
throw new CommonException('学员信息不存在'); |
|
|
|
} |
|
|
|
@ -225,7 +227,7 @@ class StudentService extends BaseService |
|
|
|
// 允许更新的字段 |
|
|
|
$allowedFields = ['name', 'gender', 'birthday', 'emergency_contact', 'contact_phone', 'note', 'headimg']; |
|
|
|
$updateData = []; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($allowedFields as $field) { |
|
|
|
if (isset($data[$field]) && $data[$field] !== '') { |
|
|
|
$updateData[$field] = $data[$field]; |
|
|
|
@ -244,7 +246,7 @@ class StudentService extends BaseService |
|
|
|
$result = Db::table('school_student') |
|
|
|
->where('id', $studentId) |
|
|
|
->update($updateData); |
|
|
|
|
|
|
|
|
|
|
|
if ($result === false) { |
|
|
|
throw new CommonException('更新学员信息失败'); |
|
|
|
} |
|
|
|
@ -317,17 +319,17 @@ class StudentService extends BaseService |
|
|
|
private function calculateAge($birthday) |
|
|
|
{ |
|
|
|
if (!$birthday) return 0; |
|
|
|
|
|
|
|
|
|
|
|
$birthTime = strtotime($birthday); |
|
|
|
if (!$birthTime) return 0; |
|
|
|
|
|
|
|
|
|
|
|
$age = date('Y') - date('Y', $birthTime); |
|
|
|
|
|
|
|
|
|
|
|
// 如果还没过生日,年龄减1 |
|
|
|
if (date('md') < date('md', $birthTime)) { |
|
|
|
$age--; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return max(0, $age); |
|
|
|
} |
|
|
|
|
|
|
|
@ -339,18 +341,18 @@ class StudentService extends BaseService |
|
|
|
private function calculateAgeFromBirthday($birthday) |
|
|
|
{ |
|
|
|
if (!$birthday) return 0; |
|
|
|
|
|
|
|
|
|
|
|
$birthTime = strtotime($birthday); |
|
|
|
if (!$birthTime) return 0; |
|
|
|
|
|
|
|
|
|
|
|
$today = new \DateTime(); |
|
|
|
$birthDate = new \DateTime($birthday); |
|
|
|
|
|
|
|
|
|
|
|
$interval = $today->diff($birthDate); |
|
|
|
|
|
|
|
|
|
|
|
$years = $interval->y; |
|
|
|
$months = $interval->m; |
|
|
|
|
|
|
|
|
|
|
|
// 将月份转换为小数,如3岁11个月 = 3.11 |
|
|
|
return $years + ($months / 100); |
|
|
|
} |
|
|
|
@ -363,7 +365,7 @@ class StudentService extends BaseService |
|
|
|
public function addChild($data) |
|
|
|
{ |
|
|
|
$customerId = $this->getUserId(); |
|
|
|
|
|
|
|
|
|
|
|
// 创建学员数据 |
|
|
|
$studentData = [ |
|
|
|
'user_id' => $customerId, |
|
|
|
@ -383,7 +385,7 @@ class StudentService extends BaseService |
|
|
|
try { |
|
|
|
// 插入学员数据 |
|
|
|
$studentId = Db::table('school_student')->insertGetId($studentData); |
|
|
|
|
|
|
|
|
|
|
|
if (!$studentId) { |
|
|
|
throw new CommonException('添加孩子失败'); |
|
|
|
} |
|
|
|
@ -394,7 +396,7 @@ class StudentService extends BaseService |
|
|
|
'gender_text' => $data['gender'] == 1 ? '男' : '女', |
|
|
|
'age' => $this->calculateAge($data['birthday']) |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
throw new CommonException('添加孩子失败:' . $e->getMessage()); |
|
|
|
} |
|
|
|
@ -414,22 +416,22 @@ class StudentService extends BaseService |
|
|
|
['pcs.deleted_at', '=', 0], |
|
|
|
['cs.deleted_at', '=', 0] |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
// 日期筛选 |
|
|
|
if (!empty($params['date'])) { |
|
|
|
$where[] = ['cs.course_date', '=', $params['date']]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 日期范围筛选 |
|
|
|
if (!empty($params['start_date']) && !empty($params['end_date'])) { |
|
|
|
$where[] = ['cs.course_date', 'between', [$params['start_date'], $params['end_date']]]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 状态筛选 |
|
|
|
if (isset($params['status']) && $params['status'] !== '') { |
|
|
|
$where[] = ['pcs.status', '=', $params['status']]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 查询课程安排数据,联合两个表 |
|
|
|
$scheduleList = Db::table('school_person_course_schedule pcs') |
|
|
|
->leftJoin('school_course_schedule cs', 'pcs.schedule_id = cs.id') |
|
|
|
@ -464,12 +466,12 @@ class StudentService extends BaseService |
|
|
|
->order('cs.course_date desc, cs.start_time desc') |
|
|
|
->select() |
|
|
|
->toArray(); |
|
|
|
|
|
|
|
|
|
|
|
// 处理数据格式 |
|
|
|
foreach ($scheduleList as &$schedule) { |
|
|
|
// 状态处理 |
|
|
|
$schedule['status_text'] = $this->getScheduleStatusText($schedule['status']); |
|
|
|
|
|
|
|
|
|
|
|
// 时间处理 |
|
|
|
if (!$schedule['start_time'] || !$schedule['end_time']) { |
|
|
|
// 如果没有具体时间,从time_slot中解析 |
|
|
|
@ -478,20 +480,20 @@ class StudentService extends BaseService |
|
|
|
$schedule['start_time'] = $times[0] ?? '09:00'; |
|
|
|
$schedule['end_time'] = $times[1] ?? '10:00'; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$schedule['time_slot'] = $schedule['start_time'] . '-' . $schedule['end_time']; |
|
|
|
$schedule['duration'] = $schedule['duration'] ?: 60; |
|
|
|
|
|
|
|
|
|
|
|
// 准备事项(模拟数据,实际可从课程信息中获取) |
|
|
|
$schedule['preparation_items'] = $this->getCoursePreparationItems($schedule['course_name']); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
'list' => $scheduleList, |
|
|
|
'total' => count($scheduleList) |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取课程安排详情 |
|
|
|
* @param int $scheduleId |
|
|
|
@ -536,23 +538,23 @@ class StudentService extends BaseService |
|
|
|
TIMESTAMPDIFF(MINUTE, cs.start_time, cs.end_time) as duration |
|
|
|
') |
|
|
|
->find(); |
|
|
|
|
|
|
|
|
|
|
|
if (!$schedule) { |
|
|
|
throw new CommonException('课程安排不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 验证权限 |
|
|
|
$this->checkStudentPermission($schedule['student_id']); |
|
|
|
|
|
|
|
|
|
|
|
// 处理数据格式 |
|
|
|
$schedule['status_text'] = $this->getScheduleStatusText($schedule['status']); |
|
|
|
$schedule['time_slot'] = $schedule['start_time'] . '-' . $schedule['end_time']; |
|
|
|
$schedule['duration'] = $schedule['duration'] ?: 60; |
|
|
|
$schedule['preparation_items'] = $this->getCoursePreparationItems($schedule['course_name']); |
|
|
|
|
|
|
|
|
|
|
|
return $schedule; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 申请课程请假 |
|
|
|
* @param array $data |
|
|
|
@ -562,41 +564,41 @@ class StudentService extends BaseService |
|
|
|
{ |
|
|
|
$scheduleId = $data['schedule_id']; |
|
|
|
$reason = $data['reason'] ?? ''; |
|
|
|
|
|
|
|
|
|
|
|
// 查询课程安排 |
|
|
|
$schedule = Db::table('school_person_course_schedule') |
|
|
|
->where('id', $scheduleId) |
|
|
|
->where('deleted_at', 0) |
|
|
|
->find(); |
|
|
|
|
|
|
|
|
|
|
|
if (!$schedule) { |
|
|
|
throw new CommonException('课程安排不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 验证权限 |
|
|
|
$this->checkStudentPermission($schedule['student_id']); |
|
|
|
|
|
|
|
|
|
|
|
// 检查课程状态 |
|
|
|
if ($schedule['status'] != 0) { // 0-待上课 |
|
|
|
throw new CommonException('当前课程状态不允许请假'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取对应的课程安排信息来检查时间 |
|
|
|
$courseSchedule = Db::table('school_course_schedule') |
|
|
|
->where('id', $schedule['schedule_id']) |
|
|
|
->find(); |
|
|
|
|
|
|
|
|
|
|
|
if ($courseSchedule) { |
|
|
|
// 检查请假时间限制(上课前6小时) |
|
|
|
$courseDateTime = $courseSchedule['course_date'] . ' ' . $courseSchedule['start_time']; |
|
|
|
$courseTimestamp = strtotime($courseDateTime); |
|
|
|
$currentTimestamp = time(); |
|
|
|
|
|
|
|
|
|
|
|
if ($courseTimestamp - $currentTimestamp < 6 * 3600) { |
|
|
|
throw new CommonException('上课前6小时内不允许请假'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新状态为请假 |
|
|
|
$result = Db::table('school_person_course_schedule') |
|
|
|
->where('id', $scheduleId) |
|
|
|
@ -605,16 +607,16 @@ class StudentService extends BaseService |
|
|
|
'cancel_reason' => $reason, |
|
|
|
'updated_at' => date('Y-m-d H:i:s') |
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
if ($result === false) { |
|
|
|
throw new CommonException('请假申请失败'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: 发送消息通知相关人员 |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取课程状态文本 |
|
|
|
* @param int $status |
|
|
|
@ -624,14 +626,14 @@ class StudentService extends BaseService |
|
|
|
{ |
|
|
|
$statusMap = [ |
|
|
|
0 => '待上课', |
|
|
|
1 => '已完成', |
|
|
|
1 => '已完成', |
|
|
|
2 => '请假', |
|
|
|
3 => '取消' |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
return $statusMap[$status] ?? '未知状态'; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取课程准备事项 |
|
|
|
* @param string $courseName |
|
|
|
@ -650,7 +652,7 @@ class StudentService extends BaseService |
|
|
|
'儿童游泳' => ['泳衣', '泳帽', '游泳镜', '毛巾'], |
|
|
|
'暑季特训营' => ['运动服装', '防晒用品', '充足水分', '能量补充食品'] |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
return $preparationMap[$courseName] ?? ['运动服装', '运动鞋', '毛巾', '水杯']; |
|
|
|
} |
|
|
|
|
|
|
|
@ -689,10 +691,10 @@ class StudentService extends BaseService |
|
|
|
|
|
|
|
// 有效课程 = total_hours + gift_hours |
|
|
|
$totalValidHours = $totalHours + $giftHours; |
|
|
|
|
|
|
|
|
|
|
|
// 已使用 = use_total_hours + use_gift_hours |
|
|
|
$usedHours = $useTotalHours + $useGiftHours; |
|
|
|
|
|
|
|
|
|
|
|
// 剩余课时数 = total_hours + gift_hours - use_total_hours - use_gift_hours |
|
|
|
$remainingHours = $totalValidHours - $usedHours; |
|
|
|
|
|
|
|
@ -714,7 +716,7 @@ class StudentService extends BaseService |
|
|
|
if ($memberId) { |
|
|
|
return $memberId; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果没有中间件设置,尝试解析token |
|
|
|
$token = request()->header('token'); |
|
|
|
if ($token) { |
|
|
|
@ -729,7 +731,7 @@ class StudentService extends BaseService |
|
|
|
throw new CommonException('用户未登录或token无效'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果都没有,抛出异常 |
|
|
|
throw new CommonException('用户未登录'); |
|
|
|
} |
|
|
|
@ -756,19 +758,19 @@ class StudentService extends BaseService |
|
|
|
$studentId = $params['student_id']; |
|
|
|
$startDate = $params['start_date']; |
|
|
|
$endDate = $params['end_date']; |
|
|
|
|
|
|
|
|
|
|
|
// 构建查询条件 |
|
|
|
$where = [ |
|
|
|
['pcs.student_id', '=', $studentId], |
|
|
|
['pcs.deleted_at', '=', 0], |
|
|
|
['cs.deleted_at', '=', 0] |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
// 日期筛选 |
|
|
|
if (!empty($startDate) && !empty($endDate)) { |
|
|
|
$where[] = ['cs.course_date', 'between', [$startDate, $endDate]]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 查询课程安排数据 |
|
|
|
$scheduleList = Db::table('school_person_course_schedule pcs') |
|
|
|
->leftJoin('school_course_schedule cs', 'pcs.schedule_id = cs.id') |
|
|
|
@ -776,13 +778,13 @@ class StudentService extends BaseService |
|
|
|
->field('pcs.status') |
|
|
|
->select() |
|
|
|
->toArray(); |
|
|
|
|
|
|
|
|
|
|
|
// 统计各状态数量 |
|
|
|
$totalCourses = count($scheduleList); |
|
|
|
$completedCourses = 0; |
|
|
|
$scheduledCourses = 0; |
|
|
|
$cancelledCourses = 0; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($scheduleList as $schedule) { |
|
|
|
switch ($schedule['status']) { |
|
|
|
case 1: // 已完成 |
|
|
|
@ -797,7 +799,7 @@ class StudentService extends BaseService |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
'total_courses' => $totalCourses, |
|
|
|
'completed_courses' => $completedCourses, |
|
|
|
@ -805,4 +807,68 @@ class StudentService extends BaseService |
|
|
|
'cancelled_courses' => $cancelledCourses |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function checkMemberOldPwd(string $old_passowrd) |
|
|
|
{ |
|
|
|
$res = [ |
|
|
|
'code' => 0, |
|
|
|
'msg' => '操作失败', |
|
|
|
'data' => [] |
|
|
|
]; |
|
|
|
$customerResources = new CustomerResources(); |
|
|
|
|
|
|
|
$personnel_id = $this->getUserId(); |
|
|
|
$member = new Member(); |
|
|
|
$phone = $customerResources->where('id', $personnel_id)->value('phone_number'); |
|
|
|
|
|
|
|
$password = $customerResources->where('id', $personnel_id)->value('password'); |
|
|
|
|
|
|
|
if (!check_password($old_passowrd, $password)) { |
|
|
|
$res['msg'] = '旧密码错误'; |
|
|
|
return $res; |
|
|
|
} |
|
|
|
$res['code'] = 1; |
|
|
|
$res['msg'] = '密码正确'; |
|
|
|
$res['data'] = [ |
|
|
|
'key_value' => $this->setEditPasswordKey($phone) |
|
|
|
]; |
|
|
|
return $res; |
|
|
|
} |
|
|
|
|
|
|
|
public function updatePassword($new_password){ |
|
|
|
$customerResources = new CustomerResources(); |
|
|
|
|
|
|
|
$personnel_id = $this->getUserId(); |
|
|
|
|
|
|
|
$update = $customerResources->where('id', $personnel_id)->update([ |
|
|
|
'password' => create_password($new_password) |
|
|
|
]); |
|
|
|
if (!$update) { |
|
|
|
$res = [ |
|
|
|
'code' => 0, |
|
|
|
'msg' => '操作失败', |
|
|
|
'data' => [] |
|
|
|
]; |
|
|
|
} else { |
|
|
|
$res = [ |
|
|
|
'code' => 1, |
|
|
|
'msg' => '操作成功', |
|
|
|
'data' => [] |
|
|
|
]; |
|
|
|
} |
|
|
|
return $res; |
|
|
|
} |
|
|
|
|
|
|
|
public function setEditPasswordKey(string $phone) |
|
|
|
{ |
|
|
|
$key_name = 'edit_password_' . $phone; |
|
|
|
//生成字符串,存入cache中 |
|
|
|
//check_password()//验证 |
|
|
|
//create_password()//创建 |
|
|
|
|
|
|
|
$key_value = create_password($key_name); |
|
|
|
// 缓存在3600秒之后过期 |
|
|
|
Cache::set($key_name, $key_value, 3600); |
|
|
|
return $key_value; |
|
|
|
} |
|
|
|
} |
|
|
|
|