|
|
@ -1,6 +1,7 @@ |
|
|
|
package com.ccsens.mt.service; |
|
|
|
|
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.ccsens.mt.bean.dto.TopicDto; |
|
|
|
import com.ccsens.mt.bean.po.MtGroup; |
|
|
|
import com.ccsens.mt.bean.po.MtVote; |
|
|
@ -9,15 +10,20 @@ import com.ccsens.mt.persist.dao.VoteDao; |
|
|
|
import com.ccsens.mt.persist.mapper.MtGroupMapper; |
|
|
|
import com.ccsens.mt.util.Constant; |
|
|
|
import com.ccsens.util.CodeEnum; |
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
|
import com.ccsens.util.bean.dto.QueryDto; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
@ -30,6 +36,26 @@ public class VoteService implements IVoteService{ |
|
|
|
private MtGroupMapper mtGroupMapper; |
|
|
|
@Resource |
|
|
|
private Snowflake snowflake; |
|
|
|
@Resource |
|
|
|
private RedisUtil redisUtil; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void startVote(QueryDto<TopicDto.Project> params) { |
|
|
|
TopicDto.Project project = params.getParam(); |
|
|
|
if(ObjectUtil.isNull(project)){ |
|
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
|
} |
|
|
|
redisUtil.set(project.getProjectId()+ Constant.Redis.START_VOTE,1, Constant.Redis.VOTE_TIME); |
|
|
|
|
|
|
|
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(Constant.Redis.VOTE_START, |
|
|
|
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build()); |
|
|
|
executorService.schedule(new Thread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
redisUtil.set(project.getProjectId()+ Constant.Redis.START_VOTE,Constant.Redis.VOTE_COMPLETED, Constant.Redis.VOTE_TIME); |
|
|
|
} |
|
|
|
}), Constant.Redis.VOTE_TIME_OUT, TimeUnit.MILLISECONDS); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void saveVote(QueryDto<List<TopicDto.Group>> params) { |
|
|
@ -56,7 +82,13 @@ public class VoteService implements IVoteService{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 检查投票时间是否有效
|
|
|
|
Object o = redisUtil.get(projectId + Constant.Redis.START_VOTE); |
|
|
|
if(ObjectUtil.isNull(o)){ |
|
|
|
throw new BaseException(CodeEnum.VOTE_NOT_START); |
|
|
|
}else if((int)o == Constant.Redis.VOTE_COMPLETED){ |
|
|
|
throw new BaseException(CodeEnum.VOTE_COMPLETED); |
|
|
|
} |
|
|
|
// 判断是否在当前项目中已投
|
|
|
|
long count = voteDao.countUserVoteNums(params.getUserId(), projectId); |
|
|
|
log.info("{}-{}投票数:{}", params.getUserId(), projectId, count); |
|
|
@ -70,8 +102,6 @@ public class VoteService implements IVoteService{ |
|
|
|
mtVote.setUserId(params.getUserId()); |
|
|
|
voteDao.insertSelective(mtVote); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|