|
|
@@ -358,22 +358,13 @@ public class VoteServiceImpl implements VoteService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<VoteResultDto> voteResult(Integer voteActivityId) {
|
|
|
- VoteActivity activity = voteActivityDao.selectByPrimaryKey(voteActivityId);
|
|
|
- if (activity == null) {
|
|
|
- throw new BusinessException("参与的投票不存在");
|
|
|
- }
|
|
|
- List<Vote> votes = voteDao.selectByVoteActivityId(voteActivityId)
|
|
|
- .stream()
|
|
|
- .sorted(Comparator.comparingInt(Vote::getSequence))
|
|
|
- .collect(Collectors.toList());
|
|
|
- if (votes.isEmpty()) {
|
|
|
+ public VoteResultDto voteResult(Integer voteId) {
|
|
|
+ Vote vote = voteDao.selectByPrimaryKey(voteId);
|
|
|
+ if (vote == null) {
|
|
|
throw new BusinessException("参与的投票不存在");
|
|
|
}
|
|
|
|
|
|
- List<Integer> voteIds = votes.stream().map(Vote::getId).collect(Collectors.toList());
|
|
|
-
|
|
|
- List<Option> options = optionDao.selectByVoteIds(voteIds);
|
|
|
+ List<Option> options = optionDao.selectByVoteIds(Collections.singletonList(voteId));
|
|
|
Map<Integer, List<Option>> optionMap = new HashMap<>();
|
|
|
for (Option option : options) {
|
|
|
if (!optionMap.containsKey(option.getVoteId())) {
|
|
|
@@ -382,48 +373,44 @@ public class VoteServiceImpl implements VoteService {
|
|
|
optionMap.get(option.getVoteId()).add(option);
|
|
|
}
|
|
|
|
|
|
- List<OptionSummary> list = voteIds.isEmpty() ? Collections.emptyList() : choiceDao.selectByVoteIds(voteIds);
|
|
|
+ List<OptionSummary> list = choiceDao.selectByVoteIds(Collections.singletonList(voteId));
|
|
|
Map<Integer, OptionSummary> summaryMap = new HashMap<>();
|
|
|
for (OptionSummary summary : list) {
|
|
|
summaryMap.put(summary.getOptionId(), summary);
|
|
|
}
|
|
|
|
|
|
- List<VoteResultDto> results = new ArrayList<>(votes.size());
|
|
|
- for (Vote vote : votes) {
|
|
|
- VoteResultDto resultDto = new VoteResultDto();
|
|
|
- resultDto.setVoteId(vote.getId());
|
|
|
+ VoteResultDto resultDto = new VoteResultDto();
|
|
|
+ resultDto.setVoteId(vote.getId());
|
|
|
|
|
|
- List<Option> matchedOptions = optionMap.get(vote.getId());
|
|
|
- if (matchedOptions == null || matchedOptions.isEmpty()) {
|
|
|
- resultDto.setItems(Collections.emptyList());
|
|
|
- continue;
|
|
|
- }
|
|
|
+ List<Option> matchedOptions = optionMap.get(vote.getId());
|
|
|
+ if (matchedOptions == null || matchedOptions.isEmpty()) {
|
|
|
+ resultDto.setItems(Collections.emptyList());
|
|
|
+ return resultDto;
|
|
|
+ }
|
|
|
|
|
|
- resultDto.setItems(new ArrayList<>());
|
|
|
- int total = 0;
|
|
|
- for (Option option : matchedOptions) {
|
|
|
- VoteResultItemDto item = new VoteResultItemDto();
|
|
|
-
|
|
|
- item.setOptionId(option.getId());
|
|
|
- item.setValue(option.getValue());
|
|
|
- item.setPercent(0);
|
|
|
- OptionSummary summary = summaryMap.get(option.getId());
|
|
|
- if (summary == null) {
|
|
|
- item.setQuantity(0);
|
|
|
- } else {
|
|
|
- item.setQuantity(summary.getQuantity());
|
|
|
- total += summary.getQuantity();
|
|
|
- }
|
|
|
- resultDto.getItems().add(item);
|
|
|
+ resultDto.setItems(new ArrayList<>());
|
|
|
+ int total = 0;
|
|
|
+ for (Option option : matchedOptions) {
|
|
|
+ VoteResultItemDto item = new VoteResultItemDto();
|
|
|
+
|
|
|
+ item.setOptionId(option.getId());
|
|
|
+ item.setValue(option.getValue());
|
|
|
+ item.setPercent(0);
|
|
|
+ OptionSummary summary = summaryMap.get(option.getId());
|
|
|
+ if (summary == null) {
|
|
|
+ item.setQuantity(0);
|
|
|
+ } else {
|
|
|
+ item.setQuantity(summary.getQuantity());
|
|
|
+ total += summary.getQuantity();
|
|
|
}
|
|
|
- if (total != 0) {
|
|
|
- for (VoteResultItemDto item : resultDto.getItems()) {
|
|
|
- item.setPercent(calcPercent(total, item.getQuantity()));
|
|
|
- }
|
|
|
+ resultDto.getItems().add(item);
|
|
|
+ }
|
|
|
+ if (total != 0) {
|
|
|
+ for (VoteResultItemDto item : resultDto.getItems()) {
|
|
|
+ item.setPercent(calcPercent(total, item.getQuantity()));
|
|
|
}
|
|
|
- results.add(resultDto);
|
|
|
}
|
|
|
- return results;
|
|
|
+ return resultDto;
|
|
|
}
|
|
|
|
|
|
private static int calcPercent(int total, int number) {
|