TASK:AiDA模块化

This commit is contained in:
shahaibo
2025-03-18 13:43:59 +08:00
parent f17d171231
commit 971b2034ee
7 changed files with 269 additions and 67 deletions

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.ai.da.common.config.FileProperties;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.context.UserContext;
import com.ai.da.common.enums.*;
import com.ai.da.common.utils.*;
import com.ai.da.mapper.primary.CollocationMapper;
@@ -46,7 +47,9 @@ import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -272,11 +275,6 @@ public class PythonService {
boolean isDuplicate = assembledObjects.contains(designPythonObjectCopy);
if (isDuplicate) {
// if (lastAssembledObject != null && assembledObjects.contains(lastAssembledObject)) {
// // 如果当前组装与前一个组装的对象重复,且前一个组装也重复,结束组装
// System.out.println("当前组装的对象与前两个组装的对象重复,结束组装。");
// break;
// }
elementVO.setHasUseMd5List(beforeAssemblyHasUseMd5List);
i --;
@@ -296,7 +294,6 @@ public class PythonService {
// 将当前对象添加到已组装的集合中,并记录
assembledObjects.add(designPythonObjectCopy);
// lastAssembledObject = designPythonObjectCopy; // 更新上一次组装的对象
objects.add(pythonObject);
redisUtil.addProcessId(processId, i + 1);
@@ -613,9 +610,10 @@ public class PythonService {
if (elementVO.getSingleOverall().equals(SingleOverallEnum.OVERALL.getRealName())) {
List<String> otherSketchCategoryList = getOtherSketchCategoryList(elementVO.getModelSex(), designPythonItem);
if (!otherSketchCategoryList.isEmpty()) {
JSONObject attributeRecognition = getAttributeRecognition(designPythonItem.getPath(), designPythonItem.getType(), elementVO.getModelSex());
// JSONObject attributeRecognition = getAttributeRecognition(designPythonItem.getPath(), designPythonItem.getType(), elementVO.getModelSex());
for (String styleCategory : otherSketchCategoryList) {
DesignPythonItem otherSketch = processAttributeRecognition(attributeRecognition, elementVO, designPictureType, styleCategory, systemScale);
// DesignPythonItem otherSketch = processAttributeRecognition(attributeRecognition, elementVO, designPictureType, styleCategory, systemScale);
DesignPythonItem otherSketch = processAttributeRecognition(null, elementVO, designPictureType, styleCategory, systemScale);
itemList.add(otherSketch);
}
}
@@ -654,29 +652,69 @@ public class PythonService {
return processNoPinOrSysFileAttributeRecognitionWithPool(collectNoPin, attributeRecognition, elementVO, styleCategory, systemScale);
}
// 生成两位小数随机数(线程安全)
private BigDecimal generateRandomValue() {
// 使用ThreadLocalRandom提高并发性能
double random = ThreadLocalRandom.current().nextDouble(0.0, 1.0 + 1e-4);
return new BigDecimal(random)
.setScale(2, RoundingMode.HALF_UP)
.min(BigDecimal.ONE); // 确保不超过1.00
}
private DesignPythonItem processNoPinOrSysFileAttributeRecognitionWithPool(List<CollectionElement> collectionElements, JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory, BigDecimal systemScale) {
int poolNum = 20;
if (CollectionUtil.isNotEmpty(collectionElements)) {
int collectionNoPinSize = collectionElements.size();
if (systemScale.compareTo(BigDecimal.ZERO) == 0) {
BigDecimal randomValue = generateRandomValue();
boolean useSystem = randomValue.compareTo(systemScale) <= 0;
if (useSystem) {
// 获取系统推荐sketch
String categoryParam = elementVO.getModelSex().toLowerCase() + "_" + styleCategory.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, styleCategory, elementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
}else {
if (CollectionUtil.isNotEmpty(collectionElements)) {
int collectionNoPinSize = collectionElements.size();
int randomNum = RandomsUtil.randomSysFile(collectionNoPinSize);
return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
} else if (systemScale.compareTo(BigDecimal.ONE) != 0) {
BigDecimal collectNoPinSize = BigDecimal.valueOf(collectionNoPinSize);
poolNum = collectNoPinSize.divide(systemScale, 0, RoundingMode.DOWN).intValue();
List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
collectionElements.addAll(list);
int randomNum = RandomsUtil.randomSysFile(collectionElements.size());
if (randomNum < collectionNoPinSize) {
return coverSketchToDesignPythonItem(collectionElements.get(randomNum).getId(), collectionElements.get(randomNum), elementVO);
} else {
return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
return coverSketchToDesignPythonItem(collectionElements.get(randomNum).getId(), collectionElements.get(randomNum), elementVO);
}else {
String categoryParam = elementVO.getModelSex().toLowerCase() + "_" + styleCategory.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, styleCategory, elementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
}
}
List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
int randomNum = RandomsUtil.randomSysFile(list.size());
return coverSketchToDesignPythonItem(null, list.get(randomNum), elementVO);
// int poolNum = 20;
// if (CollectionUtil.isNotEmpty(collectionElements)) {
// int collectionNoPinSize = collectionElements.size();
// if (systemScale.compareTo(BigDecimal.ZERO) == 0) {
// int randomNum = RandomsUtil.randomSysFile(collectionNoPinSize);
// return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
// } else if (systemScale.compareTo(BigDecimal.ONE) != 0) {
// BigDecimal collectNoPinSize = BigDecimal.valueOf(collectionNoPinSize);
// poolNum = collectNoPinSize.divide(systemScale, 0, RoundingMode.DOWN).intValue();
// List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
// collectionElements.addAll(list);
// int randomNum = RandomsUtil.randomSysFile(collectionElements.size());
// if (randomNum < collectionNoPinSize) {
// return coverSketchToDesignPythonItem(collectionElements.get(randomNum).getId(), collectionElements.get(randomNum), elementVO);
// } else {
// return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
// }
// }
// }
// List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
// int randomNum = RandomsUtil.randomSysFile(list.size());
// return coverSketchToDesignPythonItem(null, list.get(randomNum), elementVO);
}
private List<CollectionElement> getFilteredCollectionElements(List<CollectionElement> elements, int hasPin, String styleCategory) {
@@ -998,8 +1036,18 @@ public class PythonService {
if (CollectionUtil.isNotEmpty(sketchBoardPins)) {
Integer randomNum = RandomsUtil.randomSysFile(sketchBoardPins.size());
CollectionElement element = sketchBoardPins.get(randomNum);
JSONObject attributeRecognition = getAttributeRecognitionBySameCategory(element, validateElementVO.getModelSex());
return processAttributeRecognitionBySameCategory(attributeRecognition, validateElementVO, element.getLevel2Type());
// 获取随机pin sketch的category
String category = element.getLevel2Type();
String categoryParam = validateElementVO.getModelSex().toLowerCase() + "_" + category.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, category, validateElementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
// JSONObject attributeRecognition = getAttributeRecognitionBySameCategory(element, validateElementVO.getModelSex());
// return processAttributeRecognitionBySameCategory(attributeRecognition, validateElementVO, element.getLevel2Type());
}
QueryWrapper<Dressing> qw = new QueryWrapper<>();
qw.lambda().eq(Dressing::getApparel, validateElementVO.getModelSex());
@@ -1012,10 +1060,20 @@ public class PythonService {
}
Integer randomNum = RandomsUtil.randomSysFile(dressings.size());
String category = dressings.get(randomNum).getStyleCategory();
String tableName = getTableName(validateElementVO.getModelSex(), category);
AttributeRetrieval attributeRetrieval = attributeRetrievalMapper.getSystemRandom(tableName, validateElementVO.getStyle());
CollectionElement collectionElement = toCollectionElement(attributeRetrieval, category, validateElementVO.getModelSex());
return coverSketchToDesignPythonItem(null, collectionElement, validateElementVO);
String categoryParam = validateElementVO.getModelSex().toLowerCase() + "_" + category.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, category, validateElementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
// String tableName = getTableName(validateElementVO.getModelSex(), category);
// AttributeRetrieval attributeRetrieval = attributeRetrievalMapper.getSystemRandom(tableName, validateElementVO.getStyle());
// CollectionElement collectionElement = toCollectionElement(attributeRetrieval, category, validateElementVO.getModelSex());
// return coverSketchToDesignPythonItem(null, collectionElement, validateElementVO);
}
Random random = new Random();
int randomNumber = random.nextInt(noPinCollectionData.size());
@@ -1037,8 +1095,18 @@ public class PythonService {
if (CollectionUtil.isNotEmpty(sketchBoardCollectionElements)) {
Integer randomNum = RandomsUtil.randomSysFile(sketchBoardCollectionElements.size());
CollectionElement element = sketchBoardCollectionElements.get(randomNum);
JSONObject attributeRecognition = getAttributeRecognitionBySameCategory(element, validateElementVO.getModelSex());
return processAttributeRecognitionBySameCategory(attributeRecognition, validateElementVO, element.getLevel2Type());
String category = element.getLevel2Type();
String categoryParam = validateElementVO.getModelSex().toLowerCase() + "_" + category.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, category, validateElementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
// JSONObject attributeRecognition = getAttributeRecognitionBySameCategory(element, validateElementVO.getModelSex());
// return processAttributeRecognitionBySameCategory(attributeRecognition, validateElementVO, element.getLevel2Type());
}
}
QueryWrapper<Dressing> qw = new QueryWrapper<>();
@@ -1052,11 +1120,20 @@ public class PythonService {
}
Integer randomNum = RandomsUtil.randomSysFile(dressings.size());
String category = dressings.get(randomNum).getStyleCategory();
String tableName = getTableName(validateElementVO.getModelSex(), category);
AttributeRetrieval attributeRetrieval = attributeRetrievalMapper.getSystemRandom(tableName, validateElementVO.getStyle());
CollectionElement collectionElement = toCollectionElement(attributeRetrieval, category, validateElementVO.getModelSex());
return coverSketchToDesignPythonItem(null, collectionElement, validateElementVO);
String categoryParam = validateElementVO.getModelSex().toLowerCase() + "_" + category.toLowerCase();
List<String> recommentdUrlList = getSystemSketchByCategory(categoryParam);
if (!CollectionUtils.isEmpty(recommentdUrlList)) {
String recommendSystemSketch = recommentdUrlList.get(0);
return coverSystemSketchUrlToDesignPythonItem(recommendSystemSketch, category, validateElementVO);
}else {
throw new BusinessException("Failed to obtain system sketch recommendation.");
}
// String tableName = getTableName(validateElementVO.getModelSex(), category);
//
// AttributeRetrieval attributeRetrieval = attributeRetrievalMapper.getSystemRandom(tableName, validateElementVO.getStyle());
// CollectionElement collectionElement = toCollectionElement(attributeRetrieval, category, validateElementVO.getModelSex());
// return coverSketchToDesignPythonItem(null, collectionElement, validateElementVO);
}
private String getTableName(String modelSex, String category) {
@@ -2048,6 +2125,35 @@ public class PythonService {
return designPythonItemBlouse;
}
private DesignPythonItem coverSystemSketchUrlToDesignPythonItem(String url, String category, ValidateElementVO elementVO) {
DesignPythonItem designPythonItemBlouse = new DesignPythonItem();
// if (Objects.nonNull(elementId)) {
// designPythonItemBlouse.setElementId(null);
// designPythonItemBlouse.setBusinessId(null);
// }
designPythonItemBlouse.setType(category);
designPythonItemBlouse.setPath(url);
//所有的icon都是none
designPythonItemBlouse.setIcon("none");
CollectionColorDTO randomColor = getRandomColor(elementVO.getColorBoards());
designPythonItemBlouse.setColor(randomColor.getRgbValue());
designPythonItemBlouse.setGradient(randomColor.getGradientMinioUrl());
designPythonItemBlouse.setGradientString(randomColor.getGradientString());
if (!elementVO.getDesignPythonItemPrint().getPath().equals("none")
&& elementVO.getDesignPrintPictureTypeLayoutList().contains(category)) {
DesignPythonItemPrint designPythonItemPrint = CopyUtil.copyObject(elementVO.getDesignPythonItemPrint(), DesignPythonItemPrint.class);
// designPythonItemPrint.setIfSingle(false);
designPythonItemPrint.setPrint_path_list(Collections.singletonList(designPythonItemPrint.getPath()));
designPythonItemBlouse.setPrint(new PrintToPython(designPythonItemPrint));
} else {
DesignPythonItemPrint designPythonItemPrint = new DesignPythonItemPrint();
// designPythonItemPrint.setIfSingle(false);
designPythonItemPrint.setPrint_path_list(new ArrayList<>());
designPythonItemBlouse.setPrint(new PrintToPython(designPythonItemPrint));
}
return designPythonItemBlouse;
}
private List<CollectionElement> residuePinData(List<CollectionElement> pinData, List<Long> existPinDataIds, List<String> hasUseMd5List) {
if (CollectionUtils.isEmpty(pinData)) {
return null;
@@ -3799,4 +3905,65 @@ public class PythonService {
throw new BusinessException("design.interface.exception");
}
public List<String> getSystemSketchByCategory(String category) {
AuthPrincipalVo userHolder = UserContext.getUserHolder();
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
// 构建动态路径参数(保留两位小数)
// String url = String.format("%s:%s/recommend/%d/%s/1",
// accessPythonIp,
// accessPythonPort,
// userHolder.getId(),
// category);
String url = String.format("%s:%s/recommend/%d/%s/1",
"127.0.0.1",
"8000",
userHolder.getId(),
category);
log.info("Recommendation request URL: {}", url);
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.build();
try (Response response = client.newCall(request).execute()) {
log.info("Response status: {}", response.code());
if (!response.isSuccessful()) {
String errorBody = response.body() != null ?
response.body().string() : "empty response";
log.error("Request failed with code: {}, body: {}",
response.code(), errorBody);
throw new BusinessException("Recommendation service unavailable");
}
// 解析图片路径数组
String responseBody = response.body().string();
List<String> imagePaths = JSON.parseArray(responseBody, String.class);
if (imagePaths == null || imagePaths.isEmpty()) {
log.warn("Empty recommendation result for {}", category);
return Collections.emptyList();
}
log.info("Received {} recommended images", imagePaths.size());
return imagePaths;
} catch (IOException e) {
log.error("Network error: {}", ExceptionUtil.getRootCauseMessage(e));
throw new BusinessException("Failed to get recommendations: " + e.getMessage());
} catch (JSONException e) {
log.error("Response parsing error: {}", ExceptionUtil.getMessage(e));
throw new BusinessException("Invalid service response format");
}
}
}