mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
 */
@Component
public class RaffleMessage {
    public static final String DELAYED_QUEUE="huacheng.raffle.queue";
 
    @Resource
    private ComActRaffleDao comActRaffleDao;
    @Resource
    private ComActRaffleRecordDao comActRaffleRecordDao;
    @Resource
    private ComActRafflePrizeDao comActRafflePrizeDao;
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @RabbitListener(queues=DELAYED_QUEUE)
    public void doRaffle(ComActRaffle comActRaffleVO){
        ComActRaffle comActRaffle=comActRaffleDao.selectOne(new QueryWrapper<ComActRaffle>().lambda().eq(ComActRaffle::getId,comActRaffleVO.getId()));
        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);
            rabbitTemplate.convertAndSend("huacheng.raffle.exchange", "huacheng.raffle.key", comActRaffleVO, message -> {
                message.getMessageProperties().setHeader("x-delay", dateToSecond(comActRaffle.getRaffleStopTime()));
                return message;
            });
        }
        if(comActRaffle!=null&&comActRaffle.getStatus()==0&&comActRaffle.getStartTime().before(new Date())){
            comActRaffle.setStatus(1);
            comActRaffleDao.updateById(comActRaffle);
            rabbitTemplate.convertAndSend("huacheng.raffle.exchange", "huacheng.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("huacheng.raffle.exchange", "huacheng.raffle.key", comActRaffleVO, message -> {
                message.getMessageProperties().setHeader("x-delay", dateToSecond(comActRaffle.getLotteryTime()));
                return message;
            });
        }
        if(comActRaffle!=null&&comActRaffle.getStatus()==3&&comActRaffle.getRaffleStopTime().before(new Date())){
            comActRaffleRecordDao.updateStatusByRaffleId(comActRaffle.getId());
        }
    }
    private Long dateToSecond(Date expireTime){
        return DateUtil.between(new Date(),expireTime, DateUnit.MS);
    }
}