diff --git a/vote/src/main/java/com/votesystem/ssl/dao/ActivityDAO.java b/vote/src/main/java/com/votesystem/ssl/dao/ActivityDAO.java index 2ffe9c7..c38ccca 100644 --- a/vote/src/main/java/com/votesystem/ssl/dao/ActivityDAO.java +++ b/vote/src/main/java/com/votesystem/ssl/dao/ActivityDAO.java @@ -58,7 +58,7 @@ public interface ActivityDAO extends JpaRepository, JpaSpecific @Query(value = " select * from tb_activity a where a.state = '2'",nativeQuery = true) List findByState(Pageable pageable); //这个不对 -// @Query(value = "SELECT * FROM tb_activity WHERE state = 1 ",nativeQuery = true) + @Query(value = "SELECT * FROM tb_activity WHERE state != '-1' ",nativeQuery = true) Page findAllByAuthor(String author ,Pageable pageable); diff --git a/vote/src/main/java/com/votesystem/ssl/service/impl/VoteRecordServiceImpl.java b/vote/src/main/java/com/votesystem/ssl/service/impl/VoteRecordServiceImpl.java index 84c2fbb..a756f75 100644 --- a/vote/src/main/java/com/votesystem/ssl/service/impl/VoteRecordServiceImpl.java +++ b/vote/src/main/java/com/votesystem/ssl/service/impl/VoteRecordServiceImpl.java @@ -54,37 +54,40 @@ public class VoteRecordServiceImpl implements IVoteRecordService { @Autowired UserDAO userDAO; - private boolean isInIpRestrictAddress(VoteRecord voteRecord,String ipAddress,long time){ - Map aidMap = (Map) redisUtils.get(ipAddress); +// private boolean isInIpRestrictAddress(VoteRecord voteRecord,String ipAddress,long time){ +// Map aidMap = (Map) redisUtils.get(ipAddress); + +// // Map ipMap = (Map) redisUtils.get(ipAddress); +// if (aidMap == null){ +// aidMap = new HashMap<>(); +// } +// // User user = userDAO.findOneById(voteRecord.getUid()); //获取用户 +// if (aidMap.containsKey(voteRecord.getAid())){ +// return true; +// } else { +// aidMap.put(voteRecord.getAid(),ipAddress); +// redisUtils.set(ipAddress,aidMap,time); +// } +// return false; +// } + + private int isInIpRestrictAddress(String aid, String ipAddress){ + Map aidMap = (Map) redisUtils.get(ipAddress); // Map ipMap = (Map) redisUtils.get(ipAddress); if (aidMap == null){ - aidMap = new HashMap<>(); - } -// User user = userDAO.findOneById(voteRecord.getUid()); //获取用户 - if (aidMap.containsKey(voteRecord.getAid())){ - return true; + return 0; + }else if(aidMap.containsKey(aid)){ + return aidMap.get(aid); // 返回此活动这个ip下最多可以投的总票数 } else { - aidMap.put(voteRecord.getAid(),ipAddress); - redisUtils.set(ipAddress,aidMap,time); + return 0; } - return false; } - private boolean isInIpRestrictAddress(String aid,String ipAddress,long time){ - Map aidMap = (Map) redisUtils.get(ipAddress); -// Map ipMap = (Map) redisUtils.get(ipAddress); - if (aidMap == null){ - aidMap = new HashMap<>(); - } -// User user = userDAO.findOneById(voteRecord.getUid()); //获取用户 - if (aidMap.containsKey(aid)){ - return true; - } else { - aidMap.put(aid,ipAddress); - redisUtils.set(ipAddress,aidMap,time); - } - return false; + private void setCurrentIpuserTotalVotes(String aid, String ipAddress, int userTotalVotes,long time){ + Map aidMap = new HashMap<>(); + aidMap.put(aid,userTotalVotes); + redisUtils.set(ipAddress,aidMap,time); } /** @@ -121,18 +124,31 @@ public class VoteRecordServiceImpl implements IVoteRecordService { } } // ip验证 - if(activity.isIpRestrict()||true){ - if(isInIpRestrictAddress( - voteRecord.getAid(), - request.getRemoteAddr(), - 24*60*60)) { - return ResultFactory.buildFailResult("该IP已投过票"); - } - } + // if(activity.isIpRestrict()||true){ + // if(isInIpRestrictAddress( + // voteRecord.getAid(), + // request.getRemoteAddr(), + // 24*60*60)) { + // return ResultFactory.buildFailResult("该IP已投过票"); + // } + // } //4.如果是非周期性投票 if(typeMap.get("cycleType").equals("false")){ int userTotalVotes = voteRecordDAO.countByAidAndUid(activity.getId(),voteRecord.getUid()); int userOneVotes = voteRecordDAO.countByAidAndCidAndUid(activity.getId(),candidate.getId(),voteRecord.getUid()); + // int result = 0; + // ip验证 + if(activity.isIpRestrict()){ + int result = isInIpRestrictAddress( + voteRecord.getAid(), + request.getRemoteAddr()); + if(0 != result){ + userTotalVotes = result; + if(userTotalVotes >= totalVotes){ + return ResultFactory.buildFailResult("你已投过票或该IP投票次数已达上限") + } + } + } //1.判断总投票数是否超过限制 if(userTotalVotes >= totalVotes){ return ResultFactory.buildFailResult("你已经投过票了"); @@ -148,10 +164,33 @@ public class VoteRecordServiceImpl implements IVoteRecordService { int voteCount = candidate.getVoteCount()+1; candidate.setVoteCount(voteCount); candidateService.updateCandidate(candidate.getId(),candidate); + + if(activity.isIpRestrict()){ + // 保存改ip段下的总投票数 + setCurrentIpuserTotalVotes(voteRecord.getAid(), + request.getRemoteAddr(), + userTotalVotes + 1, + 24*60*60); + } + return ResultFactory.buildSuccessResult("投票成功"); }else{ //4.如果是周期性投票 List userTotalVotes = voteRecordDAO.listToday(voteRecord.getAid(),voteRecord.getUid()); List userOneVotes = voteRecordDAO.listTodayCid(String.valueOf(voteRecord.getAid()),voteRecord.getCid(),voteRecord.getUid()); + + // ip验证 + if(activity.isIpRestrict()){ + int result = isInIpRestrictAddress( + voteRecord.getAid(), + request.getRemoteAddr()); + if(0 != result){ + // userTotalVotes = result; + if(result >= totalVotes){ + return ResultFactory.buildFailResult("你已投过票或该IP投票次数已达上限") + } + } + } + //1.判断今天总投票数是否超过限制 if(userTotalVotes.size() >= totalVotes){ return ResultFactory.buildFailResult("今天投票已经超过限制了,明天再来吧"); @@ -160,6 +199,8 @@ public class VoteRecordServiceImpl implements IVoteRecordService { if(userOneVotes.size() >= oneVotes){ return ResultFactory.buildFailResult("今天为同一人投票超过限制了,支持一下别的选手吧"); } + + //3.满足条件,增加记录,更新候选人票数 voteRecord.setId(idWorker.nextId()+""); voteRecord.setVoteTime(new Date()); @@ -167,6 +208,15 @@ public class VoteRecordServiceImpl implements IVoteRecordService { int voteCount = candidate.getVoteCount()+1; candidate.setVoteCount(voteCount); candidateService.updateCandidate(candidate.getId(),candidate); + + if(activity.isIpRestrict()){ + // 保存改ip段下的总投票数 + setCurrentIpuserTotalVotes(voteRecord.getAid(), + request.getRemoteAddr(), + userTotalVotes.size()+1, + 24*60*60); + } + return ResultFactory.buildSuccessResult("投票成功"); } } @@ -216,6 +266,18 @@ public class VoteRecordServiceImpl implements IVoteRecordService { //1.非周期性投票 if(typeMap.get("cycleType").equals("false")){ int userTotalVotes = voteRecordDAO.countByAidAndUid(activity.getId(),uid); + // ip验证 + if(activity.isIpRestrict()){ + int result = isInIpRestrictAddress( + voteRecord.getAid(), + request.getRemoteAddr()); + if(0 != result){ + userTotalVotes = result; + if(userTotalVotes >= 0){ + return ResultFactory.buildFailResult("你已投过票或该IP投票次数已达上限") + } + } + } if(userTotalVotes > 0){ return ResultFactory.buildFailResult("你已经投过票"); }else { @@ -233,12 +295,35 @@ public class VoteRecordServiceImpl implements IVoteRecordService { int voteCount = candidate.getVoteCount()+1; candidate.setVoteCount(voteCount); candidateService.updateCandidate(candidate.getId(),candidate); + + if(activity.isIpRestrict()){ + // 保存改ip段下的总投票数 + setCurrentIpuserTotalVotes(aid, + request.getRemoteAddr(), + 1, + 24*60*60); + } + } return ResultFactory.buildSuccessResult("投票成功"); } }else { //周期性投票 List vs = voteRecordDAO.listToday(aid,uid); + + // ip验证 + if(activity.isIpRestrict()){ + int result = isInIpRestrictAddress( + voteRecord.getAid(), + request.getRemoteAddr()); + if(0 != result){ + // userTotalVotes = result; + if(result >= totalVotes){ + return ResultFactory.buildFailResult("你已投过票或该IP投票次数已达上限") + } + } + } + if(vs.size() > 0){ return ResultFactory.buildFailResult("今天已经投过票了,明天再来吧!"); }else { @@ -256,6 +341,16 @@ public class VoteRecordServiceImpl implements IVoteRecordService { int voteCount = candidate.getVoteCount()+1; candidate.setVoteCount(voteCount); candidateService.updateCandidate(candidate.getId(),candidate); + + if(activity.isIpRestrict()){ + // 保存改ip段下的总投票数 + setCurrentIpuserTotalVotes(aid, + request.getRemoteAddr(), + vs.size()+1, + 24*60*60); + } + + } return ResultFactory.buildSuccessResult("投票成功"); }