|
@@ -2,6 +2,8 @@ package com.citu.module.promotion.service.luck;
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.lock.annotation.Lock4j;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.citu.framework.common.pojo.PageResult;
|
|
|
import com.citu.framework.common.util.object.BeanUtils;
|
|
|
import com.citu.module.product.api.spu.ProductSpuApi;
|
|
@@ -11,7 +13,9 @@ import com.citu.module.promotion.controller.admin.luck.vo.LuckLotteryPageReqVO;
|
|
|
import com.citu.module.promotion.controller.admin.luck.vo.LuckLotterySaveReqVO;
|
|
|
import com.citu.module.promotion.convert.luck.LuckLotteryConvert;
|
|
|
import com.citu.module.promotion.dal.dataobject.luck.LuckLotteryDO;
|
|
|
+import com.citu.module.promotion.dal.dataobject.luck.LuckUserDO;
|
|
|
import com.citu.module.promotion.dal.mysql.luck.LuckLotteryMapper;
|
|
|
+import com.citu.module.promotion.dal.mysql.luck.LuckUserMapper;
|
|
|
import com.citu.module.promotion.enums.luck.LuckLotteryFactorEnum;
|
|
|
import com.citu.module.promotion.enums.luck.LuckStatusEnum;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -41,6 +45,9 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
@Resource
|
|
|
private ProductSpuApi productSpuApi;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private LuckUserMapper luckUserMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public Long createLuckLottery(LuckLotterySaveReqVO createReqVO) {
|
|
|
// 插入
|
|
@@ -56,7 +63,7 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
throw exception(LUCK_LOTTERY_PRODUCT_NOT_SELECTED);
|
|
|
}
|
|
|
for (String spuId : createReqVO.getFactorInfo()) {
|
|
|
- LuckLotteryDO luckLottery = luckLotteryMapper.getLuckLotteryListBySpuId(spuId,null);
|
|
|
+ LuckLotteryDO luckLottery = luckLotteryMapper.getLuckLotteryListBySpuId(spuId, null);
|
|
|
if (null != luckLottery) {
|
|
|
// 已经存在
|
|
|
throw exception(LUCK_LOTTERY_PRODUCT_USED);
|
|
@@ -74,7 +81,7 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
public void updateLuckLottery(LuckLotterySaveReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
LuckLotteryDO luckLottery = validateLuckLotteryExists(updateReqVO.getId());
|
|
|
- LuckLotteryDO exist = luckLotteryMapper.selectByName(updateReqVO.getName());
|
|
|
+ LuckLotteryDO exist = luckLotteryMapper.selectByName(updateReqVO.getName());
|
|
|
if (null != exist &&
|
|
|
!Objects.equals(luckLottery.getId(), exist.getId())) {
|
|
|
throw exception(LUCK_LOTTERY_NAME_USED, updateReqVO.getName());
|
|
@@ -88,7 +95,7 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
throw exception(LUCK_LOTTERY_PRODUCT_NOT_SELECTED);
|
|
|
}
|
|
|
for (String spuId : updateReqVO.getFactorInfo()) {
|
|
|
- LuckLotteryDO spuExist = luckLotteryMapper.getLuckLotteryListBySpuId(spuId,null);
|
|
|
+ LuckLotteryDO spuExist = luckLotteryMapper.getLuckLotteryListBySpuId(spuId, null);
|
|
|
if (null != spuExist && !Objects.equals(luckLottery.getId(), spuExist.getId())) {
|
|
|
// 已经存在
|
|
|
throw exception(LUCK_LOTTERY_PRODUCT_USED);
|
|
@@ -125,6 +132,12 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
@Override
|
|
|
public LuckLotteryDO valid(Long id) {
|
|
|
LuckLotteryDO luckLottery = validateLuckLotteryExists(id);
|
|
|
+ checkTimeExpired(luckLottery);
|
|
|
+ return luckLottery;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkTimeExpired(LuckLotteryDO luckLottery) {
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
// 判断活动有没有开始,没有开始抛出异常
|
|
|
if (luckLottery.getStartTime().isAfter(now)) {
|
|
@@ -134,7 +147,6 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
if (luckLottery.getEndTime().isBefore(now)) {
|
|
|
throw exception(LUCK_LOTTERY_END);
|
|
|
}
|
|
|
- return luckLottery;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -183,7 +195,60 @@ public class LuckLotteryServiceImpl implements LuckLotteryService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public LuckLotteryDO getLuckLotteryListBySpuId(String spuId) {
|
|
|
+ public LuckLotteryDO getLuckLotteryBySpuId(String spuId) {
|
|
|
return luckLotteryMapper.getLuckLotteryListBySpuId(spuId, LuckStatusEnum.ENABLE.getStatus());
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Lock4j(keys = {"#spuId", "#userId"}, acquireTimeout = 6000)
|
|
|
+ public void updateUserNumIncr(Long spuId, Long userId) {
|
|
|
+ // 根据spuId获取抽奖活动
|
|
|
+ LuckLotteryDO luckLottery = getLuckLotteryBySpuId(String.valueOf(spuId));
|
|
|
+ if (null == luckLottery) {
|
|
|
+ // 没有对应的活动
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 校验活动
|
|
|
+ checkTimeExpired(luckLottery);
|
|
|
+ LuckUserDO luckUser = luckUserMapper.getByUserId(luckLottery.getId(), userId);
|
|
|
+ if (null == luckUser) {
|
|
|
+ // 不存在
|
|
|
+ luckUser = new LuckUserDO();
|
|
|
+ luckUser.setLotteryId(luckLottery.getId());
|
|
|
+ luckUser.setUserId(userId);
|
|
|
+ luckUser.setNum(1);
|
|
|
+ luckUserMapper.insert(luckUser);
|
|
|
+ } else {
|
|
|
+ // 存在
|
|
|
+ luckUser.setNum(luckUser.getNum() + 1);
|
|
|
+ luckUserMapper.updateById(luckUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Lock4j(keys = {"#lotteryId", "#userId"}, acquireTimeout = 6000)
|
|
|
+ public void updateUserNumDecr(Long lotteryId, Long userId) {
|
|
|
+ LuckLotteryDO luckLottery = valid(lotteryId);
|
|
|
+ Integer num = getUserNum(luckLottery.getId(), userId);
|
|
|
+ if (num <= 0) {
|
|
|
+ // 没有抽奖的额度
|
|
|
+ throw exception(LUCK_LOTTERY_USER_NUM_EXCEED);
|
|
|
+ }
|
|
|
+ luckUserMapper.update(new LambdaUpdateWrapper<LuckUserDO>()
|
|
|
+ .eq(LuckUserDO::getLotteryId, luckLottery.getId())
|
|
|
+ .eq(LuckUserDO::getUserId, userId)
|
|
|
+ .eq(LuckUserDO::getNum, num)
|
|
|
+ .setSql("num = num - 1")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer getUserNum(Long lotteryId, Long userId) {
|
|
|
+ LuckUserDO luckUser =
|
|
|
+ luckUserMapper.selectOne(LuckUserDO::getLotteryId, lotteryId, LuckUserDO::getUserId, userId);
|
|
|
+ if (null == luckUser) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return luckUser.getNum();
|
|
|
+ }
|
|
|
}
|