mirror of
https://gitea.gofwd.group/Forward_Group/ballistic-builder-spring.git
synced 2025-12-06 02:56:44 -05:00
small changes. still working
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package group.goforward.ballistic.imports;
|
package group.goforward.ballistic.imports;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import group.goforward.ballistic.model.Brand;
|
import group.goforward.ballistic.model.Brand;
|
||||||
import group.goforward.ballistic.model.Merchant;
|
import group.goforward.ballistic.model.Merchant;
|
||||||
@@ -41,7 +41,7 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
|
|
||||||
// TODO: replace this with real feed parsing:
|
// TODO: replace this with real feed parsing:
|
||||||
// List<MerchantFeedRow> rows = feedClient.fetch(merchant);
|
// List<MerchantFeedRow> rows = feedClient.fetch(merchant);
|
||||||
// rows.forEach(row -> upsertProduct(merchant, row));
|
// rows.forEach(row -> upsertProduct(merchant, brand, row));
|
||||||
MerchantFeedRow row = new MerchantFeedRow(
|
MerchantFeedRow row = new MerchantFeedRow(
|
||||||
"TEST-SKU-001",
|
"TEST-SKU-001",
|
||||||
"APPG100002",
|
"APPG100002",
|
||||||
@@ -69,9 +69,9 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
Product p = createProduct(brand, row);
|
Product p = upsertProduct(merchant, brand, row);
|
||||||
|
|
||||||
System.out.println("IMPORT >>> created product id=" + p.getId()
|
System.out.println("IMPORT >>> upserted product id=" + p.getId()
|
||||||
+ ", name=" + p.getName()
|
+ ", name=" + p.getName()
|
||||||
+ ", slug=" + p.getSlug()
|
+ ", slug=" + p.getSlug()
|
||||||
+ ", platform=" + p.getPlatform()
|
+ ", platform=" + p.getPlatform()
|
||||||
@@ -79,13 +79,54 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
+ ", merchant=" + merchant.getName());
|
+ ", merchant=" + merchant.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Product createProduct(Brand brand, MerchantFeedRow row) {
|
/**
|
||||||
System.out.println("IMPORT >>> createProduct brand=" + brand.getName()
|
* Upsert logic:
|
||||||
|
* - Try Brand+MPN, then Brand+UPC (for now using sku as a stand-in)
|
||||||
|
* - If found, update fields but keep existing slug
|
||||||
|
* - If not found, create a new Product and generate a unique slug
|
||||||
|
*/
|
||||||
|
private Product upsertProduct(Merchant merchant, Brand brand, MerchantFeedRow row) {
|
||||||
|
System.out.println("IMPORT >>> upsertProduct brand=" + brand.getName()
|
||||||
+ ", sku=" + row.sku()
|
+ ", sku=" + row.sku()
|
||||||
+ ", productName=" + row.productName());
|
+ ", productName=" + row.productName());
|
||||||
|
|
||||||
Product p = new Product();
|
String mpn = trimOrNull(row.manufacturerId());
|
||||||
|
String upc = trimOrNull(row.sku()); // later: real UPC column
|
||||||
|
|
||||||
|
java.util.List<Product> candidates = java.util.Collections.emptyList();
|
||||||
|
|
||||||
|
if (mpn != null) {
|
||||||
|
candidates = productRepository.findAllByBrandAndMpn(brand, mpn);
|
||||||
|
}
|
||||||
|
if ((candidates == null || candidates.isEmpty()) && upc != null) {
|
||||||
|
candidates = productRepository.findAllByBrandAndUpc(brand, upc);
|
||||||
|
}
|
||||||
|
|
||||||
|
Product p;
|
||||||
|
boolean isNew = (candidates == null || candidates.isEmpty());
|
||||||
|
|
||||||
|
if (isNew) {
|
||||||
|
p = new Product();
|
||||||
p.setBrand(brand);
|
p.setBrand(brand);
|
||||||
|
} else {
|
||||||
|
if (candidates.size() > 1) {
|
||||||
|
System.out.println("IMPORT !!! WARNING: multiple existing products found for brand="
|
||||||
|
+ brand.getName() + ", mpn=" + mpn + ", upc=" + upc
|
||||||
|
+ ". Using the first match (id=" + candidates.get(0).getId() + ")");
|
||||||
|
}
|
||||||
|
p = candidates.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateProductFromRow(p, row, isNew);
|
||||||
|
|
||||||
|
return productRepository.save(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared mapping logic from feed row -> Product entity.
|
||||||
|
* If isNew = true, we generate a slug. Otherwise we leave the slug alone.
|
||||||
|
*/
|
||||||
|
private void updateProductFromRow(Product p, MerchantFeedRow row, boolean isNew) {
|
||||||
|
|
||||||
// ---------- NAME ----------
|
// ---------- NAME ----------
|
||||||
String name = coalesce(
|
String name = coalesce(
|
||||||
@@ -100,6 +141,7 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
p.setName(name);
|
p.setName(name);
|
||||||
|
|
||||||
// ---------- SLUG ----------
|
// ---------- SLUG ----------
|
||||||
|
if (isNew || p.getSlug() == null || p.getSlug().isBlank()) {
|
||||||
String baseForSlug = coalesce(
|
String baseForSlug = coalesce(
|
||||||
trimOrNull(name),
|
trimOrNull(name),
|
||||||
trimOrNull(row.sku())
|
trimOrNull(row.sku())
|
||||||
@@ -119,6 +161,7 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
// Ensure slug is unique by appending a numeric suffix if needed
|
// Ensure slug is unique by appending a numeric suffix if needed
|
||||||
String uniqueSlug = generateUniqueSlug(slug);
|
String uniqueSlug = generateUniqueSlug(slug);
|
||||||
p.setSlug(uniqueSlug);
|
p.setSlug(uniqueSlug);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- DESCRIPTIONS ----------
|
// ---------- DESCRIPTIONS ----------
|
||||||
p.setShortDescription(trimOrNull(row.shortDescription()));
|
p.setShortDescription(trimOrNull(row.shortDescription()));
|
||||||
@@ -141,24 +184,19 @@ public class MerchantFeedImportServiceImpl implements MerchantFeedImportService
|
|||||||
p.setMpn(mpn);
|
p.setMpn(mpn);
|
||||||
|
|
||||||
// Feed doesn’t give us UPC in the header you showed.
|
// Feed doesn’t give us UPC in the header you showed.
|
||||||
// We’ll leave UPC null for now.
|
// We’ll leave UPC null for now (or map later).
|
||||||
p.setUpc(null);
|
p.setUpc(null);
|
||||||
|
|
||||||
// ---------- PLATFORM ----------
|
// ---------- PLATFORM ----------
|
||||||
// For now, hard-code to AR-15 to satisfy not-null constraint.
|
|
||||||
// Later we can infer from row.category()/row.department().
|
|
||||||
String platform = inferPlatform(row);
|
String platform = inferPlatform(row);
|
||||||
p.setPlatform(platform != null ? platform : "AR-15");
|
p.setPlatform(platform != null ? platform : "AR-15");
|
||||||
|
|
||||||
// ---------- PART ROLE ----------
|
// ---------- PART ROLE ----------
|
||||||
// We can do a tiny heuristic off category/subcategory.
|
|
||||||
String partRole = inferPartRole(row);
|
String partRole = inferPartRole(row);
|
||||||
if (partRole == null || partRole.isBlank()) {
|
if (partRole == null || partRole.isBlank()) {
|
||||||
partRole = "unknown";
|
partRole = "unknown";
|
||||||
}
|
}
|
||||||
p.setPartRole(partRole);
|
p.setPartRole(partRole);
|
||||||
|
|
||||||
return productRepository.save(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Helpers ----------------------------------------------------------
|
// --- Helpers ----------------------------------------------------------
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
||||||
|
|
||||||
Optional<Product> findByUuid(UUID uuid);
|
Optional<Product> findByUuid(UUID uuid);
|
||||||
|
|
||||||
Optional<Product> findByBrandAndMpn(Brand brand, String mpn);
|
|
||||||
|
|
||||||
Optional<Product> findByBrandAndUpc(Brand brand, String upc);
|
|
||||||
|
|
||||||
boolean existsBySlug(String slug);
|
boolean existsBySlug(String slug);
|
||||||
|
|
||||||
|
List<Product> findAllByBrandAndMpn(Brand brand, String mpn);
|
||||||
|
|
||||||
|
List<Product> findAllByBrandAndUpc(Brand brand, String upc);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user