|
@@ -0,0 +1,355 @@
|
|
|
+package com.yami.shop.service.hb.impl;
|
|
|
+
|
|
|
+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.extension.plugins.pagination.Page;
|
|
|
+import com.yami.shop.bean.model.hb.GoodsSku;
|
|
|
+import com.yami.shop.bean.model.hb.GoodsSpu;
|
|
|
+import com.yami.shop.common.util.hb.HBR;
|
|
|
+import com.yami.shop.common.util.hb.HBSignUtil;
|
|
|
+import com.yami.shop.service.hb.IGoodsService;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商品信息管理实现
|
|
|
+ *
|
|
|
+ * @author fubojin
|
|
|
+ * @version 1.0.0
|
|
|
+ * @since 2025-09-5
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Transactional(readOnly = true)
|
|
|
+public class GoodsService implements IGoodsService {
|
|
|
+ private final HBSignUtil hbSignUtil;
|
|
|
+ private final GoodsSpuDao goodsSpuDao;
|
|
|
+ private final GoodsSkuDao goodsSkuDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public HBR addHBGoods(JSONObject hbRequest) {
|
|
|
+ try {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品信息,body:{}", bodyStr);
|
|
|
+ JSONObject bodyJson = JSON.parseObject(bodyStr);
|
|
|
+ JSONArray productList = bodyJson.getJSONArray("productList");
|
|
|
+ List<GoodsSpu> spuList = new ArrayList<>();
|
|
|
+ List<GoodsSku> skuList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (Object product : productList) {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(product.toString());
|
|
|
+ String outSkuId = jsonObject.getString("outSkuId");
|
|
|
+ String goodsInfoStr = selectHBGoodsInfo(outSkuId);
|
|
|
+ JSONObject goodsInfoJson = JSON.parseObject(goodsInfoStr);
|
|
|
+
|
|
|
+ // 2. 逐层解析获取productHostInfoDTOS数组
|
|
|
+ JSONObject result = goodsInfoJson.getJSONObject("result");
|
|
|
+ JSONObject innerResult = result.getJSONObject("result");
|
|
|
+ JSONArray productHostInfoDTOS = innerResult.getJSONArray("productHostInfoDTOS");
|
|
|
+
|
|
|
+
|
|
|
+ // 3. 遍历获取每个商品信息
|
|
|
+ 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()); // 设置关联关系
|
|
|
+
|
|
|
+
|
|
|
+ //添加到数据库
|
|
|
+ goodsSpuDao.insert(goodsSpu);
|
|
|
+ goodsSkuDao.insert(goodsSku);
|
|
|
+
|
|
|
+ spuList.add(goodsSpu);
|
|
|
+ skuList.add(goodsSku);
|
|
|
+
|
|
|
+ log.info("SPU创建成功: spuId={}, spuName={}", goodsSpu.getSpuId(), goodsSpu.getSpuName());
|
|
|
+ log.info("SKU创建成功: skuId={}, skuName={}", goodsSku.getSkuId(), goodsSku.getSkuName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 这里可以保存到数据库
|
|
|
+ // goodsSpuService.saveBatch(spuList);
|
|
|
+ // goodsSkuService.saveBatch(skuList);
|
|
|
+
|
|
|
+ return HBR.success("商品信息处理成功,共处理 " + spuList.size() + " 个SPU和SKU");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理商品信息失败:{}", e.getMessage(), e);
|
|
|
+ return HBR.error("处理商品信息失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HBR updateGoods(JSONObject hbRequest) {
|
|
|
+ try {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品,body:{}", bodyStr);
|
|
|
+ JSONObject bodyJson = JSON.parseObject(bodyStr); // 使用Fastjson的解析方法
|
|
|
+ String outStationNo = bodyJson.getString("outStationNo");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("商品失败:{}", e.getMessage(), e);
|
|
|
+ return HBR.error("未知异常");
|
|
|
+
|
|
|
+ }
|
|
|
+ return HBR.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HBR updateGoodsStatus(JSONObject hbRequest) {
|
|
|
+ try {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品,body:{}", bodyStr);
|
|
|
+ JSONObject bodyJson = JSON.parseObject(bodyStr); // 使用Fastjson的解析方法
|
|
|
+ String outStationNo = bodyJson.getString("outStationNo");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("商品失败:{}", e.getMessage(), e);
|
|
|
+ return HBR.error("未知异常");
|
|
|
+
|
|
|
+ }
|
|
|
+ return HBR.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HBR updateGoodsPrice(JSONObject hbRequest) {
|
|
|
+ try {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品,body:{}", bodyStr);
|
|
|
+ JSONObject bodyJson = JSON.parseObject(bodyStr);
|
|
|
+ JSONArray requestList = bodyJson.getJSONArray("requestList");
|
|
|
+ for (Object request : requestList) {
|
|
|
+ JSONObject jsonObject = JSON.parseObject((String) request);
|
|
|
+ Long skuId = jsonObject.getLong("skuId");
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("商品失败:{}", e.getMessage(), e);
|
|
|
+ return HBR.error("未知异常");
|
|
|
+
|
|
|
+ }
|
|
|
+ return HBR.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HBR updateGoodsStock(JSONObject hbRequest) {
|
|
|
+ try {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品库存变动,body:{}", bodyStr);
|
|
|
+ JSONObject bodyJson = JSON.parseObject(bodyStr); // 使用Fastjson的解析方法
|
|
|
+ String outStationNo = bodyJson.getString("outStationNo");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("商品失败:{}", e.getMessage(), e);
|
|
|
+ return HBR.error("未知异常");
|
|
|
+
|
|
|
+ }
|
|
|
+ return HBR.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteGoods(JSONObject hbRequest) {
|
|
|
+ String bodyStr = hbRequest.getString("body");
|
|
|
+ log.info("商品删除,body:{}", bodyStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String selectHBGoodsInfo(String outSkuId) {
|
|
|
+
|
|
|
+ HBBaseReq<Object> build = HBBaseReq.create();
|
|
|
+ build.setAppId(hbSignUtil.getAppId());
|
|
|
+
|
|
|
+ JSONObject skuItem = new JSONObject();
|
|
|
+// skuItem.put("skuId", "1328633"); // 海博商品编码
|
|
|
+ skuItem.put("outSkuId", outSkuId); // 商家商品编码
|
|
|
+ // 2. 创建商品列表并添加SKU对象
|
|
|
+ List<JSONObject> productList = new ArrayList<>();
|
|
|
+ productList.add(skuItem);
|
|
|
+ // 3. 构建外层请求体
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("productList", productList);
|
|
|
+ // 4. 将整个JSON对象序列化为字符串并设置
|
|
|
+ build.setBody(JSON.toJSONString(requestBody));
|
|
|
+
|
|
|
+ build.setSign(hbSignUtil.signMd5(build));
|
|
|
+ log.info("post 商品查询请求参数:{}", JSON.toJSONString(build));
|
|
|
+ String post = post(hbSignUtil.getHBHost() + "/api/product/getSku", build);
|
|
|
+ log.info("post 商品查询结果:{}", post);
|
|
|
+ return post;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getSelfSkuStatus() {
|
|
|
+ HBBaseReq<Object> build = HBBaseReq.create();
|
|
|
+ build.setAppId(hbSignUtil.getAppId());
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ List<JSONObject> onlineProductDTO = new ArrayList();
|
|
|
+ jsonObject.put("skuId", "0");//海博商品编码
|
|
|
+ jsonObject.put("stationId", "0");//海博门店编码
|
|
|
+// jsonObject.put("outStationNo", "0");//商家门店编码
|
|
|
+// jsonObject.put("outSkuId", "0");//商家商品编码
|
|
|
+ onlineProductDTO.add(jsonObject);
|
|
|
+ build.setBody(JSON.toJSONString(onlineProductDTO));
|
|
|
+ build.setSign(hbSignUtil.signMd5(build));
|
|
|
+ String post = post(hbSignUtil.getHBHost() + "/api/productStation/getSelfSkuStatus", build);
|
|
|
+ log.info("post 商品查询请求参数:{}", JSON.toJSONString(build));
|
|
|
+ log.info("post 商品查询结果:{}", post);
|
|
|
+ return post;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<GoodsSpu> selectGoodsPage(Integer pageNum, Integer pageSize,GoodsSpu goods) {
|
|
|
+ // 1. 构建分页对象
|
|
|
+ Page<GoodsSpu> 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 添加排序
|
|
|
+ queryWrapper.orderByDesc(GoodsSpu::getCreateTime);
|
|
|
+
|
|
|
+ // 4. 执行分页查询
|
|
|
+ Page<GoodsSpu> goodsSpuPage = goodsSpuDao.selectPage(page, queryWrapper);
|
|
|
+ List<GoodsSpu> records = goodsSpuPage.getRecords();
|
|
|
+ for (GoodsSpu record : records) {
|
|
|
+
|
|
|
+ LambdaQueryWrapper<GoodsSku> skuQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ skuQueryWrapper.like(GoodsSku::getSpuId, record.getSpuId());
|
|
|
+
|
|
|
+ // 3. 添加排序
|
|
|
+ skuQueryWrapper.orderByDesc(GoodsSku::getCreateTime);
|
|
|
+ record.setGoodsSkuList(goodsSkuDao.selectList(skuQueryWrapper));
|
|
|
+ }
|
|
|
+ return goodsSpuPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从productInfo创建GoodsSpu对象
|
|
|
+ */
|
|
|
+ private GoodsSpu createGoodsSpuFromProductInfo(JSONObject productInfo) {
|
|
|
+ GoodsSpu goodsSpu = new GoodsSpu();
|
|
|
+
|
|
|
+ // 设置默认值
|
|
|
+ goodsSpu.setDefaultValues();
|
|
|
+ // 直接映射字段
|
|
|
+ goodsSpu.setSpuId(productInfo.getLong("spuId"));
|
|
|
+ goodsSpu.setSpuName(productInfo.getString("spuName"));
|
|
|
+ goodsSpu.setDescription(productInfo.getString("sellPoint"));
|
|
|
+
|
|
|
+ // 需要转换或默认值的字段
|
|
|
+ goodsSpu.setMerchantId(1L); // 默认商家ID,根据实际情况修改
|
|
|
+ goodsSpu.setCategoryId(convertCategoryCodeToId(productInfo.getString("frontCategoryCode"))); // 分类转换
|
|
|
+ goodsSpu.setBrandId(convertBrandToId(productInfo.getString("brandName"))); // 品牌转换
|
|
|
+
|
|
|
+ // 其他可能需要处理的字段
|
|
|
+ if (productInfo.containsKey("desc")) {
|
|
|
+// goodsSpu.setDesc(productInfo.getString("desc"));
|
|
|
+ }
|
|
|
+
|
|
|
+ return goodsSpu;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从productInfo创建GoodsSku对象
|
|
|
+ */
|
|
|
+ private GoodsSku createGoodsSkuFromProductInfo(JSONObject productInfo) {
|
|
|
+ GoodsSku goodsSku = new GoodsSku();
|
|
|
+ // 设置默认值
|
|
|
+ goodsSku.setDefaultValues();
|
|
|
+ // 直接映射字段
|
|
|
+ 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"));
|
|
|
+
|
|
|
+ // 数值类型字段处理
|
|
|
+ goodsSku.setPrice(productInfo.getBigDecimal("basicPrice") != null ?
|
|
|
+ productInfo.getBigDecimal("basicPrice") : BigDecimal.ZERO);
|
|
|
+ goodsSku.setStock(productInfo.getInteger("initStock") != null ?
|
|
|
+ productInfo.getInteger("initStock") : 0);
|
|
|
+
|
|
|
+ // 重量和体积转换(需要根据实际数据格式处理)
|
|
|
+ if (productInfo.containsKey("weight")) {
|
|
|
+ try {
|
|
|
+ String weightStr = productInfo.getString("weight");
|
|
|
+ goodsSku.setWeight(new BigDecimal(weightStr));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("重量格式转换失败: {}", productInfo.getString("weight"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 状态处理
|
|
|
+ Integer status = productInfo.getInteger("flag");
|
|
|
+ if (status != null && status == 0) {
|
|
|
+ goodsSku.setStatus(0); // 停售
|
|
|
+ } else {
|
|
|
+ goodsSku.setStatus(1); // 正常
|
|
|
+ }
|
|
|
+
|
|
|
+ // 其他字段映射
|
|
|
+ if (productInfo.containsKey("spec")) {
|
|
|
+ // spec字段可能包含规格信息,可以用于skuName或单独存储
|
|
|
+ goodsSku.setSkuName(goodsSku.getSkuName() + " " + productInfo.getString("spec"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (productInfo.containsKey("unit")) {
|
|
|
+ // 单位信息可以用于skuName
|
|
|
+ goodsSku.setSkuName(goodsSku.getSkuName() + "(" + productInfo.getString("unit") + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ return goodsSku;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分类代码转换(需要根据实际业务实现)
|
|
|
+ */
|
|
|
+ private Long convertCategoryCodeToId(String categoryCode) {
|
|
|
+ // 这里需要根据您的分类映射关系实现
|
|
|
+ // 例如:从数据库或配置文件中根据categoryCode查找对应的categoryId
|
|
|
+ if ("101101".equals(categoryCode)) {
|
|
|
+ return 1L; // 每日坚果分类ID
|
|
|
+ } else if ("101000".equals(categoryCode)) {
|
|
|
+ return 2L; // 坚果分类ID
|
|
|
+ }
|
|
|
+ return 0L; // 默认分类
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 品牌名称转换(需要根据实际业务实现)
|
|
|
+ */
|
|
|
+ private Long convertBrandToId(String brandName) {
|
|
|
+ // 这里需要根据您的品牌映射关系实现
|
|
|
+ if ("沃隆".equals(brandName)) {
|
|
|
+ return 1L; // 沃隆品牌ID
|
|
|
+ }
|
|
|
+ return 0L; // 默认品牌
|
|
|
+ }
|
|
|
+
|
|
|
+}
|