You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
4.1 KiB
Java
80 lines
4.1 KiB
Java
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.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 com.sztzjy.linkCommerce.service.HardwareCostStepFourService;
|
|
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 HardwareCostStepFourServiceImpl implements HardwareCostStepFourService {
|
|
private static final String TASK_KEY = "hardware-cost";
|
|
private static final int MAX_OPTIMIZATION_PLAN_LENGTH = 3000;
|
|
|
|
@Autowired
|
|
private StudentTrainingAnswerMapper studentTrainingAnswerMapper;
|
|
@Autowired
|
|
private TeachingClassStudentMapper teachingClassStudentMapper;
|
|
@Autowired
|
|
private TrainingTaskMapper trainingTaskMapper;
|
|
|
|
@Override
|
|
public HardwareCostStepFourValidationResult validate(HardwareCostStepFourValidationRequest request, JwtUser user) {
|
|
requireStudent(user);
|
|
requirePreviousStepsCompleted(user);
|
|
|
|
String smallBatchChartUrl = request == null ? "" : StringUtils.trimToEmpty(request.getSmallBatchChartUrl());
|
|
String largeBatchChartUrl = request == null ? "" : StringUtils.trimToEmpty(request.getLargeBatchChartUrl());
|
|
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.");
|
|
}
|
|
if (optimizationPlan.length() > MAX_OPTIMIZATION_PLAN_LENGTH) {
|
|
throw new ServiceException(HttpStatus.BAD_REQUEST,
|
|
"The cost optimisation decision cannot exceed " + MAX_OPTIMIZATION_PLAN_LENGTH + " characters.");
|
|
}
|
|
|
|
HardwareCostStepFourValidationResult result = new HardwareCostStepFourValidationResult();
|
|
result.setValid(true);
|
|
result.setMessage("Cost structure charts and optimisation decision have been validated.");
|
|
return result;
|
|
}
|
|
|
|
private void requirePreviousStepsCompleted(JwtUser user) {
|
|
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.");
|
|
}
|
|
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.");
|
|
}
|
|
}
|
|
|
|
private void requireStudent(JwtUser user) {
|
|
if (user == null || StringUtils.isBlank(user.getUserId())) {
|
|
throw new ServiceException(HttpStatus.UNAUTHORIZED, "Please log in first.");
|
|
}
|
|
if (user.getRoleId() != 4) {
|
|
throw new ServiceException(HttpStatus.FORBIDDEN, "Only students can validate training answers.");
|
|
}
|
|
}
|
|
}
|