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.TargetUserProfileStepTwoValidationRequest; import com.sztzjy.linkCommerce.entity.dto.TargetUserProfileStepTwoValidationResult; import com.sztzjy.linkCommerce.service.StudentTrainingAnswerService; import com.sztzjy.linkCommerce.service.TargetUserProfileStepTwoService; 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 TargetUserProfileStepTwoServiceImpl implements TargetUserProfileStepTwoService { private static final String TASK_KEY = "target-user-profile"; private static final Set REQUIRED_CHART_TYPES = new LinkedHashSet<>(Arrays.asList("segment-share", "average-spend", "score-heatmap")); @Autowired private StudentTrainingAnswerService studentTrainingAnswerService; @Override public TargetUserProfileStepTwoValidationResult validate(TargetUserProfileStepTwoValidationRequest request, JwtUser user) { if (user == null || StringUtils.isBlank(user.getUserId())) { throw new ServiceException(HttpStatus.UNAUTHORIZED, "请先登录后再校验"); } if (user.getRoleId() != 4) { throw new ServiceException(HttpStatus.FORBIDDEN, "仅学生可进行实训校验"); } StudentTrainingAnswer savedAnswer = studentTrainingAnswerService.get(TASK_KEY, null, user); if (savedAnswer == null || StringUtils.isBlank(savedAnswer.getStep1Answer())) { throw new ServiceException(HttpStatus.BAD_REQUEST, "请先完成并保存第 1 步 RFM 用户分层分析"); } if (request == null || request.getCharts() == null || request.getCharts().size() != REQUIRED_CHART_TYPES.size()) { throw new ServiceException(HttpStatus.BAD_REQUEST, "请上传三类 RFM 可视化图表"); } Set chartTypes = new LinkedHashSet<>(); for (TargetUserProfileStepTwoValidationRequest.ChartUpload chart : request.getCharts()) { String type = chart == null ? "" : StringUtils.trimToEmpty(chart.getType()); String url = chart == null ? "" : StringUtils.trimToEmpty(chart.getUrl()); String name = chart == null ? "" : StringUtils.trimToEmpty(chart.getName()).toLowerCase(); if (!REQUIRED_CHART_TYPES.contains(type) || !chartTypes.add(type) || StringUtils.isBlank(url) || !(name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".webp"))) { throw new ServiceException(HttpStatus.BAD_REQUEST, "请完整上传客户分层占比饼图、平均消费对比柱状图和 RFM 分数分布热力图"); } } if (!chartTypes.equals(REQUIRED_CHART_TYPES)) { throw new ServiceException(HttpStatus.BAD_REQUEST, "提交的 RFM 图表类型不完整"); } TargetUserProfileStepTwoValidationResult result = new TargetUserProfileStepTwoValidationResult(); result.setValid(true); result.setMessage("校验通过"); return result; } }