1、generate 积分扣除并添加相关积分详细
2、获取服装category bug修改
This commit is contained in:
@@ -21,10 +21,14 @@ public enum CreditsEventsEnum {
|
||||
// SUPER_RESOLUTION("Super Resolution","30"),
|
||||
SUPER_RESOLUTION("Super Resolution","10"),
|
||||
SLOGAN("Slogan","10"),
|
||||
NORMAL_GENERATE("Normal_Generate","5"),
|
||||
LOGO("Logo","5"),
|
||||
PATTERN("Pattern","5"),
|
||||
MOOD_BOARD("MoodBoard","5"),
|
||||
SKETCH_BOARD("SketchBoard","5"),
|
||||
TO_PRODUCT_IMAGE("ToProductImage","5"),
|
||||
QUESTIONNAIRE("Questionnaire","100"),
|
||||
|
||||
OTHER("Other","10");
|
||||
OTHER("Other","5");
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import java.math.BigDecimal;
|
||||
public class CreditsDetail extends BaseEntity {
|
||||
/** 用户id */
|
||||
private Long accountId;
|
||||
/** 任务id或uuid */
|
||||
private String taskId;
|
||||
/** 积分变更事件 */
|
||||
private String changeEvent;
|
||||
/** 变更积分 ( + 表示加,- 表示减) */
|
||||
|
||||
@@ -3160,8 +3160,9 @@ public class PythonService {
|
||||
try {
|
||||
Boolean result = JSON.parseObject(JSON.toJSONString(response)).getBoolean("successful");
|
||||
if (result && jsonObject.get("msg").equals("OK!")) {
|
||||
JSONObject data = jsonObject.getJSONObject("data");
|
||||
JSONArray list = JSONArray.parseArray(data.get("list").toString());
|
||||
// JSONObject data = jsonObject.getJSONObject("data");
|
||||
// JSONArray list = JSONArray.parseArray(data.get("list").toString());
|
||||
JSONArray list = jsonObject.getJSONArray("data");
|
||||
JSONObject map = (JSONObject) list.get(0);
|
||||
return map.get("category").toString();
|
||||
}
|
||||
|
||||
@@ -29,7 +29,11 @@ public interface CreditsService extends IService<CreditsDetail> {
|
||||
|
||||
void addRecordToCreditsDeduction(Long accountId, String taskId, CreditsEventsEnum creditsEventsEnum);
|
||||
|
||||
void taskCreditsDeduction(Long accountId, String taskId);
|
||||
Boolean taskCreditsDeduction(Long accountId, String taskId);
|
||||
|
||||
CreditsDetail getByAccountIdAndChangeEvent(Long accountId, String changeEvent, String changedCredits);
|
||||
|
||||
void preInsert(Long accountId, String changeEventName, String taskId);
|
||||
|
||||
void updateChangedCredits(String accountId, String taskId);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.ai.da.model.dto.QueryIncomeOrExpenditureDTO;
|
||||
import com.ai.da.service.AccountService;
|
||||
import com.ai.da.service.CreditsService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
@@ -190,7 +191,9 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
* 3、执行成功后,从redis中拿出当前任务id对应需要扣除的积分,扣除积分,更新数据库积分,移除redis的记录
|
||||
* 4、执行失败,直接移除记录
|
||||
*/
|
||||
/** 积分预扣除 */
|
||||
/**
|
||||
* 积分预扣除
|
||||
*/
|
||||
public Boolean creditsPreDeduction(CreditsEventsEnum event, Integer num) {
|
||||
|
||||
Long accountId = UserContext.getUserHolder().getId();
|
||||
@@ -222,10 +225,12 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
redisUtil.addToString(creditsDeduction + ":" + accountId + ":" + taskId, creditsEventsEnum.getValue(), CommonConstant.CREDITS_EXPIRE_TIME);
|
||||
}
|
||||
|
||||
/** 执行扣除积分,更新数据库 */
|
||||
/**
|
||||
* 执行扣除积分,更新数据库
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void taskCreditsDeduction(Long accountId, String taskId){
|
||||
public Boolean taskCreditsDeduction(Long accountId, String taskId) {
|
||||
log.info("指定任务的积分扣除 : {}", taskId);
|
||||
String key = creditsDeduction + ":" + accountId + ":" + taskId;
|
||||
// 1、获取当前任务id对应的积分
|
||||
@@ -234,7 +239,7 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
// 1.1 没有。返回,报错,未找到当前任务
|
||||
if (StringUtil.isNullOrEmpty(value)) {
|
||||
log.info("当前任务 {} 不存在,或当前任务已完成积分扣除", taskId);
|
||||
return;
|
||||
return Boolean.FALSE;
|
||||
// throw new BusinessException("当前任务不存在,无法扣除积分");
|
||||
} else {
|
||||
log.info("指定任务 {} 扣除积分 {}", taskId, value);
|
||||
@@ -247,6 +252,7 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
|
||||
// 3、从redis中移除当前待扣积分
|
||||
redisUtil.removeFromString(key);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
public CreditsDetail getByAccountIdAndChangeEvent(Long accountId, String changeEvent, String changedCredits) {
|
||||
@@ -258,4 +264,50 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
return baseMapper.selectOne(qw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preInsert(Long accountId, String changeEventName, String taskId) {
|
||||
CreditsDetail creditsDetail = new CreditsDetail();
|
||||
Account account = accountMapper.selectById(accountId);
|
||||
creditsDetail.setAccountId(accountId);
|
||||
creditsDetail.setTaskId(taskId);
|
||||
creditsDetail.setChangeEvent(changeEventName);
|
||||
creditsDetail.setChangedCredits("-0");
|
||||
creditsDetail.setCredits(account.getCredits());
|
||||
creditsDetail.setCreateTime(LocalDateTime.now());
|
||||
|
||||
baseMapper.insert(creditsDetail);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateChangedCredits(String accountId, String taskId) {
|
||||
QueryWrapper<CreditsDetail> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("account_id", accountId)
|
||||
.eq("task_id", taskId);
|
||||
|
||||
CreditsDetail creditsDetail = baseMapper.selectOne(queryWrapper);
|
||||
String changeEvent = creditsDetail.getChangeEvent();
|
||||
BigDecimal currentCredits = creditsDetail.getCredits();
|
||||
String credits = "0";
|
||||
if (changeEvent.equals("Logo") ||
|
||||
changeEvent.equals("Pattern") ||
|
||||
changeEvent.equals("MoodBoard") ||
|
||||
changeEvent.equals("SketchBoard")) {
|
||||
credits = CreditsEventsEnum.LOGO.getValue();
|
||||
}else if (changeEvent.equals("Slogan")){
|
||||
credits = CreditsEventsEnum.SLOGAN.getValue();
|
||||
}
|
||||
|
||||
BigDecimal finalCredits = currentCredits.subtract(new BigDecimal(credits));
|
||||
String changeCredits = "-" + credits;
|
||||
|
||||
UpdateWrapper<CreditsDetail> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.lambda()
|
||||
.eq(CreditsDetail::getTaskId, taskId)
|
||||
.set(CreditsDetail::getChangedCredits, changeCredits)
|
||||
.set(CreditsDetail::getCredits, finalCredits);
|
||||
|
||||
baseMapper.update(null, updateWrapper);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -231,9 +231,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
redisUtil.addToString(key, new Gson().toJson(generateResultVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
|
||||
|
||||
// 执行积分扣除
|
||||
// ** 注:如果生成的图片都是空白 则不扣积分
|
||||
if (!status.equals("Invalid")){
|
||||
String accountId = taskId.substring(taskId.lastIndexOf("-") + 1);
|
||||
String uuid = taskId.substring(0, taskId.substring(0, taskId.lastIndexOf("-")).lastIndexOf("-"));
|
||||
creditsService.taskCreditsDeduction(Long.parseLong(accountId), uuid);
|
||||
Boolean flag = creditsService.taskCreditsDeduction(Long.parseLong(accountId), uuid);
|
||||
if (flag) creditsService.updateChangedCredits(accountId, uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@Resource
|
||||
@@ -475,7 +479,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
return new PrepareForGenerateVO(0);
|
||||
}
|
||||
}
|
||||
CreditsEventsEnum creditsEventsEnum = CreditsEventsEnum.NORMAL_GENERATE;
|
||||
CreditsEventsEnum creditsEventsEnum = CreditsEventsEnum.OTHER;
|
||||
int times = 4;
|
||||
// 当level1Type为Print_board时,level2Type为pattern时需要确定generateType
|
||||
if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())) {
|
||||
@@ -493,6 +497,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
validateGeneraType(generate, text, elementId);
|
||||
// 校验后获取
|
||||
generateThroughImageTextDTO.setGenerateType(generate.getGenerateType());
|
||||
creditsEventsEnum = CreditsEventsEnum.PATTERN;
|
||||
}
|
||||
// Slogan 参数校验
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.SLOGAN.getRealName())) {
|
||||
@@ -545,7 +550,12 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
if (seed < 0 || seed > 99999) {
|
||||
throw new BusinessException("the.value.range.of.seed");
|
||||
}
|
||||
creditsEventsEnum = CreditsEventsEnum.LOGO;
|
||||
}
|
||||
} else if (generateThroughImageTextDTO.getLevel1Type().equals(MOOD_BOARD.getRealName())) {
|
||||
creditsEventsEnum = CreditsEventsEnum.MOOD_BOARD;
|
||||
} else if (generateThroughImageTextDTO.getLevel1Type().equals(SKETCH_BOARD.getRealName())) {
|
||||
creditsEventsEnum = CreditsEventsEnum.SKETCH_BOARD;
|
||||
}
|
||||
|
||||
// 2、判断用户当前积分是否够本次生成消耗
|
||||
@@ -580,6 +590,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
|
||||
// 6、添加预扣除积分到redis
|
||||
creditsService.addRecordToCreditsDeduction(generateThroughImageTextDTO.getUserId(), uuid, creditsEventsEnum);
|
||||
// 6.1 添加积分扣除记录到db
|
||||
creditsService.preInsert(generateThroughImageTextDTO.getUserId(), creditsEventsEnum.getName(), uuid);
|
||||
|
||||
// 7、返回唯一id
|
||||
return new PrepareForGenerateVO(taskIdList, 2 - trialsCount);
|
||||
|
||||
Reference in New Issue
Block a user