|
@ -1,7 +1,6 @@ |
|
|
package com.lh.service; |
|
|
package com.lh.service; |
|
|
|
|
|
|
|
|
import com.lh.bean.Candidate; |
|
|
import com.lh.bean.Candidate; |
|
|
import com.lh.bean.Voter; |
|
|
|
|
|
import com.lh.exception.MyException; |
|
|
import com.lh.exception.MyException; |
|
|
import com.lh.mapper.CandidatesMapper; |
|
|
import com.lh.mapper.CandidatesMapper; |
|
|
import com.lh.mapper.VoterMapper; |
|
|
import com.lh.mapper.VoterMapper; |
|
@ -12,11 +11,6 @@ import org.springframework.kafka.annotation.EnableKafka; |
|
|
import org.springframework.kafka.annotation.KafkaListener; |
|
|
import org.springframework.kafka.annotation.KafkaListener; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.time.Duration; |
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@EnableKafka |
|
|
@EnableKafka |
|
|
public class VoteConsumer { |
|
|
public class VoteConsumer { |
|
@ -52,13 +46,17 @@ public class VoteConsumer { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 3. 检查用户是否已经为该候选人投过票 |
|
|
// 3. 检查用户是否已经为该候选人投过票 |
|
|
List<Voter> hasVotes = voterMapper.countVotesToday(voterJwcode); |
|
|
|
|
|
//遍历列表,判断是否有记录 |
|
|
|
|
|
for (Voter vote : hasVotes) { |
|
|
|
|
|
if (vote.getCandidateJwCode().equals(candidateJwcode)) { |
|
|
|
|
|
throw new MyException("已投票,可以选择其他人试试哦~"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//List<Voter> hasVotes = voterMapper.countVotesToday(voterJwcode); |
|
|
|
|
|
////遍历列表,判断是否有记录 |
|
|
|
|
|
//boolean flag = true; |
|
|
|
|
|
//for (Voter vote : hasVotes) { |
|
|
|
|
|
// if (vote.getCandidateJwCode().equals(candidateJwcode)) { |
|
|
|
|
|
// flag = false; |
|
|
|
|
|
// } |
|
|
|
|
|
//} |
|
|
|
|
|
//if (!flag){ |
|
|
|
|
|
// throw new MyException("已投票,可以选择其他人试试哦~"); |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
// 4. 增加候选人票数 |
|
|
// 4. 增加候选人票数 |
|
|
if (!candidatesMapper.addVotes(candidateJwcode)) { |
|
|
if (!candidatesMapper.addVotes(candidateJwcode)) { |
|
@ -68,16 +66,7 @@ public class VoteConsumer { |
|
|
// 5. 插入投票记录 |
|
|
// 5. 插入投票记录 |
|
|
voterMapper.insertVote(voterJwcode, candidateJwcode, voterName); |
|
|
voterMapper.insertVote(voterJwcode, candidateJwcode, voterName); |
|
|
|
|
|
|
|
|
// 6. 更新 Redis 中的投票次数 |
|
|
|
|
|
String redisKey = "vote_count:" + voterJwcode + ":" + LocalDateTime.now().toLocalDate(); |
|
|
|
|
|
redisTemplate.opsForValue().increment(redisKey, 1); |
|
|
|
|
|
// 设置 Redis 键的过期时间为当天的23:59:59 |
|
|
|
|
|
LocalDateTime now = LocalDateTime.now(); |
|
|
|
|
|
LocalDateTime endOfDay = now.toLocalDate().atTime(23, 59, 59); |
|
|
|
|
|
long secondsUntilEndOfDay = Duration.between(now, endOfDay).getSeconds(); |
|
|
|
|
|
redisTemplate.expire(redisKey, secondsUntilEndOfDay, TimeUnit.SECONDS); |
|
|
|
|
|
//打印剩余长时间过期 |
|
|
|
|
|
System.out.println("Redis键" + redisKey + "将在" + secondsUntilEndOfDay + "秒后过期。"); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("投票成功!用户:" + voterJwcode + " 投给了 " + candidateJwcode); |
|
|
System.out.println("投票成功!用户:" + voterJwcode + " 投给了 " + candidateJwcode); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|