TASK:模块化;
This commit is contained in:
@@ -15,6 +15,8 @@ public class PoseTransformation extends BaseEntity {
|
||||
|
||||
private String uniqueId;
|
||||
|
||||
private String taskIdBatch;
|
||||
|
||||
private String productImage;
|
||||
|
||||
private int poseId;
|
||||
|
||||
@@ -3,9 +3,13 @@ package com.ai.da.model.dto;
|
||||
import com.ai.da.mapper.primary.entity.CloudTask;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class CloudTaskDTO extends CloudTask {
|
||||
private ToProductImageDTO toProductImage;
|
||||
|
||||
private List<PoseTransformBatchDTO> poseTransform;
|
||||
|
||||
|
||||
}
|
||||
|
||||
10
src/main/java/com/ai/da/model/dto/PoseTransformBatchDTO.java
Normal file
10
src/main/java/com/ai/da/model/dto/PoseTransformBatchDTO.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.ai.da.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PoseTransformBatchDTO {
|
||||
private String productImage;
|
||||
|
||||
private Integer poseId;
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
public enum BuildType implements IEnumDisplay {
|
||||
DESIGN("design"),
|
||||
TO_PRODUCT_IMAGE("toProductImage"),
|
||||
|
||||
RELIGHT("relight")
|
||||
RELIGHT("relight"),
|
||||
POSE_TRANSFORM("poseTransform")
|
||||
;
|
||||
|
||||
private String value;
|
||||
|
||||
@@ -4342,4 +4342,63 @@ public class PythonService {
|
||||
//生成失败
|
||||
throw new BusinessException("relightImage.interface.exception");
|
||||
}
|
||||
|
||||
public Boolean poseTransformationBatch(String productImage, Integer poseId, String taskId) {
|
||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
|
||||
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
|
||||
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
|
||||
.build();
|
||||
MediaType mediaType = MediaType.parse("application/json");
|
||||
Map<String, Object> content = Maps.newHashMap();
|
||||
content.put("image_url", productImage);
|
||||
content.put("tasks_id", taskId);
|
||||
content.put("pose_id", String.valueOf(poseId));
|
||||
content.put("batch_size", 1);
|
||||
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
|
||||
|
||||
log.info("poseTransformation 请求地址: {}", accessPythonIp + ":" + accessPythonPort + "/api/pose_transform");
|
||||
Request request = new Request.Builder()
|
||||
.url(accessPythonIp + ":" + accessPythonPort + "/api/pose_transform")
|
||||
.method("POST", body)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.build();
|
||||
Response response = null;
|
||||
String bodyString;
|
||||
try {
|
||||
log.info("poseTransformation请求入参content###{}", JSON.toJSONString(content));
|
||||
response = client.newCall(request).execute();
|
||||
} catch (IOException ioException) {
|
||||
log.error("PythonService##poseTransformation异常###{}", ExceptionUtil.getThrowableList(ioException));
|
||||
throw new BusinessException(ioException.getMessage());
|
||||
}
|
||||
|
||||
// 判断是否生成失败
|
||||
if (Objects.isNull(response.body())) {
|
||||
log.error("PythonService##poseTransformation异常###{}", "response or body is empty!");
|
||||
throw new BusinessException("PythonService##poseTransformation异常###: response or body is empty!");
|
||||
} else if (response.code() != HttpURLConnection.HTTP_OK) {
|
||||
log.error("PythonService##poseTransformation异常###{}", "Response error!Response code ## " + response.code() + " ##");
|
||||
throw new BusinessException("PythonService##poseTransformation异常### Response error!Response code ## " + response.code() + " ##");
|
||||
} else {
|
||||
try {
|
||||
bodyString = response.body().string();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
throw new BusinessException(e.getMessage());
|
||||
}
|
||||
}
|
||||
JSONObject jsonObject = JSON.parseObject(bodyString);
|
||||
Boolean result = JSON.parseObject(JSON.toJSONString(response)).getBoolean("successful");
|
||||
|
||||
if (result && jsonObject.get("code").equals(200)) {
|
||||
log.info("poseTransformation##responseObject###{}", jsonObject);
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
log.info("poseTransformation失败###{}", jsonObject);
|
||||
log.info("poseTransformation Exception! Code : {}", jsonObject.get("code"));
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +124,8 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
@Resource
|
||||
private PoseTransformationMapper poseTransformationMapper;
|
||||
|
||||
@Resource
|
||||
private DesignBatchMapper designBatchMapper;
|
||||
@@ -2062,6 +2064,45 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
|
||||
cloudTask.setStatus(0);
|
||||
cloudTaskMapper.insert(cloudTask);
|
||||
return batchTaskId;
|
||||
} else if (cloudTaskDTO.getBuildType().equals(BuildType.POSE_TRANSFORM.getValue())) {
|
||||
Long accountId = UserContext.getUserHolder().getId();
|
||||
|
||||
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();
|
||||
String taskId = uuid + "-" + accountId;
|
||||
|
||||
PoseTransformation poseTransformation = new PoseTransformation();
|
||||
poseTransformation.setProjectId(cloudTaskDTO.getProjectId());
|
||||
poseTransformation.setAccountId(accountId);
|
||||
poseTransformation.setUniqueId(taskId);
|
||||
poseTransformation.setProductImage(poseTransformBatchDTO.getProductImage());
|
||||
poseTransformation.setPoseId(poseTransformBatchDTO.getPoseId());
|
||||
poseTransformation.setCreateTime(LocalDateTime.now());
|
||||
poseTransformation.setTaskIdBatch(taskBatchId);
|
||||
poseTransformationMapper.insert(poseTransformation);
|
||||
|
||||
Boolean b = pythonService.poseTransformationBatch(poseTransformBatchDTO.getProductImage(), poseTransformBatchDTO.getPoseId(), taskId);
|
||||
if (b){
|
||||
// 6、添加预扣除积分到redis
|
||||
creditsService.addRecordToCreditsDeduction(accountId, uuid, creditsEventsEnum);
|
||||
// 6.1 添加积分扣除记录到db
|
||||
creditsService.preInsert(accountId, creditsEventsEnum.getName(), uuid, Boolean.TRUE, null);
|
||||
return taskId;
|
||||
}
|
||||
throw new BusinessException("pose transformation error", ResultEnum.ERROR.getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user