礼包赠送过期校验

This commit is contained in:
2025-07-07 16:15:26 +08:00
parent 24ec417907
commit c3a55d8eea
3 changed files with 101 additions and 62 deletions

View File

@@ -969,6 +969,8 @@ public enum BusiStatus {
GUILD_USD_WITHDRAW_ACCOUNT_TYPE_ERROR(500, "提現方式錯誤,請重新綁定帳戶後重試。"),
PACK_NUM_NOT_ENOUGH(500, "礼包数量不足"),
PACK_EXPIRE(500, "PACK EXPIRE"),
;
private final int value;

View File

@@ -17,13 +17,17 @@ import com.accompany.business.vo.packcenter.PackItemRewardVo;
import com.accompany.business.vo.packcenter.UserPackRecordVo;
import com.accompany.business.vo.packcenter.UserPackVo;
import com.accompany.common.result.BusiResult;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.DateTimeUtil;
import com.accompany.common.utils.PageUtil;
import com.accompany.core.enumeration.PartitionEnum;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Users;
import com.accompany.core.util.I18NMessageSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -32,12 +36,16 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.accompany.common.redis.RedisKey.lock_user_pack;
import static com.accompany.common.status.BusiStatus.PACK_EXPIRE;
import static com.accompany.common.status.BusiStatus.PACK_NUM_NOT_ENOUGH;
import static com.accompany.core.enumeration.I18nAlertEnum.PACK_SEND_SYS;
import static com.accompany.core.enumeration.I18nAlertEnum.PACK_USE_SELF_SYS;
@Slf4j
@Service
public class PackCenterService {
@Autowired
@@ -58,6 +66,8 @@ public class PackCenterService {
private ActivityH5AwardRecordService activityH5AwardRecordService;
@Autowired
private BaseSendService baseSendService;
@Autowired
private RedissonClient redissonClient;
public List<UserPackVo> userPackList(Long uid, Integer pageNo, Integer pageSize) {
@@ -129,77 +139,102 @@ public class PackCenterService {
}
public BusiResult<Void> usePack(Long uid, Long targetUid, Long userPackId, Integer useNum) {
log.info("pack-send,uid:{}, targetUid:{}, userPackId:{}, useNum:{}", uid, targetUid, userPackId, useNum);
Users users = usersService.getUsersByUid(uid);
Users targetUser = usersService.getUsersByUid(targetUid);
if (!users.getPartitionId().equals(targetUser.getPartitionId())) {
throw new ServiceException("PARTITION ERROR");
}
UserPack userPack = userPackService.getById(userPackId);
Integer remainNum = userPack.getPackNum() - userPack.getUseNum();
if (remainNum < useNum) {
throw new ServiceException(PACK_NUM_NOT_ENOUGH);
}
userPackService.packUseNumAdd(userPackId, useNum);
Date createTime = new Date();
UserUsePackRecord usePackRecord = new UserUsePackRecord();
usePackRecord.setUserPackId(userPackId);
usePackRecord.setTargetUid(targetUid);
usePackRecord.setUid(uid);
usePackRecord.setPackId(userPack.getPackId());
usePackRecord.setPackNum(useNum);
usePackRecord.setCreateTime(createTime);
PackInfo packInfo = packInfoService.getById(userPack.getPackId());
String i18nId = PackInfo.class.getSimpleName() + StrUtil.DOT + packInfo.getPackName();
String packName = I18NMessageSourceUtil.getMessage(i18nId, packInfo.getPackName(), targetUid);
String message;
if (uid.equals(targetUid)) {
usePackRecord.setUseType(PackConstant.PackUseType.SELF);
List<PackItem> packItems = packItemService.listByPackId(userPack.getPackId());
List<Long> awardIds = packItems.stream().map(PackItem::getAwardId).collect(Collectors.toList());
List<ActivityH5LevelAward> activityH5LevelAwards = activityH5LevelAwardService.listByIds(awardIds);
for (int i = 0; i < useNum; i++) {
for (ActivityH5LevelAward award : activityH5LevelAwards) {
award.setAwardNum(award.getAwardNum() * useNum);
activityH5AwardRecordService.sendDetailAward(targetUid, targetUser.getPartitionId(), "" , award, ActivityType.PACK_CENTER_REWARD);
}
String lockKey = lock_user_pack.getKey(userPackId.toString());
RLock lock = redissonClient.getLock(lockKey);
boolean isLocked = false;
try {
isLocked = lock.tryLock(3, TimeUnit.SECONDS);
if (!isLocked){
throw new ServiceException(BusiStatus.SERVERBUSY);
}
usePackRecord.setNumberList(awardIds);
message = I18NMessageSourceUtil.getMessage(PACK_USE_SELF_SYS, new Object[]{packName}, targetUser.getPartitionId());
} else {
usePackRecord.setUseType(PackConstant.PackUseType.SEND);
UserPack userPack = userPackService.getById(userPackId);
Integer remainNum = userPack.getPackNum() - userPack.getUseNum();
if (remainNum < useNum || PackConstant.UserPackStatus.valid != userPack.getStatus()) {
throw new ServiceException(PACK_NUM_NOT_ENOUGH);
}
if (userPack.getExpireTime().before(new Date())) {
userPack.setStatus(PackConstant.UserPackStatus.invalid);
userPackService.updateById(userPack);
throw new ServiceException(PACK_EXPIRE);
}
userPackService.packUseNumAdd(userPackId, useNum);
UserPack pack = new UserPack();
Integer packId = userPack.getPackId();
pack.setPackId(packId);
pack.setUid(targetUid);
pack.setPackNum(useNum);
pack.setSource(PackConstant.PackSource.USER_SEND);
pack.setStatus(PackConstant.UserPackStatus.valid);
pack.setCreateTime(createTime);
pack.setExpireTime(userPack.getExpireTime());
pack.setUpdateTime(createTime);
userPackService.save(pack);
if ((remainNum - useNum) == 0) {
userPack.setStatus(PackConstant.UserPackStatus.empty);
userPackService.updateById(userPack);
}
UserPackRecord packRecord = new UserPackRecord();
packRecord.setPackId(packId);
packRecord.setUid(uid);
packRecord.setUserPackId(pack.getId());
packRecord.setPackNum(useNum);
packRecord.setSendUid(uid);
packRecord.setSource(PackConstant.PackSource.USER_SEND);
packRecord.setStatus(PackConstant.UserPackRecordStatus.NORMAL);
packRecord.setCreateTime(createTime);
packRecord.setExpireDay(0);
packRecord.setExpireTime(userPack.getExpireTime());
userPackRecordService.save(packRecord);
message = I18NMessageSourceUtil.getMessage(PACK_SEND_SYS, new Object[]{users.getNick(), packName, useNum}, targetUid);
Date createTime = new Date();
UserUsePackRecord usePackRecord = new UserUsePackRecord();
usePackRecord.setUserPackId(userPackId);
usePackRecord.setTargetUid(targetUid);
usePackRecord.setUid(uid);
usePackRecord.setPackId(userPack.getPackId());
usePackRecord.setPackNum(useNum);
usePackRecord.setCreateTime(createTime);
PackInfo packInfo = packInfoService.getById(userPack.getPackId());
String i18nId = PackInfo.class.getSimpleName() + StrUtil.DOT + packInfo.getPackName();
String packName = I18NMessageSourceUtil.getMessage(i18nId, packInfo.getPackName(), targetUid);
String message;
if (uid.equals(targetUid)) {
usePackRecord.setUseType(PackConstant.PackUseType.SELF);
List<PackItem> packItems = packItemService.listByPackId(userPack.getPackId());
List<Long> awardIds = packItems.stream().map(PackItem::getAwardId).collect(Collectors.toList());
List<ActivityH5LevelAward> activityH5LevelAwards = activityH5LevelAwardService.listByIds(awardIds);
for (int i = 0; i < useNum; i++) {
for (ActivityH5LevelAward award : activityH5LevelAwards) {
award.setAwardNum(award.getAwardNum() * useNum);
activityH5AwardRecordService.sendDetailAward(targetUid, targetUser.getPartitionId(), "" , award, ActivityType.PACK_CENTER_REWARD);
}
}
usePackRecord.setNumberList(awardIds);
message = I18NMessageSourceUtil.getMessage(PACK_USE_SELF_SYS, new Object[]{packName}, targetUser.getPartitionId());
} else {
usePackRecord.setUseType(PackConstant.PackUseType.SEND);
UserPack pack = new UserPack();
Integer packId = userPack.getPackId();
pack.setPackId(packId);
pack.setUid(targetUid);
pack.setPackNum(useNum);
pack.setSource(PackConstant.PackSource.USER_SEND);
pack.setStatus(PackConstant.UserPackStatus.valid);
pack.setCreateTime(createTime);
pack.setExpireTime(userPack.getExpireTime());
pack.setUpdateTime(createTime);
userPackService.save(pack);
UserPackRecord packRecord = new UserPackRecord();
packRecord.setPackId(packId);
packRecord.setUid(uid);
packRecord.setUserPackId(pack.getId());
packRecord.setPackNum(useNum);
packRecord.setSendUid(uid);
packRecord.setSource(PackConstant.PackSource.USER_SEND);
packRecord.setStatus(PackConstant.UserPackRecordStatus.NORMAL);
packRecord.setCreateTime(createTime);
packRecord.setExpireDay(0);
packRecord.setExpireTime(userPack.getExpireTime());
userPackRecordService.save(packRecord);
message = I18NMessageSourceUtil.getMessage(PACK_SEND_SYS, new Object[]{users.getNick(), packName, useNum}, targetUid);
}
userUsePackRecordService.save(usePackRecord);
baseSendService.sendSystemMsg(targetUid.toString(), message);
} catch (Exception e){
log.error("pack-send,uid:{}, targetUid:{}, userPackId:{}, useNum:{}e:{}", uid, targetUid, userPackId, useNum, e.getMessage(), e);
} finally {
if (isLocked){
lock.unlock();
}
}
userUsePackRecordService.save(usePackRecord);
baseSendService.sendSystemMsg(targetUid.toString(), message);
return BusiResult.success();
}

View File

@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
@@ -34,6 +35,7 @@ public class UserPackServiceImpl extends ServiceImpl<UserPackMapper, UserPack> i
LambdaQueryWrapper<UserPack> wrapper = Wrappers.lambdaQuery();
wrapper.eq(UserPack::getUid, uid);
wrapper.eq(UserPack::getStatus, PackConstant.UserPackStatus.valid)
.gt(UserPack::getExpireTime, new Date())
.orderByAsc(UserPack::getExpireTime);
return baseMapper.selectList(wrapper);
}