feat: validate product value positioning step one
parent
4f4798c42d
commit
9c67878ac7
@ -0,0 +1,31 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ProductValuePositioningStepOneValidationRequest {
|
||||||
|
private List<CorePainRow> rows;
|
||||||
|
|
||||||
|
public List<CorePainRow> getRows() {
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(List<CorePainRow> rows) {
|
||||||
|
this.rows = rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CorePainRow {
|
||||||
|
private Integer id;
|
||||||
|
private String type;
|
||||||
|
private String description;
|
||||||
|
private String scene;
|
||||||
|
|
||||||
|
public Integer getId() { return id; }
|
||||||
|
public void setId(Integer id) { this.id = id; }
|
||||||
|
public String getType() { return type; }
|
||||||
|
public void setType(String type) { this.type = type; }
|
||||||
|
public String getDescription() { return description; }
|
||||||
|
public void setDescription(String description) { this.description = description; }
|
||||||
|
public String getScene() { return scene; }
|
||||||
|
public void setScene(String scene) { this.scene = scene; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package com.sztzjy.linkCommerce.entity.dto;
|
||||||
|
|
||||||
|
public class ProductValuePositioningStepOneValidationResult {
|
||||||
|
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.ProductValuePositioningStepOneValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepOneValidationResult;
|
||||||
|
|
||||||
|
public interface ProductValuePositioningStepOneService {
|
||||||
|
ProductValuePositioningStepOneValidationResult validate(ProductValuePositioningStepOneValidationRequest request, JwtUser user);
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
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.dto.ProductValuePositioningStepOneValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepOneValidationResult;
|
||||||
|
import com.sztzjy.linkCommerce.service.ProductValuePositioningStepOneService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ProductValuePositioningStepOneServiceImpl implements ProductValuePositioningStepOneService {
|
||||||
|
private static final List<String> TYPES = Arrays.asList("核心痛点1", "核心痛点2", "核心痛点3", "弱需求", "伪需求");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ProductValuePositioningStepOneValidationResult validate(ProductValuePositioningStepOneValidationRequest request, JwtUser user) {
|
||||||
|
requireStudent(user);
|
||||||
|
List<ProductValuePositioningStepOneValidationRequest.CorePainRow> rows = request == null ? null : request.getRows();
|
||||||
|
if (rows == null || rows.size() != TYPES.size()) {
|
||||||
|
throw badRequest("请完成 3 个核心痛点、弱需求和伪需求的分析");
|
||||||
|
}
|
||||||
|
for (int index = 0; index < TYPES.size(); index++) {
|
||||||
|
ProductValuePositioningStepOneValidationRequest.CorePainRow row = rows.get(index);
|
||||||
|
if (row == null || row.getId() == null || row.getId() != index + 1 || !TYPES.get(index).equals(row.getType())
|
||||||
|
|| StringUtils.isBlank(row.getDescription()) || StringUtils.isBlank(row.getScene())) {
|
||||||
|
throw badRequest("请按固定分类完整填写需求描述和场景描述");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ProductValuePositioningStepOneValidationResult result = new ProductValuePositioningStepOneValidationResult();
|
||||||
|
result.setValid(true);
|
||||||
|
result.setMessage("核心痛点分类填写完整。");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, "仅学生可以提交实训答案");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServiceException badRequest(String message) {
|
||||||
|
return new ServiceException(HttpStatus.BAD_REQUEST, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
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.dto.ProductValuePositioningStepOneValidationRequest;
|
||||||
|
import com.sztzjy.linkCommerce.entity.dto.ProductValuePositioningStepOneValidationResult;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class ProductValuePositioningStepOneServiceImplTest {
|
||||||
|
private final ProductValuePositioningStepOneServiceImpl service = new ProductValuePositioningStepOneServiceImpl();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void acceptsCompleteFixedPainCategories() {
|
||||||
|
ProductValuePositioningStepOneValidationResult result = service.validate(request(), student());
|
||||||
|
|
||||||
|
assertTrue(result.isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsChangedCategoryOrder() {
|
||||||
|
ProductValuePositioningStepOneValidationRequest request = request();
|
||||||
|
request.getRows().get(0).setType("弱需求");
|
||||||
|
|
||||||
|
assertThrows(ServiceException.class, () -> service.validate(request, student()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsBlankScene() {
|
||||||
|
ProductValuePositioningStepOneValidationRequest request = request();
|
||||||
|
request.getRows().get(2).setScene(" ");
|
||||||
|
|
||||||
|
assertThrows(ServiceException.class, () -> service.validate(request, student()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProductValuePositioningStepOneValidationRequest request() {
|
||||||
|
List<String> types = Arrays.asList("核心痛点1", "核心痛点2", "核心痛点3", "弱需求", "伪需求");
|
||||||
|
List<ProductValuePositioningStepOneValidationRequest.CorePainRow> rows = new ArrayList<>();
|
||||||
|
for (int index = 0; index < types.size(); index++) {
|
||||||
|
ProductValuePositioningStepOneValidationRequest.CorePainRow row = new ProductValuePositioningStepOneValidationRequest.CorePainRow();
|
||||||
|
row.setId(index + 1);
|
||||||
|
row.setType(types.get(index));
|
||||||
|
row.setDescription("需求描述" + (index + 1));
|
||||||
|
row.setScene("场景描述" + (index + 1));
|
||||||
|
rows.add(row);
|
||||||
|
}
|
||||||
|
ProductValuePositioningStepOneValidationRequest request = new ProductValuePositioningStepOneValidationRequest();
|
||||||
|
request.setRows(rows);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JwtUser student() {
|
||||||
|
JwtUser user = new JwtUser();
|
||||||
|
user.setUserId("student-id");
|
||||||
|
user.setRoleId(4);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue