LotteryCountLogServiceImpl.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.ylx.lottery.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.ylx.common.core.domain.model.WxLoginUser;
  9. import com.ylx.common.exception.ServiceException;
  10. import com.ylx.common.utils.SecurityUtils;
  11. import com.ylx.lottery.domain.LotteryCountLog;
  12. import com.ylx.lottery.domain.dto.IsLotteryDTO;
  13. import com.ylx.lottery.domain.vo.IsLotteryVO;
  14. import com.ylx.lottery.domain.vo.LotteryStatVO;
  15. import com.ylx.lottery.mapper.LotteryCountLogMapper;
  16. import com.ylx.lottery.service.LotteryCountLogService;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.stream.Collectors;
  22. @Service
  23. public class LotteryCountLogServiceImpl extends ServiceImpl<LotteryCountLogMapper, LotteryCountLog> implements LotteryCountLogService {
  24. @Override
  25. public List<LotteryStatVO> selectSumByGroup(LambdaQueryWrapper<LotteryCountLog> queryWrapper) {
  26. return this.baseMapper.selectSumByGroup(queryWrapper);
  27. }
  28. @Override
  29. @Transactional(rollbackFor = Exception.class)
  30. public IsLotteryVO isLottery(IsLotteryDTO dto) {
  31. IsLotteryVO vo = new IsLotteryVO();
  32. // 1. 获取当前登录用户
  33. WxLoginUser loginUser = SecurityUtils.getWxLoginUser();
  34. if (ObjectUtil.isNull(loginUser)) {
  35. throw new ServiceException("用户未登录或登录已过期");
  36. }
  37. String userId = loginUser.getId();
  38. if (StrUtil.isBlank(userId)) {
  39. throw new ServiceException("用户ID不合法");
  40. }
  41. // 2. 构建查询条件 (用于查找未弹窗的记录)
  42. LambdaQueryWrapper<LotteryCountLog> queryWrapper = new LambdaQueryWrapper<>();
  43. queryWrapper.eq(LotteryCountLog::getUserId, userId)
  44. .eq(LotteryCountLog::getIsLottery, 0) // 只查未弹窗的
  45. .eq(LotteryCountLog::getIsDelete, 0)
  46. .in(LotteryCountLog::getType, dto.getTypes());
  47. // 3.提取列表
  48. List<LotteryCountLog> lotteryCountLogs = this.baseMapper.selectList(queryWrapper);
  49. // 4. 设置返回结果
  50. if (CollUtil.isEmpty(lotteryCountLogs)) {
  51. vo.setIsLottery(0);
  52. return vo;
  53. }
  54. LotteryCountLog lotteryCountLog = CollUtil.getFirst(lotteryCountLogs);
  55. vo.setIsLottery(1);
  56. vo.setActivityId(lotteryCountLog.getLocalActivityTableId());
  57. vo.setActivityUrl(lotteryCountLog.getActivityUrl());
  58. // 5. 如果有记录,批量更新状态为“已弹窗”
  59. if (CollUtil.isNotEmpty(lotteryCountLogs)) {
  60. LambdaUpdateWrapper<LotteryCountLog> updateWrapper = new LambdaUpdateWrapper<>();
  61. updateWrapper.in(LotteryCountLog::getType, dto.getTypes())
  62. .eq(LotteryCountLog::getUserId, userId)
  63. .set(LotteryCountLog::getIsLottery, 1)
  64. .set(LotteryCountLog::getUpdateTime, new Date());
  65. this.update(updateWrapper);
  66. }
  67. return vo;
  68. }
  69. }