Explorar el Código

Merge remote-tracking branch 'origin/master'

finikes hace 2 años
padre
commit
aed4ef165b

+ 3 - 3
src/main/java/com/finikes/oc/vote/controller/VoteController.java

@@ -156,9 +156,9 @@ public class VoteController {
     }
 
     @GetMapping("/vote/result")
-    public ApiResponse<List<VoteResultDto>> voteResult(@RequestParam("activityId") Integer activityId) {
-        List<VoteResultDto> list = voteService.voteResult(activityId);
-        return ApiResponse.successful(list);
+    public ApiResponse<VoteResultDto> voteResult(@RequestParam("voteId") Integer voteId) {
+        VoteResultDto dto = voteService.voteResult(voteId);
+        return ApiResponse.successful(dto);
     }
 
 

+ 1 - 1
src/main/java/com/finikes/oc/vote/service/VoteService.java

@@ -69,5 +69,5 @@ public interface VoteService {
      */
     void madeChoiceDelegation(ChoiceDelegationDto dto);
 
-    List<VoteResultDto> voteResult(Integer voteActivityId);
+    VoteResultDto voteResult(Integer voteId);
 }

+ 32 - 45
src/main/java/com/finikes/oc/vote/service/VoteServiceImpl.java

@@ -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) {

+ 1 - 1
src/main/resources/mapper/ChoiceMapper.xml

@@ -38,7 +38,7 @@
     </delete>
 
     <select id="selectByVoteIds" resultType="com.finikes.oc.vote.dto.OptionSummary">
-        SELECT count(1), option_id, value, vote_id
+        SELECT count(1) quantity, option_id, value, vote_id
         FROM t_option,
         t_choice
         WHERE t_option.vote_id IN

+ 9 - 3
src/main/resources/mapper/HouseRelationMapper.xml

@@ -37,9 +37,15 @@
     <select id="selectByState" resultType="com.finikes.oc.management.HouseRelationViewDto">
         SELECT passportId,
         houseId,
-        state,
+        t_house_relation.state,
         certificateNo,
-        certificateUrl
-        FROM t_house_relation WHERE state = #{state}
+        certificateUrl,
+        name realName
+        FROM t_house_relation,
+        t_passport
+        WHERE t_passport.id = t_house_relation.passportId
+        <if test="state != null">
+            AND t_house_relation.state = #{state}
+        </if>
     </select>
 </mapper>

+ 31 - 0
src/test/java/com/finikes/oc/management/dao/HouseRelationDAOTest.java

@@ -0,0 +1,31 @@
+package com.finikes.oc.management.dao;
+
+import com.finikes.oc.App;
+import com.finikes.oc.management.HouseRelationViewDto;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = App.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class HouseRelationDAOTest {
+
+    @Autowired
+    private HouseRelationDAO houseRelationDAO;
+
+    @Test
+    void selectByState() {
+        List<HouseRelationViewDto> viewDtos = houseRelationDAO.selectByState(null);
+        viewDtos.forEach(System.out::println);
+    }
+}

+ 3 - 1
src/test/java/com/finikes/oc/vote/dao/ChoiceDaoTest.java

@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -28,7 +29,8 @@ class ChoiceDaoTest {
 
     @Test
     void selectByVoteIds() {
-        List<OptionSummary> list = choiceDao.selectByVoteIds(IntStream.range(0, 10).boxed().collect(Collectors.toList()));
+
+        List<OptionSummary> list = choiceDao.selectByVoteIds(Collections.singletonList(3));
         list.forEach(System.out::println);
     }
 }

+ 30 - 0
src/test/java/com/finikes/oc/vote/service/VoteServiceTest.java

@@ -0,0 +1,30 @@
+package com.finikes.oc.vote.service;
+
+import com.finikes.oc.App;
+import com.finikes.oc.vote.dto.VoteResultDto;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = App.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class VoteServiceTest {
+
+    @Autowired
+    private VoteService voteService;
+
+    @Test
+    void voteResult() {
+        List<VoteResultDto> list = voteService.voteResult(17);
+        list.forEach(System.out::println);
+    }
+}