diff --git a/pom.xml b/pom.xml
index 1be26df..baa2950 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.lh
- vote_invest
+ vote_invest_simple
0.0.2
vote_invest
Demo project for Spring Boot
@@ -54,11 +54,6 @@
org.springframework.boot
spring-boot-starter-cache
-
-
- org.springframework.kafka
- spring-kafka
-
org.apache.httpcomponents
diff --git a/src/main/java/com/lh/bean/VoteMessage.java b/src/main/java/com/lh/bean/VoteMessage.java
deleted file mode 100644
index 5433a6d..0000000
--- a/src/main/java/com/lh/bean/VoteMessage.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.lh.bean;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-@Data
-@AllArgsConstructor
-@NoArgsConstructor
-public class VoteMessage {
- private String voterJwcode; // 投票人ID(JWCode)
- private String candidateJwcode; // 候选人ID(JWCode)
- private String voterName; // 投票人姓名
- private String voteTime; // 投票时间
-}
diff --git a/src/main/java/com/lh/controller/VoteController.java b/src/main/java/com/lh/controller/VoteController.java
index 4cc6ffd..777bc0b 100644
--- a/src/main/java/com/lh/controller/VoteController.java
+++ b/src/main/java/com/lh/controller/VoteController.java
@@ -46,4 +46,11 @@ public class VoteController {
public RespBean getVotesByCandidate(@PathVariable("candidateJwcode") String candidateJwcode) throws MyException {
return RespBean.ok("获取成功",voteService.getVotesByCandidate(candidateJwcode));
}
+
+ //重置所有投票记录
+ @GetMapping("/resetVote")
+ public RespBean resetVote() throws MyException {
+ boolean result = voteService.resetVote();
+ return RespBean.ok("重置成功",result);
+ }
}
diff --git a/src/main/java/com/lh/mapper/CandidatesMapper.java b/src/main/java/com/lh/mapper/CandidatesMapper.java
index e8773c6..932309f 100644
--- a/src/main/java/com/lh/mapper/CandidatesMapper.java
+++ b/src/main/java/com/lh/mapper/CandidatesMapper.java
@@ -14,4 +14,7 @@ public interface CandidatesMapper {
Candidate getByCandidateJwcode(@Param("jwcode") String jwcode);
//候选人票数+1
boolean addVotes(@Param("jwcode") String jwcode);
+
+ //重置
+ boolean resetVotes();
}
diff --git a/src/main/java/com/lh/mapper/VoterMapper.java b/src/main/java/com/lh/mapper/VoterMapper.java
index 923106d..97092d8 100644
--- a/src/main/java/com/lh/mapper/VoterMapper.java
+++ b/src/main/java/com/lh/mapper/VoterMapper.java
@@ -14,4 +14,7 @@ public interface VoterMapper {
void insertVote(@Param("jwcode") String jwcode, @Param("candidateJwcode") String candidateJwcode,@Param("name") String namne);
//根据候选人的jwcode查询投票记录
List getVotesByCandidate(@Param("candidateJwcode") String candidateJwcode);
+ //重置
+ boolean resetVote();
+
}
diff --git a/src/main/java/com/lh/service/VoteConsumer.java b/src/main/java/com/lh/service/VoteConsumer.java
deleted file mode 100644
index 1252fd1..0000000
--- a/src/main/java/com/lh/service/VoteConsumer.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package com.lh.service;
-
-import com.lh.bean.Candidate;
-import com.lh.exception.MyException;
-import com.lh.mapper.CandidatesMapper;
-import com.lh.mapper.VoterMapper;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.StringRedisTemplate;
-import org.springframework.kafka.annotation.EnableKafka;
-import org.springframework.kafka.annotation.KafkaListener;
-import org.springframework.stereotype.Service;
-
-@Service
-@EnableKafka
-public class VoteConsumer {
- @Autowired
- private VoterMapper voterMapper;
- @Autowired
- private CandidatesMapper candidatesMapper;
- @Autowired
- private StringRedisTemplate redisTemplate;
-
- @KafkaListener(topics = "vote-topic", groupId = "vote-group")
- public void handleVoteMessage(ConsumerRecord record) throws MyException {
- String message = record.value();
-
- // 将消息分割为投票信息
- String[] parts = message.split(",");
- String voterJwcode = parts[0];
- String candidateJwcode = parts[1];
- String voterName = parts[2];
- String voteTime = parts[3];
-
- // 处理投票
- processVote(voterJwcode, candidateJwcode, voterName, voteTime);
- }
-
- private boolean processVote(String voterJwcode, String candidateJwcode, String voterName, String voteTime) throws MyException {
- // 获取候选人信息
- Candidate candidate = candidatesMapper.getByCandidateJwcode(candidateJwcode);
-
- // 2. 获取候选人信息
- if (candidate == null) {
- throw new MyException("候选人不存在!");
- }
-
- // 3. 检查用户是否已经为该候选人投过票
- //List hasVotes = voterMapper.countVotesToday(voterJwcode);
- ////遍历列表,判断是否有记录
- //boolean flag = true;
- //for (Voter vote : hasVotes) {
- // if (vote.getCandidateJwCode().equals(candidateJwcode)) {
- // flag = false;
- // }
- //}
- //if (!flag){
- // throw new MyException("已投票,可以选择其他人试试哦~");
- //}
-
- // 4. 增加候选人票数
- if (!candidatesMapper.addVotes(candidateJwcode)) {
- throw new MyException ("候选人票数更新失败,请联系管理员!");
- }
-
- // 5. 插入投票记录
- voterMapper.insertVote(voterJwcode, candidateJwcode, voterName);
-
-
- System.out.println("投票成功!用户:" + voterJwcode + " 投给了 " + candidateJwcode);
- return true;
- }
-
-}
diff --git a/src/main/java/com/lh/service/VoteProducer.java b/src/main/java/com/lh/service/VoteProducer.java
deleted file mode 100644
index a410d83..0000000
--- a/src/main/java/com/lh/service/VoteProducer.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.lh.service;
-
-import com.lh.bean.VoteMessage;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.kafka.core.KafkaTemplate;
-import org.springframework.stereotype.Service;
-
-@Service
-public class VoteProducer {
-
- private static final String TOPIC = "vote-topic"; // Kafka topic
-
- @Autowired
- private KafkaTemplate kafkaTemplate;
-
- // 发送投票消息
- public void sendVoteMessage(VoteMessage voteMessage) {
- String message = String.format("%s,%s,%s,%s",
- voteMessage.getVoterJwcode(),
- voteMessage.getCandidateJwcode(),
- voteMessage.getVoterName(),
- voteMessage.getVoteTime());
-
- kafkaTemplate.send(TOPIC, message); // 将投票信息发送到Kafka
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/lh/service/VoteService.java b/src/main/java/com/lh/service/VoteService.java
index 18c915e..7b420b0 100644
--- a/src/main/java/com/lh/service/VoteService.java
+++ b/src/main/java/com/lh/service/VoteService.java
@@ -13,4 +13,6 @@ public interface VoteService {
List getCandidates(String VoterJwcode);
//获取某个候选人的被投票记录
List getVotesByCandidate(String candidateJwcode) throws MyException;
+ //重置投票
+ boolean resetVote() throws MyException;
}
diff --git a/src/main/java/com/lh/service/VoteServiceImpl.java b/src/main/java/com/lh/service/VoteServiceImpl.java
index 91a4314..695112d 100644
--- a/src/main/java/com/lh/service/VoteServiceImpl.java
+++ b/src/main/java/com/lh/service/VoteServiceImpl.java
@@ -1,23 +1,22 @@
package com.lh.service;
import com.lh.bean.Candidate;
-import com.lh.bean.VoteMessage;
import com.lh.bean.Voter;
import com.lh.config.CandidateCacheRepository;
import com.lh.exception.MyException;
import com.lh.mapper.CandidatesMapper;
import com.lh.mapper.VoterMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
-import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
@@ -39,8 +38,6 @@ public class VoteServiceImpl implements VoteService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
- private VoteProducer voteProducer;
- @Autowired
private RedisTemplate redisTemplate;
@Autowired
private CandidateCacheRepository candidateCacheRepository;
@@ -104,8 +101,8 @@ public class VoteServiceImpl implements VoteService {
// 更新投票重复键
stringRedisTemplate.opsForValue().set(voteStatusKey, "true", secondsUntilEndOfDay, TimeUnit.SECONDS);
- // 将投票请求发送到 Kafka 消息队列
- voteProducer.sendVoteMessage(new VoteMessage(voterJwcode, candidateJwcode, voterName, Timestamp.valueOf(LocalDateTime.now()).toString()));
+ // 数据库操作
+ processVote(voterJwcode, candidateJwcode, voterName);
return dailyVoteLimit - voteCountToday - 1;
}
@@ -161,4 +158,44 @@ public class VoteServiceImpl implements VoteService {
}
}
}
+
+ @Async
+ @Transactional(rollbackFor = Exception.class)
+ protected void processVote(String voterJwcode, String candidateJwcode, String voterName) throws MyException {
+ // 获取候选人信息
+ Candidate candidate = candidatesMapper.getByCandidateJwcode(candidateJwcode);
+
+ // 2. 获取候选人信息
+ if (candidate == null) {
+ throw new MyException("候选人不存在!");
+ }
+
+ // 4. 增加候选人票数
+ if (!candidatesMapper.addVotes(candidateJwcode)) {
+ throw new MyException ("候选人票数更新失败,请联系管理员!");
+ }
+
+ // 5. 插入投票记录
+ voterMapper.insertVote(voterJwcode, candidateJwcode, voterName);
+
+
+ System.out.println("投票成功!用户:" + voterJwcode + " 投给了 " + candidateJwcode);
+ }
+
+ //重置投票
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public boolean resetVote() throws MyException {
+ if (voterMapper.resetVote() && candidatesMapper.resetVotes()){
+ //删除redis所有缓存
+ redisTemplate.delete(redisTemplate.keys("*"));
+ candidateCacheRepository.deleteAllCandidatesFromCache();
+ } else {
+ throw new MyException("重置投票失败");
+ }
+ //重新初始化
+ initCandidatesCache();
+ return true;
+ }
+
}
diff --git a/src/main/resources/com/lh/mapper/CandidatesMapper.xml b/src/main/resources/com/lh/mapper/CandidatesMapper.xml
index e1e5b47..bf2286f 100644
--- a/src/main/resources/com/lh/mapper/CandidatesMapper.xml
+++ b/src/main/resources/com/lh/mapper/CandidatesMapper.xml
@@ -4,6 +4,9 @@
update candidates set votes = votes + 1 where jwcode = #{jwcode}
+
+ update candidates set votes = 0
+
diff --git a/src/main/resources/com/lh/mapper/VoterMapper.xml b/src/main/resources/com/lh/mapper/VoterMapper.xml
index a2a999a..22c198b 100644
--- a/src/main/resources/com/lh/mapper/VoterMapper.xml
+++ b/src/main/resources/com/lh/mapper/VoterMapper.xml
@@ -5,6 +5,9 @@
INSERT INTO voters(jwcode, candidate_jwcode, name)
VALUES(#{jwcode}, #{candidateJwcode}, #{name})
+
+ DELETE FROM voters
+