| | |
| | | package com.panzhihua.service_community.message; |
| | | |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.panzhihua.common.model.vos.community.raffle.ComActRaffleVO; |
| | | import com.panzhihua.common.utlis.StringUtils; |
| | | import com.panzhihua.service_community.dao.ComActRaffleDao; |
| | | import com.panzhihua.service_community.dao.ComActRafflePrizeDao; |
| | | import com.panzhihua.service_community.dao.ComActRaffleRecordDao; |
| | | import com.panzhihua.service_community.entity.ComActRaffle; |
| | | import com.panzhihua.service_community.entity.ComActRafflePrize; |
| | | import com.panzhihua.service_community.entity.ComActRaffleRecord; |
| | | import org.springframework.amqp.rabbit.annotation.RabbitListener; |
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * @author zzj |
| | |
| | | |
| | | @Resource |
| | | private ComActRaffleDao comActRaffleDao; |
| | | @Resource |
| | | private ComActRaffleRecordDao comActRaffleRecordDao; |
| | | @Resource |
| | | private ComActRafflePrizeDao comActRafflePrizeDao; |
| | | @Resource |
| | | private RabbitTemplate rabbitTemplate; |
| | | |
| | | @RabbitListener(queues=DELAYED_QUEUE) |
| | | public void doRaffle(ComActRaffleVO comActRaffleVO){ |
| | | ComActRaffle comActRaffle=comActRaffleDao.selectOne(new QueryWrapper<ComActRaffle>().lambda().eq(ComActRaffle::getId,comActRaffleVO.getId())); |
| | | if(comActRaffle!=null&&comActRaffle.getStatus()==0&&comActRaffle.getLotteryTime().before(new Date())){ |
| | | if(comActRaffle!=null&&comActRaffle.getStatus()==2&&comActRaffle.getLotteryTime().before(new Date())){ |
| | | List<ComActRafflePrize> comActRafflePrizeList=comActRafflePrizeDao.selectList(new QueryWrapper<ComActRafflePrize>().lambda().eq(ComActRafflePrize::getRaffleId,comActRaffleVO.getId())); |
| | | if(StringUtils.isNotEmpty(comActRafflePrizeList)){ |
| | | List<ComActRaffleRecord> comActRaffleRecords=comActRaffleRecordDao.selectList(new QueryWrapper<ComActRaffleRecord>().lambda().eq(ComActRaffleRecord::getRaffleId,comActRaffleVO.getId())); |
| | | if(StringUtils.isNotEmpty(comActRaffleRecords)){ |
| | | Random random=new Random(); |
| | | comActRafflePrizeList.forEach(comActRafflePrize -> { |
| | | int a=0; |
| | | for(int i=1;i<=comActRafflePrize.getSurplus();i++){ |
| | | if(comActRaffleRecords.size()>0){ |
| | | ComActRaffleRecord comActRaffleRecord=comActRaffleRecords.get(random.nextInt(comActRaffleRecords.size())); |
| | | comActRaffleRecord.setPrizeId(comActRafflePrize.getId()); |
| | | comActRaffleRecord.setStatus(1); |
| | | comActRaffleRecordDao.updateById(comActRaffleRecord); |
| | | comActRaffleRecords.remove(comActRaffleRecord); |
| | | a++; |
| | | } |
| | | } |
| | | comActRafflePrize.setSurplus(comActRafflePrize.getSurplus()-a); |
| | | comActRafflePrizeDao.updateById(comActRafflePrize); |
| | | }); |
| | | |
| | | } |
| | | } |
| | | comActRaffle.setStatus(3); |
| | | comActRaffleDao.updateById(comActRaffle); |
| | | } |
| | | if(comActRaffle!=null&&comActRaffle.getStatus()==0&&comActRaffle.getStartTime().before(new Date())){ |
| | | comActRaffle.setStatus(1); |
| | | comActRaffleDao.updateById(comActRaffle); |
| | | rabbitTemplate.convertAndSend("raffle.exchange", "raffle.key", comActRaffleVO, message -> { |
| | | message.getMessageProperties().setHeader("x-delay", dateToSecond(comActRaffle.getStopTime())); |
| | | return message; |
| | | }); |
| | | } |
| | | if(comActRaffle!=null&&comActRaffle.getStatus()==1&&comActRaffle.getStopTime().before(new Date())){ |
| | | comActRaffle.setStatus(2); |
| | | comActRaffleDao.updateById(comActRaffle); |
| | | rabbitTemplate.convertAndSend("raffle.exchange", "raffle.key", comActRaffleVO, message -> { |
| | | message.getMessageProperties().setHeader("x-delay", dateToSecond(comActRaffle.getLotteryTime())); |
| | | return message; |
| | | }); |
| | | } |
| | | } |
| | | private Long dateToSecond(Date expireTime){ |
| | | return DateUtil.between(new Date(),expireTime, DateUnit.MS); |
| | | } |
| | | } |