Browse Source

完成商城一键匹配逻辑

hulh 8 years ago
parent
commit
904724ea28

+ 13 - 0
src/main/java/com/uas/ps/product/controller/ProductController.java

@@ -118,6 +118,19 @@ public class ProductController {
         return productService.match(enUU, userUU, type);
     }
 
+    /**
+     * 商城一键匹配
+     *
+     * @param enUU 企业UU
+     * @param userUU 用户UU
+     * @return
+     */
+    @RequestMapping(value = "/match/nonProduct", method = RequestMethod.POST)
+    @ResponseBody
+    public ModelMap matchB2cAll(@RequestParam Long enUU, @RequestParam Long userUU) {
+        return productService.matchB2cAll(enUU, userUU);
+    }
+
     /**
      * 匹配选中的列表
      * @param idList

+ 10 - 0
src/main/java/com/uas/ps/product/service/ProductService.java

@@ -50,6 +50,16 @@ public interface ProductService {
      */
     ModelMap match(Long enUU, Long userUU, String type);
 
+    /**
+     * 通过 enUU 查询非标准器件进行匹配
+     *
+     * @param enUU 企业UU
+     * @param userUU 用户UU
+     * @return
+     */
+    ModelMap matchB2cAll(Long enUU, Long userUU);
+
+
     /**
      * 分配个人物料
      *

+ 72 - 19
src/main/java/com/uas/ps/product/service/impl/ProductServiceImpl.java

@@ -1,12 +1,12 @@
 package com.uas.ps.product.service.impl;
 
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.JSON;
 import com.uas.ps.brand.entity.BrandInfo;
 import com.uas.ps.component.entity.ComponentInfo;
 import com.uas.ps.entity.Product;
+import com.uas.ps.entity.ProductMatchResult;
 import com.uas.ps.entity.ProductUsers;
 import com.uas.ps.entity.Status;
-import com.uas.ps.kind.entity.KindInfo;
 import com.uas.ps.product.ProductConstant;
 import com.uas.ps.product.entity.Constant;
 import com.uas.ps.product.entity.Prod;
@@ -16,6 +16,7 @@ import com.uas.ps.product.repository.ProductMatchResultDao;
 import com.uas.ps.product.repository.ProductStoreStatusDao;
 import com.uas.ps.product.repository.ProductUsersDao;
 import com.uas.ps.product.service.ProductService;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -23,13 +24,10 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.ModelMap;
-import org.springframework.util.CollectionUtils;
 import org.springframework.web.client.RestTemplate;
 
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * @author sunyj
@@ -93,6 +91,12 @@ public class ProductServiceImpl implements ProductService {
 
     @Override
     public ModelMap match(Long enUU, Long userUU, String matchtype) {
+        ModelMap map = new ModelMap();
+        return map;
+    }
+
+    @Override
+    public ModelMap matchB2cAll(Long enUU, Long userUU) {
         ModelMap map = new ModelMap();
         int success = 0;
         List<Product> nonStandardList = productDao.findByEnUUAndStandard(enUU, ProductConstant.NON_STANDARD);
@@ -126,38 +130,87 @@ public class ProductServiceImpl implements ProductService {
      * @param   enUU
      */
     private int matchOne(Product product, Long enUU) {
-        BrandInfo brand;
-        ComponentInfo component;
-        List<Product> standardList;
         // 获取品牌
         String brandUrl = "http://10.1.51.89:8080/platform-b2c/api/product/brand/byName/brand?name=" + product.getpBrandEn();
-        brand = restTemplate.getForEntity(brandUrl, BrandInfo.class).getBody();
+        BrandInfo brand = restTemplate.getForEntity(brandUrl, BrandInfo.class).getBody();
         if (brand.getUuid() != null) {
             String componentUrl = "http://10.1.51.89:8080/platform-b2c/api/product/component/codeAndBrand/info?brandId="
                     + brand.getId() + "&cmpCode=" + product.getpCmpCode();
-            component = restTemplate.getForEntity(componentUrl, ComponentInfo.class).getBody();
+            ComponentInfo component = restTemplate.getForEntity(componentUrl, ComponentInfo.class).getBody();
             if (component != null) {
                 // 更新物料信息
-                updateProductByComponent(product, brand, component);
-                standardList = productDao.findProductByPcmpcodeAndPbrandenAndEnUU(product.getpCmpCode(), product.getpBrand(), enUU);
-                if (!CollectionUtils.isEmpty(standardList)) {
+                updateProductByComponent(product, component);
+                List<Product> standardList = productDao.findProductByPcmpcodeAndPbrandenAndEnUU(product.getpCmpCode(), product.getpBrand(), enUU);
+                if (CollectionUtils.isNotEmpty(standardList)) {
                     // TODO 已有标准产品,该如何处理
                 }
                 productDao.save(product);
                 return 1;
             }
         }
+        // 根据型号匹配
+        String codeUrl = "http://10.1.51.89: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);
+        if (CollectionUtils.isNotEmpty(productMatchResults)) {
+            product.setMatchResults(productMatchResults);
+            productDao.save(product);
+        }
         return 0;
     }
 
-    private void updateProductByComponent(Product product, BrandInfo brandInfo, ComponentInfo component) {
+    /**
+     * 器件转化为ProductMatchResult list
+     * @param componentInfoList 器件list
+     * @param enUU  企业uu
+     * @return
+     */
+    private Set<ProductMatchResult> convertComponentToResult(List<ComponentInfo> componentInfoList, Long enUU) {
+        Set<ProductMatchResult> resultSet = new HashSet<>(componentInfoList.size());
+        for (ComponentInfo info : componentInfoList) {
+            ProductMatchResult result = convertOneInfoToResult(info, enUU);
+            resultSet.add(result);
+        }
+        return resultSet;
+    }
+
+    /**
+     * 将单个器件转化为ProductMatchResult实体
+     * @param info  单个器件
+     * @param enUU  企业uu
+     * @return
+     */
+    private ProductMatchResult convertOneInfoToResult(ComponentInfo info, Long enUU) {
+        ProductMatchResult result = new ProductMatchResult();
+        result.setBrandCn(info.getBrand().getNameCn());
+        result.setBrandEn(info.getBrand().getNameEn());
+        result.setBrandId(info.getBrand().getId());
+        result.setpBrandUuid(info.getBrand().getUuid());
+        result.setCmpCode(info.getCode());
+        result.setCmpId(info.getId());
+        result.setKindId(info.getKind().getId());
+        result.setKindCn(info.getKind().getNameCn());
+        result.setKindEn(info.getKind().getNameEn());
+        result.setUuid(info.getUuid());
+        result.setCmpImg(info.getImg());
+        result.setEnUU(enUU);
+        return result;
+    }
+
+    /**
+     * 匹配成标准后更新product信息
+     * @param product   物料
+     * @param component 器件
+     */
+    private void updateProductByComponent(Product product, ComponentInfo component) {
         product.setCmpUuid(component.getUuid());
         product.setCmpImg(component.getImg());
         product.setpCmpCode(component.getCode());
-        product.setpBrandId(brandInfo.getId());
-        product.setpBrand(brandInfo.getNameCn());
-        product.setpBrandEn(brandInfo.getNameEn());
-        product.setpBrandUuid(brandInfo.getUuid());
+        product.setpBrandId(component.getBrand().getId());
+        product.setpBrand(component.getBrand().getNameCn());
+        product.setpBrandEn(component.getBrand().getNameEn());
+        product.setpBrandUuid(component.getBrand().getUuid());
         product.setKind(component.getKind().getNameCn());
         product.setKindEn(component.getKind().getNameEn());
         product.setKindId(component.getKindid());