TASK:模块化;

This commit is contained in:
shahaibo
2025-04-08 15:15:39 +08:00
parent 87b702c24b
commit fd8d71e2a2
7 changed files with 227 additions and 25 deletions

View File

@@ -0,0 +1,8 @@
package com.ai.da.service;
import com.ai.da.model.dto.ProgressDTO;
public interface ProductImageService {
void asyncInitialize(Long brandId);
double getInitializeProgress(Long brandId);
}

View File

@@ -76,6 +76,8 @@ public interface UserLikeGroupService extends IService<UserLikeGroup> {
Boolean productImageInitialize(ProductImageInitializeDTO productImageInitializeDTO);
double getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO);
IPage<ProjectVO> getPage(ProjectQueryDTO projectQueryDTO);
ModuleChooseVO getModuleContent(ProjectDTO projectDTO);
@@ -105,4 +107,6 @@ public interface UserLikeGroupService extends IService<UserLikeGroup> {
String downloadZip(Long threeDSimpleId, String sizeType, String size, HttpServletResponse response) throws MinioException, IOException;
Boolean delete(Long projectId);
Boolean brandDNADelete(BrandDNADTO brandDNADTO);
}

View File

@@ -0,0 +1,151 @@
package com.ai.da.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.ai.da.common.utils.RedisUtil;
import com.ai.da.mapper.primary.BrandRelLibraryMapper;
import com.ai.da.mapper.primary.LibraryMapper;
import com.ai.da.mapper.primary.ProductImageAttributeMapper;
import com.ai.da.mapper.primary.entity.BrandRelLibrary;
import com.ai.da.mapper.primary.entity.Library;
import com.ai.da.mapper.primary.entity.ProductImageAttribute;
import com.ai.da.mapper.secondary.entity.AttributeRecognitionJSON;
import com.ai.da.model.dto.ProgressDTO;
import com.ai.da.python.PythonService;
import com.ai.da.service.ProductImageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ProductImageServiceImpl implements ProductImageService {
@Autowired
private BrandRelLibraryMapper brandRelLibraryMapper;
@Autowired
private LibraryMapper libraryMapper;
@Autowired
private ProductImageAttributeMapper productImageAttributeMapper;
@Autowired
private PythonService pythonService;
@Resource
private RedisUtil redisUtil;
@Async
@Override
public void asyncInitialize(Long brandId) {
String progressKey = String.valueOf(brandId);
ProgressDTO progressDTO = new ProgressDTO(0, 0, false);
redisUtil.setTaskProgressDTO(progressKey, progressDTO);
try {
List<BrandRelLibrary> brandRelLibraries = brandRelLibraryMapper.selectList(
new QueryWrapper<BrandRelLibrary>().lambda().eq(BrandRelLibrary::getBrandId, brandId)
);
Set<Long> libraryIds = brandRelLibraries.stream().map(BrandRelLibrary::getLibraryId).collect(Collectors.toSet());
progressDTO.setTotal(libraryIds.size());
int current = 0;
for (Long libraryId : libraryIds) {
Library library = libraryMapper.selectById(libraryId);
String url = library.getUrl();
JSONObject result = pythonService.segProduct(url);
JSONArray data = result.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
JSONObject obj = data.getJSONObject(i);
JSONObject attribute = obj.getJSONObject("attribute");
AttributeRecognitionJSON attrJSON = attribute.toJavaObject(AttributeRecognitionJSON.class);
ProductImageAttribute attr = toAttrDict(attrJSON);
attr.setLibraryId(libraryId);
productImageAttributeMapper.insert(attr);
}
// 更新当前进度
current++;
progressDTO.setCurrent(current);
redisUtil.setTaskProgressDTO(progressKey, progressDTO);
}
} catch (Exception e) {
// log.error("初始化失败", e);
redisUtil.setTaskProgressDTO(progressKey, new ProgressDTO(0, 0, true));
}
}
@Override
public double getInitializeProgress(Long brandId) {
ProgressDTO dto = redisUtil.getTaskProgressDTO(String.valueOf(brandId));
if (dto.getTotal() == 0 || dto.isError()) return 0.0;
return (dto.getCurrent() * 1.0 / dto.getTotal());
}
private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) {
ProductImageAttribute attributeRetrieval = new ProductImageAttribute();
// 处理 imgName
if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) {
attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0));
}
// 处理 length
if (CollectionUtil.isNotEmpty(attrDictJSON.getLength()) && attrDictJSON.getLength().get(0) != null) {
attributeRetrieval.setLength(attrDictJSON.getLength().get(0));
}
// 处理 sleeveLength
if (CollectionUtil.isNotEmpty(attrDictJSON.getSleeveLength()) && attrDictJSON.getSleeveLength().get(0) != null) {
attributeRetrieval.setSleeveLength(attrDictJSON.getSleeveLength().get(0));
}
// 处理 sleeveShape
if (CollectionUtil.isNotEmpty(attrDictJSON.getSleeveShape()) && attrDictJSON.getSleeveShape().get(0) != null) {
attributeRetrieval.setSleeveShape(attrDictJSON.getSleeveShape().get(0));
}
// 处理 sleeveShoulder
if (CollectionUtil.isNotEmpty(attrDictJSON.getSleeveShoulder()) && attrDictJSON.getSleeveShoulder().get(0) != null) {
attributeRetrieval.setSleeveShoulder(attrDictJSON.getSleeveShoulder().get(0));
}
// 处理 neckline
if (CollectionUtil.isNotEmpty(attrDictJSON.getNeckline()) && attrDictJSON.getNeckline().get(0) != null) {
attributeRetrieval.setNeckline(attrDictJSON.getNeckline().get(0));
}
// 处理 collar
if (CollectionUtil.isNotEmpty(attrDictJSON.getCollar()) && attrDictJSON.getCollar().get(0) != null) {
attributeRetrieval.setCollar(attrDictJSON.getCollar().get(0));
}
// 处理 design
if (CollectionUtil.isNotEmpty(attrDictJSON.getDesign()) && attrDictJSON.getDesign().get(0) != null) {
attributeRetrieval.setDesign(attrDictJSON.getDesign().get(0));
}
// 处理 silhouette
if (CollectionUtil.isNotEmpty(attrDictJSON.getSilhouette()) && attrDictJSON.getSilhouette().get(0) != null) {
attributeRetrieval.setSilhouette(attrDictJSON.getSilhouette().get(0));
}
// 处理 type
if (CollectionUtil.isNotEmpty(attrDictJSON.getType()) && attrDictJSON.getType().get(0) != null) {
attributeRetrieval.setType(attrDictJSON.getType().get(0));
}
// 处理 openingType
if (CollectionUtil.isNotEmpty(attrDictJSON.getOpeningType()) && attrDictJSON.getOpeningType().get(0) != null) {
attributeRetrieval.setOpeningType(attrDictJSON.getOpeningType().get(0));
}
// 处理 subtype
if (CollectionUtil.isNotEmpty(attrDictJSON.getSubtype()) && attrDictJSON.getSubtype().get(0) != null) {
attributeRetrieval.setSubtype(attrDictJSON.getSubtype().get(0));
}
return attributeRetrieval;
}
}

View File

@@ -129,6 +129,8 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
private ThreeDSimpleMapper threeDSimpleMapper;
@Resource
private ThreeDModuleMapper threeDModuleMapper;
@Resource
private ProductImageService productImageService;
@Override
public void deleteUserGroup(Long userGroupId) {
@@ -1156,33 +1158,14 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
@Override
public Boolean productImageInitialize(ProductImageInitializeDTO productImageInitializeDTO) {
Long brandId = productImageInitializeDTO.getBrandId();
QueryWrapper<BrandRelLibrary> brandRelLibraryQueryWrapper = new QueryWrapper<>();
brandRelLibraryQueryWrapper.lambda().eq(BrandRelLibrary::getBrandId, brandId);
List<BrandRelLibrary> brandRelLibraries = brandRelLibraryMapper.selectList(brandRelLibraryQueryWrapper);
Set<Long> collect = brandRelLibraries.stream().map(BrandRelLibrary::getLibraryId).collect(Collectors.toSet());
productImageService.asyncInitialize(productImageInitializeDTO.getBrandId());
return true;
}
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
accountService.getById(authPrincipalVo.getId());
@Override
public double getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO) {
for (Long libraryId : collect) {
Library library = libraryMapper.selectById(libraryId);
String url = library.getUrl();
String gender = library.getLevel2Type();
// 提取sketch
JSONObject jsonObject = pythonService.segProduct(url);
JSONArray data = jsonObject.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
JSONObject jsonObject1 = data.getJSONObject(i);
JSONObject attribute = jsonObject1.getJSONObject("attribute");
AttributeRecognitionJSON attrDictJSON = attribute.toJavaObject(AttributeRecognitionJSON.class);
ProductImageAttribute productImageAttribute = toAttrDict(attrDictJSON);
productImageAttribute.setLibraryId(library.getId());
productImageAttributeMapper.insert(productImageAttribute);
}
}
return null;
return productImageService.getInitializeProgress(productImageInitializeDTO.getBrandId());
}
private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) {
@@ -2184,4 +2167,10 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
projectMapper.deleteById(projectId);
return Boolean.TRUE;
}
@Override
public Boolean brandDNADelete(BrandDNADTO brandDNADTO) {
brandDNAMapper.deleteById(brandDNADTO.getId());
return Boolean.TRUE;
}
}