|
@@ -17,11 +17,12 @@ 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.dal.mysql.luck.LuckUserMapper;
|
|
|
+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;
|
|
@@ -29,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.LUCK_LOTTERY_RECORD_NOT_EXISTS;
|
|
|
+import static com.citu.module.promotion.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 幸运抽奖-抽奖记录 Service 实现类
|
|
@@ -148,6 +149,62 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
return LuckLotteryRecordConvert.INSTANCE.convert(luckLotteryRecord);
|
|
|
}
|
|
|
|
|
|
+ @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();
|
|
|
+ // 判断活动有没有开始,判断活动有没有结束
|
|
|
+ if (luckLottery.getStartTime().isAfter(now)||luckLottery.getEndTime().isBefore(now)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取奖品
|
|
|
+ List<LuckPrizeDO> luckPrizeList = luckPrizeService.getListByLotteryId(luckLottery.getId());
|
|
|
+ if (CollUtil.isEmpty(luckPrizeList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 筛选出 喜爱城市
|
|
|
+ luckPrizeList = luckPrizeList.stream().filter(prize->
|
|
|
+ prize.getType().equals(LuckPrizeTypeEnum.CUSTOM.getType())
|
|
|
+ && prize.getExtend().getCityName().equals(cityName)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 计算总权重并随机选择奖品
|
|
|
+ int totalWeight = luckPrizeList.stream().mapToInt(prize -> prize.getChance() * prize.getTotal()).sum();
|
|
|
+ Random random = ThreadLocalRandom.current();
|
|
|
+ int randomNumber = random.nextInt(totalWeight);
|
|
|
+
|
|
|
+ LuckPrizeDO selectedPrize = luckPrizeService.selectPrizeByWeight(randomNumber, luckPrizeList);
|
|
|
+
|
|
|
+ // 扣除奖品剩余数量
|
|
|
+ if (null == selectedPrize) {
|
|
|
+ // 炸了
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (selectedPrize.getTotal() <= 0) {
|
|
|
+ // 没数了
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ selectedPrize.setTotal(selectedPrize.getTotal() - 1);
|
|
|
+ luckPrizeService.updateById(selectedPrize);
|
|
|
+
|
|
|
+ // 插入抽奖记录
|
|
|
+ LuckLotteryRecordDO luckLotteryRecord = LuckLotteryRecordDO.builder()
|
|
|
+ .userId(userId)
|
|
|
+ .lotteryId(luckLottery.getId())
|
|
|
+ .prizeId(selectedPrize.getId())
|
|
|
+ .type(selectedPrize.getType())
|
|
|
+ .extend(selectedPrize.getExtend())
|
|
|
+ .isReceive(false)
|
|
|
+ .isDeliver(false)
|
|
|
+ .build();
|
|
|
+ luckLotteryRecordMapper.insert(luckLotteryRecord);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public List<LuckLotteryRecordDetailRespVO> getLuckLotteryRecordList(Long lotteryId, Long userId) {
|
|
@@ -214,6 +271,20 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
|
|
|
luckLotteryRecord.setReceiveInfo(reqVO.getReceiveInfo());
|
|
|
luckLotteryRecordMapper.updateById(luckLotteryRecord);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public LuckLotteryRecordDetailRespVO getLast(Long lotteryId, Long userId) {
|
|
|
+ LuckLotteryRecordDO record = luckLotteryRecordMapper.getLast(lotteryId, userId);
|
|
|
+ if (null == record) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ UserInfoRespDTO user =
|
|
|
+ mendunerUserApi.getUser(record.getUserId()).getCheckedData();
|
|
|
+ return LuckLotteryRecordConvert.INSTANCE.convertDetail(
|
|
|
+ LuckLotteryRecordConvert.INSTANCE.convert(record),
|
|
|
+ luckLotteryService.detail(record.getLotteryId()),
|
|
|
+ luckPrizeService.detail(record.getPrizeId()),
|
|
|
+ user);
|
|
|
}
|
|
|
}
|