wangxy 3 дней назад
Родитель
Сommit
ccce82d1d4

+ 71 - 1
fs-service/src/main/java/com/fs/his/service/impl/FsStoreProductServiceImpl.java

@@ -51,6 +51,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 import com.fs.his.mapper.FsStoreProductMapper;
+import com.fs.his.mapper.FsStoreMapper;
 import com.fs.his.service.IFsStoreProductService;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -68,6 +69,9 @@ public class FsStoreProductServiceImpl implements IFsStoreProductService {
     @Autowired
     private FsStoreProductMapper fsStoreProductMapper;
 
+    @Autowired
+    private FsStoreMapper fsStoreMapper;
+
     @Autowired
     private FsStoreProductAttrValueMapper fsStoreProductAttrValueMapper;
 
@@ -985,7 +989,7 @@ public class FsStoreProductServiceImpl implements IFsStoreProductService {
         String syncedImageUrl = normalizeErpImageUrl(erpProduct.getPic());
 
         FsStoreProduct newProduct = new FsStoreProduct();
-        newProduct.setStoreId(20191853L);
+        newProduct.setStoreId(getStoreIdByErpBrand(erpProduct.getBrand()));
         newProduct.setBarCode(erpProduct.getSkuId());
         newProduct.setProductName(erpProduct.getName());
         newProduct.setPrice(erpProduct.getSalePrice());
@@ -1034,12 +1038,14 @@ public class FsStoreProductServiceImpl implements IFsStoreProductService {
         newAttr.setDoctorBrokerage(BigDecimal.ZERO);
         newAttr.setGiveIntegral(0);
         fsStoreProductAttrValueMapper.insertFsStoreProductAttrValue(newAttr);
+        syncProductAttrDefinition(newProduct.getProductId());
         return 1;
     }
 
     private void updateProductByErpData(FsStoreProduct product, ProductResponseDTO.ProductInfo erpProduct) {
         FsStoreProduct updateProduct = new FsStoreProduct();
         updateProduct.setProductId(product.getProductId());
+        updateProduct.setStoreId(getStoreIdByErpBrand(erpProduct.getBrand()));
         updateProduct.setProductName(erpProduct.getName());
         updateProduct.setPrice(erpProduct.getSalePrice());
         updateProduct.setCostPrice(erpProduct.getCostPrice());
@@ -1063,6 +1069,70 @@ public class FsStoreProductServiceImpl implements IFsStoreProductService {
         updateAttr.setErpImageUrl(erpProduct.getPic());
         updateAttr.setWeight(erpProduct.getWeight());
         fsStoreProductAttrValueMapper.updateFsStoreProductAttrValue(updateAttr);
+        syncProductAttrDefinition(product.getProductId());
+    }
+
+    private Long getStoreIdByErpBrand(String brand) {
+        if (StringUtils.isBlank(brand)) {
+            return null;
+        }
+        FsStore query = new FsStore();
+        query.setStoreName(brand.trim());
+        List<FsStore> stores = fsStoreMapper.selectFsStoreList(query);
+        if (stores == null || stores.isEmpty()) {
+            return null;
+        }
+        for (FsStore store : stores) {
+            if (StringUtils.equals(StringUtils.trim(store.getStoreName()), brand.trim())) {
+                return store.getStoreId();
+            }
+        }
+        return stores.get(0).getStoreId();
+    }
+
+    private void syncProductAttrDefinition(Long productId) {
+        if (productId == null) {
+            return;
+        }
+        FsStoreProductAttrValue attrValueQuery = new FsStoreProductAttrValue();
+        attrValueQuery.setProductId(productId);
+        List<FsStoreProductAttrValue> attrValues = fsStoreProductAttrValueMapper.selectFsStoreProductAttrValueList(attrValueQuery);
+        if (attrValues == null || attrValues.isEmpty()) {
+            return;
+        }
+        String attrValueText = attrValues.stream()
+                .map(FsStoreProductAttrValue::getSku)
+                .filter(StringUtils::isNotBlank)
+                .map(String::trim)
+                .distinct()
+                .collect(Collectors.joining(","));
+        if (StringUtils.isBlank(attrValueText)) {
+            return;
+        }
+        FsStoreProductAttr attrQuery = new FsStoreProductAttr();
+        attrQuery.setProductId(productId);
+        List<FsStoreProductAttr> attrs = fsStoreProductAttrMapper.selectFsStoreProductAttrList(attrQuery);
+        FsStoreProductAttr specAttr = null;
+        if (attrs != null) {
+            for (FsStoreProductAttr attr : attrs) {
+                if (StringUtils.equals(attr.getAttrName(), "规格")) {
+                    specAttr = attr;
+                    break;
+                }
+            }
+        }
+        if (specAttr == null) {
+            FsStoreProductAttr newAttr = new FsStoreProductAttr();
+            newAttr.setProductId(productId);
+            newAttr.setAttrName("规格");
+            newAttr.setAttrValues(attrValueText);
+            fsStoreProductAttrMapper.insertFsStoreProductAttr(newAttr);
+            return;
+        }
+        if (!StringUtils.equals(specAttr.getAttrValues(), attrValueText)) {
+            specAttr.setAttrValues(attrValueText);
+            fsStoreProductAttrMapper.updateFsStoreProductAttr(specAttr);
+        }
     }
 
     private LocalDateTime parseDateTime(String time) {