Browse Source

同步商品信息时,如果没有对应分类,创建分类

v1.0
15989082884@163.com 2 years ago
parent
commit
b42fcdd6d0
  1. 4
      src/main/java/com/qs/serve/modules/goods/mapper/GoodsCategoryMapper.java
  2. 140
      src/main/java/com/qs/serve/modules/goods/service/GoodsApplicationService.java
  3. 2
      src/main/java/com/qs/serve/modules/goods/service/GoodsCategoryService.java
  4. 16
      src/main/java/com/qs/serve/modules/goods/service/impl/GoodsCategoryServiceImpl.java

4
src/main/java/com/qs/serve/modules/goods/mapper/GoodsCategoryMapper.java

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qs.serve.modules.goods.entity.GoodsCategory;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
@ -15,5 +16,8 @@ public interface GoodsCategoryMapper extends BaseMapper<GoodsCategory> {
@Update("update goods_category set del_flag = 1 , `code`= #{obj.code} where id = #{obj.id}")
int deleteByCate(@Param("obj") GoodsCategory entity);
@Select("SELECT MAX(code) FROM goods_category WHERE code LIKE CONCAT(#{prefix}, '%')")
String findMaxCodeStartingWith(String prefix);
}

140
src/main/java/com/qs/serve/modules/goods/service/GoodsApplicationService.java

@ -47,6 +47,26 @@ public class GoodsApplicationService {
R<String> result = seeYonRequestBaseService.getBase(TbsSeeYonConst.ERP_CUS_INV_SYNC_PRICE,"");
}
public void syncStandGoodsSpu(){
//兼容任务调度
AuthContextUtils.setTenant("001");
//获取stand表的存货
R<String> result = seeYonRequestBaseService.getBase(TbsSeeYonConst.ERP_CUS_INV_STAND,"");
String listJson = result.getData();
List<StandInventory> inventoryList = JsonUtil.jsonToList(listJson,StandInventory.class);
LambdaQueryWrapper<GoodsSpu> spuLqw = new LambdaQueryWrapper<>();
spuLqw.eq(GoodsSpu::getCategoryFirst,"1");
List<GoodsSpu> goodsSpuList = goodsSpuService.list(spuLqw);
for(GoodsSpu spu:goodsSpuList) {
GoodsSpu finalSpu = spu;
List<StandInventory> tempInvList = inventoryList.stream().filter(a->a.getInvSkuCode().equals(finalSpu.getSpuCode())).collect(Collectors.toList());
StandInventory inventory = tempInvList.get(0);
spu = setCategoryInfo(inventory, spu);
goodsSpuService.updateById(spu);
}
}
public void syncStandGoods(boolean isFullUpdate){
//兼容任务调度
AuthContextUtils.setTenant("001");
@ -100,28 +120,8 @@ public class GoodsApplicationService {
spu.setId(spuId);
spu.setSpuCode(inventory.getInvSkuCode());
spu.setName(inventory.getInvSku());
//匹配类目
LambdaQueryWrapper<GoodsCategory> cateLqw = new LambdaQueryWrapper<>();
cateLqw.likeRight(GoodsCategory::getLevelPathNames,inventory.getInvBrand())
.likeLeft(GoodsCategory::getLevelPathNames,inventory.getInvSeries())
.like(GoodsCategory::getLevelPathNames,inventory.getInvCategory());
List<GoodsCategory> categoryList = goodsCategoryService.list(cateLqw);
if(categoryList.size()>0){
GoodsCategory goodsCategory = categoryList.get(0);
String[] cateIds = goodsCategory.getLevelPath().split("_");
if(cateIds.length>2){
spu.setCategoryFirst(cateIds[0]);
spu.setCategorySecond(cateIds[1]);
spu.setCategoryThird(cateIds[2]);
spu.setCategoryLast(cateIds[2]);
}
}
if(spu.getCategoryFirst()==null) {
spu.setCategoryFirst("1");
spu.setCategorySecond("2");
spu.setCategoryThird("3");
spu.setCategoryLast("3");
}
spu = setCategoryInfo(inventory,spu);
spu.setShelf(1);
spu.setOrderFlag(Integer.parseInt(inventory.getInvOrderStatus()));
@ -130,6 +130,15 @@ public class GoodsApplicationService {
//防止多次保存
spuId = spu.getId();
spuCodeGetIdMap.put(spu.getSpuCode(),spuId);
}else{
GoodsSpu spu = new GoodsSpu();
spu.setId(spuId);
spu.setName(inventory.getInvSku());
spu.setShelf(1);
spu.setOrderFlag(Integer.parseInt(inventory.getInvOrderStatus()));
spu.setCostFlag(Integer.parseInt(inventory.getInvCostStatus()));
spu = setCategoryInfo(inventory,spu);
goodsSpuService.updateById(spu);
}
GoodsSku sku = new GoodsSku();
sku.setId(skuId);
@ -164,6 +173,93 @@ public class GoodsApplicationService {
seeYonRequestBaseService.getBase(TbsSeeYonConst.ERP_CUS_INV_SHELF,"");
}
private GoodsSpu setCategoryInfo(StandInventory inventory, GoodsSpu spu) {
// 创建品牌级别的查询包装器
LambdaQueryWrapper<GoodsCategory> brandLqw = new LambdaQueryWrapper<>();
brandLqw.eq(GoodsCategory::getName, inventory.getInvBrand());
List<GoodsCategory> brandList = goodsCategoryService.list(brandLqw);
// 如果品牌不存在,则创建一个新的品牌类别
if (brandList.size() == 0) {
GoodsCategory brand = new GoodsCategory();
brand.setEnable(1);
brand.setParentId(null); // 设置父ID为null,表示这是顶级类别
brand.setCode(goodsCategoryService.findMaxCodeStartingWith("B")); // 生成以"B"开头的最大代码
brand.setName(inventory.getInvBrand()); // 设置品牌名称
brand.setDescription(null); // 描述设置为null
brand.setCostFlag(1); // 设置成本标志
brand.setSort(0); // 排序设置为0
goodsCategoryService.modify(brand); // 保存新的品牌类别
brandList = goodsCategoryService.list(brandLqw); // 重新获取品牌列表
}
// 如果品牌列表不为空,设置第一级类别ID
if (brandList.size() > 0) {
spu.setCategoryFirst(brandList.get(0).getId().toString());
}
// 创建类别级别的查询包装器
LambdaQueryWrapper<GoodsCategory> categoryLqw = new LambdaQueryWrapper<>();
categoryLqw.eq(GoodsCategory::getParentId, Long.parseLong(spu.getCategoryFirst()));
categoryLqw.eq(GoodsCategory::getName, inventory.getInvCategory());
List<GoodsCategory> brandCategoryList = goodsCategoryService.list(categoryLqw);
// 如果类别不存在,则创建一个新的类别
if (brandCategoryList.size() == 0) {
GoodsCategory category = new GoodsCategory();
category.setEnable(1);
category.setParentId(Long.parseLong(spu.getCategoryFirst())); // 设置父ID为第一级类别ID
category.setCode(goodsCategoryService.findMaxCodeStartingWith("C")); // 生成以"C"开头的最大代码
category.setName(inventory.getInvCategory()); // 设置类别名称
category.setDescription(null); // 描述设置为null
category.setCostFlag(1); // 设置成本标志
category.setSort(0); // 排序设置为0
goodsCategoryService.modify(category); // 保存新的类别
brandCategoryList = goodsCategoryService.list(categoryLqw); // 重新获取类别列表
}
// 如果类别列表不为空,设置第二级类别ID
if (brandCategoryList.size() > 0) {
spu.setCategorySecond(brandCategoryList.get(0).getId().toString());
}
// 创建系列级别的查询包装器
LambdaQueryWrapper<GoodsCategory> seriesLqw = new LambdaQueryWrapper<>();
seriesLqw.eq(GoodsCategory::getParentId, Long.parseLong(spu.getCategorySecond()));
seriesLqw.eq(GoodsCategory::getName, inventory.getInvSeries());
List<GoodsCategory> seriesList = goodsCategoryService.list(seriesLqw);
// 如果系列不存在,则创建一个新的系列
if (seriesList.size() == 0) {
GoodsCategory series = new GoodsCategory();
series.setEnable(1);
series.setParentId(Long.parseLong(spu.getCategorySecond())); // 设置父ID为第二级类别ID
series.setCode(goodsCategoryService.findMaxCodeStartingWith("S")); // 生成以"S"开头的最大代码
series.setName(inventory.getInvSeries()); // 设置系列名称
series.setDescription(null); // 描述设置为null
series.setCostFlag(1); // 设置成本标志
series.setSort(0); // 排序设置为0
goodsCategoryService.modify(series); // 保存新的系列
seriesList = goodsCategoryService.list(seriesLqw); // 重新获取系列列表
}
// 如果系列列表不为空,设置第三级和最终类别ID
if (seriesList.size() > 0) {
spu.setCategoryThird(seriesList.get(0).getId().toString());
spu.setCategoryLast(spu.getCategoryThird());
}
// 如果最终类别为空,则设置默认类别
if(spu.getCategoryLast() == null) {
spu.setCategoryFirst("1");
spu.setCategorySecond("2");
spu.setCategoryThird("3");
spu.setCategoryLast("3");
}
return spu;
}
public GoodsTargetInfo getGoodsTargetInfo(String goodsType,Long goodsIds){
List<GoodsTargetInfo> list = this.getGoodsTargetInfo(goodsType, Arrays.asList(goodsIds));
if(list.size()>0){

2
src/main/java/com/qs/serve/modules/goods/service/GoodsCategoryService.java

@ -21,5 +21,7 @@ public interface GoodsCategoryService extends IService<GoodsCategory> {
void deleteCateById(Long id);
String findMaxCodeStartingWith(String prefix);
}

16
src/main/java/com/qs/serve/modules/goods/service/impl/GoodsCategoryServiceImpl.java

@ -168,5 +168,21 @@ public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper,Go
goodsCategory.setCode(goodsCategory.getCode()+"_del_"+ IdUtil.getSnowFlakeId());
baseMapper.deleteByCate(goodsCategory);
}
@Override
public String findMaxCodeStartingWith(String prefix){
String maxCode = baseMapper.findMaxCodeStartingWith(prefix);
if (maxCode != null && !maxCode.isEmpty()) {
try {
int numericPart = Integer.parseInt(maxCode.substring(1));
return prefix + (numericPart + 1);
} catch (NumberFormatException e) {
// 处理异常情况
e.printStackTrace();
}
}
return ""; // 如果没有找到现有的code,使用默认值
}
}

Loading…
Cancel
Save