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 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().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().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); } }