思迅商品同步

This commit is contained in:
Jack 2025-01-13 00:41:25 +08:00
parent b8bc6f658e
commit 729d67f8f3
8 changed files with 164 additions and 16 deletions

View File

@ -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);
}
}

View File

@ -117,6 +117,15 @@ public interface ShopBaseProductCategoryService extends IBaseService<ShopBasePro
*/
ShopBaseProductCategory getCategoryByName(String categoryName);
/**
* 根据父类id分类名称店铺Id获取一条记录
* @param parentId
* @param categoryName
* @param storeId
* @return
*/
ShopBaseProductCategory getCategoryByName(Integer parentId, String categoryName, String storeId);
/**
* 把思迅的商品分类转出澜弛的商品分类
* @param sxSyncCategory

View File

@ -21,4 +21,11 @@ public interface ShopBaseProductTypeService extends IBaseService<ShopBaseProduct
Map getType(String type_id);
Map getType(String type_id, Long product_id);
/**
* 根据类型名称获取一条记录
* @param typeName
* @return
*/
ShopBaseProductType getProductTypeByName(String typeName);
}

View File

@ -1134,6 +1134,28 @@ public class ShopBaseProductCategoryServiceImpl extends BaseServiceImpl<ShopBase
return null;
}
/**
* 根据父类id分类名称店铺Id获取一条记录
*
* @param parentId
* @param categoryName
* @param storeId
* @return
*/
@Override
public ShopBaseProductCategory getCategoryByName(Integer parentId, String categoryName, String storeId) {
QueryWrapper<ShopBaseProductCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("category_name", categoryName);
queryWrapper.eq("category_parent_id", parentId);
queryWrapper.eq("store_id", storeId);
queryWrapper.orderByAsc("category_order");
List<ShopBaseProductCategory> l = list(queryWrapper);
if (CollUtil.isNotEmpty(l)) {
return l.get(0);
}
return null;
}
/**
* 把思迅的商品分类转出澜弛的商品分类
*

View File

@ -114,4 +114,17 @@ public class ShopBaseProductTypeServiceImpl extends BaseServiceImpl<ShopBaseProd
return data;
}
/**
* 根据类型名称获取一条记录
*
* @param typeName
* @return
*/
@Override
public ShopBaseProductType getProductTypeByName(String typeName) {
QueryWrapper<ShopBaseProductType> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type_name", typeName);
return getOne(queryWrapper);
}
}

View File

@ -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) {

View File

@ -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);
}

View File

@ -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<ShopBaseProductCategory> 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<ShopBaseProductBrand> 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);
}
}