feat: validate industry demand analysis step three
parent
3a62dbd3a0
commit
154c0e8f42
@ -0,0 +1,26 @@
|
||||
package com.sztzjy.linkCommerce.entity.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IndustryDemandAnalysisStepThreeValidationRequest {
|
||||
private List<Row> rows;
|
||||
|
||||
public List<Row> getRows() { return rows; }
|
||||
public void setRows(List<Row> rows) { this.rows = rows; }
|
||||
|
||||
public static class Row {
|
||||
private String dimension;
|
||||
private String opportunities;
|
||||
private String challenges;
|
||||
private String strategies;
|
||||
|
||||
public String getDimension() { return dimension; }
|
||||
public void setDimension(String dimension) { this.dimension = dimension; }
|
||||
public String getOpportunities() { return opportunities; }
|
||||
public void setOpportunities(String opportunities) { this.opportunities = opportunities; }
|
||||
public String getChallenges() { return challenges; }
|
||||
public void setChallenges(String challenges) { this.challenges = challenges; }
|
||||
public String getStrategies() { return strategies; }
|
||||
public void setStrategies(String strategies) { this.strategies = strategies; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.sztzjy.linkCommerce.entity.dto;
|
||||
|
||||
public class IndustryDemandAnalysisStepThreeValidationResult {
|
||||
private boolean valid;
|
||||
private String message;
|
||||
|
||||
public boolean isValid() { return valid; }
|
||||
public void setValid(boolean valid) { this.valid = valid; }
|
||||
public String getMessage() { return message; }
|
||||
public void setMessage(String message) { this.message = message; }
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.sztzjy.linkCommerce.service;
|
||||
|
||||
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||||
import com.sztzjy.linkCommerce.entity.dto.IndustryDemandAnalysisStepThreeValidationRequest;
|
||||
import com.sztzjy.linkCommerce.entity.dto.IndustryDemandAnalysisStepThreeValidationResult;
|
||||
|
||||
public interface IndustryDemandAnalysisStepThreeService {
|
||||
IndustryDemandAnalysisStepThreeValidationResult validate(IndustryDemandAnalysisStepThreeValidationRequest request, JwtUser user);
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
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, "仅学生可进行实训校验");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
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.StudentTrainingAnswerService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class IndustryDemandAnalysisStepThreeServiceImplTest {
|
||||
@Test
|
||||
void validateAcceptsCompleteAnalysisAfterFirstTwoStepsAreSaved() {
|
||||
IndustryDemandAnalysisStepThreeServiceImpl service = serviceWithFirstTwoSteps();
|
||||
IndustryDemandAnalysisStepThreeValidationResult result = service.validate(completedRequest(), student());
|
||||
assertTrue(result.isValid());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRejectsIncompleteAnalysisAndMissingPrerequisiteAnswers() {
|
||||
IndustryDemandAnalysisStepThreeServiceImpl service = serviceWithFirstTwoSteps();
|
||||
IndustryDemandAnalysisStepThreeValidationRequest incomplete = completedRequest();
|
||||
incomplete.getRows().get(0).setStrategies(" ");
|
||||
assertThrows(ServiceException.class, () -> service.validate(incomplete, student()));
|
||||
|
||||
IndustryDemandAnalysisStepThreeServiceImpl noPrerequisiteService = new IndustryDemandAnalysisStepThreeServiceImpl();
|
||||
StudentTrainingAnswerService answerService = mock(StudentTrainingAnswerService.class);
|
||||
ReflectionTestUtils.setField(noPrerequisiteService, "studentTrainingAnswerService", answerService);
|
||||
when(answerService.get(anyString(), any(), any(JwtUser.class))).thenReturn(new StudentTrainingAnswer());
|
||||
assertThrows(ServiceException.class, () -> noPrerequisiteService.validate(completedRequest(), student()));
|
||||
}
|
||||
|
||||
private IndustryDemandAnalysisStepThreeServiceImpl serviceWithFirstTwoSteps() {
|
||||
IndustryDemandAnalysisStepThreeServiceImpl service = new IndustryDemandAnalysisStepThreeServiceImpl();
|
||||
StudentTrainingAnswerService answerService = mock(StudentTrainingAnswerService.class);
|
||||
StudentTrainingAnswer savedAnswer = new StudentTrainingAnswer();
|
||||
savedAnswer.setStep1Answer("{\"pest\":{}}");
|
||||
savedAnswer.setStep2Answer("{\"supplements\":{}}");
|
||||
when(answerService.get(anyString(), any(), any(JwtUser.class))).thenReturn(savedAnswer);
|
||||
ReflectionTestUtils.setField(service, "studentTrainingAnswerService", answerService);
|
||||
return service;
|
||||
}
|
||||
|
||||
private IndustryDemandAnalysisStepThreeValidationRequest completedRequest() {
|
||||
IndustryDemandAnalysisStepThreeValidationRequest request = new IndustryDemandAnalysisStepThreeValidationRequest();
|
||||
request.setRows(Arrays.asList(row("political"), row("economic"), row("social"), row("technology")));
|
||||
return request;
|
||||
}
|
||||
|
||||
private IndustryDemandAnalysisStepThreeValidationRequest.Row row(String dimension) {
|
||||
IndustryDemandAnalysisStepThreeValidationRequest.Row row = new IndustryDemandAnalysisStepThreeValidationRequest.Row();
|
||||
row.setDimension(dimension);
|
||||
row.setOpportunities("增长机会");
|
||||
row.setChallenges("潜在挑战");
|
||||
row.setStrategies("应对思路");
|
||||
return row;
|
||||
}
|
||||
|
||||
private JwtUser student() { JwtUser user = new JwtUser(); user.setUserId("stu-1"); user.setRoleId(4); return user; }
|
||||
}
|
||||
Loading…
Reference in New Issue