|
@@ -4,11 +4,14 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.yami.shop.bean.dto.hb.HBBaseReq;
|
|
|
-import com.yami.shop.bean.model.hb.GoodsSku;
|
|
|
-import com.yami.shop.bean.model.hb.GoodsSpu;
|
|
|
+import com.yami.shop.bean.model.Product;
|
|
|
+import com.yami.shop.bean.model.Sku;
|
|
|
import com.yami.shop.common.util.hb.HBR;
|
|
|
+import com.yami.shop.dao.ProductMapper;
|
|
|
+import com.yami.shop.dao.SkuMapper;
|
|
|
import com.yami.shop.service.hb.IGoodsService;
|
|
|
import com.yami.shop.utils.HBSignUtil;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -37,8 +40,8 @@ import static com.yami.shop.common.util.HttpUtil.post;
|
|
|
@Transactional(readOnly = true)
|
|
|
public class GoodsService implements IGoodsService {
|
|
|
private final HBSignUtil hbSignUtil;
|
|
|
- private final GoodsSpuDao goodsSpuDao;
|
|
|
- private final GoodsSkuDao goodsSkuDao;
|
|
|
+ private final ProductMapper productMapper;
|
|
|
+ private final SkuMapper skuMapper;
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
@@ -48,8 +51,8 @@ public class GoodsService implements IGoodsService {
|
|
|
log.info("商品信息,body:{}", bodyStr);
|
|
|
JSONObject bodyJson = JSON.parseObject(bodyStr);
|
|
|
JSONArray productList = bodyJson.getJSONArray("productList");
|
|
|
- List<GoodsSpu> spuList = new ArrayList<>();
|
|
|
- List<GoodsSku> skuList = new ArrayList<>();
|
|
|
+ List<Product> spuList = new ArrayList<>();
|
|
|
+ List<Sku> skuList = new ArrayList<>();
|
|
|
|
|
|
for (Object product : productList) {
|
|
|
JSONObject jsonObject = JSON.parseObject(product.toString());
|
|
@@ -67,27 +70,27 @@ public class GoodsService implements IGoodsService {
|
|
|
for (int i = 0; i < productHostInfoDTOS.size(); i++) {
|
|
|
JSONObject productInfo = productHostInfoDTOS.getJSONObject(i);
|
|
|
|
|
|
- // 创建并填充GoodsSpu对象
|
|
|
- GoodsSpu goodsSpu = createGoodsSpuFromProductInfo(productInfo);
|
|
|
- // 创建并填充GoodsSku对象
|
|
|
- GoodsSku goodsSku = createGoodsSkuFromProductInfo(productInfo);
|
|
|
- goodsSku.setSpuId(goodsSpu.getSpuId()); // 设置关联关系
|
|
|
+ // 创建并填充Product对象
|
|
|
+ Product Product = createProductFromProductInfo(productInfo);
|
|
|
+ // 创建并填充Sku对象
|
|
|
+ Sku Sku = createSkuFromProductInfo(productInfo);
|
|
|
+ Sku.setProdId(Product.getProdId()); // 设置关联关系
|
|
|
|
|
|
|
|
|
//添加到数据库
|
|
|
- goodsSpuDao.insert(goodsSpu);
|
|
|
- goodsSkuDao.insert(goodsSku);
|
|
|
+ productMapper.insert(Product);
|
|
|
+ skuMapper.insert(Sku);
|
|
|
|
|
|
- spuList.add(goodsSpu);
|
|
|
- skuList.add(goodsSku);
|
|
|
+ spuList.add(Product);
|
|
|
+ skuList.add(Sku);
|
|
|
|
|
|
- log.info("SPU创建成功: spuId={}, spuName={}", goodsSpu.getSpuId(), goodsSpu.getSpuName());
|
|
|
- log.info("SKU创建成功: skuId={}, skuName={}", goodsSku.getSkuId(), goodsSku.getSkuName());
|
|
|
+ log.info("SPU创建成功: spuId={}, spuName={}", Product.getProdId(), Product.getProdName());
|
|
|
+ log.info("SKU创建成功: skuId={}, skuName={}", Sku.getSkuId(), Sku.getSkuName());
|
|
|
}
|
|
|
}
|
|
|
// 这里可以保存到数据库
|
|
|
- // goodsSpuService.saveBatch(spuList);
|
|
|
- // goodsSkuService.saveBatch(skuList);
|
|
|
+ // ProductService.saveBatch(spuList);
|
|
|
+ // SkuService.saveBatch(skuList);
|
|
|
|
|
|
return HBR.success("商品信息处理成功,共处理 " + spuList.size() + " 个SPU和SKU");
|
|
|
|
|
@@ -214,95 +217,88 @@ public class GoodsService implements IGoodsService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<GoodsSpu> selectGoodsPage(Integer pageNum, Integer pageSize,GoodsSpu goods) {
|
|
|
+ public IPage<Product> selectGoodsPage(Integer pageNum, Integer pageSize,Product product) {
|
|
|
// 1. 构建分页对象
|
|
|
- Page<GoodsSpu> page = new Page<>(pageNum, pageSize);
|
|
|
+ Page<Product> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
|
// 2. 构建查询条件
|
|
|
- LambdaQueryWrapper<GoodsSpu> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
-
|
|
|
- if (goods != null) {
|
|
|
- queryWrapper.like(StringUtils.isNotBlank(goods.getSpuName()),
|
|
|
- GoodsSpu::getSpuName, goods.getSpuName());
|
|
|
- queryWrapper.eq(goods.getCategoryId() != null,
|
|
|
- GoodsSpu::getCategoryId, goods.getCategoryId());
|
|
|
- queryWrapper.eq(goods.getBrandId() != null,
|
|
|
- GoodsSpu::getBrandId, goods.getBrandId());
|
|
|
- queryWrapper.eq(goods.getStatus() != null,
|
|
|
- GoodsSpu::getStatus, goods.getStatus());
|
|
|
- queryWrapper.eq(goods.getMerchantId() != null,
|
|
|
- GoodsSpu::getMerchantId, goods.getMerchantId());
|
|
|
+ LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+
|
|
|
+ if (product != null) {
|
|
|
+ queryWrapper.like(StringUtils.isNotBlank(product.getProdName()),
|
|
|
+ Product::getProdName, product.getProdName());
|
|
|
+ queryWrapper.eq(product.getCategoryId() != null,
|
|
|
+ Product::getCategoryId, product.getCategoryId());
|
|
|
+ queryWrapper.eq(product.getBrandId() != null,
|
|
|
+ Product::getBrandId, product.getBrandId());
|
|
|
+ queryWrapper.eq(product.getStatus() != null,
|
|
|
+ Product::getStatus, product.getStatus());
|
|
|
}
|
|
|
|
|
|
// 3. 添加排序
|
|
|
- queryWrapper.orderByDesc(GoodsSpu::getCreateTime);
|
|
|
+ queryWrapper.orderByDesc(Product::getCreateTime);
|
|
|
|
|
|
// 4. 执行分页查询
|
|
|
- Page<GoodsSpu> goodsSpuPage = goodsSpuDao.selectPage(page, queryWrapper);
|
|
|
- List<GoodsSpu> records = goodsSpuPage.getRecords();
|
|
|
- for (GoodsSpu record : records) {
|
|
|
+ IPage<Product> ProductPage = productMapper.selectPage(page, queryWrapper);
|
|
|
+ List<Product> records = ProductPage.getRecords();
|
|
|
+ for (Product record : records) {
|
|
|
|
|
|
- LambdaQueryWrapper<GoodsSku> skuQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- skuQueryWrapper.like(GoodsSku::getSpuId, record.getSpuId());
|
|
|
+ LambdaQueryWrapper<Sku> skuQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ skuQueryWrapper.like(Sku::getProdId, record.getProdId());
|
|
|
|
|
|
// 3. 添加排序
|
|
|
- skuQueryWrapper.orderByDesc(GoodsSku::getCreateTime);
|
|
|
- record.setGoodsSkuList(goodsSkuDao.selectList(skuQueryWrapper));
|
|
|
+ skuQueryWrapper.orderByDesc(Sku::getRecTime);
|
|
|
+ record.setSkuList(skuMapper.selectList(skuQueryWrapper));
|
|
|
}
|
|
|
- return goodsSpuPage;
|
|
|
+ return ProductPage;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 从productInfo创建GoodsSpu对象
|
|
|
+ * 从productInfo创建Product对象
|
|
|
*/
|
|
|
- private GoodsSpu createGoodsSpuFromProductInfo(JSONObject productInfo) {
|
|
|
- GoodsSpu goodsSpu = new GoodsSpu();
|
|
|
+ private Product createProductFromProductInfo(JSONObject productInfo) {
|
|
|
+ Product Product = new Product();
|
|
|
|
|
|
// 设置默认值
|
|
|
- goodsSpu.setDefaultValues();
|
|
|
// 直接映射字段
|
|
|
- goodsSpu.setSpuId(productInfo.getLong("spuId"));
|
|
|
- goodsSpu.setSpuName(productInfo.getString("spuName"));
|
|
|
- goodsSpu.setDescription(productInfo.getString("sellPoint"));
|
|
|
+ Product.setProdId(productInfo.getLong("spuId"));
|
|
|
+ Product.setProdName(productInfo.getString("spuName"));
|
|
|
|
|
|
// 需要转换或默认值的字段
|
|
|
- goodsSpu.setMerchantId(1L); // 默认商家ID,根据实际情况修改
|
|
|
- goodsSpu.setCategoryId(convertCategoryCodeToId(productInfo.getString("frontCategoryCode"))); // 分类转换
|
|
|
- goodsSpu.setBrandId(convertBrandToId(productInfo.getString("brandName"))); // 品牌转换
|
|
|
+ Product.setCategoryId(convertCategoryCodeToId(productInfo.getString("frontCategoryCode"))); // 分类转换
|
|
|
+ Product.setBrandId(convertBrandToId(productInfo.getString("brandName"))); // 品牌转换
|
|
|
|
|
|
// 其他可能需要处理的字段
|
|
|
if (productInfo.containsKey("desc")) {
|
|
|
-// goodsSpu.setDesc(productInfo.getString("desc"));
|
|
|
+// Product.setDesc(productInfo.getString("desc"));
|
|
|
}
|
|
|
|
|
|
- return goodsSpu;
|
|
|
+ return Product;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 从productInfo创建GoodsSku对象
|
|
|
+ * 从productInfo创建Sku对象
|
|
|
*/
|
|
|
- private GoodsSku createGoodsSkuFromProductInfo(JSONObject productInfo) {
|
|
|
- GoodsSku goodsSku = new GoodsSku();
|
|
|
- // 设置默认值
|
|
|
- goodsSku.setDefaultValues();
|
|
|
+ private Sku createSkuFromProductInfo(JSONObject productInfo) {
|
|
|
+ Sku Sku = new Sku();
|
|
|
// 直接映射字段
|
|
|
- goodsSku.setSkuId(productInfo.getLong("skuId"));
|
|
|
- goodsSku.setSpuId(productInfo.getLong("spuId"));
|
|
|
- goodsSku.setSkuCode(productInfo.getString("skuId")); // 使用海博skuId作为skuCode
|
|
|
- goodsSku.setSkuName(productInfo.getString("skuName"));
|
|
|
- goodsSku.setMainImage(productInfo.getString("images"));
|
|
|
+ Sku.setSkuId(productInfo.getLong("skuId"));
|
|
|
+ Sku.setProdId(productInfo.getLong("spuId"));
|
|
|
+ Sku.setSkuCode(productInfo.getString("skuId")); // 使用海博skuId作为skuCode
|
|
|
+ Sku.setSkuName(productInfo.getString("skuName"));
|
|
|
+ Sku.setPic(productInfo.getString("images"));//sku图片
|
|
|
|
|
|
// 数值类型字段处理
|
|
|
- goodsSku.setPrice(productInfo.getBigDecimal("basicPrice") != null ?
|
|
|
- productInfo.getBigDecimal("basicPrice") : BigDecimal.ZERO);
|
|
|
- goodsSku.setStock(productInfo.getInteger("initStock") != null ?
|
|
|
+ Sku.setPrice(productInfo.getDouble("basicPrice") != null ?
|
|
|
+ productInfo.getDouble("basicPrice") : Double.NaN);
|
|
|
+ Sku.setActualStocks(productInfo.getInteger("initStock") != null ?
|
|
|
productInfo.getInteger("initStock") : 0);
|
|
|
|
|
|
// 重量和体积转换(需要根据实际数据格式处理)
|
|
|
if (productInfo.containsKey("weight")) {
|
|
|
try {
|
|
|
String weightStr = productInfo.getString("weight");
|
|
|
- goodsSku.setWeight(new BigDecimal(weightStr));
|
|
|
+ Sku.setWeight(Double.NaN);
|
|
|
} catch (Exception e) {
|
|
|
log.warn("重量格式转换失败: {}", productInfo.getString("weight"));
|
|
|
}
|
|
@@ -311,23 +307,23 @@ public class GoodsService implements IGoodsService {
|
|
|
// 状态处理
|
|
|
Integer status = productInfo.getInteger("flag");
|
|
|
if (status != null && status == 0) {
|
|
|
- goodsSku.setStatus(0); // 停售
|
|
|
+ Sku.setStatus(0); // 停售
|
|
|
} else {
|
|
|
- goodsSku.setStatus(1); // 正常
|
|
|
+ Sku.setStatus(1); // 正常
|
|
|
}
|
|
|
|
|
|
// 其他字段映射
|
|
|
if (productInfo.containsKey("spec")) {
|
|
|
// spec字段可能包含规格信息,可以用于skuName或单独存储
|
|
|
- goodsSku.setSkuName(goodsSku.getSkuName() + " " + productInfo.getString("spec"));
|
|
|
+ Sku.setSkuName(Sku.getSkuName() + " " + productInfo.getString("spec"));
|
|
|
}
|
|
|
|
|
|
if (productInfo.containsKey("unit")) {
|
|
|
// 单位信息可以用于skuName
|
|
|
- goodsSku.setSkuName(goodsSku.getSkuName() + "(" + productInfo.getString("unit") + ")");
|
|
|
+ Sku.setSkuName(Sku.getSkuName() + "(" + productInfo.getString("unit") + ")");
|
|
|
}
|
|
|
|
|
|
- return goodsSku;
|
|
|
+ return Sku;
|
|
|
}
|
|
|
|
|
|
/**
|