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,8 +191,10 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
* 3、执行成功后,从redis中拿出当前任务id对应需要扣除的积分,扣除积分,更新数据库积分,移除redis的记录
|
||||
* 4、执行失败,直接移除记录
|
||||
*/
|
||||
/** 积分预扣除 */
|
||||
public Boolean creditsPreDeduction(CreditsEventsEnum event, Integer num){
|
||||
/**
|
||||
* 积分预扣除
|
||||
*/
|
||||
public Boolean creditsPreDeduction(CreditsEventsEnum event, Integer num) {
|
||||
|
||||
Long accountId = UserContext.getUserHolder().getId();
|
||||
|
||||
@@ -208,7 +211,7 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
BigDecimal subtract = existingCredits.subtract(new BigDecimal(sum));
|
||||
|
||||
// 3、判断剩余积分是否够本次操作
|
||||
if (subtract.compareTo(BigDecimal.ZERO) < 0){
|
||||
if (subtract.compareTo(BigDecimal.ZERO) < 0) {
|
||||
// 3.1 不够,直接返回余额不够,充值
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
@@ -217,27 +220,29 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRecordToCreditsDeduction(Long accountId, String taskId, CreditsEventsEnum creditsEventsEnum){
|
||||
public void addRecordToCreditsDeduction(Long accountId, String taskId, CreditsEventsEnum creditsEventsEnum) {
|
||||
// 5、添加当前任务的预扣积分到redis 任务有效期一天,若待扣积分两天还没被移除,说明任务已经失败,待扣积分自动失效
|
||||
redisUtil.addToString(creditsDeduction + ":" + accountId + ":" + taskId, creditsEventsEnum.getValue(), CommonConstant.CREDITS_EXPIRE_TIME);
|
||||
}
|
||||
|
||||
/** 执行扣除积分,更新数据库 */
|
||||
/**
|
||||
* 执行扣除积分,更新数据库
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void taskCreditsDeduction(Long accountId, String taskId){
|
||||
log.info("指定任务的积分扣除 : {}",taskId);
|
||||
public Boolean taskCreditsDeduction(Long accountId, String taskId) {
|
||||
log.info("指定任务的积分扣除 : {}", taskId);
|
||||
String key = creditsDeduction + ":" + accountId + ":" + taskId;
|
||||
// 1、获取当前任务id对应的积分
|
||||
String value = redisUtil.getFromString(key);
|
||||
|
||||
// 1.1 没有。返回,报错,未找到当前任务
|
||||
if (StringUtil.isNullOrEmpty(value)){
|
||||
if (StringUtil.isNullOrEmpty(value)) {
|
||||
log.info("当前任务 {} 不存在,或当前任务已完成积分扣除", taskId);
|
||||
return;
|
||||
return Boolean.FALSE;
|
||||
// throw new BusinessException("当前任务不存在,无法扣除积分");
|
||||
}else {
|
||||
log.info("指定任务 {} 扣除积分 {}",taskId, value);
|
||||
} else {
|
||||
log.info("指定任务 {} 扣除积分 {}", taskId, value);
|
||||
}
|
||||
|
||||
// 2、操作数据库,扣除积分
|
||||
@@ -247,9 +252,10 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
|
||||
|
||||
// 3、从redis中移除当前待扣积分
|
||||
redisUtil.removeFromString(key);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
public CreditsDetail getByAccountIdAndChangeEvent(Long accountId, String changeEvent, String changedCredits){
|
||||
public CreditsDetail getByAccountIdAndChangeEvent(Long accountId, String changeEvent, String changedCredits) {
|
||||
QueryWrapper<CreditsDetail> qw = new QueryWrapper<>();
|
||||
qw.eq("account_id", accountId);
|
||||
qw.eq("change_event", changeEvent);
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -143,11 +143,11 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
String jsonString = "";
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
// 3.1 确定不同类型的印花分别调哪个接口
|
||||
if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())){
|
||||
switch(generateThroughImageTextDTO.getLevel2Type()){
|
||||
if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())) {
|
||||
switch (generateThroughImageTextDTO.getLevel2Type()) {
|
||||
case "Logo":
|
||||
path = CommonConstant.GENERATE_SINGLE_LOGO;
|
||||
params.put("tasks_id",generateThroughImageTextDTO.getUniqueId());
|
||||
params.put("tasks_id", generateThroughImageTextDTO.getUniqueId());
|
||||
params.put("prompt", text);
|
||||
params.put("seed", generateThroughImageTextDTO.getSeed());
|
||||
jsonString = JSON.toJSONString(params, SerializerFeature.WriteMapNullValue);
|
||||
@@ -155,8 +155,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
case "Slogan":
|
||||
path = CommonConstant.GENERATE_SLOGAN;
|
||||
port = CommonConstant.PYTHON_PORT_9997;
|
||||
params.put("num_point","16");
|
||||
params.put("tasks_id",generateThroughImageTextDTO.getUniqueId());
|
||||
params.put("num_point", "16");
|
||||
params.put("tasks_id", generateThroughImageTextDTO.getUniqueId());
|
||||
params.put("prompt", text);
|
||||
params.put("image_url", collectionElement.getUrl());
|
||||
jsonString = JSON.toJSONString(params, SerializerFeature.WriteMapNullValue);
|
||||
@@ -166,7 +166,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
mode, category, generateThroughImageTextDTO.getGender());
|
||||
jsonString = JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue);
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
GenerateToPythonDTO generateToPythonDTO = new GenerateToPythonDTO(generateThroughImageTextDTO.getUniqueId(), text, Objects.isNull(collectionElement) ? "" : collectionElement.getUrl(),
|
||||
mode, category, generateThroughImageTextDTO.getGender());
|
||||
jsonString = JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue);
|
||||
@@ -231,9 +231,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
redisUtil.addToString(key, new Gson().toJson(generateResultVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
|
||||
|
||||
// 执行积分扣除
|
||||
String accountId = taskId.substring(taskId.lastIndexOf("-") + 1);
|
||||
String uuid = taskId.substring(0,taskId.substring(0, taskId.lastIndexOf("-")).lastIndexOf("-"));
|
||||
creditsService.taskCreditsDeduction(Long.parseLong(accountId), uuid);
|
||||
// ** 注:如果生成的图片都是空白 则不扣积分
|
||||
if (!status.equals("Invalid")){
|
||||
String accountId = taskId.substring(taskId.lastIndexOf("-") + 1);
|
||||
String uuid = taskId.substring(0, taskId.substring(0, taskId.lastIndexOf("-")).lastIndexOf("-"));
|
||||
Boolean flag = creditsService.taskCreditsDeduction(Long.parseLong(accountId), uuid);
|
||||
if (flag) creditsService.updateChangedCredits(accountId, uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@Resource
|
||||
@@ -315,7 +319,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
text = "Flat coating,romantic,soft,pencil strokes,accentuating and widening the depth of pencil strokes,paper patterns,block colors,crayons,reducing image contrast,and hand drawn painting marks," + translated + ", fabric print, high quality";
|
||||
} else if (userInput.contains("Real Style")) {
|
||||
text = "Still life photography,hyper realism,3d,deepened projection,increased permutation value,increased concavity and convexity value," + translated + ", fabric print, high quality";
|
||||
}else {
|
||||
} else {
|
||||
text = translated;
|
||||
}
|
||||
// text = userInput + ", fabric print, high quality";
|
||||
@@ -475,33 +479,34 @@ 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())){
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getLevel2Type())){
|
||||
if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())) {
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getLevel2Type())) {
|
||||
throw new BusinessException("level2Type.cannot.be.empty");
|
||||
}else if (!CollectionLevel2TypeEnum.printType().contains(generateThroughImageTextDTO.getLevel2Type())){
|
||||
} else if (!CollectionLevel2TypeEnum.printType().contains(generateThroughImageTextDTO.getLevel2Type())) {
|
||||
throw new BusinessException("unknown.parameter.level2Type");
|
||||
}
|
||||
|
||||
// Pattern 参数校验
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.Pattern.getRealName())){
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.Pattern.getRealName())) {
|
||||
String text = generateThroughImageTextDTO.getText();
|
||||
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
|
||||
Generate generate = new Generate();
|
||||
validateGeneraType(generate, text, elementId);
|
||||
// 校验后获取
|
||||
generateThroughImageTextDTO.setGenerateType(generate.getGenerateType());
|
||||
creditsEventsEnum = CreditsEventsEnum.PATTERN;
|
||||
}
|
||||
// Slogan 参数校验
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.SLOGAN.getRealName())){
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getSloganBase64())){
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.SLOGAN.getRealName())) {
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getSloganBase64())) {
|
||||
log.error("Printboard-Slogan模式下,slogan image为空");
|
||||
throw new BusinessException("slogan.image.cannot.be.empty");
|
||||
}
|
||||
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getText())){
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getText())) {
|
||||
log.error("Printboard-Slogan模式下,slogan text为空");
|
||||
throw new BusinessException("slogan.style.cannot.be.empty");
|
||||
}
|
||||
@@ -532,20 +537,25 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
}
|
||||
|
||||
// Logo参数校验
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.LOGO.getRealName())){
|
||||
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.LOGO.getRealName())) {
|
||||
// logo模式下一次只生成一张
|
||||
times = 1;
|
||||
// 校验是否输入内容
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getText().trim())){
|
||||
if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getText().trim())) {
|
||||
throw new BusinessException("please.input.the.prompt");
|
||||
}
|
||||
|
||||
// 校验seed的取值范围
|
||||
int seed = Integer.parseInt(generateThroughImageTextDTO.getSeed());
|
||||
if (seed < 0 || seed > 99999){
|
||||
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);
|
||||
@@ -665,15 +677,15 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
}
|
||||
}*/
|
||||
String path;
|
||||
if (type.equals("Logo")){
|
||||
if (type.equals("Logo")) {
|
||||
path = CommonConstant.GENERATE_LOGO_SINGLE_CANCEL;
|
||||
}else {
|
||||
} else {
|
||||
path = CommonConstant.GENERATE_CANCEL;
|
||||
}
|
||||
|
||||
String key = generateResultKey + ":" + uniqueId;
|
||||
GenerateResultVO generateResultVO = new Gson().fromJson(redisUtil.getFromString(key), GenerateResultVO.class);
|
||||
if (Objects.isNull(generateResultVO)){
|
||||
if (Objects.isNull(generateResultVO)) {
|
||||
log.warn("任务不存在,无法取消");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user