Browse Source

一键出货代码优化

zhangwl 1 month ago
parent
commit
4d3578018b

+ 121 - 0
src/main/java/zs/payment/mapper/flymall/B2cGoodsStatusMapper.java

@@ -0,0 +1,121 @@
+package zs.payment.mapper.flymall;
+
+import org.apache.ibatis.annotations.*;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 云商城商品状态变更相关操作(对应PHP changeStatus接口中的DB操作)
+ * 涉及表:b2c_goods_spu_in_online, b2c_goods_sku_in_online, b2c_shelf_goods, b2c_shelf,
+ *         b2c_goods_spu_yd_distribution, b2c_jf_goods_spu_in_online, b2c_jf_goods_sku_in_online, b2c_jf_mall_goods
+ */
+@Mapper
+public interface B2cGoodsStatusMapper {
+
+    // ============ SPU 状态变更(对应 goodsHandleByQiYue) ============
+
+    /**
+     * 更新商品上下架状态(对应 goodsHandleByQiYue type=1, status=2)
+     * 下架时不更新upd_time
+     */
+    @Update("UPDATE b2c_goods_spu_in_online SET is_on_sale = #{status} WHERE gd_id = #{gdId}")
+    int updateOnSaleStatus(@Param("gdId") Integer gdId, @Param("status") Integer status);
+
+    /**
+     * 软删除商品(对应 goodsHandleByQiYue type=2)
+     */
+    @Update("UPDATE b2c_goods_spu_in_online SET is_del = 1 WHERE gd_id = #{gdId}")
+    int softDeleteSpu(@Param("gdId") Integer gdId);
+
+    // ============ SKU 操作(对应 delSkuByGdId / getSkuByGdId) ============
+
+    /**
+     * 查询商品下所有SKU ID(用于清除Redis缓存)
+     */
+    @Select("SELECT sku_id FROM b2c_goods_sku_in_online WHERE gd_id = #{gdId}")
+    List<Integer> findSkuIdsByGdId(@Param("gdId") Integer gdId);
+
+    /**
+     * 删除商品下所有SKU(硬删除,对应PHP delSkuByGdId)
+     */
+    @Delete("DELETE FROM b2c_goods_sku_in_online WHERE gd_id = #{gdId}")
+    int deleteSkusByGdId(@Param("gdId") Integer gdId);
+
+    // ============ 货架商品操作(对应 delGoodsByShelvesId + updateshelfnumbygdid) ============
+
+    /**
+     * 查询商品关联的货架ID列表(对应 updateshelfnumbygdid 中的 SELECT distinct shelf_id)
+     */
+    @Select("SELECT DISTINCT shelf_id FROM b2c_shelf_goods WHERE shop_id = #{shopId} AND gd_id = #{gdId}")
+    List<Integer> findShelfIdsByShopAndGd(@Param("shopId") Integer shopId, @Param("gdId") Integer gdId);
+
+    /**
+     * 查询货架信息(parent_id, is_has_goods)
+     */
+    @Select("SELECT shelf_id, parent_id, is_has_goods FROM b2c_shelf WHERE shelf_id = #{shelfId}")
+    Map<String, Object> findShelfById(@Param("shelfId") Integer shelfId);
+
+    /**
+     * 查询子货架ID列表
+     */
+    @Select("SELECT shelf_id FROM b2c_shelf WHERE parent_id = #{parentId} AND is_del = 0")
+    List<Integer> findChildShelfIds(@Param("parentId") Integer parentId);
+
+    /**
+     * 统计货架下有效商品数
+     */
+    @Select("SELECT COUNT(shelf_goods_id) FROM b2c_shelf_goods WHERE shelf_id = #{shelfId} AND is_del = 0")
+    int countShelfGoods(@Param("shelfId") Integer shelfId);
+
+    /**
+     * 统计多个货架下有效商品总数
+     */
+    @Select("<script>" +
+            "SELECT COUNT(shelf_goods_id) FROM b2c_shelf_goods WHERE is_del = 0 AND shelf_id IN " +
+            "<foreach collection='shelfIds' item='id' open='(' separator=',' close=')'>" +
+            "#{id}" +
+            "</foreach>" +
+            "</script>")
+    int countShelfGoodsByIds(@Param("shelfIds") List<Integer> shelfIds);
+
+    /**
+     * 更新货架商品数量
+     */
+    @Update("UPDATE b2c_shelf SET is_has_goods = #{count} WHERE shelf_id = #{shelfId}")
+    int updateShelfGoodsCount(@Param("shelfId") Integer shelfId, @Param("count") int count);
+
+    /**
+     * 软删除货架商品(对应PHP delGoodsByShelvesId)
+     */
+    @Update("UPDATE b2c_shelf_goods SET is_del = 1 WHERE shop_id = #{shopId} AND gd_id = #{gdId}")
+    int softDeleteShelfGoods(@Param("shopId") Integer shopId, @Param("gdId") Integer gdId);
+
+    // ============ 悦店分销商品(对应 editYdDistributionGoodsStatus) ============
+
+    /**
+     * 下架悦店分销商品
+     */
+    @Update("UPDATE b2c_goods_spu_yd_distribution SET status = 2 WHERE status != 3 AND gd_id = #{gdId}")
+    int offShelfYdDistributionGoods(@Param("gdId") Integer gdId);
+
+    // ============ 积分商品(对应 batchOffTheShelfJfGoods) ============
+
+    /**
+     * 下架积分SPU商品
+     */
+    @Update("UPDATE b2c_jf_goods_spu_in_online SET status = 2 WHERE status = 0 AND gd_id = #{gdId}")
+    int offShelfJfGoodsSpu(@Param("gdId") Integer gdId);
+
+    /**
+     * 下架积分SKU商品
+     */
+    @Update("UPDATE b2c_jf_goods_sku_in_online SET status = 2 WHERE gd_id = #{gdId} AND status = 0")
+    int offShelfJfGoodsSku(@Param("gdId") Integer gdId);
+
+    /**
+     * 下架积分商城商品
+     */
+    @Update("UPDATE b2c_jf_mall_goods SET status = 1 WHERE gd_id = #{gdId} AND status = 0")
+    int offShelfJfMallGoods(@Param("gdId") Integer gdId);
+}

+ 110 - 19
src/main/java/zs/payment/service/commodity/impl/CommodityProcureServiceImpl.java

@@ -42,6 +42,7 @@ import zs.payment.service.flymall.b2cshop.B2cShopService;
 
 
 import zs.payment.mapper.flymall.B2cGoodsAttrMapper;
 import zs.payment.mapper.flymall.B2cGoodsAttrMapper;
 import zs.payment.mapper.flymall.B2cGoodsSpuInOnlineMapper;
 import zs.payment.mapper.flymall.B2cGoodsSpuInOnlineMapper;
+import zs.payment.mapper.flymall.B2cGoodsStatusMapper;
 import zs.payment.messages.KafkaProducer;
 import zs.payment.messages.KafkaProducer;
 import zs.payment.utils.RedisUtils;
 import zs.payment.utils.RedisUtils;
 
 
@@ -95,6 +96,8 @@ public class CommodityProcureServiceImpl implements CommodityProcureService {
     private B2cGoodsSpuInOnlineMapper b2cGoodsSpuInOnlineMapper;
     private B2cGoodsSpuInOnlineMapper b2cGoodsSpuInOnlineMapper;
     @Autowired
     @Autowired
     private B2cGoodsAttrMapper b2cGoodsAttrMapper;
     private B2cGoodsAttrMapper b2cGoodsAttrMapper;
+    @Autowired
+    private B2cGoodsStatusMapper b2cGoodsStatusMapper;
 
 
     private static final BigDecimal HUNDRED = new BigDecimal("100");
     private static final BigDecimal HUNDRED = new BigDecimal("100");
     private static final BigDecimal COMMISSION_DEFAULT_RATE = new BigDecimal("0.01");
     private static final BigDecimal COMMISSION_DEFAULT_RATE = new BigDecimal("0.01");
@@ -1196,33 +1199,49 @@ public class CommodityProcureServiceImpl implements CommodityProcureService {
     }
     }
 
 
     /**
     /**
-     * 停止供货:通知云商城停止供货
-     * 对应PHP supply()中issupply==0的逻辑(1015-1034行)
+     * 停止供货:直连BD完成云商城下架+删除操作(替换原 PHP changeStatus 接口)
+     * 对应PHP changeStatus() 完整流程:
+     *   1. goodsHandleByQiyue type=1 → is_on_sale=2
+     *   2. goodsHandleByQiyue type=2 → is_del=1
+     *   3. updateshelfnumbygdid + delGoodsByShelvesId → 更新货架计数 + 软删除货架商品
+     *   4. delSkuByGdId → 删除SKU
+     *   5. 清除Redis缓存
+     *   6. editYdDistributionGoodsStatus → 下架悦店分销商品
+     *   7. batchOffTheShelfJfGoods → 下架积分商品
      */
      */
     private void stopSupplyFromMall(ImsEweiShopGoods item, Integer storeid) {
     private void stopSupplyFromMall(ImsEweiShopGoods item, Integer storeid) {
         if (item.getGdId() == null || item.getGdId() == 0) {
         if (item.getGdId() == null || item.getGdId() == 0) {
             throw new ApiException("该商品未在云商城供货,无法停止供货");
             throw new ApiException("该商品未在云商城供货,无法停止供货");
         }
         }
+        Integer gdId = item.getGdId();
 
 
-        // 调用云商城接口通知停止供货
-        String changeStatusUrl = mallHost + "/changeStatus?shop_id=" + storeid
-                + "&gd_id=" + item.getGdId() + "&status=2";
-        try {
-            String resp = HttpUtil.restTemplateGet(changeStatusUrl, null);
-            log.info("云商城停止供货 goodsId={}, gd_id={}, url={}, resp={}", item.getId(), item.getGdId(), changeStatusUrl, resp);
-            JSONObject result = JSON.parseObject(resp);
-            if (result == null || result.getIntValue("head") != 200) {
-                throw new ApiException("停止供货失败!");
-            }
-        } catch (ApiException e) {
-            throw e;
-        } catch (Exception e) {
-            log.error("云商城停止供货接口异常 goodsId={}", item.getId(), e);
-            throw new ApiException("停止供货接口调用失败!");
+        // Step 1: 下架商品(is_on_sale=2)
+        int affected = b2cGoodsStatusMapper.updateOnSaleStatus(gdId, 2);
+        if (affected <= 0) {
+            throw new ApiException("停止供货失败!");
         }
         }
 
 
-        //取消供货
-        yunMallGoodsService.saveGoods(item.getGdId().toString(),0);
+        // Step 2: 软删除商品(is_del=1)
+        b2cGoodsStatusMapper.softDeleteSpu(gdId);
+
+        // Step 3: 更新货架商品数量 + 软删除货架商品
+        updateShelfCountAndDelete(storeid, gdId);
+
+        // Step 4: 删除SKU + 清除Redis缓存
+        deleteSkusAndClearCache(gdId);
+
+        // Step 5: 下架悦店分销商品
+        b2cGoodsStatusMapper.offShelfYdDistributionGoods(gdId);
+
+        // Step 6: 下架积分商品(SPU + SKU + 商城)
+        b2cGoodsStatusMapper.offShelfJfGoodsSpu(gdId);
+        b2cGoodsStatusMapper.offShelfJfGoodsSku(gdId);
+        b2cGoodsStatusMapper.offShelfJfMallGoods(gdId);
+
+        log.info("云商城停止供货成功 goodsId={}, gdId={}, storeid={}", item.getId(), gdId, storeid);
+
+        // 取消供货
+        yunMallGoodsService.saveGoods(gdId.toString(), 2);
         // 停止供货时取消推荐状态
         // 停止供货时取消推荐状态
         ImsEweiShopGoods update = new ImsEweiShopGoods();
         ImsEweiShopGoods update = new ImsEweiShopGoods();
         update.setId(item.getId());
         update.setId(item.getId());
@@ -1230,6 +1249,78 @@ public class CommodityProcureServiceImpl implements CommodityProcureService {
         goodsService.updateById(update);
         goodsService.updateById(update);
     }
     }
 
 
+    /**
+     * 更新货架商品数量并软删除货架商品(对应PHP updateshelfnumbygdid + delGoodsByShelvesId)
+     */
+    private void updateShelfCountAndDelete(Integer shopId, Integer gdId) {
+        try {
+            // 查询关联的货架ID
+            List<Integer> shelfIds = b2cGoodsStatusMapper.findShelfIdsByShopAndGd(shopId, gdId);
+            if (shelfIds != null && !shelfIds.isEmpty()) {
+                for (Integer shelfId : shelfIds) {
+                    updateShelfNum(shelfId);
+                }
+            }
+            // 软删除货架商品
+            b2cGoodsStatusMapper.softDeleteShelfGoods(shopId, gdId);
+        } catch (Exception e) {
+            log.warn("更新货架商品数量失败 shopId={}, gdId={}", shopId, gdId, e);
+        }
+    }
+
+    /**
+     * 重新计算货架及其父货架的商品数量(对应PHP updateshelfnum)
+     * 逻辑:计算当前货架商品数,同时更新父货架商品总数
+     */
+    private void updateShelfNum(Integer shelfId) {
+        Map<String, Object> shelfInfo = b2cGoodsStatusMapper.findShelfById(shelfId);
+        if (shelfInfo == null) return;
+
+        Object parentIdObj = shelfInfo.get("parent_id");
+        Integer parentId = parentIdObj != null ? ((Number) parentIdObj).intValue() : 0;
+
+        // 重新计算当前货架商品数(减1,因为待删除的商品还未标记is_del)
+        int currentCount = b2cGoodsStatusMapper.countShelfGoods(shelfId);
+        int newCount = Math.max(currentCount - 1, 0);
+        b2cGoodsStatusMapper.updateShelfGoodsCount(shelfId, newCount);
+
+        // 更新父货架商品数
+        if (parentId > 0) {
+            Map<String, Object> parentInfo = b2cGoodsStatusMapper.findShelfById(parentId);
+            if (parentInfo != null) {
+                List<Integer> childShelfIds = b2cGoodsStatusMapper.findChildShelfIds(parentId);
+                int parentCount = 0;
+                if (childShelfIds != null && !childShelfIds.isEmpty()) {
+                    parentCount = b2cGoodsStatusMapper.countShelfGoodsByIds(childShelfIds);
+                    parentCount = Math.max(parentCount - 1, 0);
+                }
+                b2cGoodsStatusMapper.updateShelfGoodsCount(parentId, parentCount);
+            }
+        }
+    }
+
+    /**
+     * 删除SKU并清除Redis缓存(对应PHP delSkuByGdId + Redis删除)
+     */
+    private void deleteSkusAndClearCache(Integer gdId) {
+        // 先查询sku_id用于清除缓存
+        List<Integer> skuIds = b2cGoodsStatusMapper.findSkuIdsByGdId(gdId);
+
+        // 删除SKU记录
+        b2cGoodsStatusMapper.deleteSkusByGdId(gdId);
+
+        // 清除Redis缓存
+        if (skuIds != null && !skuIds.isEmpty()) {
+            try {
+                for (Integer skuId : skuIds) {
+                    redisUtils.del(String.valueOf(skuId));
+                }
+            } catch (Exception e) {
+                log.warn("Redis缓存清除失败 gdId={}", gdId, e);
+            }
+        }
+    }
+
     @Override
     @Override
     @Transactional(rollbackFor = Exception.class)
     @Transactional(rollbackFor = Exception.class)
     public Result processSupplyItem(Long goodsId, Integer issupply, Integer cate, Integer uniacid, Integer storeid) {
     public Result processSupplyItem(Long goodsId, Integer issupply, Integer cate, Integer uniacid, Integer storeid) {