|
|
@@ -16,10 +16,12 @@ import com.finikes.oc.vote.mapper.VoteMapper;
|
|
|
import com.github.pagehelper.Page;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -101,6 +103,7 @@ public class VoteServiceImpl implements VoteService {
|
|
|
}).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
@Override
|
|
|
public int createVote(VoteCreateDto dto) {
|
|
|
VoteActivity activity = voteActivityDao.selectByPrimaryKey(dto.getActivityId());
|
|
|
@@ -123,9 +126,64 @@ public class VoteServiceImpl implements VoteService {
|
|
|
return vote.getId();
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
@Override
|
|
|
public void updateVote(VoteUpdateDto dto) {
|
|
|
+ VoteActivity activity = voteActivityDao.selectByPrimaryKey(dto.getActivityId());
|
|
|
+ if (activity == null) {
|
|
|
+ throw new BusinessException("投票活动不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Vote vote = VoteMapper.INSTANCE.voteUpdateDtoToVote(dto);
|
|
|
+ int effectRow = voteDao.update(vote);
|
|
|
+ if (effectRow != 1) {
|
|
|
+ throw new BusinessException("投票不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ optionDao.deleteByVoteId(vote.getId());
|
|
|
+
|
|
|
+ List<Option> options = dto.getOptions().stream()
|
|
|
+ .map(value -> {
|
|
|
+ Option option = new Option();
|
|
|
+ option.setVoteId(vote.getId());
|
|
|
+ option.setValue(value);
|
|
|
+ return option;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ optionDao.bulkInsert(options);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<VoteViewDto> searchVotes(Integer voteActivityId) {
|
|
|
+ if (voteActivityId == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Vote> votes = voteDao.selectByVoteActivityId(voteActivityId);
|
|
|
+ if (votes.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Option> options = optionDao.selectByVoteIds(votes.stream()
|
|
|
+ .map(Vote::getId)
|
|
|
+ .collect(Collectors.toList()));
|
|
|
+
|
|
|
+ Map<Integer, List<OptionDto>> map = new HashMap<>();
|
|
|
+ for (Option option : options) {
|
|
|
+ if (!map.containsKey(option.getVoteId())) {
|
|
|
+ map.put(option.getVoteId(), new ArrayList<>(5));
|
|
|
+ }
|
|
|
+ OptionDto optionDto = new OptionDto();
|
|
|
+ optionDto.setId(option.getId());
|
|
|
+ optionDto.setValue(option.getValue());
|
|
|
+
|
|
|
+ map.get(option.getVoteId()).add(optionDto);
|
|
|
+ }
|
|
|
|
|
|
+ return votes.stream()
|
|
|
+ .map(VoteMapper.INSTANCE::voteToVoteViewDto)
|
|
|
+ .peek(i -> i.setOptions(map.get(i.getId())))
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|