|
|
@@ -22,6 +22,8 @@ import com.github.pagehelper.PageHelper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -91,7 +93,7 @@ public class VoteServiceImpl implements VoteService {
|
|
|
public List<VoteActivityViewDto> searchVoteActivity(VoteActivitySearchDto dto) {
|
|
|
List<VoteActivity> activities;
|
|
|
|
|
|
- try (Page<VoteActivity> page = PageHelper.startPage(dto.getPageNum(), dto.getPageCapacity(), false)){
|
|
|
+ try (Page<VoteActivity> page = PageHelper.startPage(dto.getPageNum(), dto.getPageCapacity(), false)) {
|
|
|
if (dto.getStartTime() == null || dto.getEndTime() == null) {
|
|
|
activities = voteActivityDao.selectByTitleAndStartTimeAndEndTime(dto.getKey(),
|
|
|
null, null);
|
|
|
@@ -355,4 +357,81 @@ public class VoteServiceImpl implements VoteService {
|
|
|
lockService.unlock(houseResourceId, ownerId);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ 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()) {
|
|
|
+ throw new BusinessException("参与的投票不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Integer> voteIds = votes.stream().map(Vote::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<Option> options = optionDao.selectByVoteIds(voteIds);
|
|
|
+ Map<Integer, List<Option>> optionMap = new HashMap<>();
|
|
|
+ for (Option option : options) {
|
|
|
+ if (!optionMap.containsKey(option.getVoteId())) {
|
|
|
+ optionMap.put(option.getVoteId(), new ArrayList<>(3));
|
|
|
+ }
|
|
|
+ optionMap.get(option.getVoteId()).add(option);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<OptionSummary> list = voteIds.isEmpty() ? Collections.emptyList() : choiceDao.selectByVoteIds(voteIds);
|
|
|
+ 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());
|
|
|
+
|
|
|
+ List<Option> matchedOptions = optionMap.get(vote.getId());
|
|
|
+ if (matchedOptions == null || matchedOptions.isEmpty()) {
|
|
|
+ resultDto.setItems(Collections.emptyList());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ if (total != 0) {
|
|
|
+ for (VoteResultItemDto item : resultDto.getItems()) {
|
|
|
+ item.setPercent(calcPercent(total, item.getQuantity()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ results.add(resultDto);
|
|
|
+ }
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int calcPercent(int total, int number) {
|
|
|
+ if (total == 0 || number == 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return new BigDecimal(number)
|
|
|
+ .divide(new BigDecimal(total), 2, RoundingMode.HALF_UP)
|
|
|
+ .multiply(new BigDecimal(100)).intValue();
|
|
|
+ }
|
|
|
}
|