package com.sztzjy.linkCommerce.service.impl; import com.sztzjy.linkCommerce.config.exception.handler.ServiceException; import com.sztzjy.linkCommerce.config.security.JwtUser; import com.sztzjy.linkCommerce.entity.SchoolClass; import com.sztzjy.linkCommerce.entity.TeachingClassStudent; import com.sztzjy.linkCommerce.mapper.SchoolClassMapper; import com.sztzjy.linkCommerce.mapper.TeachingClassStudentMapper; import com.sztzjy.linkCommerce.service.StudentTeachingClassResolver; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @Service public class StudentTeachingClassResolverImpl implements StudentTeachingClassResolver { @Autowired public SchoolClassMapper schoolClassMapper; @Autowired public TeachingClassStudentMapper teachingClassStudentMapper; @Override public String resolveRequired(JwtUser user) { requireStudent(user); String teachingClassId; if (user.isDemoMode()) { teachingClassId = StringUtils.trimToNull(user.getDemoTeachingClassId()); if (teachingClassId == null) { throw new ServiceException(HttpStatus.FORBIDDEN, "Demo teaching class is required"); } } else { TeachingClassStudent membership = teachingClassStudentMapper.selectActiveByStudentUserId(user.getUserId()); teachingClassId = membership == null ? null : StringUtils.trimToNull(membership.getTeachingClassId()); if (teachingClassId == null) { throw new ServiceException(HttpStatus.BAD_REQUEST, "Student has no active teaching class"); } } validateTeachingClass(user, teachingClassId); return teachingClassId; } @Override public String resolveRequested(JwtUser user, String requestedTeachingClassId) { String resolved = resolveRequired(user); String requested = StringUtils.trimToNull(requestedTeachingClassId); if (requested != null && !StringUtils.equals(resolved, requested)) { throw new ServiceException(HttpStatus.FORBIDDEN, "Teaching class does not match current student context"); } return resolved; } private void validateTeachingClass(JwtUser user, String teachingClassId) { SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(teachingClassId); if (schoolClass == null || !"TEACHING".equals(schoolClass.getClassType()) || !StringUtils.equals(user.getSchoolId(), schoolClass.getSchoolId())) { throw new ServiceException(HttpStatus.FORBIDDEN, "Teaching class is unavailable"); } } private void requireStudent(JwtUser user) { if (user == null || user.getRoleId() != 4 || StringUtils.isBlank(user.getUserId())) { throw new ServiceException(HttpStatus.FORBIDDEN, "Student only"); } } }