eric 2 роки тому
батько
коміт
8d58b0955f

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

@@ -27,8 +27,8 @@ public class VoteController {
      * @param dto 投票活动创建数据传输对象
      * @return 接口返回对象
      */
-    @PutMapping("vote_activity")
-    public ApiResponse<Integer> createVoteActivity(@RequestBody VoteActivityCreateDto dto) {
+    @PutMapping("vote_activity/")
+    public ApiResponse<Integer> createVoteActivity(@RequestBody @Valid VoteActivityCreateDto dto) {
         int voteActivityId = voteService.createVoteActivity(dto);
         return ApiResponse.successful(voteActivityId);
     }
@@ -39,8 +39,8 @@ public class VoteController {
      * @param dto 投票活动创建数据传输对象
      * @return 接口返回对象
      */
-    @PostMapping("vote_activity")
-    public ApiResponse<Integer> updateVoteActivity(@RequestBody VoteActivityUpdateDto dto) {
+    @PostMapping("vote_activity/")
+    public ApiResponse<Integer> updateVoteActivity(@RequestBody @Valid VoteActivityUpdateDto dto) {
         voteService.updateVoteActivity(dto);
         return ApiResponse.successful();
     }
@@ -55,7 +55,7 @@ public class VoteController {
      * @param endTime      结束时间
      * @return 接口返回对象
      */
-    @GetMapping("vote_activities/section")
+    @GetMapping("vote_activities/section/")
     public ApiResponse<List<VoteActivityViewDto>> searchVoteActivity(
             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
             @RequestParam(value = "pageCapacity", defaultValue = "10") int pageCapacity,
@@ -72,13 +72,49 @@ public class VoteController {
         return ApiResponse.successful(list);
     }
 
+    /**
+     * 创建投票
+     *
+     * @param dto 投票创建数据传输对象
+     * @return 接口返回对象
+     */
+    @PutMapping("vote/")
+    public ApiResponse<Integer> createVote(@RequestBody @Valid VoteCreateDto dto) {
+        int voteId = voteService.createVote(dto);
+        return ApiResponse.successful(voteId);
+    }
+
+    /**
+     * 更新投票
+     *
+     * @param dto 投票更新数据传输对象
+     * @return 接口返回对象
+     */
+    @PostMapping("vote/")
+    public ApiResponse<Integer> createVote(@RequestBody @Valid VoteUpdateDto dto) {
+        voteService.updateVote(dto);
+        return ApiResponse.successful();
+    }
+
+    /**
+     * 依据投票活动 ID 获取投票列表
+     *
+     * @param activityId 投票活动 ID
+     * @return 接口返回对象
+     */
+    @GetMapping(" votes/")
+    public ApiResponse<List<VoteViewDto>> searchVote(@RequestParam("activityId") Integer activityId) {
+        List<VoteViewDto> votes = voteService.searchVotes(activityId);
+        return ApiResponse.successful(votes);
+    }
+
     /**
      * 投票
      *
      * @param dto 投票数据传输对象
      * @return 接口返回对象
      */
-    @PutMapping("choice")
+    @PutMapping("choice/")
     public ApiResponse<Void> choice(@RequestBody @Valid ChoiceDto dto) {
         voteService.madeChoice(dto);
         return ApiResponse.successful();

+ 6 - 1
src/main/java/com/finikes/oc/vote/dao/OptionDao.java

@@ -2,12 +2,17 @@ package com.finikes.oc.vote.dao;
 
 import com.finikes.oc.vote.entity.Option;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
 @Mapper
 public interface OptionDao extends BaseDao<Option, Integer> {
 
-    int bulkInsert(List<Option> entities);
+    int deleteByVoteId(Integer voteId);
+
+    int bulkInsert(@Param("entities") List<Option> entities);
+
+    List<Option> selectByVoteIds(@Param("voteIds") List<Integer> voteIds);
 
 }

+ 5 - 0
src/main/java/com/finikes/oc/vote/dao/VoteDao.java

@@ -2,8 +2,13 @@ package com.finikes.oc.vote.dao;
 
 import com.finikes.oc.vote.entity.Vote;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 @Mapper
 public interface VoteDao extends BaseDao<Vote, Integer>{
 
+    List<Vote> selectByVoteActivityId(@Param("voteActivityId") Integer voteActivityId);
+
 }

+ 30 - 0
src/main/java/com/finikes/oc/vote/dto/OptionDto.java

@@ -0,0 +1,30 @@
+package com.finikes.oc.vote.dto;
+
+public class OptionDto {
+    private Integer id;
+    private String value;
+
+    @Override
+    public String toString() {
+        return "OptionDto{" +
+                "id=" + id +
+                ", value='" + value + '\'' +
+                '}';
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}

+ 71 - 0
src/main/java/com/finikes/oc/vote/dto/VoteUpdateDto.java

@@ -1,4 +1,75 @@
 package com.finikes.oc.vote.dto;
 
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import java.util.List;
+
 public class VoteUpdateDto {
+
+    @NotNull
+    private Integer id;
+
+    @NotNull
+    private Integer activityId;
+    @NotBlank
+    @Max(256)
+    private String title;
+    @NotBlank
+    @Max(256)
+    private String content;
+    @NotEmpty
+    private List<String> options;
+
+    @Override
+    public String toString() {
+        return "VoteUpdateDto{" +
+                "id=" + id +
+                ", activityId=" + activityId +
+                ", title='" + title + '\'' +
+                ", content='" + content + '\'' +
+                ", options=" + options +
+                '}';
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getActivityId() {
+        return activityId;
+    }
+
+    public void setActivityId(Integer activityId) {
+        this.activityId = activityId;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public List<String> getOptions() {
+        return options;
+    }
+
+    public void setOptions(List<String> options) {
+        this.options = options;
+    }
 }

+ 62 - 0
src/main/java/com/finikes/oc/vote/dto/VoteViewDto.java

@@ -0,0 +1,62 @@
+package com.finikes.oc.vote.dto;
+
+import java.util.List;
+
+public class VoteViewDto {
+    private Integer id;
+    private Integer activityId;
+    private String title;
+    private String content;
+    private List<OptionDto> options;
+
+    @Override
+    public String toString() {
+        return "VoteViewDto{" +
+                "id=" + id +
+                ", activityId=" + activityId +
+                ", title='" + title + '\'' +
+                ", content='" + content + '\'' +
+                ", options=" + options +
+                '}';
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getActivityId() {
+        return activityId;
+    }
+
+    public void setActivityId(Integer activityId) {
+        this.activityId = activityId;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public List<OptionDto> getOptions() {
+        return options;
+    }
+
+    public void setOptions(List<OptionDto> options) {
+        this.options = options;
+    }
+}

+ 3 - 0
src/main/java/com/finikes/oc/vote/mapper/VoteMapper.java

@@ -2,6 +2,7 @@ package com.finikes.oc.vote.mapper;
 
 import com.finikes.oc.vote.dto.VoteCreateDto;
 import com.finikes.oc.vote.dto.VoteUpdateDto;
+import com.finikes.oc.vote.dto.VoteViewDto;
 import com.finikes.oc.vote.entity.Vote;
 import org.mapstruct.Mapper;
 import org.mapstruct.factory.Mappers;
@@ -15,4 +16,6 @@ public interface VoteMapper {
 
     Vote voteUpdateDtoToVote(VoteUpdateDto dto);
 
+    VoteViewDto voteToVoteViewDto(Vote vote);
+
 }

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

@@ -32,10 +32,29 @@ public interface VoteService {
      */
     List<VoteActivityViewDto> searchVoteActivity(VoteActivitySearchDto dto);
 
+    /**
+     * 创建投票
+     *
+     * @param dto 投票创建数据传输对象
+     * @return 主键
+     */
     int createVote(VoteCreateDto dto);
 
+    /**
+     * 更新投票
+     *
+     * @param dto 投票更新数据传输对象
+     */
     void updateVote(VoteUpdateDto dto);
 
+    /**
+     * 依据投票活动 ID 获取投票列表
+     *
+     * @param voteActivityId 投票活动 ID
+     * @return 投票视图数据传输对象列表
+     */
+    List<VoteViewDto> searchVotes(Integer voteActivityId);
+
     /**
      * 投票
      *

+ 58 - 0
src/main/java/com/finikes/oc/vote/service/VoteServiceImpl.java

@@ -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());
     }
 
     /**

+ 30 - 0
src/main/resources/mapper/ChoiceMapper.xml

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.finikes.oc.vote.dao.ChoiceDao">
+
+
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
+            parameterType="com.finikes.oc.vote.entity.Choice">
+        INSERT INTO t_choice (option_id, passport_id, choose_time, proxy, assignee_id)
+        VALUES (#{optionId}, #{passportId}, #{chooseTime}, #{proxy}, #{assigneeId})
+    </insert>
+
+    <update id="update" parameterType="com.finikes.oc.vote.entity.Choice">
+        UPDATE t_choice SET option_id = #{optionId}, passport_id = #{passportId}, choose_time = #{chooseTime},
+        proxy = #{proxy}, assignee_id = #{assigneeId}
+        WHERE id = #{id}
+    </update>
+
+    <select id="selectByPrimaryKey" parameterType="integer" resultType="com.finikes.oc.vote.entity.Choice">
+        SELECT id, option_id, passport_id, choose_time, proxy, assignee_id
+        FROM t_choice
+        WHERE id = #{primaryKey}
+    </select>
+
+    <delete id="deleteByPrimaryKey" parameterType="integer">
+        DELETE
+        FROM t_choice
+        WHERE id = #{primaryKey}
+    </delete>
+
+</mapper>

+ 54 - 0
src/main/resources/mapper/OptionMapper.xml

@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.finikes.oc.vote.dao.OptionDao">
+
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
+            parameterType="com.finikes.oc.vote.entity.Option">
+        INSERT INTO t_option (vote_id, value)
+        VALUES (#{voteId}, #{value})
+    </insert>
+
+    <update id="update" parameterType="com.finikes.oc.vote.entity.Option">
+        UPDATE t_option SET vote_id = #{voteId}, value = #{value}
+        WHERE id = #{id}
+    </update>
+
+    <select id="selectByPrimaryKey" parameterType="integer" resultType="com.finikes.oc.vote.entity.Option">
+        SELECT id, vote_id, value
+        FROM t_option
+        WHERE id = #{primaryKey}
+    </select>
+    <select id="selectByVoteIds" resultType="com.finikes.oc.vote.entity.Option">
+        SELECT id, vote_id, value
+        FROM t_option
+        <where>
+            <if test="voteIds != null and voteIds.size() != 0">
+                vote_id IN
+                <foreach collection="voteIds" item="item" open="(" separator="," close=")">
+                    #{item}
+                </foreach>
+            </if>
+        </where>
+    </select>
+
+    <delete id="deleteByPrimaryKey" parameterType="integer">
+        DELETE
+        FROM t_option
+        WHERE id = #{primaryKey}
+    </delete>
+
+    <delete id="deleteByVoteId" parameterType="integer">
+        DELETE
+        FROM t_option
+        WHERE vote_id = #{voteId}
+    </delete>
+
+    <insert id="bulkInsert" parameterType="com.finikes.oc.vote.entity.Option">
+        INSERT INTO t_option (vote_id, value)
+        VALUES
+        <foreach collection="entities" item="entity" separator=",">
+            (#{voteId}, #{value})
+        </foreach>
+    </insert>
+
+</mapper>

+ 34 - 0
src/main/resources/mapper/VoteMapper.xml

@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.finikes.oc.vote.dao.VoteDao">
+
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
+            parameterType="com.finikes.oc.vote.entity.Vote">
+        INSERT INTO t_vote (activity_id, sequence, title, content)
+        VALUES (#{activityId}, #{sequence}, #{title}, #{content})
+    </insert>
+
+    <update id="update" parameterType="com.finikes.oc.vote.entity.Vote">
+        UPDATE t_vote SET activity_id = #{activityId}, sequence = #{sequence}, title = #{title}, content = #{content}
+        WHERE id = #{id}
+    </update>
+
+    <select id="selectByPrimaryKey" parameterType="integer" resultType="com.finikes.oc.vote.entity.Vote">
+        SELECT id, activity_id, sequence, title, content
+        FROM t_vote
+        WHERE id = #{primaryKey}
+    </select>
+
+    <select id="selectByVoteActivityId" resultType="com.finikes.oc.vote.entity.Vote">
+        SELECT id, activity_id, sequence, title, content
+        FROM t_vote
+        WHERE activity_id = #{voteActivityId}
+    </select>
+
+    <delete id="deleteByPrimaryKey" parameterType="integer">
+        DELETE
+        FROM t_vote
+        WHERE id = #{primaryKey}
+    </delete>
+
+</mapper>