1. add lottery
This commit is contained in:
2026-02-14 21:42:38 +08:00
parent 8be8964eff
commit 9674374fad
16 changed files with 254 additions and 11 deletions

View File

@@ -10,17 +10,11 @@ import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.AddressRemoveRequest;
import org.springblade.modules.api.request.AddressSubmitRequest;
import org.springblade.modules.api.request.CreateOrderRequest;
import org.springblade.modules.api.request.PaymentRequest;
import org.springblade.modules.api.request.*;
import org.springblade.modules.api.response.CreateOrderResponse;
import org.springblade.modules.api.response.PaymentResponse;
import org.springblade.modules.api.vo.*;
import org.springblade.modules.common.service.ICommonUserAddressService;
import org.springblade.modules.common.service.ICommonUserOrderService;
import org.springblade.modules.common.service.ICommonUserNoticeService;
import org.springblade.modules.common.service.ICommonUserPointService;
import org.springblade.modules.common.service.*;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
@@ -44,6 +38,10 @@ public class APPUserController {
private final ICommonUserPointService pointService;
private final ICommonLotteryRecordService lotteryRecordService;
private final ICommonLotteryPrizeService lotteryPrizeService;
/**
* 是否修改过密码
@@ -181,4 +179,25 @@ public class APPUserController {
return R.data(pointService.queryPoint());
}
@GetMapping("/lottery-prizes")
@ApiOperationSupport(order = 15)
@Operation(summary = "抽奖记录")
public R<List<CommonLotteryPrizeApiVO>> lotteryPrizes() {
return R.data(lotteryPrizeService.staticList());
}
@GetMapping("/lottery-records")
@ApiOperationSupport(order = 16)
@Operation(summary = "抽奖记录")
public R<IPage<CommonLotteryRecordApiVO>> lotteryRecords(Query query) {
return R.data(lotteryRecordService.selectLotteryRecordApiPage(Condition.getPage(query), new CommonLotteryRecordApiVO()));
}
@PostMapping("/lottery-draw")
@ApiOperationSupport(order = 17)
@Operation(summary = "用户抽奖", description = "传入LotteriesDrawRequest")
public R<CommonLotteryPrizeApiVO> draw(@Valid @RequestBody LotteryDrawRequest request) {
return R.data(lotteryPrizeService.draw(request));
}
}

View File

@@ -0,0 +1,15 @@
package org.springblade.modules.api.request;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class LotteryDrawRequest implements Serializable {
@NotNull(message = "无效积分")
private BigDecimal amount;
}

View File

@@ -0,0 +1,16 @@
package org.springblade.modules.api.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class CommonLotteryPrizeApiVO implements Serializable {
private Long id;
private String prizeName;
}

View File

@@ -0,0 +1,20 @@
package org.springblade.modules.api.vo;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class CommonLotteryRecordApiVO implements Serializable {
private Long id;
private String prizeName;
private BigDecimal pointsUsed;
private Date createTime;
}

View File

@@ -8,7 +8,8 @@ public enum BusinessModule {
SELF_OPERATED_MALL("自营商城", "self_mall"),
ECOSYSTEM_PRODUCT("生态商品", "eco_product"),
TRAVEL_GROUP("旅游团", "travel_group");
TRAVEL_GROUP("旅游团", "travel_group"),
LOTTERY("幸运抽奖", "lottery");
@Getter
private final String displayName;

View File

@@ -0,0 +1,19 @@
package org.springblade.modules.common.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springblade.modules.common.eums.BusinessModule;
import java.math.BigDecimal;
@Data
@AllArgsConstructor
public class SpendUserPointEvent {
private Long userId;
private BigDecimal point;
private BusinessModule businessModule;
}

View File

@@ -7,6 +7,7 @@ import org.springblade.modules.common.eums.OrderStatus;
import org.springblade.modules.common.eums.UserPointFlowType;
import org.springblade.modules.common.event.GrantUserPointEvent;
import org.springblade.modules.common.event.PaymentSuccessEvent;
import org.springblade.modules.common.event.SpendUserPointEvent;
import org.springblade.modules.common.pojo.entity.CommonUserNoticeEntity;
import org.springblade.modules.common.service.ICommonUserNoticeService;
import org.springframework.scheduling.annotation.Async;
@@ -56,4 +57,21 @@ public class NoticeListener {
log.warn("用户获取积分事务回滚,orderId={}", event.getOrderId());
}
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onSpendUserPoint(SpendUserPointEvent event) {
CommonUserNoticeEntity notice = new CommonUserNoticeEntity();
notice.setUserId(event.getUserId());
notice.setNoticeType(NoticeType.APP_USER.getCode());
notice.setModuleType(event.getBusinessModule().getCode());
notice.setTitle(event.getBusinessModule().getDisplayName());
notice.setContent(UserPointFlowType.SPEND.formatUserMessage(null, String.valueOf(event.getPoint())));
noticeService.save(notice);
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void onSpendUserPointFailure(SpendUserPointEvent event) {
log.warn("用户获取积分事务回滚,userId={}", event.getUserId());
}
}

View File

@@ -25,6 +25,7 @@
*/
package org.springblade.modules.common.mapper;
import org.springblade.modules.api.vo.CommonLotteryRecordApiVO;
import org.springblade.modules.common.pojo.entity.CommonLotteryRecordEntity;
import org.springblade.modules.common.pojo.vo.CommonLotteryRecordVO;
import org.springblade.modules.common.excel.CommonLotteryRecordExcel;
@@ -60,4 +61,5 @@ public interface CommonLotteryRecordMapper extends BaseMapper<CommonLotteryRecor
*/
List<CommonLotteryRecordExcel> exportLotteryRecord(@Param("ew") Wrapper<CommonLotteryRecordEntity> queryWrapper);
}

View File

@@ -21,6 +21,9 @@
<select id="selectLotteryRecordPage" resultMap="lotteryRecordResultMap">
select * from common_lottery_record where is_deleted = 0
<if test="lotteryRecord.userId!=null">
and user_id = #{lotteryRecord.userId}
</if>
</select>

View File

@@ -25,11 +25,15 @@
*/
package org.springblade.modules.common.service;
import org.springblade.modules.api.request.LotteryDrawRequest;
import org.springblade.modules.api.vo.CommonLotteryPrizeApiVO;
import org.springblade.modules.common.pojo.entity.CommonLotteryPrizeEntity;
import org.springblade.modules.common.pojo.vo.CommonLotteryPrizeVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import java.util.List;
/**
* 奖品配置表 服务类
*
@@ -46,4 +50,7 @@ public interface ICommonLotteryPrizeService extends BaseService<CommonLotteryPri
*/
IPage<CommonLotteryPrizeVO> selectLotteryPrizePage(IPage<CommonLotteryPrizeVO> page, CommonLotteryPrizeVO lotteryPrize);
CommonLotteryPrizeApiVO draw(LotteryDrawRequest request);
List<CommonLotteryPrizeApiVO> staticList();
}

View File

@@ -26,11 +26,14 @@
package org.springblade.modules.common.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springblade.modules.api.vo.CommonLotteryRecordApiVO;
import org.springblade.modules.common.pojo.entity.CommonLotteryRecordEntity;
import org.springblade.modules.common.pojo.vo.CommonLotteryRecordVO;
import org.springblade.modules.common.excel.CommonLotteryRecordExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -58,4 +61,7 @@ public interface ICommonLotteryRecordService extends BaseService<CommonLotteryRe
*/
List<CommonLotteryRecordExcel> exportLotteryRecord(Wrapper<CommonLotteryRecordEntity> queryWrapper);
IPage<CommonLotteryRecordApiVO> selectLotteryRecordApiPage(IPage<CommonLotteryRecordApiVO> page, CommonLotteryRecordApiVO vo);
Boolean addRecord(Long userId, BigDecimal amount);
}

View File

@@ -28,4 +28,6 @@ public interface ICommonUserPointService extends BaseService<CommonUserPointEnti
BigDecimal queryPoint();
void grantPointsByUserOrder(CommonUserOrderEntity order);
void spendPointsForLottery(BigDecimal amount, Long userId);
}

View File

@@ -25,13 +25,30 @@
*/
package org.springblade.modules.common.service.impl;
import io.jsonwebtoken.lang.Assert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.debounce.DebounceClient;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.api.request.LotteryDrawRequest;
import org.springblade.modules.api.vo.CommonLotteryPrizeApiVO;
import org.springblade.modules.common.pojo.entity.CommonLotteryPrizeEntity;
import org.springblade.modules.common.pojo.vo.CommonLotteryPrizeVO;
import org.springblade.modules.common.mapper.CommonLotteryPrizeMapper;
import org.springblade.modules.common.service.ICommonLotteryPrizeService;
import org.springblade.modules.common.service.ICommonLotteryRecordService;
import org.springblade.modules.common.service.ICommonUserPointService;
import org.springblade.modules.market.eums.MarketBusinessModule;
import org.springblade.modules.market.pojo.dto.MarketCashCouponApiDTO;
import org.springblade.modules.market.service.IMarketCashCouponService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 奖品配置表 服务实现类
@@ -39,12 +56,63 @@ import org.springblade.core.mp.base.BaseServiceImpl;
* @author RayLiu
* @since 2026-02-14
*/
@Slf4j
@Service
@AllArgsConstructor
public class CommonLotteryPrizeServiceImpl extends BaseServiceImpl<CommonLotteryPrizeMapper, CommonLotteryPrizeEntity> implements ICommonLotteryPrizeService {
private final DebounceClient debounceClient;
private final ICommonUserPointService userPointService;
private final IMarketCashCouponService cashCouponService;
private final ICommonLotteryRecordService lotteryRecordService;
private static final List<CommonLotteryPrizeApiVO> STATIC_PRIZE_LIST = List.of(
new CommonLotteryPrizeApiVO(0L, "谢谢参与"),
new CommonLotteryPrizeApiVO(1L, "100积分"),
new CommonLotteryPrizeApiVO(2L, "1000积分"),
new CommonLotteryPrizeApiVO(3L, "生态礼包"),
new CommonLotteryPrizeApiVO(4L, "黑茶礼盒"),
new CommonLotteryPrizeApiVO(5L, "幸运锦鲤")
);
private static final CommonLotteryPrizeApiVO PRIZE = new CommonLotteryPrizeApiVO(0L, "谢谢参与");
@Override
public IPage<CommonLotteryPrizeVO> selectLotteryPrizePage(IPage<CommonLotteryPrizeVO> page, CommonLotteryPrizeVO lotteryPrize) {
return page.setRecords(baseMapper.selectLotteryPrizePage(page, lotteryPrize));
}
@Override
@Transactional(rollbackFor = Exception.class)
public CommonLotteryPrizeApiVO draw(LotteryDrawRequest request) {
Long userId = AuthUtil.getUserId();
String key = "lottery:" + userId;
if (debounceClient.tryDebounce(key, 3, TimeUnit.SECONDS)) {
Assert.isTrue(request.getAmount().compareTo(BigDecimal.valueOf(100)) == 0 || request.getAmount().compareTo(BigDecimal.valueOf(1000)) == 0, "无效积分");
userPointService.spendPointsForLottery(request.getAmount(), userId);
Boolean addCashCoupon = cashCouponService.addCashCoupon(new MarketCashCouponApiDTO().setAmount(request.getAmount()).setUserId(userId).setSourceType(MarketBusinessModule.LOTTERY));
if (!addCashCoupon) {
log.error("新增现金券失败");
throw new RuntimeException("操作失败");
}
Boolean addedRecord = lotteryRecordService.addRecord(userId, request.getAmount());
if (!addedRecord) {
log.error("新增抽奖记录失败");
throw new RuntimeException("操作失败");
}
} else {
long remaining = debounceClient.getRemainingTime(key, TimeUnit.SECONDS);
throw new RuntimeException("操作过于频繁,请" + remaining + "秒后重试");
}
return PRIZE;
}
@Override
public List<CommonLotteryPrizeApiVO> staticList() {
return STATIC_PRIZE_LIST;
}
}

View File

@@ -25,6 +25,8 @@
*/
package org.springblade.modules.common.service.impl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.api.vo.CommonLotteryRecordApiVO;
import org.springblade.modules.common.pojo.entity.CommonLotteryRecordEntity;
import org.springblade.modules.common.pojo.vo.CommonLotteryRecordVO;
import org.springblade.modules.common.excel.CommonLotteryRecordExcel;
@@ -34,6 +36,8 @@ import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -60,4 +64,28 @@ public class CommonLotteryRecordServiceImpl extends BaseServiceImpl<CommonLotter
return lotteryRecordList;
}
@Override
public IPage<CommonLotteryRecordApiVO> selectLotteryRecordApiPage(IPage<CommonLotteryRecordApiVO> page, CommonLotteryRecordApiVO vo) {
CommonLotteryRecordVO lotteryRecord = new CommonLotteryRecordVO();
lotteryRecord.setUserId(AuthUtil.getUserId());
List<CommonLotteryRecordVO> voList = baseMapper.selectLotteryRecordPage(page, lotteryRecord);
List<CommonLotteryRecordApiVO> list = voList.stream().map(record -> {
CommonLotteryRecordApiVO apiVO = new CommonLotteryRecordApiVO();
apiVO.setId(record.getId());
apiVO.setPrizeName(record.getPrizeId() == null ? "谢谢参与。" : "中奖了!");
apiVO.setPointsUsed(record.getPointsUsed());
apiVO.setCreateTime(record.getCreateTime());
return apiVO;
}).toList();
return page.setRecords(list);
}
@Override
public Boolean addRecord(Long userId, BigDecimal amount) {
CommonLotteryRecordEntity lotteryRecord = new CommonLotteryRecordEntity();
lotteryRecord.setUserId(userId);
lotteryRecord.setPointsUsed(amount);
return save(lotteryRecord);
}
}

View File

@@ -8,6 +8,7 @@ import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.common.eums.BusinessModule;
import org.springblade.modules.common.eums.UserPointFlowType;
import org.springblade.modules.common.event.GrantUserPointEvent;
import org.springblade.modules.common.event.SpendUserPointEvent;
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
import org.springblade.modules.common.pojo.entity.CommonUserPointEntity;
import org.springblade.modules.common.pojo.vo.CommonUserPointVO;
@@ -22,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import static org.springblade.modules.common.eums.BusinessModule.LOTTERY;
import static org.springblade.modules.common.eums.BusinessModule.SELF_OPERATED_MALL;
/**
@@ -73,7 +75,24 @@ public class CommonUserPointServiceImpl extends BaseServiceImpl<CommonUserPointM
return;
}
}
throw new RuntimeException("奖励积分失败");
throw new RuntimeException("奖励用户积分失败");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void spendPointsForLottery(BigDecimal amount, Long userId) {
Assert.isTrue(amount.compareTo(BigDecimal.valueOf(100)) == 0 || amount.compareTo(BigDecimal.valueOf(1000)) == 0, "无效积分");
Assert.notNull(userId, "无效用户");
CommonUserPointEntity userPoint = getById(userId);
BigDecimal lastAmount = userPoint.getAmount();
int i = baseMapper.deductUserPoint(userId, amount, userPoint.getVersion());
if (i > 0) {
if (userPointLogService.recordPointChangeLog(userPoint, lastAmount, LOTTERY.getCode().toUpperCase(), amount, UserPointFlowType.GRANT.getCode(), null, null)) {
eventPublisher.publishEvent(new SpendUserPointEvent(userId, amount, LOTTERY));
return;
}
}
throw new RuntimeException("消费用户积分失败");
}

View File

@@ -62,8 +62,8 @@ public class MarketCashCouponServiceImpl extends BaseServiceImpl<MarketCashCoupo
return page.setRecords(baseMapper.selectCommonCashCouponPage(page, commonCashCoupon));
}
@Transactional(rollbackFor = RuntimeException.class)
@Override
@Transactional(rollbackFor = RuntimeException.class)
public Boolean addCashCoupon(MarketCashCouponApiDTO dto) {
MarketCashCouponLogEntity cashCouponLog = MarketUserMarketInfoConvert.INSTANCE.convertCashCouponDTOToEntity(dto);
cashCouponLog.setSourceType(dto.getSourceType().getCode());