From 729d67f8f328d2b77ec9ed392b93a5379e02d79a Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Mon, 13 Jan 2025 00:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=9D=E8=BF=85=E5=95=86=E5=93=81=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall/common/pojo/res/ThirdApiRes.java | 4 + .../ShopBaseProductCategoryService.java | 9 ++ .../service/ShopBaseProductTypeService.java | 7 ++ .../ShopBaseProductCategoryServiceImpl.java | 22 +++++ .../impl/ShopBaseProductTypeServiceImpl.java | 13 +++ .../controller/SyncThirdDataController.java | 17 ++-- .../sync/service/SyncThirdDataService.java | 10 +- .../impl/SyncThirdDataServiceImpl.java | 98 +++++++++++++++++-- 8 files changed, 164 insertions(+), 16 deletions(-) diff --git a/mall-common/src/main/java/com/suisung/mall/common/pojo/res/ThirdApiRes.java b/mall-common/src/main/java/com/suisung/mall/common/pojo/res/ThirdApiRes.java index b4d6f95e..fef6cabe 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/pojo/res/ThirdApiRes.java +++ b/mall-common/src/main/java/com/suisung/mall/common/pojo/res/ThirdApiRes.java @@ -42,4 +42,8 @@ public class ThirdApiRes implements Serializable { public ThirdApiRes success(String errorMsg) { return new ThirdApiRes(0, errorMsg, null, null); } + + public ThirdApiRes success(String errorMsg, Object result) { + return new ThirdApiRes(0, errorMsg, result, null); + } } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/base/service/ShopBaseProductCategoryService.java b/mall-shop/src/main/java/com/suisung/mall/shop/base/service/ShopBaseProductCategoryService.java index f6b6b877..880fba4f 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/base/service/ShopBaseProductCategoryService.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/base/service/ShopBaseProductCategoryService.java @@ -117,6 +117,15 @@ public interface ShopBaseProductCategoryService extends IBaseService queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("category_name", categoryName); + queryWrapper.eq("category_parent_id", parentId); + queryWrapper.eq("store_id", storeId); + queryWrapper.orderByAsc("category_order"); + List l = list(queryWrapper); + if (CollUtil.isNotEmpty(l)) { + return l.get(0); + } + return null; + } + /** * 把思迅的商品分类转出澜弛的商品分类 * diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/base/service/impl/ShopBaseProductTypeServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/base/service/impl/ShopBaseProductTypeServiceImpl.java index 8f3d7001..db6ae310 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/base/service/impl/ShopBaseProductTypeServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/base/service/impl/ShopBaseProductTypeServiceImpl.java @@ -114,4 +114,17 @@ public class ShopBaseProductTypeServiceImpl extends BaseServiceImpl queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("type_name", typeName); + return getOne(queryWrapper); + } } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/controller/SyncThirdDataController.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/controller/SyncThirdDataController.java index 24f459b2..a66cb493 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/controller/SyncThirdDataController.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/controller/SyncThirdDataController.java @@ -8,6 +8,7 @@ package com.suisung.mall.shop.sync.controller; +import cn.hutool.json.JSONArray; import com.suisung.mall.common.api.CommonResult; import com.suisung.mall.common.modules.base.ShopBaseProductBrand; import com.suisung.mall.common.pojo.res.ThirdApiRes; @@ -17,7 +18,6 @@ import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.util.List; @Api(tags = "第三方数据同步") @RestController @@ -26,9 +26,16 @@ public class SyncThirdDataController { @Resource private SyncThirdDataService syncThirdDataService; + + @ApiOperation(value = "商品分类数据同步", notes = "商品分类数据同步") + @RequestMapping(value = "/goods/category", method = RequestMethod.POST) + public ThirdApiRes syncGoodsCategory(@RequestBody JSONArray categoryListJSON) { + return syncThirdDataService.saveOrUpdateShopBaseProductCategoryBatch(categoryListJSON); + } + @ApiOperation(value = "商品品牌数据同步", notes = "商品品牌数据同步") @RequestMapping(value = "/goods/brand", method = RequestMethod.POST) - public ThirdApiRes syncGoodsBrand(@RequestBody String brandListJSON) { + public ThirdApiRes syncGoodsBrand(@RequestBody JSONArray brandListJSON) { return syncThirdDataService.saveOrUpdateShopBaseProductBrandBatch(brandListJSON); } @@ -38,12 +45,6 @@ public class SyncThirdDataController { return null; } - @ApiOperation(value = "商品分类数据同步", notes = "商品分类数据同步") - @RequestMapping(value = "/goods/category", method = RequestMethod.POST) - public ThirdApiRes syncGoodsCategory(@RequestBody String reqBody) { - return null; - } - @ApiOperation(value = "商品数据同步", notes = "商品数据同步") @RequestMapping(value = "/goods", method = RequestMethod.POST) public ThirdApiRes syncGoods(@RequestBody String reqBody) { diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/SyncThirdDataService.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/SyncThirdDataService.java index 5a659b7c..c882b61e 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/SyncThirdDataService.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/SyncThirdDataService.java @@ -8,6 +8,7 @@ package com.suisung.mall.shop.sync.service; +import cn.hutool.json.JSONArray; import com.suisung.mall.common.modules.base.ShopBaseProductBrand; import com.suisung.mall.common.pojo.res.ThirdApiRes; @@ -15,10 +16,17 @@ import java.util.List; public interface SyncThirdDataService { + /** + * 批量保存商品的分类 + * @param categoryListJSON + * @return + */ + ThirdApiRes saveOrUpdateShopBaseProductCategoryBatch(JSONArray categoryListJSON); + /** * 批量保存商品品牌记录 * @param brandListJSON * @return */ - ThirdApiRes saveOrUpdateShopBaseProductBrandBatch(String brandListJSON); + ThirdApiRes saveOrUpdateShopBaseProductBrandBatch(JSONArray brandListJSON); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java index ac61c348..6ceee17b 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java @@ -8,21 +8,25 @@ package com.suisung.mall.shop.sync.service.impl; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.suisung.mall.common.modules.base.ShopBaseProductBrand; import com.suisung.mall.common.modules.base.ShopBaseProductCategory; +import com.suisung.mall.common.modules.base.ShopBaseProductType; import com.suisung.mall.common.pojo.res.ThirdApiRes; import com.suisung.mall.common.utils.I18nUtil; import com.suisung.mall.shop.base.service.ShopBaseProductBrandService; import com.suisung.mall.shop.base.service.ShopBaseProductCategoryService; +import com.suisung.mall.shop.base.service.ShopBaseProductTypeService; import com.suisung.mall.shop.sync.service.SyncThirdDataService; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import javax.annotation.Resource; +import java.util.HashMap; import java.util.List; @Service @@ -33,6 +37,88 @@ public class SyncThirdDataServiceImpl implements SyncThirdDataService { @Resource private ShopBaseProductCategoryService productCategoryService; + @Resource + private ShopBaseProductTypeService productTypeService; + + /** + * 批量保存商品的分类 + * + * @param categoryListJSON + * @return + */ + @Override + public ThirdApiRes saveOrUpdateShopBaseProductCategoryBatch(JSONArray categoryListJSON) { + // TODO 验签、appid,必要参数判断 + + if (ObjectUtil.isEmpty(categoryListJSON)) { + return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); + } + + List list = JSONUtil.toList(categoryListJSON, ShopBaseProductCategory.class); + if (list == null) { + return new ThirdApiRes().fail(1004, I18nUtil._("请求参数有误!")); + } + + if (list != null && list.size() > 500) { + return new ThirdApiRes().fail(1004, I18nUtil._("单次同步记录最多500条!")); + } + + int count = 0; + for (int i = 0; i < list.size(); i++) { + list.get(i).setStore_id("3"); // app 记录传进来 + list.get(i).setData_source(2); // 思迅数据来源 + list.get(i).setCategory_is_enable(1); + + JSONObject o = (JSONObject) categoryListJSON.get(i); + if (o != null) { + // 处理父类字段 产品分类 + if (StrUtil.isNotBlank(o.getStr("parent_name"))) { + ShopBaseProductCategory cate = productCategoryService.getCategoryByName(o.getStr("parent_name")); + if (cate != null) { + list.get(i).setParent_id(cate.getCategory_id()); + } else { + // TODO 新增一个分类 + list.get(i).setParent_id(0); + } + } else { + list.get(i).setParent_id(0); + } + + // 处理商品类型(强调共性) + if (StrUtil.isNotBlank(o.getStr("type_name"))) { + ShopBaseProductType productType = productTypeService.getProductTypeByName(o.getStr("type_name")); + if (productType != null) { + list.get(i).setType_id(productType.getType_id()); + } else { + // TODO 新增一个类型 + } + } else { + list.get(i).setType_id(1001); + } + } + + ShopBaseProductCategory productCategoryTemp = productCategoryService.getCategoryByName(list.get(i).getParent_id(), list.get(i).getCategory_name(), list.get(i).getStore_id()); + if (productCategoryTemp != null) { + // 更改记录 + if (!productCategoryTemp.getCategory_image().equals(list.get(i).getCategory_image()) + || !productCategoryTemp.getType_id().equals(list.get(i).getType_id()) + || !productCategoryTemp.getCategory_virtual_enable().equals(list.get(i).getCategory_virtual_enable()) + || !productCategoryTemp.getCategory_commission_rate().equals(list.get(i).getCategory_commission_rate()) + || !productCategoryTemp.getCategory_show_type().equals(list.get(i).getCategory_show_type())) { + list.get(i).setCategory_id(productCategoryTemp.getCategory_id()); + } else { + continue; + } + } + + if (productCategoryService.saveOrUpdate(list.get(i))) { + count++; + } + } + + return new ThirdApiRes().success("成功同步" + count + "条记录!", count); + } + /** * 批量保存商品品牌记录 * @@ -40,29 +126,27 @@ public class SyncThirdDataServiceImpl implements SyncThirdDataService { * @return */ @Override - public ThirdApiRes saveOrUpdateShopBaseProductBrandBatch(String brandListJSON) { + public ThirdApiRes saveOrUpdateShopBaseProductBrandBatch(JSONArray brandListJSON) { // TODO 验签、appid,必要参数判断 - if (StringUtils.isEmpty(brandListJSON)) { + if (ObjectUtil.isEmpty(brandListJSON)) { return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); } List goodBrandList = JSONUtil.toList(brandListJSON, ShopBaseProductBrand.class); if (goodBrandList == null) { - return new ThirdApiRes().fail(1004, I18nUtil._("请求参数发生异常!")); + return new ThirdApiRes().fail(1004, I18nUtil._("请求参数有误!")); } if (goodBrandList != null && goodBrandList.size() > 500) { return new ThirdApiRes().fail(1004, I18nUtil._("单次同步记录最多500条!")); } - JSONArray brandListJSONObj = JSONUtil.parseArray(brandListJSON); - int count = 0; for (int i = 0; i < goodBrandList.size(); i++) { goodBrandList.get(i).setStore_id(3); // app 记录传进来 // 处理大分类字段 - JSONObject o = (JSONObject) brandListJSONObj.get(i); + JSONObject o = (JSONObject) brandListJSON.get(i); if (o != null && StrUtil.isNotBlank(o.getStr("category"))) { ShopBaseProductCategory cate = productCategoryService.getCategoryByName(o.getStr("category")); if (cate != null) { @@ -73,6 +157,6 @@ public class SyncThirdDataServiceImpl implements SyncThirdDataService { count += productBrandService.saveOrUpdateBrand2(goodBrandList.get(i)); } - return new ThirdApiRes().success("成功同步" + count + "条记录!"); + return new ThirdApiRes().success("成功同步" + count + "条记录!", count); } }