TASK:模块化;

This commit is contained in:
shahaibo
2025-04-08 17:29:10 +08:00
parent fd8d71e2a2
commit 4cda43f3b5
10 changed files with 66 additions and 28 deletions

View File

@@ -3,11 +3,13 @@ package com.ai.da;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@Slf4j @Slf4j
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
@EnableAsync
public class AiDaApplication { public class AiDaApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -514,7 +514,8 @@ public class RedisUtil {
String key = "task:progress:" + taskId; String key = "task:progress:" + taskId;
String json = redisTemplate.opsForValue().get(key); String json = redisTemplate.opsForValue().get(key);
if (StringUtils.isBlank(json)) { if (StringUtils.isBlank(json)) {
return new ProgressDTO(0, 0, false); // return new ProgressDTO(0, 0, false);
return null;
} }
try { try {
return JSON.parseObject(json, ProgressDTO.class); return JSON.parseObject(json, ProgressDTO.class);

View File

@@ -261,7 +261,7 @@ public class SavedCollectionController {
@ApiOperation(value = "getInitializeProgress") @ApiOperation(value = "getInitializeProgress")
@PostMapping("/getInitializeProgress") @PostMapping("/getInitializeProgress")
public Response<Double> getInitializeProgress(@Valid @RequestBody ProductImageInitializeDTO productImageInitializeDTO) { public Response<InitializeProgressVO> getInitializeProgress(@Valid @RequestBody ProductImageInitializeDTO productImageInitializeDTO) {
return Response.success(userLikeGroupService.getInitializeProgress(productImageInitializeDTO)); return Response.success(userLikeGroupService.getInitializeProgress(productImageInitializeDTO));
} }

View File

@@ -18,7 +18,7 @@ public class ProductImageAttribute implements Serializable {
@TableId(value = "id", type = IdType.AUTO) @TableId(value = "id", type = IdType.AUTO)
private Long id; private Long id;
private String imgName; // private String imgName;
private String length; private String length;
private String sleeveLength; private String sleeveLength;
private String sleeveShape; private String sleeveShape;

View File

@@ -9,4 +9,6 @@ public class ProductImageInitializeDTO {
private List<Long> libraryIds; private List<Long> libraryIds;
private Long brandId; private Long brandId;
private Boolean update;
} }

View File

@@ -0,0 +1,10 @@
package com.ai.da.model.vo;
import lombok.Data;
@Data
public class InitializeProgressVO {
private Boolean analyzed;
private double percent;
}

View File

@@ -4,5 +4,5 @@ import com.ai.da.model.dto.ProgressDTO;
public interface ProductImageService { public interface ProductImageService {
void asyncInitialize(Long brandId); void asyncInitialize(Long brandId);
double getInitializeProgress(Long brandId); // double getInitializeProgress(Long brandId);
} }

View File

@@ -76,7 +76,7 @@ public interface UserLikeGroupService extends IService<UserLikeGroup> {
Boolean productImageInitialize(ProductImageInitializeDTO productImageInitializeDTO); Boolean productImageInitialize(ProductImageInitializeDTO productImageInitializeDTO);
double getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO); InitializeProgressVO getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO);
IPage<ProjectVO> getPage(ProjectQueryDTO projectQueryDTO); IPage<ProjectVO> getPage(ProjectQueryDTO projectQueryDTO);

View File

@@ -17,6 +17,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -45,6 +46,8 @@ public class ProductImageServiceImpl implements ProductImageService {
@Async @Async
@Override @Override
public void asyncInitialize(Long brandId) { public void asyncInitialize(Long brandId) {
System.out.println(">>> [asyncInitialize] 当前线程:" + Thread.currentThread().getName());
String progressKey = String.valueOf(brandId); String progressKey = String.valueOf(brandId);
ProgressDTO progressDTO = new ProgressDTO(0, 0, false); ProgressDTO progressDTO = new ProgressDTO(0, 0, false);
redisUtil.setTaskProgressDTO(progressKey, progressDTO); redisUtil.setTaskProgressDTO(progressKey, progressDTO);
@@ -62,15 +65,20 @@ public class ProductImageServiceImpl implements ProductImageService {
Library library = libraryMapper.selectById(libraryId); Library library = libraryMapper.selectById(libraryId);
String url = library.getUrl(); String url = library.getUrl();
JSONObject result = pythonService.segProduct(url); QueryWrapper<ProductImageAttribute> qw = new QueryWrapper<>();
JSONArray data = result.getJSONArray("data"); qw.lambda().eq(ProductImageAttribute::getLibraryId, libraryId);
for (int i = 0; i < data.size(); i++) { List<ProductImageAttribute> productImageAttributes = productImageAttributeMapper.selectList(qw);
JSONObject obj = data.getJSONObject(i); if (!CollectionUtil.isNotEmpty(productImageAttributes)) {
JSONObject attribute = obj.getJSONObject("attribute"); JSONObject result = pythonService.segProduct(url);
AttributeRecognitionJSON attrJSON = attribute.toJavaObject(AttributeRecognitionJSON.class); JSONArray data = result.getJSONArray("data");
ProductImageAttribute attr = toAttrDict(attrJSON); for (int i = 0; i < data.size(); i++) {
attr.setLibraryId(libraryId); JSONObject obj = data.getJSONObject(i);
productImageAttributeMapper.insert(attr); JSONObject attribute = obj.getJSONObject("attribute");
AttributeRecognitionJSON attrJSON = attribute.toJavaObject(AttributeRecognitionJSON.class);
ProductImageAttribute attr = toAttrDict(attrJSON);
attr.setLibraryId(libraryId);
productImageAttributeMapper.insert(attr);
}
} }
// 更新当前进度 // 更新当前进度
@@ -80,26 +88,26 @@ public class ProductImageServiceImpl implements ProductImageService {
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.getMessage());
// log.error("初始化失败", e); // log.error("初始化失败", e);
redisUtil.setTaskProgressDTO(progressKey, new ProgressDTO(0, 0, true)); redisUtil.setTaskProgressDTO(progressKey, new ProgressDTO(0, 0, true));
} }
} }
@Override // public double getInitializeProgress(Long brandId) {
public double getInitializeProgress(Long brandId) { // ProgressDTO dto = redisUtil.getTaskProgressDTO(String.valueOf(brandId));
ProgressDTO dto = redisUtil.getTaskProgressDTO(String.valueOf(brandId)); // if (dto.getTotal() == 0 || dto.isError()) return 0.0;
if (dto.getTotal() == 0 || dto.isError()) return 0.0; // return (dto.getCurrent() * 1.0 / dto.getTotal());
return (dto.getCurrent() * 1.0 / dto.getTotal()); // }
}
private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) { private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) {
ProductImageAttribute attributeRetrieval = new ProductImageAttribute(); ProductImageAttribute attributeRetrieval = new ProductImageAttribute();
// 处理 imgName // 处理 imgName
if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) { // if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) {
attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0)); // attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0));
} // }
// 处理 length // 处理 length
if (CollectionUtil.isNotEmpty(attrDictJSON.getLength()) && attrDictJSON.getLength().get(0) != null) { if (CollectionUtil.isNotEmpty(attrDictJSON.getLength()) && attrDictJSON.getLength().get(0) != null) {
attributeRetrieval.setLength(attrDictJSON.getLength().get(0)); attributeRetrieval.setLength(attrDictJSON.getLength().get(0));

View File

@@ -1163,18 +1163,33 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
} }
@Override @Override
public double getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO) { public InitializeProgressVO getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO) {
InitializeProgressVO vo = new InitializeProgressVO();
return productImageService.getInitializeProgress(productImageInitializeDTO.getBrandId()); // QueryWrapper<>
ProgressDTO dto = redisUtil.getTaskProgressDTO(String.valueOf(productImageInitializeDTO.getBrandId()));
if (Objects.isNull(dto)) {
vo.setAnalyzed(false);
return vo;
}
vo.setAnalyzed(true);
if (dto.getTotal() == 0 || dto.isError()) {
vo.setPercent(0.0);
return vo;
}
vo.setPercent(dto.getCurrent() * 1.0 / dto.getTotal());
return vo;
} }
private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) { private ProductImageAttribute toAttrDict(AttributeRecognitionJSON attrDictJSON) {
ProductImageAttribute attributeRetrieval = new ProductImageAttribute(); ProductImageAttribute attributeRetrieval = new ProductImageAttribute();
// 处理 imgName // 处理 imgName
if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) { // if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) {
attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0)); // attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0));
} // }
// 处理 length // 处理 length
if (CollectionUtil.isNotEmpty(attrDictJSON.getLength()) && attrDictJSON.getLength().get(0) != null) { if (CollectionUtil.isNotEmpty(attrDictJSON.getLength()) && attrDictJSON.getLength().get(0) != null) {
attributeRetrieval.setLength(attrDictJSON.getLength().get(0)); attributeRetrieval.setLength(attrDictJSON.getLength().get(0));