From fd22a2c83f60ff0daebff5fdb78f637f17d58cee Mon Sep 17 00:00:00 2001 From: 101captain <237651143@qq.com> Date: 星期二, 22 二月 2022 15:01:59 +0800 Subject: [PATCH] 三社功能提交 --- springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/message/RaffleMessage.java | 62 ++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 1 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/message/RaffleMessage.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/message/RaffleMessage.java index 21b1fe9..8b05192 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/message/RaffleMessage.java +++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/message/RaffleMessage.java @@ -1,14 +1,25 @@ 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 @@ -19,12 +30,61 @@ @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); } } -- Gitblit v1.7.1