Merge branch 'test-training2' of https://git.code.tencent.com/lian-cbtc/rtss-server into test-training2-xzb1

This commit is contained in:
xzb 2022-09-19 18:09:44 +08:00
commit 8b7f9ef947

View File

@ -28,6 +28,7 @@ import org.springframework.util.StringUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -355,29 +356,29 @@ public class Training2DraftService {
} }
} }
/** /**
* 修改实训所有步骤 * 修改实训所有步骤
*/ */
@Transactional(rollbackFor = Exception.class)
public void updateSteps(String group, Long trainingId, List<Step2VO> step2VOList) { public void updateSteps(String group, Long trainingId, List<Step2VO> step2VOList) {
DraftTraining2WithBLOBs draftTraining2 = trainingDao.selectByPrimaryKey(trainingId); DraftTraining2WithBLOBs draftTraining2 = trainingDao.selectByPrimaryKey(trainingId);
if (draftTraining2 == null) { if (draftTraining2 == null) {
throw new SimulationException(SimulationExceptionType.Data_Not_Exist, "实训不存在"); throw new SimulationException(SimulationExceptionType.Data_Not_Exist, "实训不存在");
} }
AtomicInteger operationId = new AtomicInteger(0); // 操作ID
// 设置Operation的Id
step2VOList.stream().filter(s -> !CollectionUtils.isEmpty(s.getOperations())).forEach(s -> {
operationId.set(0);
s.getOperations().forEach(o -> o.setId(operationId.getAndIncrement()));
});
// 保存当前背景 // 保存当前背景
Simulation simulation = this.roupSimulationService.getSimulationByGroup(group); Simulation simulation = this.roupSimulationService.getSimulationByGroup(group);
DraftTraining2WithBLOBs updateObj = new DraftTraining2WithBLOBs(); DraftTraining2WithBLOBs updateObj = new DraftTraining2WithBLOBs();
updateObj.setId(trainingId); updateObj.setId(trainingId);
String stepJson;
if (CollectionUtils.isEmpty(step2VOList)) { if (CollectionUtils.isEmpty(step2VOList)) {
updateObj.setStepJson(StringUtil.EMPTY_STRING); updateObj.setStepJson(StringUtil.EMPTY_STRING);
updateObj.setScoringRuleJson(StringUtil.EMPTY_STRING);
} else { } else {
stepJson = JsonUtils.writeValueAsString(step2VOList); String scoringRuleJson = handleStepScoringRule(draftTraining2, step2VOList);
updateObj.setScoringRuleJson(scoringRuleJson);
// 处理步骤跟操作ID
handleStepAndOperationId(getStartStepId(draftTraining2), step2VOList);
updateObj.setStepJson(JsonUtils.writeValueAsString(step2VOList));
// 扮演者集合 // 扮演者集合
List<String> playerList = step2VOList.stream(). List<String> playerList = step2VOList.stream().
filter(s -> !StringUtils.isEmpty(s.getMemberId())).map(s -> s.getMemberId()) filter(s -> !StringUtils.isEmpty(s.getMemberId())).map(s -> s.getMemberId())
@ -386,7 +387,6 @@ public class Training2DraftService {
throw new SimulationException(SimulationExceptionType.Data_Not_Exist, "错误数据:无扮演者"); throw new SimulationException(SimulationExceptionType.Data_Not_Exist, "错误数据:无扮演者");
} }
updateObj.setPlayerIdJson(JsonUtils.writeValueAsString(playerList)); updateObj.setPlayerIdJson(JsonUtils.writeValueAsString(playerList));
updateObj.setStepJson(stepJson);
} }
// 背景 // 背景
StorageSimulation scenesSaving = new StorageSimulation(simulation, false); StorageSimulation scenesSaving = new StorageSimulation(simulation, false);
@ -410,6 +410,7 @@ public class Training2DraftService {
updateObj.setStepJson(StringUtil.EMPTY_STRING); updateObj.setStepJson(StringUtil.EMPTY_STRING);
updateObj.setPlayerIdJson(StringUtil.EMPTY_STRING); updateObj.setPlayerIdJson(StringUtil.EMPTY_STRING);
updateObj.setFinalScenesJson(StringUtil.EMPTY_STRING); updateObj.setFinalScenesJson(StringUtil.EMPTY_STRING);
updateObj.setScoringRuleJson(StringUtil.EMPTY_STRING);
trainingDao.updateByPrimaryKeySelective(updateObj); trainingDao.updateByPrimaryKeySelective(updateObj);
} }
@ -481,4 +482,63 @@ public class Training2DraftService {
List<String> playerIds = JsonUtils.readCollection(playerIdJson, List.class, String.class); List<String> playerIds = JsonUtils.readCollection(playerIdJson, List.class, String.class);
return playerIds.stream().map(memberMap::get).collect(Collectors.toList()); return playerIds.stream().map(memberMap::get).collect(Collectors.toList());
} }
/**
* 保存步骤时获取步骤起始ID
*/
private AtomicInteger getStartStepId(DraftTraining2WithBLOBs draftTraining2) {
Integer curStepId;
if (StringUtils.isEmpty(draftTraining2.getStepJson())) {
curStepId = 0;
} else {
// 数据库中的步骤信息
List<Step2VO> oldStep2VOS = JsonUtils.readCollection(draftTraining2.getStepJson(), List.class, Step2VO.class);
curStepId = oldStep2VOS.stream().map(Step2VO::getId).max(Comparator.comparing(Integer::intValue)).get();
}
return new AtomicInteger(curStepId + 1);
}
/**
* 处理步骤跟操作ID
*/
private void handleStepAndOperationId(AtomicInteger startStepId, List<Step2VO> step2VOList) {
AtomicInteger operationId = new AtomicInteger(0); // 操作ID
step2VOList.stream().forEach(s -> {
if (s.getId() == null) {
s.setId(startStepId.getAndIncrement());
}
// 设置Operation的Id
operationId.set(0);
if (!CollectionUtils.isEmpty(s.getOperations())) {
s.getOperations().forEach(o -> o.setId(operationId.getAndIncrement()));
}
});
}
/**
* 处理已经保存的打分规则
*/
private String handleStepScoringRule(DraftTraining2WithBLOBs draftTraining2, List<Step2VO> step2VOList) {
// 当前编辑后的步骤
Map<String, List<String>> curStepIdMap = step2VOList.stream().filter(s -> s.getId() != null)
.collect(Collectors.groupingBy(Step2VO::getMemberId, Collectors.mapping(s -> s.getId().toString(), Collectors.toList())));
if (StringUtils.isEmpty(draftTraining2.getScoringRuleJson()) || CollectionUtils.isEmpty(curStepIdMap)) {
return StringUtil.EMPTY_STRING;
} else {
// 比对删除打分规则
List<ScoringRuleVO> scoringRuleList = JsonUtils.readCollection(draftTraining2.getScoringRuleJson(), List.class, ScoringRuleVO.class);
scoringRuleList.stream().filter(s -> curStepIdMap.containsKey(s.getMemberId())).map(s -> {
List<ScoringRuleVO.DetailVO> details = s.getDetails().stream()
.filter(d -> curStepIdMap.get(s.getMemberId()).contains(d.getElementId()))
.collect(Collectors.toList());
s.setDetails(details);
if (CollectionUtils.isEmpty(details)) {
return null;
} else {
return s;
}
}).filter(s -> s != null).collect(Collectors.toList());
return CollectionUtils.isEmpty(scoringRuleList) ? StringUtil.EMPTY_STRING : JsonUtils.writeValueAsString(scoringRuleList);
}
}
} }