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 org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@Slf4j
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class AiDaApplication {
public static void main(String[] args) {

View File

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

View File

@@ -261,7 +261,7 @@ public class SavedCollectionController {
@ApiOperation(value = "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));
}

View File

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

View File

@@ -9,4 +9,6 @@ public class ProductImageInitializeDTO {
private List<Long> libraryIds;
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 {
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);
double getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO);
InitializeProgressVO getInitializeProgress(ProductImageInitializeDTO productImageInitializeDTO);
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -45,6 +46,8 @@ public class ProductImageServiceImpl implements ProductImageService {
@Async
@Override
public void asyncInitialize(Long brandId) {
System.out.println(">>> [asyncInitialize] 当前线程:" + Thread.currentThread().getName());
String progressKey = String.valueOf(brandId);
ProgressDTO progressDTO = new ProgressDTO(0, 0, false);
redisUtil.setTaskProgressDTO(progressKey, progressDTO);
@@ -62,15 +65,20 @@ public class ProductImageServiceImpl implements ProductImageService {
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);
QueryWrapper<ProductImageAttribute> qw = new QueryWrapper<>();
qw.lambda().eq(ProductImageAttribute::getLibraryId, libraryId);
List<ProductImageAttribute> productImageAttributes = productImageAttributeMapper.selectList(qw);
if (!CollectionUtil.isNotEmpty(productImageAttributes)) {
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);
}
}
// 更新当前进度
@@ -80,26 +88,26 @@ public class ProductImageServiceImpl implements ProductImageService {
}
} catch (Exception e) {
System.out.println(e.getMessage());
// 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());
}
// 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));
}
// 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));

View File

@@ -1163,18 +1163,33 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
@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) {
ProductImageAttribute attributeRetrieval = new ProductImageAttribute();
// 处理 imgName
if (CollectionUtil.isNotEmpty(attrDictJSON.getImgName()) && attrDictJSON.getImgName().get(0) != null) {
attributeRetrieval.setImgName(attrDictJSON.getImgName().get(0));
}
// 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));