package beilv.competition.service.impl; import beilv.common.core.text.Convert; import beilv.common.utils.ShiroUtils; import beilv.competition.domain.Competition; import beilv.competition.domain.RedisTask; import beilv.competition.mapper.CompetitionMapper; import beilv.competition.service.ICompetitionService; import beilv.competition.task.CloseReg; import beilv.stadium.domain.Stadium; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.Date; import java.util.List; import java.util.Timer; import static beilv.competition.domain.Constant.TASK_QUEUE; /** * 赛事发布Service业务层处理 * * @author LG * @date 2024-12-31 */ @Service public class CompetitionServiceImpl implements ICompetitionService { @Resource private CompetitionMapper competitionMapper; @Autowired private StringRedisTemplate redisTemplate; private final static String DAI_FA_BU = "competiton_state_1"; private final static String BAO_MING_ZHONG = "competiton_state_2"; private final static String BAO_MING_JIE_SHU = "competiton_state_3"; private final static String YI_GUAN_BI = "competiton_state_4"; @PostConstruct public void init() { // 找到所有报名中的赛事 Competition competition = new Competition(); competition.setCompetitionState(BAO_MING_ZHONG); List competitionList = this.selectCompetitionList(competition); competitionList.forEach(item -> { Date date = new Date(item.getApplyStartTime().getTime() - (long) (item.getApplyBeforeTime() * 60 * 60 * 1000)); if (new Date().getTime() >= date.getTime()) { item.setCompetitionState(BAO_MING_JIE_SHU); competitionMapper.updateCompetition(item); Stadium stadium = new Stadium(); stadium.setCompetitionId(String.valueOf(item.getId())); stadium.setPaymentStatus("payment_status_verification"); stadium.setVerificationTime(new Date()); competitionMapper.updateBookARace(stadium); } else { Timer timer = new Timer(); timer.schedule(new CloseReg(competitionMapper, item.getId()), date); } }); } /** * 查询赛事发布 * * @param id 赛事发布主键 * @return 赛事发布 */ @Override public Competition selectCompetitionById(Integer id) { return competitionMapper.selectCompetitionById(id); } /** * 查询赛事发布列表 * * @param competition 赛事发布 * @return 赛事发布 */ @Override public List selectCompetitionList(Competition competition) { return competitionMapper.selectCompetitionList(competition); } /** * 新增赛事发布 * * @param competition 赛事发布 * @return 结果 */ @Override public int insertCompetition(Competition competition) { competition.setCreateTime(new Date()); competition.setCreateBy(ShiroUtils.getSysUser().getUserId().toString()); competition.setCreateName(ShiroUtils.getSysUser().getUserName()); competition.setCompetitionState(DAI_FA_BU); return competitionMapper.insertCompetition(competition); } /** * 修改赛事发布 * * @param competition 赛事发布 * @return 结果 */ @Override public int updateCompetition(Competition competition) { competition.setUpdateTime(new Date()); competition.setUpdateBy(ShiroUtils.getSysUser().getUserId().toString()); competition.setUpdateName(ShiroUtils.getSysUser().getUserName()); return competitionMapper.updateCompetition(competition); } /** * 批量删除赛事发布 * * @param ids 需要删除的赛事发布主键 * @return 结果 */ @Override public int deleteCompetitionByIds(String ids) { return competitionMapper.deleteCompetitionByIds(Convert.toStrArray(ids)); } /** * 删除赛事发布信息 * * @param id 赛事发布主键 * @return 结果 */ @Override public int deleteCompetitionById(Integer id) { return competitionMapper.deleteCompetitionById(id); } @Override public int publishCompetitionById(Competition competition) { competition.setPublishTime(new Date()); competition.setPublishBy(ShiroUtils.getSysUser().getUserId().toString()); competition.setPublishName(ShiroUtils.getSysUser().getUserName()); competition.setCompetitionState(BAO_MING_ZHONG); // Timer timer = new Timer(); //new Date(competition.getApplyStartTime().getTime() - (long) (competition.getApplyBeforeTime() * 60 * 1000)) // timer.schedule(new CloseReg(competitionMapper, competition.getId()), competition.getApplyStartTime()); //修改使用redis的zset存储定时任务. RedisTask redisTask = new RedisTask(competition.getId(),"competiton_state_3","payment_status_verification"); redisTemplate.opsForZSet().add(TASK_QUEUE + redisTask.getCompetitionId(), redisTask.toString(), competition.getAllowPublishTime()); return competitionMapper.publishCompetition(competition); } @Override public int closeCompetitionById(String id) { // 查询是否存在为退款的订单 if (competitionMapper.selectStadumIsOpenById(id)) { //修改赛事状态 Competition competition = new Competition(); competition.setId(Integer.valueOf(id)); competition.setCompetitionState(YI_GUAN_BI); return competitionMapper.publishCompetition(competition); } else { return 0; } } @Override public List getCompetitionList() { return competitionMapper.getCompetitionList(); } }