需求
在开会前需要在服务端创建投票,用springboot实现
实现
首先创建VoteController处理请求,并使用VoteService处理
@RestController()
@RequestMapping("vote")
public class VoteController {
@Autowired
private VoteService voteService;
@PostMapping("getVoteList")
Msg getVoteList(@RequestParam int deviceId, int meetingId){
return new Msg();
}
@PostMapping("getVoteList")
Msg getVoteList(@RequestParam int deviceId, @RequestParam int meetingId) {
List<VoteVO> voteVOList = voteService.getVoteList(meetingId);
return new Msg(200,"ok",voteVOList);
}
@PostMapping("submitOptions")
Msg submitOption(@RequestParam int voteId, @RequestParam int optionId, @RequestParam int deviceId){
return new Msg();
@PostMapping("submitOption")
Msg submitOption(@RequestParam int voteId, @RequestParam int optionId, @RequestParam int deviceId) {
switch (voteService.submitOption(voteId, optionId, deviceId)) {
case 0:
return new Msg(200, "投票成功", null);
case 1:
return new Msg(200, "禁止对同一选项多次投票", null);
case 2:
return new Msg(200, "本投票为单选,禁止多次投票", null);
default:
return new Msg(200, "未知错误", null);
}
}
}
VoteService具体实现,创建投票
@Component
public class VoteService {
@Autowired
private VoteDao voteDao;
public VoteResultVO getResultByVoteId(int voteId) {
VoteResultVO voteResultVO = new VoteResultVO(voteId);
List<Option> options = voteDao.getOptionsByVoteId(voteId);
for(Option option : options){
List<String> devices = voteDao.getDevicesByOptionId(option.getUid());
voteResultVO.addResItem(option.getUid(),devices.size(),devices);
}
return voteResultVO;
}
public List<VoteVO> getVoteList(int meetingId){
List<VoteDO> voteDOList = voteDao.getVoteListByMeetingId(meetingId);
List<VoteVO> voteVOList = new LinkedList<>();
for(VoteDO voteDO : voteDOList) {
voteVOList.add(new VoteVO(voteDO, voteDao.getOptionsByVoteId(voteDO.getUid())));
}
return voteVOList;
}
public int submitOption(int voteId, int optionId, int deviceId){
//检测机制有时间再说吧
voteDao.insertOption(optionId,deviceId);
return 0;
}
}
SQL操作,使用Mybatis对sql进行操作
@Insert("insert into `device_option` values(#{deviceId}, #{optionId})")
int insertOption(int optionId, int deviceId);
@Select("select * from `vote` where meeting_id = #{meetingId}")
List<VoteDO> getVoteListByMeetingId(int meetingId);
@Select("select * from `option` where belongs = #{voteId}")
List<Option> getOptionsByVoteId(int voteId);
@Select("select * from `device_option` where vote_id = #{voteId}")
List<Option> getResultByVoteId(int voteId);
@Select("select * from vote where uid =#{voteId}")
VoteDO getVoteById(int voteId);
@Select("select name from `device` where uid in (select device_id from `device_option` where option_id = #{optionId})")
List<String> getDevicesByOptionId(int optionId);