|
@@ -5,8 +5,10 @@ import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.baomidou.lock.annotation.Lock4j;
|
|
|
import com.citu.framework.common.pojo.PageResult;
|
|
|
import com.citu.framework.common.util.object.BeanUtils;
|
|
|
+import com.citu.framework.mybatis.core.query.LambdaQueryWrapperX;
|
|
|
import com.citu.module.menduner.system.api.user.MendunerUserApi;
|
|
|
import com.citu.module.menduner.system.api.user.UserInfoRespDTO;
|
|
|
+import com.citu.module.promotion.api.luck.LuckRaffleReqDTO;
|
|
|
import com.citu.module.promotion.controller.admin.luck.vo.record.LuckLotteryRecordDetailRespVO;
|
|
|
import com.citu.module.promotion.controller.admin.luck.vo.record.LuckLotteryRecordPageReqVO;
|
|
|
import com.citu.module.promotion.controller.admin.luck.vo.record.LuckLotteryRecordRespVO;
|
|
@@ -17,12 +19,10 @@ import com.citu.module.promotion.dal.dataobject.luck.LuckLotteryDO;
|
|
|
import com.citu.module.promotion.dal.dataobject.luck.LuckLotteryRecordDO;
|
|
|
import com.citu.module.promotion.dal.dataobject.luck.LuckPrizeDO;
|
|
|
import com.citu.module.promotion.dal.mysql.luck.LuckLotteryRecordMapper;
|
|
|
-import com.citu.module.promotion.enums.luck.LuckPrizeTypeEnum;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Random;
|
|
@@ -30,7 +30,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static com.citu.module.promotion.enums.ErrorCodeConstants.*;
|
|
|
+import static com.citu.module.promotion.enums.ErrorCodeConstants.LUCK_LOTTERY_RECORD_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* 幸运抽奖-抽奖记录 Service 实现类
|
|
@@ -151,25 +151,22 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
|
|
|
@Override
|
|
|
@DSTransactional
|
|
|
- @Lock4j(keys = {"#spuId", "#userId", "#cityName"}, acquireTimeout = 6000)
|
|
|
- public void raffle(Long spuId, Long userId, String cityName) {
|
|
|
- LuckLotteryDO luckLottery = luckLotteryService.getLuckLotteryBySpuId(String.valueOf(spuId));
|
|
|
- // 效验活动是否过期
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
+ @Lock4j(keys = {"#raffleReqDTO.spuId", "#raffleReqDTO.userId", "#raffleReqDTO.cityName"}, acquireTimeout = 6000)
|
|
|
+ public LuckLotteryRecordRespVO raffle(LuckRaffleReqDTO raffleReqDTO) {
|
|
|
+ LuckLotteryDO luckLottery = luckLotteryService.getLuckLotteryBySpuId(String.valueOf(raffleReqDTO.getSpuId()));
|
|
|
+
|
|
|
// 判断活动有没有开始,判断活动有没有结束
|
|
|
- if (luckLottery.getStartTime().isAfter(now)||luckLottery.getEndTime().isBefore(now)) {
|
|
|
- return;
|
|
|
+ if (luckLotteryService.checkTimeExpiredRet(luckLottery)) {
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
// 获取奖品
|
|
|
List<LuckPrizeDO> luckPrizeList = luckPrizeService.getListByLotteryId(luckLottery.getId());
|
|
|
if (CollUtil.isEmpty(luckPrizeList)) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
- // 筛选出 喜爱城市
|
|
|
- luckPrizeList = luckPrizeList.stream().filter(prize->
|
|
|
- prize.getType().equals(LuckPrizeTypeEnum.CUSTOM.getType())
|
|
|
- && prize.getExtend().getCityName().equals(cityName)).collect(Collectors.toList());
|
|
|
+ // 筛选出 城市
|
|
|
+ luckPrizeList = luckPrizeService.filterCustomTypeAndCity(luckPrizeList, raffleReqDTO.getCityName());
|
|
|
|
|
|
// 计算总权重并随机选择奖品
|
|
|
int totalWeight = luckPrizeList.stream().mapToInt(prize -> prize.getChance() * prize.getTotal()).sum();
|
|
@@ -181,20 +178,23 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
// 扣除奖品剩余数量
|
|
|
if (null == selectedPrize) {
|
|
|
// 炸了
|
|
|
- return ;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
if (selectedPrize.getTotal() <= 0) {
|
|
|
// 没数了
|
|
|
- return ;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- selectedPrize.setTotal(selectedPrize.getTotal() - 1);
|
|
|
- luckPrizeService.updateById(selectedPrize);
|
|
|
+ // 冻结奖品数量
|
|
|
+ luckPrizeService.freeze(selectedPrize.getId(), 1);
|
|
|
|
|
|
// 插入抽奖记录
|
|
|
LuckLotteryRecordDO luckLotteryRecord = LuckLotteryRecordDO.builder()
|
|
|
- .userId(userId)
|
|
|
+ .userId(raffleReqDTO.getUserId())
|
|
|
+ .orderId(raffleReqDTO.getOrderId())
|
|
|
+ .spuId(raffleReqDTO.getSpuId())
|
|
|
+ .skuId(raffleReqDTO.getSkuId())
|
|
|
.lotteryId(luckLottery.getId())
|
|
|
.prizeId(selectedPrize.getId())
|
|
|
.type(selectedPrize.getType())
|
|
@@ -204,6 +204,27 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
.build();
|
|
|
luckLotteryRecordMapper.insert(luckLotteryRecord);
|
|
|
|
|
|
+ return LuckLotteryRecordConvert.INSTANCE.convert(luckLotteryRecord);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @DSTransactional
|
|
|
+ public void refund(LuckRaffleReqDTO raffleReqDTO) {
|
|
|
+ LuckLotteryRecordDO record = luckLotteryRecordMapper.selectOne(new LambdaQueryWrapperX<LuckLotteryRecordDO>()
|
|
|
+ .eq(LuckLotteryRecordDO::getOrderId, raffleReqDTO.getOrderId())
|
|
|
+ .eq(LuckLotteryRecordDO::getSpuId, raffleReqDTO.getSpuId())
|
|
|
+ .eq(LuckLotteryRecordDO::getSkuId, raffleReqDTO.getSkuId())
|
|
|
+ .eq(LuckLotteryRecordDO::getUserId, raffleReqDTO.getUserId())
|
|
|
+ );
|
|
|
+ if (null == record) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解冻数量
|
|
|
+ luckPrizeService.unfreeze(record.getPrizeId(), 1);
|
|
|
+ // 删除抽奖记录
|
|
|
+ luckLotteryRecordMapper.deleteById(record.getId());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -218,7 +239,6 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
// 查询用户
|
|
|
UserInfoRespDTO user = mendunerUserApi.getUser(userId).getCheckedData();
|
|
|
for (LuckLotteryRecordDO record : luckLotteryRecordList) {
|
|
|
-
|
|
|
list.add(LuckLotteryRecordConvert.INSTANCE.convertDetail(
|
|
|
LuckLotteryRecordConvert.INSTANCE.convert(record),
|
|
|
luckLotteryService.detail(record.getLotteryId()),
|
|
@@ -263,12 +283,15 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @DSTransactional
|
|
|
public void receive(AppLuckLotteryReceiveReqVO reqVO, Long userId) {
|
|
|
LuckLotteryRecordDO luckLotteryRecord = validateLuckLotteryRecordExists(reqVO.getId());
|
|
|
if (luckLotteryRecord.getUserId().equals(userId)) {
|
|
|
// 是自己领取的
|
|
|
luckLotteryRecord.setIsReceive(true);
|
|
|
luckLotteryRecord.setReceiveInfo(reqVO.getReceiveInfo());
|
|
|
+ // 扣除奖品数量
|
|
|
+ luckPrizeService.deduct(luckLotteryRecord.getPrizeId(), 1);
|
|
|
luckLotteryRecordMapper.updateById(luckLotteryRecord);
|
|
|
}
|
|
|
}
|
|
@@ -287,4 +310,25 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
luckPrizeService.detail(record.getPrizeId()),
|
|
|
user);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<LuckLotteryRecordDetailRespVO> getByOrderId(Long orderId, Long userId) {
|
|
|
+ List<LuckLotteryRecordDO> list = luckLotteryRecordMapper
|
|
|
+ .selectList(LuckLotteryRecordDO::getOrderId, orderId);
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 同一个订单下单的绝对只有一个用户
|
|
|
+ UserInfoRespDTO user =
|
|
|
+ mendunerUserApi.getUser(userId).getCheckedData();
|
|
|
+ List<LuckLotteryRecordDetailRespVO> respList = new ArrayList<>();
|
|
|
+ for (LuckLotteryRecordDO record : list) {
|
|
|
+ respList.add(LuckLotteryRecordConvert.INSTANCE.convertDetail(
|
|
|
+ LuckLotteryRecordConvert.INSTANCE.convert(record),
|
|
|
+ luckLotteryService.detail(record.getLotteryId()),
|
|
|
+ luckPrizeService.detail(record.getPrizeId()),
|
|
|
+ user));
|
|
|
+ }
|
|
|
+ return respList;
|
|
|
+ }
|
|
|
}
|