Bläddra i källkod

完善匹配代码

hulh 7 år sedan
förälder
incheckning
c6bd51ead9

+ 7 - 0
src/main/java/com/uas/ps/product/repository/ProductMatchResultDao.java

@@ -3,6 +3,7 @@ package com.uas.ps.product.repository;
 import com.uas.ps.entity.ProductMatchResult;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.stereotype.Repository;
 
 /**
@@ -12,4 +13,10 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface ProductMatchResultDao extends JpaSpecificationExecutor<ProductMatchResult>, JpaRepository<ProductMatchResult, Long> {
 
+    /**
+     * 匹配时先删除原匹配结果
+     * @param enUU
+     */
+    @Modifying
+    void deleteByEnUU(Long enUU);
 }

+ 7 - 4
src/main/java/com/uas/ps/product/service/impl/ProductServiceImpl.java

@@ -118,6 +118,8 @@ public class ProductServiceImpl implements ProductService {
         ModelMap map = new ModelMap();
         int success = 0;
         List<Product> nonStandardList = productDao.findByEnUUAndStandard(enUU, ProductConstant.NON_STANDARD);
+        productMatchResultDao.deleteByEnUU(enUU);
+        // TODO 可以考虑用List保存,然后再批量保存
         for (Product product : nonStandardList) {
             success += matchOne(product, enUU);
         }
@@ -170,24 +172,25 @@ public class ProductServiceImpl implements ProductService {
         String codeUrl = "http://192.168.253.129:8080/platform-b2c/api/product/component/byCode?code=" + product.getpCmpCode();
         String componentJson = restTemplate.getForEntity(codeUrl, String.class).getBody();
         List<ComponentInfo> componentInfoList = JSON.parseArray(componentJson, ComponentInfo.class);
-        Set<ProductMatchResult> productMatchResults = convertComponentToResult(componentInfoList, enUU);
+        Set<ProductMatchResult> productMatchResults = convertComponentToResult(product.getId(), componentInfoList, enUU);
         if (CollectionUtils.isNotEmpty(productMatchResults)) {
-            product.setMatchResults(productMatchResults);
-            productDao.save(product);
+            productMatchResultDao.save(productMatchResults);
         }
         return 0;
     }
 
     /**
      * 器件转化为ProductMatchResult list
+     * @param id    产品id
      * @param componentInfoList 器件list
      * @param enUU  企业uu
      * @return
      */
-    private Set<ProductMatchResult> convertComponentToResult(List<ComponentInfo> componentInfoList, Long enUU) {
+    private Set<ProductMatchResult> convertComponentToResult(Long id, List<ComponentInfo> componentInfoList, Long enUU) {
         Set<ProductMatchResult> resultSet = new HashSet<>(componentInfoList.size());
         for (ComponentInfo info : componentInfoList) {
             ProductMatchResult result = convertOneInfoToResult(info, enUU);
+            result.setProductId(id);
             resultSet.add(result);
         }
         return resultSet;