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.

57 lines
3.3 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.dto.IndustryDemandAnalysisStepThreeValidationRequest;
import com.sztzjy.linkCommerce.entity.dto.IndustryDemandAnalysisStepThreeValidationResult;
import com.sztzjy.linkCommerce.service.IndustryDemandAnalysisStepThreeService;
import com.sztzjy.linkCommerce.service.StudentTrainingAnswerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
@Service
public class IndustryDemandAnalysisStepThreeServiceImpl implements IndustryDemandAnalysisStepThreeService {
private static final String TASK_KEY = "industry-demand-analysis";
private static final Set<String> DIMENSIONS = new LinkedHashSet<>(Arrays.asList("political", "economic", "social", "technology"));
@Autowired
private StudentTrainingAnswerService studentTrainingAnswerService;
@Override
public IndustryDemandAnalysisStepThreeValidationResult validate(IndustryDemandAnalysisStepThreeValidationRequest request, JwtUser user) {
requireStudent(user);
StudentTrainingAnswer savedAnswer = studentTrainingAnswerService.get(TASK_KEY, null, user);
if (savedAnswer == null || StringUtils.isBlank(savedAnswer.getStep1Answer()) || StringUtils.isBlank(savedAnswer.getStep2Answer())) {
throw new ServiceException(HttpStatus.BAD_REQUEST, "请先完成并保存前两步的 PEST 信息归类与补充");
}
if (request == null || request.getRows() == null || request.getRows().size() != DIMENSIONS.size()) {
throw badRequest("请完成政治、经济、社会和技术四个维度的分析");
}
Set<String> submitted = new LinkedHashSet<>();
for (IndustryDemandAnalysisStepThreeValidationRequest.Row row : request.getRows()) {
String dimension = row == null ? "" : StringUtils.trimToEmpty(row.getDimension());
if (!DIMENSIONS.contains(dimension) || !submitted.add(dimension)) throw badRequest("提交的 PEST 分析维度不完整或重复");
if (StringUtils.isBlank(row.getOpportunities()) || StringUtils.isBlank(row.getChallenges()) || StringUtils.isBlank(row.getStrategies())) {
throw badRequest("请完成每个 PEST 维度的机遇、挑战和应对思路");
}
}
IndustryDemandAnalysisStepThreeValidationResult result = new IndustryDemandAnalysisStepThreeValidationResult();
result.setValid(true);
result.setMessage("校验通过");
return result;
}
private ServiceException badRequest(String message) { return new ServiceException(HttpStatus.BAD_REQUEST, message); }
private void requireStudent(JwtUser user) {
if (user == null || StringUtils.isBlank(user.getUserId())) throw new ServiceException(HttpStatus.UNAUTHORIZED, "请先登录后再校验");
if (user.getRoleId() != 4) throw new ServiceException(HttpStatus.FORBIDDEN, "仅学生可进行实训校验");
}
}