diff --git a/src/main/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImpl.java b/src/main/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImpl.java index 2ec04d9..80d4d83 100644 --- a/src/main/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImpl.java +++ b/src/main/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImpl.java @@ -38,19 +38,19 @@ public class HardwareCostStepFourServiceImpl implements HardwareCostStepFourServ String optimizationPlan = request == null ? "" : StringUtils.trimToEmpty(request.getOptimizationPlan()); if (StringUtils.isBlank(smallBatchChartUrl) || StringUtils.isBlank(largeBatchChartUrl)) { throw new ServiceException(HttpStatus.BAD_REQUEST, - "Please upload both small-batch and mass-production cost structure charts."); + "请分别上传小批量试产和大批量量产成本结构图。"); } if (StringUtils.isBlank(optimizationPlan)) { - throw new ServiceException(HttpStatus.BAD_REQUEST, "Please enter the cost optimisation decision."); + throw new ServiceException(HttpStatus.BAD_REQUEST, "请填写成本优化方案。"); } if (optimizationPlan.length() > MAX_OPTIMIZATION_PLAN_LENGTH) { throw new ServiceException(HttpStatus.BAD_REQUEST, - "The cost optimisation decision cannot exceed " + MAX_OPTIMIZATION_PLAN_LENGTH + " characters."); + "成本优化方案不能超过 " + MAX_OPTIMIZATION_PLAN_LENGTH + " 字。"); } HardwareCostStepFourValidationResult result = new HardwareCostStepFourValidationResult(); result.setValid(true); - result.setMessage("Cost structure charts and optimisation decision have been validated."); + result.setMessage("成本结构图与成本优化方案校验通过。"); return result; } @@ -58,22 +58,22 @@ public class HardwareCostStepFourServiceImpl implements HardwareCostStepFourServ TeachingClassStudent membership = teachingClassStudentMapper.selectActiveByStudentUserId(user.getUserId()); TrainingTask task = trainingTaskMapper.selectByTaskKey(TASK_KEY); if (membership == null || task == null) { - throw new ServiceException(HttpStatus.BAD_REQUEST, "Current training task context was not found."); + throw new ServiceException(HttpStatus.BAD_REQUEST, "未找到当前实训任务信息。"); } StudentTrainingAnswer answer = studentTrainingAnswerMapper.selectByStudentClassAndTask( user.getUserId(), membership.getTeachingClassId(), task.getId()); if (answer == null || StringUtils.isAnyBlank(answer.getStep1Answer(), answer.getStep2Answer(), answer.getStep3Answer())) { throw new ServiceException(HttpStatus.BAD_REQUEST, - "Please save the first three cost-estimation steps before completing Step 4."); + "请先完成并保存前 3 步成本估算内容,再完成第 4 步。"); } } private void requireStudent(JwtUser user) { if (user == null || StringUtils.isBlank(user.getUserId())) { - throw new ServiceException(HttpStatus.UNAUTHORIZED, "Please log in first."); + throw new ServiceException(HttpStatus.UNAUTHORIZED, "请先登录后再操作。"); } if (user.getRoleId() != 4) { - throw new ServiceException(HttpStatus.FORBIDDEN, "Only students can validate training answers."); + throw new ServiceException(HttpStatus.FORBIDDEN, "仅学生可以校验实训答案。"); } } } diff --git a/src/test/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImplTest.java b/src/test/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImplTest.java new file mode 100644 index 0000000..4f697e5 --- /dev/null +++ b/src/test/java/com/sztzjy/linkCommerce/service/impl/HardwareCostStepFourServiceImplTest.java @@ -0,0 +1,66 @@ +package com.sztzjy.linkCommerce.service.impl; + +import com.sztzjy.linkCommerce.config.security.JwtUser; +import com.sztzjy.linkCommerce.entity.StudentTrainingAnswer; +import com.sztzjy.linkCommerce.entity.TeachingClassStudent; +import com.sztzjy.linkCommerce.entity.TrainingTask; +import com.sztzjy.linkCommerce.entity.dto.HardwareCostStepFourValidationRequest; +import com.sztzjy.linkCommerce.entity.dto.HardwareCostStepFourValidationResult; +import com.sztzjy.linkCommerce.mapper.StudentTrainingAnswerMapper; +import com.sztzjy.linkCommerce.mapper.TeachingClassStudentMapper; +import com.sztzjy.linkCommerce.mapper.TrainingTaskMapper; +import org.junit.jupiter.api.Test; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class HardwareCostStepFourServiceImplTest { + @Test + void reportsChineseValidationSuccessAfterChartsAndOptimizationPlanAreComplete() { + HardwareCostStepFourValidationResult result = serviceWithCompletedPreviousSteps().validate(completedRequest(), student()); + + assertTrue(result.isValid()); + assertEquals("成本结构图与成本优化方案校验通过。", result.getMessage()); + } + + private HardwareCostStepFourServiceImpl serviceWithCompletedPreviousSteps() { + HardwareCostStepFourServiceImpl service = new HardwareCostStepFourServiceImpl(); + TeachingClassStudentMapper memberships = mock(TeachingClassStudentMapper.class); + TrainingTaskMapper tasks = mock(TrainingTaskMapper.class); + StudentTrainingAnswerMapper answers = mock(StudentTrainingAnswerMapper.class); + TeachingClassStudent membership = new TeachingClassStudent(); + membership.setTeachingClassId("class-1"); + TrainingTask task = new TrainingTask(); + task.setId("task-16"); + StudentTrainingAnswer answer = new StudentTrainingAnswer(); + answer.setStep1Answer("{}"); + answer.setStep2Answer("{}"); + answer.setStep3Answer("{}"); + when(memberships.selectActiveByStudentUserId("student-1")).thenReturn(membership); + when(tasks.selectByTaskKey("hardware-cost")).thenReturn(task); + when(answers.selectByStudentClassAndTask(anyString(), anyString(), anyString())).thenReturn(answer); + ReflectionTestUtils.setField(service, "teachingClassStudentMapper", memberships); + ReflectionTestUtils.setField(service, "trainingTaskMapper", tasks); + ReflectionTestUtils.setField(service, "studentTrainingAnswerMapper", answers); + return service; + } + + private HardwareCostStepFourValidationRequest completedRequest() { + HardwareCostStepFourValidationRequest request = new HardwareCostStepFourValidationRequest(); + request.setSmallBatchChartUrl("/file/small-batch.png"); + request.setLargeBatchChartUrl("/file/large-batch.png"); + request.setOptimizationPlan("优先优化高成本元器件,并保留关键性能冗余。"); + return request; + } + + private JwtUser student() { + JwtUser user = new JwtUser(); + user.setUserId("student-1"); + user.setRoleId(4); + return user; + } +}