package beilv.competition.service.impl; import beilv.common.core.text.Convert; import beilv.common.utils.ShiroUtils; import beilv.competition.domain.Competition; import beilv.competition.mapper.CompetitionMapper; import beilv.competition.service.ICompetitionService; import beilv.competition.task.CloseReg; import beilv.stadium.mapper.StadiumMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.Date; import java.util.List; import java.util.Timer; /** * 赛事发布Service业务层处理 * * @author LG * @date 2024-12-31 */ @Service public class CompetitionServiceImpl implements ICompetitionService { @Autowired private CompetitionMapper competitionMapper; @Autowired private StadiumMapper stadiumMapper; 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("competiton_state_2"); 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("competiton_state_3"); competitionMapper.updateCompetition(item); } 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(String id) { Competition competition = new Competition(); competition.setPublishTime(new Date()); competition.setPublishBy(ShiroUtils.getSysUser().getUserId().toString()); competition.setPublishName(ShiroUtils.getSysUser().getUserName()); competition.setId(Integer.valueOf(id)); competition.setCompetitionState(BAO_MING_ZHONG); Timer timer = new Timer(); timer.schedule(new CloseReg(competitionMapper, Integer.parseInt(id)), new Date(competition.getApplyStartTime().getTime() - (long) (competition.getApplyBeforeTime() * 60 * 1000))); 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(); } }