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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.panzhihua.service_community.service.impl;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.property.CommonPage;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.raffle.ComActRaffleRecordVO;
import com.panzhihua.common.model.vos.community.raffle.ComActRaffleVO;
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.dao.ComActRaffleDao;
import com.panzhihua.service_community.entity.ComActRafflePrize;
import com.panzhihua.service_community.entity.ComActRaffleRecord;
import com.panzhihua.service_community.service.ComActRaffleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * 抽奖活动表(ComActRaffle)表服务实现类
 * projectName 成都呐喊信息技术有限公司-智慧社区项目
 * description: 抽奖活动表相关功能
 *
 * @author zzj
 * @since 2022-02-18 14:31:20
 */
@Slf4j
@Service
public class ComActRaffleServiceImpl extends ServiceImpl<ComActRaffleDao, ComActRaffle> implements ComActRaffleService {
 
    @Resource
    private ComActRafflePrizeDao comActRafflePrizeDao;
    @Resource
    private ComActRaffleRecordDao comActRaffleRecordDao;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Override
    public R pageList(CommonPage commonPage) {
        return R.ok(this.baseMapper.pageList(new Page(commonPage.getPage(),commonPage.getSize()),commonPage));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insert(ComActRaffleVO comActRaffleVO) {
        ComActRaffle comActRaffle=new ComActRaffle();
        BeanUtils.copyProperties(comActRaffleVO,comActRaffle);
        comActRaffle.setCreateTime(new Date());
        comActRaffle.setStatus(0);
        int count= this.baseMapper.insert(comActRaffle);
        if(count>0){
            rabbitTemplate.convertAndSend("raffle.exchange", "raffle.key", comActRaffle, message -> {
                message.getMessageProperties().setHeader("x-delay", dateToSecond(comActRaffle.getStartTime()));
                return message;
            });
            if(!CollectionUtils.isEmpty(comActRaffleVO.getComActRafflePrizeVOList())){
                comActRaffleVO.getComActRafflePrizeVOList().forEach(comActRafflePrizeVO -> {
                    ComActRafflePrize comActRafflePrize=new ComActRafflePrize();
                    BeanUtils.copyProperties(comActRafflePrizeVO,comActRafflePrize);
                    comActRafflePrize.setRaffleId(comActRaffle.getId());
                    comActRafflePrize.setSurplus(comActRafflePrize.getTotal());
                    comActRafflePrizeDao.insert(comActRafflePrize);
                });
            }
            return R.ok();
        }
        return R.fail();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R update(ComActRaffleVO comActRaffleVO) {
        ComActRaffle comActRaffle=new ComActRaffle();
        BeanUtils.copyProperties(comActRaffleVO,comActRaffle);
        comActRaffle.setCreateTime(new Date());
        comActRaffle.setStatus(0);
        int count=this.baseMapper.updateById(comActRaffle);
        if(count>0){
            if(!CollectionUtils.isEmpty(comActRaffleVO.getComActRafflePrizeVOList())){
                comActRafflePrizeDao.delete(new QueryWrapper<ComActRafflePrize>().lambda().eq(ComActRafflePrize::getRaffleId,comActRaffle.getId()));
                comActRaffleVO.getComActRafflePrizeVOList().forEach(comActRafflePrizeVO -> {
                    ComActRafflePrize comActRafflePrize=new ComActRafflePrize();
                    BeanUtils.copyProperties(comActRafflePrizeVO,comActRafflePrize);
                    comActRafflePrize.setRaffleId(comActRaffle.getId());
                    comActRafflePrize.setSurplus(comActRafflePrize.getTotal());
                    comActRafflePrizeDao.insert(comActRafflePrize);
                });
            }
            return R.ok();
        }
        return R.fail();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delete(Long id) {
        int count=this.baseMapper.deleteById(id);
        if(count>0){
            comActRafflePrizeDao.delete(new QueryWrapper<ComActRafflePrize>().lambda().eq(ComActRafflePrize::getRaffleId,id));
            return R.ok();
        }
        return R.fail();
    }
 
    @Override
    public R selectById(Long id,Long userId) {
        ComActRaffleVO comActRaffleVO=this.baseMapper.selectById(id);
        if(comActRaffleVO!=null){
            comActRaffleVO.setComActRafflePrizeVOList(comActRafflePrizeDao.selectByRaffleId(id));
        }
        if(userId!=null){
          ComActRaffleRecordVO comActRaffleRecordVO=comActRaffleRecordDao.selectByUserId(userId,id);
          if(comActRaffleRecordVO!=null){
              comActRaffleVO.setComActRaffleRecordVO(comActRaffleRecordVO);
              comActRaffleVO.setJoinStatus(0);
          }
          else {
              if(comActRaffleVO.getStatus()==1){
                  comActRaffleVO.setJoinStatus(1);
              }
              else {
                  comActRaffleVO.setJoinStatus(0);
              }
          }
        }
        return R.ok(comActRaffleVO);
    }
    private Long dateToSecond(Date expireTime){
        return DateUtil.between(new Date(),expireTime, DateUnit.MS);
    }
}