TASK:AiDA模块化

This commit is contained in:
shahaibo
2025-03-17 11:30:09 +08:00
parent 8fa76c6732
commit 340227d9a6
3 changed files with 61 additions and 566 deletions

View File

@@ -4,13 +4,10 @@ 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;
import com.ai.da.mapper.primary.DressingMapper;
import com.ai.da.mapper.primary.SketchDataMapper;
import com.ai.da.mapper.primary.UserPreferenceLogMapper;
import com.ai.da.mapper.primary.entity.*;
import com.ai.da.mapper.secondary.AttributeRetrievalMapper;
import com.ai.da.mapper.secondary.entity.AttributeRetrieval;
@@ -35,7 +32,6 @@ import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@@ -228,8 +224,7 @@ public class PythonService {
* @return
*/
public DesignPythonObjects covertDesignParam(BigDecimal systemScale, String singleOverall,
String switchCategory, ValidateElementVO elementVO, String processId, Set<DesignPythonObject> assembledObjects, Long collectionId) {
AuthPrincipalVo userHolder = UserContext.getUserHolder();
String switchCategory, ValidateElementVO elementVO, String processId, Long collectionId) {
DesignPythonObjects designPythonObjects = new DesignPythonObjects();
List<DesignPythonObject> objects = new ArrayList<>();
designPythonObjects.setObjects(objects);
@@ -241,20 +236,16 @@ public class PythonService {
elementVO.setNoPinPrintNum(noPinPrintNum);
int[] sketchNumbers = new int[3];
int designNum = elementVO.getDesignNum();
if (CollectionUtil.isEmpty(assembledObjects)) {
assembledObjects = new HashSet<>(); // 用于存储已组装的 DesignPythonObject
}
DesignPythonObject lastAssembledObject = null; // 上一次组装的对象
long totalContainsTime = 0; // 用于累计 contains 方法的时间
int containsCheckCount = 0; // contains 方法调用次数
int designNum = elementVO.getDesignNum();
Set<DesignPythonObject> assembledObjects = new HashSet<>(); // 用于存储已组装的 DesignPythonObject
DesignPythonObject lastAssembledObject = null; // 上一次组装的对象
for (int i = 0; i < designNum; i++) {
CurrentDesignPictureTypeEnum designPictureType = calculateCurrentDesignPictureTypeNew(elementVO, sketchNumbers, systemScale);
if (designPictureType == null) break;
CurrentDesignPrintPictureTypeEnum designPrintPictureType = calculateCurrentDesignPrintPictureType(pinPrintNum, noPinPrintNum, noPrintNum);
CurrentDesignPrintPictureTypeEnum designPrintPictureType = calculateCurrentDesignPintPictureType(pinPrintNum, noPinPrintNum, noPrintNum);
if (designPrintPictureType == null) break;
updateSketchNumbers(designPictureType, sketchNumbers);
@@ -269,63 +260,56 @@ public class PythonService {
noPrintNum--;
break;
}
DesignPythonItemPrint designPythonItemPrint = getRandomPrint(elementVO, designPrintPictureType);
elementVO.setDesignPythonItemPrint(designPythonItemPrint);
elementVO.setDesignPrintPictureTypeLayoutList(calculateCurrentDesignPrintPictureTypeLayout(elementVO.getModelSex()));
elementVO.setDesignPrintPictureTypeLayoutList(calculateCurrentDesignPintPictureTypeLayout(elementVO.getModelSex()));
List<String> beforeAssemblyHasUseMd5List = new ArrayList<>(elementVO.getHasUseMd5List());
elementVO.setCollectionId(collectionId);
DesignPythonObject pythonObject = createDesignPythonObject(elementVO, designPictureType, systemScale, singleOverall, switchCategory, i);
// List<String> afterAssemblyHasUseMd5List = elementVO.getHasUseMd5List();
// 如果当前对象与已组装的对象重复,则跳过当前组装
DesignPythonObject designPythonObjectCopy = getCopy(pythonObject);
// 计算 contains 方法的执行时间
long startTime = System.nanoTime();
boolean isDuplicate = assembledObjects.contains(designPythonObjectCopy);
long endTime = System.nanoTime();
System.out.println("单次 方法调用耗时(纳秒): " + (endTime - startTime));
totalContainsTime += (endTime - startTime);
containsCheckCount++;
if (isDuplicate) {
// if (lastAssembledObject != null && assembledObjects.contains(lastAssembledObject)) {
// // 如果当前组装与前一个组装的对象重复,且前一个组装也重复,结束组装
// System.out.println("当前组装的对象与前两个组装的对象重复,结束组装。");
// break;
// }
elementVO.setHasUseMd5List(beforeAssemblyHasUseMd5List);
i --;
switch (designPrintPictureType) {
case PIN:
pinPrintNum++;
break;
case NO_PIN:
noPinPrintNum++;
break;
case NO:
noPrintNum++;
break;
}
continue;
}
// 将当前对象添加到已组装的集合中,并记录
assembledObjects.add(designPythonObjectCopy);
// lastAssembledObject = designPythonObjectCopy; // 更新上一次组装的对象
for (DesignPythonItem item : pythonObject.getItems()) {
redisUtil.addPathToCache(collectionId, userHolder.getId(), item.getPath());
}
objects.add(pythonObject);
redisUtil.addProcessId(processId, i + 1);
}
// 输出统计结果
System.out.println("contains 方法调用次数: " + containsCheckCount);
System.out.println("contains 方法累计执行时间(纳秒): " + totalContainsTime);
System.out.println("contains 方法平均执行时间(纳秒): " + (containsCheckCount > 0 ? totalContainsTime / containsCheckCount : 0));
redisUtil.addAssembledObjects(collectionId, assembledObjects);
return designPythonObjects;
}
private DesignPythonObject getCopy(DesignPythonObject pythonObject) {
DesignPythonObject designPythonObjectCopy = CopyUtil.copyObject(pythonObject, DesignPythonObject.class);
designPythonObjectCopy.setObjectSign(null);
// DesignPythonBasic basic = designPythonObjectCopy.getBasic();
// basic.setSave_name(null);
// designPythonObjectCopy.setBasic(basic);
DesignPythonBasic basic = designPythonObjectCopy.getBasic();
basic.setSave_name(null);
designPythonObjectCopy.setBasic(basic);
List<DesignPythonItem> items = designPythonObjectCopy.getItems();
List<DesignPythonItem> itemsCopy = new ArrayList<>();
for (DesignPythonItem item : items) {
@@ -371,17 +355,7 @@ public class PythonService {
private DesignPythonObject createDesignPythonObject(ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType, BigDecimal systemScale, String singleOverall, String switchCategory, int i) {
DesignPythonObject pythonObject = new DesignPythonObject();
pythonObject.setItems(coverToDesignPythonItemNew(elementVO, designPictureType, systemScale));
// pythonObject.setBasic(coverToBasic(pythonObject.getItems().get(0), singleOverall, switchCategory, elementVO.getDesignLibraryModelPoint()));
// if (CollectionUtil.isNotEmpty(elementVO.getRequestIdList())) {
// pythonObject.setObjectSign(elementVO.getRequestIdList().get(i));
// }
return pythonObject;
}
private DesignPythonObject createDesignPythonObjectNew(ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType, BigDecimal systemScale, String singleOverall, String switchCategory, int i, Long accountId) {
DesignPythonObject pythonObject = new DesignPythonObject();
pythonObject.setItems(coverToDesignPythonItemNewNew(elementVO, designPictureType, systemScale, accountId));
// pythonObject.setBasic(coverToBasic(pythonObject.getItems().get(0), singleOverall, switchCategory, elementVO.getDesignLibraryModelPoint()));
pythonObject.setBasic(coverToBasic(pythonObject.getItems().get(0), singleOverall, switchCategory, elementVO.getDesignLibraryModelPoint()));
if (CollectionUtil.isNotEmpty(elementVO.getRequestIdList())) {
pythonObject.setObjectSign(elementVO.getRequestIdList().get(i));
}
@@ -512,7 +486,7 @@ public class PythonService {
// }
//计算当前的Print图片类型
private CurrentDesignPrintPictureTypeEnum calculateCurrentDesignPrintPictureType(long pinPrintNum, long noPinPrintNum, long noPrintNum) {
private CurrentDesignPrintPictureTypeEnum calculateCurrentDesignPintPictureType(long pinPrintNum, long noPinPrintNum, long noPrintNum) {
List<Integer> codes = Lists.newArrayList();
if (pinPrintNum > 0) {
//pin默认优先选择 不参与计算 后续有调整再说
@@ -537,7 +511,7 @@ public class PythonService {
}
//计算当前的Print图片类型具体分布位置 0. 上衣 1.下衣 2.上衣和下衣都print
private List<String> calculateCurrentDesignPrintPictureTypeLayout(String modelSex) {
private List<String> calculateCurrentDesignPintPictureTypeLayout(String modelSex) {
if (modelSex.equals(Sex.FEMALE.getValue())) {
Long randomIndex = RandomsUtil.randomSysFile(0L, 3L);
if (randomIndex == 0) {
@@ -633,81 +607,6 @@ public class PythonService {
private List<DesignPythonItem> coverToDesignPythonItemNew(ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType, BigDecimal systemScale) {
List<DesignPythonItem> itemList = new ArrayList<>();
// 第一部分:计算 第一件 Sketch 耗时
long firstSketchStartTime = System.currentTimeMillis();
DesignPythonItem designPythonItem = calculatePythonItem(elementVO, designPictureType);
long firstSketchEndTime = System.currentTimeMillis();
// 记录第一部分耗时(秒)
redisUtil.recordTaskElapsedTime("firstSketchTime", (firstSketchEndTime - firstSketchStartTime) / 1000.0);
if (Objects.nonNull(designPythonItem)) {
itemList.add(designPythonItem);
if (elementVO.getSingleOverall().equals(SingleOverallEnum.OVERALL.getRealName())) {
List<String> otherSketchCategoryList = getOtherSketchCategoryList(elementVO.getModelSex(), designPythonItem);
// 提前缓存 AttributeRecognition 结果,减少多次调用
long getAttributeStartTime = System.currentTimeMillis();
JSONObject attributeRecognition = getAttributeRecognitionCached(designPythonItem, elementVO);
long getAttributeEndTime = System.currentTimeMillis();
// 记录第二部分获取特征值耗时(秒)
redisUtil.recordTaskElapsedTime("getAttributeRecognitionTime", (getAttributeEndTime - getAttributeStartTime) / 1000.0);
if (!otherSketchCategoryList.isEmpty()) {
long otherSketchStartTime = System.currentTimeMillis();
// for (String styleCategory : otherSketchCategoryList) {
// DesignPythonItem otherSketch = processAttributeRecognition(attributeRecognition, elementVO, designPictureType, styleCategory, systemScale);
// }
otherSketchCategoryList.parallelStream().forEach(styleCategory -> {
DesignPythonItem otherSketch = processAttributeRecognition(attributeRecognition, elementVO, designPictureType, styleCategory, systemScale);
synchronized (itemList) {
itemList.add(otherSketch);
}
});
long otherSketchEndTime = System.currentTimeMillis();
// 记录第三部分搭配 Sketch 耗时(秒)
redisUtil.recordTaskElapsedTime("otherSketchTime", (otherSketchEndTime - otherSketchStartTime) / 1000.0);
}
itemList.addAll(calculatePythonItemHairstyleShoes(elementVO, elementVO.getDesignLibraryModelPoint()));
}
}
return itemList;
}
private JSONObject getAttributeRecognitionCached(DesignPythonItem designPythonItem, ValidateElementVO elementVO) {
try {
String cacheKey = String.format("attributeRecognition:%s:%s:%s",
MD5Utils.encryptFile(minioUtil.download(designPythonItem.getPath())),
designPythonItem.getType(),
elementVO.getModelSex());
long startContainTime = System.nanoTime();
if (redisUtil.hasKey(cacheKey)) {
long endContainTime = System.nanoTime();
log.info("redisUtil.hasKey(cacheKey) time: {} seconds", (endContainTime - startContainTime) / 1_000_000_000.0);
return JSONObject.parseObject(redisUtil.getFromString(cacheKey));
} else {
long startTime = System.nanoTime();
JSONObject attributeRecognition = getAttributeRecognition(designPythonItem.getPath(), designPythonItem.getType(), elementVO.getModelSex());
redisUtil.addToString(cacheKey, attributeRecognition.toJSONString());
long endTime = System.nanoTime();
log.info("getAttributeRecognition execution time: {} seconds", (endTime - startTime) / 1_000_000_000.0);
return attributeRecognition;
}
} catch (Exception e) {
log.error("Failed to get AttributeRecognition: {}", e.getMessage());
return null;
}
}
private List<DesignPythonItem> coverToDesignPythonItemNewNew(ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType, BigDecimal systemScale, Long accountId) {
List<DesignPythonItem> itemList = new ArrayList<>();
DesignPythonItem designPythonItem = calculatePythonItem(elementVO, designPictureType);
if (Objects.nonNull(designPythonItem)) {
itemList.add(designPythonItem);
@@ -726,31 +625,13 @@ public class PythonService {
return itemList;
}
@Resource
private SketchDataMapper sketchDataMapper;
private DesignPythonItem processAttributeRecognition(JSONObject attributeRecognition, ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType, String styleCategory, BigDecimal systemScale) {
switch (designPictureType) {
case PIN:
return processPinAttributeRecognition(attributeRecognition, elementVO, styleCategory, systemScale);
case NO_PIN:
case SYS_FILE:
List<DesignPythonItem> designPythonItemList = processNoPinOrSysFileAttributeRecognition(attributeRecognition, elementVO, styleCategory, systemScale);
for (int i = 0; i < 2; i++) {
DesignPythonItem designPythonItem = designPythonItemList.get(i);
SketchData sketchData = new SketchData();
sketchData.setAccountId(elementVO.getAccountId());
sketchData.setPath(designPythonItem.getPath());
if (i == 0) {
sketchData.setType(2);
}else {
sketchData.setType(1);
}
sketchData.setCollectionId(elementVO.getCollectionId());
sketchDataMapper.insert(sketchData);
}
return designPythonItemList.get(1);
return processNoPinOrSysFileAttributeRecognition(attributeRecognition, elementVO, styleCategory, systemScale);
default:
throw new BusinessException("unknown designPictureType");
}
@@ -764,335 +645,44 @@ public class PythonService {
return coverSketchToDesignPythonItem(collectPin.get(randomNum).getId(), collectPin.get(randomNum), elementVO);
} else {
List<CollectionElement> collectNoPin = getFilteredCollectionElements(elementVO.getSketchBoardElements(), 0, styleCategory);
List<DesignPythonItem> designPythonItemList = processNoPinOrSysFileAttributeRecognitionWithPool(collectNoPin, attributeRecognition, elementVO, styleCategory, systemScale);
for (int i = 0; i < 2; i++) {
DesignPythonItem designPythonItem = designPythonItemList.get(i);
SketchData sketchData = new SketchData();
sketchData.setAccountId(elementVO.getAccountId());
sketchData.setPath(designPythonItem.getPath());
if (i == 0) {
// 第一个 新算法
sketchData.setType(2);
}else {
// 后一个 老算法
sketchData.setType(1);
}
sketchData.setCollectionId(elementVO.getCollectionId());
sketchDataMapper.insert(sketchData);
}
return designPythonItemList.get(1);
return processNoPinOrSysFileAttributeRecognitionWithPool(collectNoPin, attributeRecognition, elementVO, styleCategory, systemScale);
}
}
private List<DesignPythonItem> processNoPinOrSysFileAttributeRecognition(JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory, BigDecimal systemScale) {
private DesignPythonItem processNoPinOrSysFileAttributeRecognition(JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory, BigDecimal systemScale) {
List<CollectionElement> collectNoPin = getFilteredCollectionElements(elementVO.getSketchBoardElements(), 0, styleCategory);
return processNoPinOrSysFileAttributeRecognitionWithPool(collectNoPin, attributeRecognition, elementVO, styleCategory, systemScale);
}
private List<DesignPythonItem> processNoPinOrSysFileAttributeRecognitionWithPool(List<CollectionElement> collectionElements, JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory, BigDecimal systemScale) {
int poolNum = 40;
List<DesignPythonItem> result = new ArrayList<>();
if (CollectionUtil.isNotEmpty(collectionElements)) {
int collectionNoPinSize = collectionElements.size();
if (systemScale.compareTo(BigDecimal.ZERO) == 0) {
int randomNum = RandomsUtil.randomSysFile(collectionNoPinSize);
CollectionElement collectionElement = getRomdomCollectionElement(collectionElements, null, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
if (Objects.isNull(collectionElement)) {
log.info("bug");
}
DesignPythonItem designPythonItemNew = coverSketchToDesignPythonItem(null, collectionElement, elementVO);
result.add(designPythonItemNew);
DesignPythonItem designPythonItemOld = coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
result.add(designPythonItemOld);
return result;
} else if (systemScale.compareTo(BigDecimal.ONE) != 0) {
BigDecimal collectNoPinSize = BigDecimal.valueOf(collectionNoPinSize);
poolNum = collectNoPinSize.divide(systemScale, 0, RoundingMode.DOWN).intValue();
if (poolNum < 20) {
poolNum = 40;
}
List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
CollectionElement collectionElementNew = getRomdomCollectionElement(collectionElements, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
if (Objects.isNull(collectionElementNew)) {
log.info("bug");
}
DesignPythonItem designPythonItemNew = coverSketchToDesignPythonItem(null, collectionElementNew, elementVO);
result.add(designPythonItemNew);
collectionElements.addAll(list);
int randomNum = RandomsUtil.randomSysFile(collectionElements.size());
DesignPythonItem designPythonItemOld = coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
// if (randomNum < collectionNoPinSize) {
// return coverSketchToDesignPythonItem(collectionElements.get(randomNum).getId(), collectionElements.get(randomNum), elementVO);
// } else {
// return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
// }
result.add(designPythonItemOld);
return result;
}
}
List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
if (CollectionUtil.isEmpty(list)) {
System.out.println("bug");
}
int randomNum = RandomsUtil.randomSysFile(list.size());
CollectionElement collectionElementNew = getRomdomCollectionElement(null, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
DesignPythonItem designPythonItemNew = coverSketchToDesignPythonItem(null, collectionElementNew, elementVO);
result.add(designPythonItemNew);
DesignPythonItem designPythonItemOld = coverSketchToDesignPythonItem(null, list.get(randomNum), elementVO);
result.add(designPythonItemOld);
return result;
}
// private DesignPythonItem processNoPinOrSysFileAttributeRecognitionWithPoolNew(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) {
// CollectionElement collectionElement = getRomdomCollectionElement(collectionElements, null, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
// return coverSketchToDesignPythonItem(null, collectionElement, 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());
//
// CollectionElement collectionElement = getRomdomCollectionElement(collectionElements, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
// return coverSketchToDesignPythonItem(null, collectionElement, elementVO);
// }
// }
// List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
// CollectionElement collectionElement = getRomdomCollectionElement(null, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
// return coverSketchToDesignPythonItem(null, collectionElement, elementVO);
// }
private DesignPythonItem processNoPinOrSysFileAttributeRecognitionWithPoolNew(List<CollectionElement> collectionElements, JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory, BigDecimal systemScale) {
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) {
CollectionElement collectionElement = getRomdomCollectionElement(collectionElements, null, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
return coverSketchToDesignPythonItem(null, collectionElement, elementVO);
int randomNum = RandomsUtil.randomSysFile(collectionNoPinSize);
return coverSketchToDesignPythonItem(null, collectionElements.get(randomNum), elementVO);
} else if (systemScale.compareTo(BigDecimal.ONE) != 0) {
BigDecimal collectNoPinSize = BigDecimal.valueOf(collectionNoPinSize);
// 使用 BigDecimal 类型来计算 poolNum
BigDecimal poolNumAsBigDecimal = collectNoPinSize.divide(systemScale, 0, RoundingMode.DOWN);
// 比较 poolNum + collectNoPinSize 是否小于 20
if (poolNumAsBigDecimal.add(collectNoPinSize).compareTo(BigDecimal.valueOf(20)) < 0) {
// 判断成立,设置 poolNum 为 20 - collectNoPinSize
poolNum = BigDecimal.valueOf(20).subtract(collectNoPinSize).intValue();
}
poolNum = collectNoPinSize.divide(systemScale, 0, RoundingMode.DOWN).intValue();
List<CollectionElement> list = getSystemSketchPool(attributeRecognition, styleCategory, elementVO.getModelSex(), poolNum, elementVO.getStyle());
CollectionElement collectionElement = getRomdomCollectionElement(collectionElements, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
return coverSketchToDesignPythonItem(null, collectionElement, elementVO);
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());
CollectionElement collectionElement = getRomdomCollectionElement(null, list, systemScale, elementVO.getCollectionId(), elementVO.getAccountId());
return coverSketchToDesignPythonItem(null, collectionElement, elementVO);
}
private CollectionElement getRomdomCollectionElement(List<CollectionElement> noPinSketchList, List<CollectionElement> systemSketchList, BigDecimal systemScale, Long collectionId, Long accountId) {
// 确定选择的池子:根据 systemScale 决定选取 noPin 或 system
boolean selectNoPin = Math.random() < (1 - systemScale.doubleValue());
List<CollectionElement> selectedList = new ArrayList<>();
if (selectNoPin) {
if (!CollectionUtils.isEmpty(noPinSketchList)) {
selectedList.addAll(noPinSketchList);
}else {
selectedList.addAll(systemSketchList);
}
}else {
selectedList.addAll(systemSketchList);
}
if (CollectionUtil.isEmpty(selectedList)) {
return null; // 如果选中的池子为空,返回 null
}
// 计算每个元素的得分
Map<CollectionElement, Double> scoreMap = new HashMap<>();
for (CollectionElement element : selectedList) {
double baseScore = Math.random(); // 基础分0 到 1 的随机数
// 根据是否是之前用过或喜欢过的 sketch 调整得分
double adjustment = 0.0;
int likedSketch = isLikedSketch(element, accountId);
int maxLikedSketch = getMaxLikedSketchNum(accountId);
if (maxLikedSketch > 0) {
if (likedSketch > 0) {
adjustment = Math.log(1 + likedSketch)/Math.log(1 + maxLikedSketch);
}
}
double finalScore = adjustment;
// double finalScore = 0.7 * baseScore + 0.3 * adjustment;
scoreMap.put(element, finalScore);
}
// 按得分排序并返回最高分的元素
return selectedList.stream()
.sorted((e1, e2) -> Double.compare(scoreMap.get(e2), scoreMap.get(e1))) // 按分数降序排序
.findFirst()
.orElse(null); // 如果池子为空,返回 null
}
@Resource
private RedisTemplate<String, String> redisTemplate;
// 检查是否是 like 过的 sketch
private int isLikedSketch(CollectionElement element, Long accountId) {
String redisKey = "user_liked_sketch:" + accountId;
// 尝试从 Redis 获取该 sketch path 的喜欢次数
Integer likedCount = (Integer) redisTemplate.opsForHash().get(redisKey, element.getUrl());
if (likedCount != null) {
return likedCount; // 如果缓存中有,直接返回
}
// 如果缓存中没有,则查询数据库
QueryWrapper<UserPreferenceLogTest> qw = new QueryWrapper<>();
qw.lambda().eq(UserPreferenceLogTest::getPath, element.getUrl());
qw.lambda().eq(UserPreferenceLogTest::getAccountId, accountId);
// List<UserPreferenceLogTest> userPreferenceLogTests = userPreferenceLogMapper.selectList(qw);
// if (CollectionUtils.isEmpty(userPreferenceLogTests)) {
// return 0;
// }
//
// 将结果存入 Redis
// redisTemplate.opsForHash().put(redisKey, element.getUrl(), userPreferenceLogTests.size());
//
// 返回查询到的结果
// return userPreferenceLogTests.size();
return 10;
}
private int getMaxLikedSketchNum(Long accountId) {
// String redisKey = "user_liked_sketch:" + accountId + ":maxLikes";
//
// // 尝试从 Redis 获取最大喜欢次数
// String maxLikesStr = (String) redisTemplate.opsForValue().get(redisKey);
// if (maxLikesStr != null) {
// return Integer.parseInt(maxLikesStr); // 如果缓存中有,直接返回
// }
//
// // 如果缓存中没有,则查询数据库
// QueryWrapper<UserPreferenceLogTest> qw = new QueryWrapper<>();
// qw.lambda().eq(UserPreferenceLogTest::getAccountId, accountId);
// List<UserPreferenceLogTest> userPreferenceLogTests = userPreferenceLogMapper.selectList(qw);
// if (CollectionUtils.isEmpty(userPreferenceLogTests)) {
// return 0;
// }
// // 按路径统计数量
// Map<String, Long> pathCountMap = userPreferenceLogTests.stream()
// .collect(Collectors.groupingBy(UserPreferenceLogTest::getPath, Collectors.counting()));
//
// // 获取最大喜欢次数
// int maxLiked = pathCountMap.values().stream()
// .max(Long::compare)
// .orElse(0L)
// .intValue();
//
// // 将最大喜欢次数存入 Redis
// redisTemplate.opsForValue().set(redisKey, String.valueOf(maxLiked));
return 10;
int randomNum = RandomsUtil.randomSysFile(list.size());
return coverSketchToDesignPythonItem(null, list.get(randomNum), elementVO);
}
private List<CollectionElement> getFilteredCollectionElements(List<CollectionElement> elements, int hasPin, String styleCategory) {
return CollectionUtil.isNotEmpty(elements) ? elements.stream().filter(o -> o.getHasPin() == hasPin && o.getLevel2Type().equals(styleCategory)).collect(Collectors.toList()) : null;
}
// 历史使用记录,可以通过一个 Map 存储 MD5 或 ID
private static Map<String, Integer> usageHistory = new HashMap<>();
// 给定扰动参数
private static final double DISTURBANCE_FACTOR = 1;
private static double calculateScore(CollectionElement element, boolean isNoPin, double systemScale, int noPinCount, int systemCount) {
double score = 0.0;
return score;
}
public static void main(String[] args) {
List<CollectionElement> noPinSketchList = new ArrayList<>();
List<CollectionElement> systemSketchList = new ArrayList<>();
// 构造 noPin 和 system 的元素
for (int i = 0; i < 50; i++) {
CollectionElement element = new CollectionElement();
element.setMd5("noPin_" + i); // 设置 md5 前缀为 noPin
noPinSketchList.add(element);
}
for (int i = 0; i < 50; i++) {
CollectionElement element = new CollectionElement();
element.setMd5("system_" + i); // 设置 md5 前缀为 system
systemSketchList.add(element);
}
// 设置 systemScale 测试值
double systemScale = 0.6;
// 模拟测试次数
int totalTests = 1000000;
int noPinWinCount = 0;
for (int i = 0; i < totalTests; i++) {
CollectionElement highestElement = getHighestScoredElement(noPinSketchList, systemSketchList, systemScale);
if (highestElement != null && highestElement.getMd5().startsWith("noPin")) {
noPinWinCount++;
}
}
double probability = (double) noPinWinCount / totalTests;
System.out.printf("Probability of highest score being noPin: %.4f%n", probability);
}
private static CollectionElement getHighestScoredElement(List<CollectionElement> noPinSketchList,
List<CollectionElement> systemSketchList,
double systemScale) {
// 获取 noPin 列表中得分最高的元素
CollectionElement highestNoPinElement = getHighestScoreElement(noPinSketchList, true, systemScale,
noPinSketchList.size(), systemSketchList.size());
// 获取 system 列表中得分最高的元素
CollectionElement highestSystemElement = getHighestScoreElement(systemSketchList, false, systemScale,
noPinSketchList.size(), systemSketchList.size());
// 比较两者得分并返回最高分的元素
// if (highestNoPinElement != null && highestSystemElement != null) {
// return highestNoPinElement.getScore() >= highestSystemElement.getScore() ? highestNoPinElement : highestSystemElement;
// }
return null;
}
private static CollectionElement getHighestScoreElement(List<CollectionElement> sketchList,
boolean isNoPin,
double systemScale,
int noPinCount,
int systemCount) {
double maxScore = Double.NEGATIVE_INFINITY;
CollectionElement highestElement = null;
for (CollectionElement element : sketchList) {
double score = calculateScore(element, isNoPin, systemScale, noPinCount, systemCount);
// element.setScore(score); // 设置得分以便比较
// if (score > maxScore) {
// maxScore = score;
// highestElement = element;
// }
}
return highestElement;
}
private DesignPythonItem processAttributeRecognitionBySameCategory(JSONObject attributeRecognition, ValidateElementVO elementVO, String styleCategory) {
List<CollectionElement> list = getSystemSketchPoolBySameCategory(attributeRecognition, styleCategory, elementVO.getModelSex(), elementVO.getStyle());
int randomNum = RandomsUtil.randomSysFile(list.size());
@@ -1276,7 +866,7 @@ public class PythonService {
paramArray.add(paramJSONObject);
String param = JSON.toJSONString(paramArray, SerializerFeature.DisableCircularReferenceDetect);
// log.info("PythonService##design 请求参数:####{}", param);
log.info("PythonService##design 请求参数:####{}", param);
RequestBody body = RequestBody.create(mediaType, param);
Request request = new Request.Builder()
.url(accessPythonIp + ":" + accessPythonPort + "/api/attribute_recognition")
@@ -1289,7 +879,7 @@ public class PythonService {
if (response.isSuccessful()) {
String responseBody = Objects.requireNonNull(response.body()).string();
JSONObject responseObject = JSON.parseObject(responseBody);
// log.info("PythonService##responseObject###{}", responseObject);
log.info("PythonService##responseObject###{}", responseObject);
return responseObject;
} else {
log.error("PythonService##design 请求异常:{}", response);
@@ -2044,10 +1634,10 @@ public class PythonService {
pythonItem.setPath(sysFileVO.getUrl());
pythonItem.setBusinessId(sysFileVO.getId());
if (SysFileLevel2TypeEnum.SHOES.getRealName().equals(type)) {
// CollectionColorDTO randomColor = getRandomColor(elementVO.getColorBoards());
// pythonItem.setColor(randomColor.getRgbValue());
// pythonItem.setGradient(randomColor.getGradientMinioUrl());
// pythonItem.setGradientString(randomColor.getGradientString());
CollectionColorDTO randomColor = getRandomColor(elementVO.getColorBoards());
pythonItem.setColor(randomColor.getRgbValue());
pythonItem.setGradient(randomColor.getGradientMinioUrl());
pythonItem.setGradientString(randomColor.getGradientString());
}
}
}
@@ -2410,10 +2000,10 @@ public class PythonService {
designPythonItemBlouse.setPath(path);
//所有的icon都是none
designPythonItemBlouse.setIcon("none");
// CollectionColorDTO randomColor = getRandomColor(elementVO.getColorBoards());
// designPythonItemBlouse.setColor(randomColor.getRgbValue());
// designPythonItemBlouse.setGradient(randomColor.getGradientMinioUrl());
// designPythonItemBlouse.setGradientString(randomColor.getGradientString());
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(type)) {
DesignPythonItemPrint designPythonItemPrint = CopyUtil.copyObject(elementVO.getDesignPythonItemPrint(), DesignPythonItemPrint.class);
@@ -2435,17 +2025,14 @@ public class PythonService {
designPythonItemBlouse.setElementId(elementId);
designPythonItemBlouse.setBusinessId(elementId);
}
if (Objects.isNull(collectionElement)) {
log.info("bug");
}
designPythonItemBlouse.setType(collectionElement.getLevel2Type());
designPythonItemBlouse.setPath(collectionElement.getUrl());
//所有的icon都是none
designPythonItemBlouse.setIcon("none");
// CollectionColorDTO randomColor = getRandomColor(elementVO.getColorBoards());
// designPythonItemBlouse.setColor(randomColor.getRgbValue());
// designPythonItemBlouse.setGradient(randomColor.getGradientMinioUrl());
// designPythonItemBlouse.setGradientString(randomColor.getGradientString());
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(collectionElement.getLevel2Type())) {
DesignPythonItemPrint designPythonItemPrint = CopyUtil.copyObject(elementVO.getDesignPythonItemPrint(), DesignPythonItemPrint.class);
@@ -4212,98 +3799,4 @@ public class PythonService {
throw new BusinessException("design.interface.exception");
}
public DesignPythonObjects covertDesignParamTest(BigDecimal systemScale, String singleOverall,
String switchCategory, ValidateElementVO elementVO, String processId, Set<DesignPythonObject> assembledObjects, Long collectionId) {
AuthPrincipalVo userHolder = UserContext.getUserHolder();
DesignPythonObjects designPythonObjects = new DesignPythonObjects();
List<DesignPythonObject> objects = new ArrayList<>();
designPythonObjects.setObjects(objects);
// designPythonObjects.setProcess_id(processId);
long pinPrintNum = calculateDesignPinPrintNum(elementVO.getPrintBoardElements());
long noPinPrintNum = calculateDesignNoPinPrintNum(elementVO.getPrintBoardElements(), elementVO.getDesignNum());
long noPrintNum = elementVO.getDesignNum() - pinPrintNum - noPinPrintNum;
elementVO.setNoPinPrintNum(noPinPrintNum);
int[] sketchNumbers = new int[3];
int designNum = elementVO.getDesignNum();
if (CollectionUtil.isEmpty(assembledObjects)) {
assembledObjects = new HashSet<>(); // 用于存储已组装的 DesignPythonObject
}
DesignPythonObject lastAssembledObject = null; // 上一次组装的对象
long totalContainsTime = 0; // 用于累计 contains 方法的时间
int containsCheckCount = 0; // contains 方法调用次数
for (int i = 0; i < designNum; i++) {
CurrentDesignPictureTypeEnum designPictureType = calculateCurrentDesignPictureTypeNew(elementVO, sketchNumbers, systemScale);
if (designPictureType == null) break;
CurrentDesignPrintPictureTypeEnum designPrintPictureType = calculateCurrentDesignPrintPictureType(pinPrintNum, noPinPrintNum, noPrintNum);
if (designPrintPictureType == null) break;
updateSketchNumbers(designPictureType, sketchNumbers);
switch (designPrintPictureType) {
case PIN:
pinPrintNum--;
break;
case NO_PIN:
noPinPrintNum--;
break;
case NO:
noPrintNum--;
break;
}
DesignPythonItemPrint designPythonItemPrint = getRandomPrint(elementVO, designPrintPictureType);
elementVO.setDesignPythonItemPrint(designPythonItemPrint);
elementVO.setDesignPrintPictureTypeLayoutList(calculateCurrentDesignPrintPictureTypeLayout(elementVO.getModelSex()));
List<String> beforeAssemblyHasUseMd5List = new ArrayList<>(elementVO.getHasUseMd5List());
elementVO.setCollectionId(collectionId);
DesignPythonObject pythonObject = createDesignPythonObject(elementVO, designPictureType, systemScale, singleOverall, switchCategory, i);
// List<String> afterAssemblyHasUseMd5List = elementVO.getHasUseMd5List();
// 如果当前对象与已组装的对象重复,则跳过当前组装
DesignPythonObject designPythonObjectCopy = getCopy(pythonObject);
// 计算 contains 方法的执行时间
long startTime = System.nanoTime();
boolean isDuplicate = assembledObjects.contains(designPythonObjectCopy);
long endTime = System.nanoTime();
// System.out.println("单次 方法调用耗时(纳秒): " + (endTime - startTime));
totalContainsTime += (endTime - startTime);
containsCheckCount++;
if (isDuplicate) {
// if (lastAssembledObject != null && assembledObjects.contains(lastAssembledObject)) {
// System.out.println("当前组装的对象与前两个组装的对象重复,结束组装。");
// break;
// }
elementVO.setHasUseMd5List(beforeAssemblyHasUseMd5List);
i --;
continue;
}
// 将当前对象添加到已组装的集合中,并记录
assembledObjects.add(designPythonObjectCopy);
// lastAssembledObject = designPythonObjectCopy; // 更新上一次组装的对象
// for (DesignPythonItem item : pythonObject.getItems()) {
// redisUtil.addPathToCache(collectionId, userHolder.getId(), item.getPath());
// }
objects.add(pythonObject);
// redisUtil.addProcessId(processId, i + 1);
}
// 输出统计结果
// System.out.println("contains 方法调用次数: " + containsCheckCount);
// System.out.println("contains 方法累计执行时间(纳秒): " + totalContainsTime);
// System.out.println("contains 方法平均执行时间(纳秒): " + (containsCheckCount > 0 ? totalContainsTime / containsCheckCount : 0));
redisUtil.addAssembledObjects(collectionId, assembledObjects);
return designPythonObjects;
}
}

View File

@@ -329,7 +329,7 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
//组装design入参
long startTime = System.currentTimeMillis();
DesignPythonObjects pythonObjects = pythonService.covertDesignParam(designDTO.getSystemScale(),
designDTO.getSingleOverall(), designDTO.getSwitchCategory(), elementVO, designDTO.getProcessId());
designDTO.getSingleOverall(), designDTO.getSwitchCategory(), elementVO, designDTO.getProcessId(), collectionIdParam);
long endTime = System.currentTimeMillis();
long totalTimeInSeconds = (endTime - startTime) / 1000;
log.info("组装入参运行时间:" + totalTimeInSeconds + "");
@@ -376,7 +376,8 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
return requestId;
}
private void parseMoodboardPosition(String moodboardPosition, Long collectionIdParam) {
@Override
public void parseMoodboardPosition(String moodboardPosition, Long collectionIdParam) {
if (!StringUtils.isEmpty(moodboardPosition)) {
// 将 JSON 字符串解析为 JSONObject
JSONObject moodboardPositionJson = JSONObject.parseObject(moodboardPosition);
@@ -1805,7 +1806,7 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
//组装design入参
long startTime = System.currentTimeMillis();
DesignPythonObjects pythonObjects = pythonService.covertDesignParam(designDTO.getSystemScale(),
designDTO.getSingleOverall(), designDTO.getSwitchCategory(), elementVO, designDTO.getProcessId());
designDTO.getSingleOverall(), designDTO.getSwitchCategory(), elementVO, designDTO.getProcessId(), collectionId);
long endTime = System.currentTimeMillis();
long totalTimeInSeconds = (endTime - startTime) / 1000;
log.info("组装入参运行时间:" + totalTimeInSeconds + "");

View File

@@ -1391,6 +1391,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
});
vo.setUserGroupId(userGroupId);
vo.setUserLikeDetails(userLikeVOS);
moduleChooseVO.setDesign(vo);
}else if (module.equals(Module.canvas.name())) {
CavasModuleChooseVO vo = new CavasModuleChooseVO();