CompetitionServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package beilv.competition.service.impl;
  2. import beilv.common.core.text.Convert;
  3. import beilv.common.utils.ShiroUtils;
  4. import beilv.competition.domain.Competition;
  5. import beilv.competition.domain.RedisTask;
  6. import beilv.competition.mapper.CompetitionMapper;
  7. import beilv.competition.service.ICompetitionService;
  8. import beilv.competition.task.CloseReg;
  9. import beilv.stadium.domain.Stadium;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.redis.core.StringRedisTemplate;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.PostConstruct;
  14. import javax.annotation.Resource;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.Timer;
  18. import static beilv.competition.domain.Constant.TASK_QUEUE;
  19. /**
  20. * 赛事发布Service业务层处理
  21. *
  22. * @author LG
  23. * @date 2024-12-31
  24. */
  25. @Service
  26. public class CompetitionServiceImpl implements ICompetitionService {
  27. @Resource
  28. private CompetitionMapper competitionMapper;
  29. @Autowired
  30. private StringRedisTemplate redisTemplate;
  31. private final static String DAI_FA_BU = "competiton_state_1";
  32. private final static String BAO_MING_ZHONG = "competiton_state_2";
  33. private final static String BAO_MING_JIE_SHU = "competiton_state_3";
  34. private final static String YI_GUAN_BI = "competiton_state_4";
  35. @PostConstruct
  36. public void init() {
  37. // 找到所有报名中的赛事
  38. Competition competition = new Competition();
  39. competition.setCompetitionState(BAO_MING_ZHONG);
  40. List<Competition> competitionList = this.selectCompetitionList(competition);
  41. competitionList.forEach(item -> {
  42. Date date = new Date(item.getApplyStartTime().getTime() - (long) (item.getApplyBeforeTime() * 60 * 60 * 1000));
  43. if (new Date().getTime() >= date.getTime()) {
  44. item.setCompetitionState(BAO_MING_JIE_SHU);
  45. competitionMapper.updateCompetition(item);
  46. Stadium stadium = new Stadium();
  47. stadium.setCompetitionId(String.valueOf(item.getId()));
  48. stadium.setPaymentStatus("payment_status_verification");
  49. stadium.setVerificationTime(new Date());
  50. competitionMapper.updateBookARace(stadium);
  51. } else {
  52. Timer timer = new Timer();
  53. timer.schedule(new CloseReg(competitionMapper, item.getId()), date);
  54. }
  55. });
  56. }
  57. /**
  58. * 查询赛事发布
  59. *
  60. * @param id 赛事发布主键
  61. * @return 赛事发布
  62. */
  63. @Override
  64. public Competition selectCompetitionById(Integer id) {
  65. return competitionMapper.selectCompetitionById(id);
  66. }
  67. /**
  68. * 查询赛事发布列表
  69. *
  70. * @param competition 赛事发布
  71. * @return 赛事发布
  72. */
  73. @Override
  74. public List<Competition> selectCompetitionList(Competition competition) {
  75. return competitionMapper.selectCompetitionList(competition);
  76. }
  77. /**
  78. * 新增赛事发布
  79. *
  80. * @param competition 赛事发布
  81. * @return 结果
  82. */
  83. @Override
  84. public int insertCompetition(Competition competition) {
  85. competition.setCreateTime(new Date());
  86. competition.setCreateBy(ShiroUtils.getSysUser().getUserId().toString());
  87. competition.setCreateName(ShiroUtils.getSysUser().getUserName());
  88. competition.setCompetitionState(DAI_FA_BU);
  89. return competitionMapper.insertCompetition(competition);
  90. }
  91. /**
  92. * 修改赛事发布
  93. *
  94. * @param competition 赛事发布
  95. * @return 结果
  96. */
  97. @Override
  98. public int updateCompetition(Competition competition) {
  99. competition.setUpdateTime(new Date());
  100. competition.setUpdateBy(ShiroUtils.getSysUser().getUserId().toString());
  101. competition.setUpdateName(ShiroUtils.getSysUser().getUserName());
  102. return competitionMapper.updateCompetition(competition);
  103. }
  104. /**
  105. * 批量删除赛事发布
  106. *
  107. * @param ids 需要删除的赛事发布主键
  108. * @return 结果
  109. */
  110. @Override
  111. public int deleteCompetitionByIds(String ids) {
  112. return competitionMapper.deleteCompetitionByIds(Convert.toStrArray(ids));
  113. }
  114. /**
  115. * 删除赛事发布信息
  116. *
  117. * @param id 赛事发布主键
  118. * @return 结果
  119. */
  120. @Override
  121. public int deleteCompetitionById(Integer id) {
  122. return competitionMapper.deleteCompetitionById(id);
  123. }
  124. @Override
  125. public int publishCompetitionById(Competition competition) {
  126. competition.setPublishTime(new Date());
  127. competition.setPublishBy(ShiroUtils.getSysUser().getUserId().toString());
  128. competition.setPublishName(ShiroUtils.getSysUser().getUserName());
  129. competition.setCompetitionState(BAO_MING_ZHONG);
  130. // Timer timer = new Timer();
  131. //new Date(competition.getApplyStartTime().getTime() - (long) (competition.getApplyBeforeTime() * 60 * 1000))
  132. // timer.schedule(new CloseReg(competitionMapper, competition.getId()), competition.getApplyStartTime());
  133. //修改使用redis的zset存储定时任务.
  134. RedisTask redisTask = new RedisTask(competition.getId(),"competiton_state_3","payment_status_verification");
  135. redisTemplate.opsForZSet().add(TASK_QUEUE + redisTask.getCompetitionId(), redisTask.toString(), competition.getAllowPublishTime());
  136. return competitionMapper.publishCompetition(competition);
  137. }
  138. @Override
  139. public int closeCompetitionById(String id) {
  140. // 查询是否存在为退款的订单
  141. if (competitionMapper.selectStadumIsOpenById(id)) {
  142. //修改赛事状态
  143. Competition competition = new Competition();
  144. competition.setId(Integer.valueOf(id));
  145. competition.setCompetitionState(YI_GUAN_BI);
  146. return competitionMapper.publishCompetition(competition);
  147. } else {
  148. return 0;
  149. }
  150. }
  151. @Override
  152. public List<Competition> getCompetitionList() {
  153. return competitionMapper.getCompetitionList();
  154. }
  155. }