TASK:模块化;

This commit is contained in:
shahaibo
2025-04-23 10:14:15 +08:00
parent 5a0c461961
commit 2d1d458929
9 changed files with 79 additions and 18 deletions

View File

@@ -27,6 +27,8 @@ public interface CreditsService extends IService<CreditsDetail> {
Boolean creditsPreDeduction(CreditsEventsEnum event, Integer num);
Boolean creditsPreDeduction(Integer credits);
void addRecordToCreditsDeduction(Long accountId, String taskId, CreditsEventsEnum creditsEventsEnum);
Boolean taskCreditsDeduction(Long accountId, String taskId);

View File

@@ -227,6 +227,32 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
return Boolean.TRUE;
}
@Override
public Boolean creditsPreDeduction(Integer credits) {
Long accountId = UserContext.getUserHolder().getId();
// 1、获取当前在积分预扣除区未来需要预扣除的积分总和
Set<String> keys = redisUtil.getKeysFromString(creditsDeduction + ":" + accountId + ":*");
List<String> multiValue = redisUtil.getMultiValue(keys);
// 1.1 预扣除区 积分总和
int sum = multiValue.stream().mapToInt(Integer::parseInt).sum();
// 1.2 加上本次操作需要扣除的积分
sum += credits;
// 2、获取当前积分
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
BigDecimal subtract = existingCredits.subtract(new BigDecimal(sum));
// 3、判断剩余积分是否够本次操作
if (subtract.compareTo(BigDecimal.ZERO) < 0) {
// 3.1 不够,直接返回余额不够,充值
return Boolean.FALSE;
}
return Boolean.TRUE;
}
@Override
public void addRecordToCreditsDeduction(Long accountId, String taskId, CreditsEventsEnum creditsEventsEnum) {
// 5、添加当前任务的预扣积分到redis 任务有效期一天,若待扣积分两天还没被移除,说明任务已经失败,待扣积分自动失效

View File

@@ -1825,10 +1825,10 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
}else if (cloudTaskDTO.getBuildType().equals(BuildType.TO_PRODUCT_IMAGE.getValue())) {
ToProductImageDTO toProductImageDTO = cloudTaskDTO.getToProductImage();
// 判断用户当前积分是否够本次生成消耗
// Boolean preDeduction = creditsService.creditsPreDeduction(CreditsEventsEnum.TO_PRODUCT_IMAGE, toProductImageDTO.getToProductImageVOList().size());
// if (!preDeduction) {
// throw new BusinessException("Your remaining credits are insufficient for this generation. Please recharge.", ResultEnum.WARNING.getCode());
// }
Boolean preDeduction = creditsService.creditsPreDeduction(cloudTaskDTO.getCostCredits());
if (!preDeduction) {
throw new BusinessException("Your remaining credits are insufficient for this generation. Please recharge.", ResultEnum.WARNING.getCode());
}
AuthPrincipalVo userHolder = UserContext.getUserHolder();
String batchTaskId = UUID.randomUUID() + "-" + userHolder.getId();
@@ -1955,10 +1955,10 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
}else if (cloudTaskDTO.getBuildType().equals(BuildType.RELIGHT.getValue())) {
ToProductImageDTO toProductImageDTO = cloudTaskDTO.getToProductImage();
// 判断用户当前积分是否够本次生成消耗
// Boolean preDeduction = creditsService.creditsPreDeduction(CreditsEventsEnum.RELIGHT, toProductImageDTO.getToProductImageVOList().size());
// if (!preDeduction) {
// throw new BusinessException("Your remaining credits are insufficient for this generation. Please recharge.", ResultEnum.WARNING.getCode());
// }
Boolean preDeduction = creditsService.creditsPreDeduction(cloudTaskDTO.getCostCredits());
if (!preDeduction) {
throw new BusinessException("Your remaining credits are insufficient for this generation. Please recharge.", ResultEnum.WARNING.getCode());
}
AuthPrincipalVo userHolder = UserContext.getUserHolder();
String batchTaskId = UUID.randomUUID() + "-" + userHolder.getId();
@@ -2066,17 +2066,16 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
return batchTaskId;
} else if (cloudTaskDTO.getBuildType().equals(BuildType.POSE_TRANSFORM.getValue())) {
Long accountId = UserContext.getUserHolder().getId();
Boolean preDeduction = creditsService.creditsPreDeduction(cloudTaskDTO.getCostCredits());
if (!preDeduction) {
throw new BusinessException("Your remaining credits are insufficient for this generation. Please recharge.", ResultEnum.WARNING.getCode());
}
List<PoseTransformBatchDTO> poseTransformList = cloudTaskDTO.getPoseTransform();
if (CollectionUtil.isNotEmpty(poseTransformList)) {
String taskBatchId = UUID.randomUUID().toString() + "-" + accountId;
for (PoseTransformBatchDTO poseTransformBatchDTO : poseTransformList) {
// 1、判断用户当前积分是否够本次生成消耗
CreditsEventsEnum creditsEventsEnum = CreditsEventsEnum.POSE_TRANSFORMATION;
Boolean preDeduction = creditsService.creditsPreDeduction(creditsEventsEnum, 1);
if (!preDeduction) {
throw new BusinessException("remaining.credits.insufficient", ResultEnum.WARNING.getCode());
}
// 3、生成唯一id 使用uuid,由于uuid重复的几率很小故取消对uuid重复性的校验
String uuid = UUID.randomUUID().toString();

View File

@@ -1643,6 +1643,10 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
if (CollectionUtil.isNotEmpty(threeDModules)) {
ThreeDModule threeDModule = threeDModules.get(0);
ThreeDVO patternMaking3D = getLayoutDetail(threeDModule.getThreeDSimpleId());
CollectionElement collectionElement = collectionElementMapper.selectById(threeDModule.getCollectionElementId());
patternMaking3D.setPrintUrl(minioUtil.getPreSignedUrl(collectionElement.getUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
patternMaking3D.setX(threeDModule.getX());
patternMaking3D.setY(threeDModule.getY());
moduleChooseVO.setPatternMaking3D(patternMaking3D);
}
}
@@ -1892,7 +1896,8 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
collectionElementMapper.deleteBatchIds(old);
}
}
if (moduleSaveDTO.getPatternMaking3D() != null) {
if (Objects.nonNull(moduleSaveDTO.getPatternMaking3D())) {
PatternMaking3DDTO patternMaking3D = moduleSaveDTO.getPatternMaking3D();
QueryWrapper<ThreeDModule> threeDModuleQueryWrapper = new QueryWrapper<>();
threeDModuleQueryWrapper.lambda().eq(ThreeDModule::getProjectId, projectId);
@@ -1901,12 +1906,18 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
if (CollectionUtils.isEmpty(threeDModuleList)) {
ThreeDModule threeDModule = new ThreeDModule();
threeDModule.setProjectId(projectId);
threeDModule.setThreeDSimpleId(moduleSaveDTO.getPatternMaking3D());
threeDModule.setThreeDSimpleId(patternMaking3D.getThreeDSimpleId());
threeDModule.setX(patternMaking3D.getX());
threeDModule.setY(patternMaking3D.getY());
threeDModule.setCollectionElementId(patternMaking3D.getCollectionElementId());
threeDModuleMapper.insert(threeDModule);
}else {
ThreeDModule threeDModule = threeDModuleList.get(0);
if (!Objects.equals(moduleSaveDTO.getPatternMaking3D(), threeDModule.getThreeDSimpleId())) {
threeDModule.setThreeDSimpleId(moduleSaveDTO.getPatternMaking3D());
threeDModule.setThreeDSimpleId(patternMaking3D.getThreeDSimpleId());
threeDModule.setX(patternMaking3D.getX());
threeDModule.setY(patternMaking3D.getY());
threeDModule.setCollectionElementId(patternMaking3D.getCollectionElementId());
threeDModuleMapper.updateById(threeDModule);
}
}