|
@@ -1,18 +1,28 @@
|
|
|
package com.sd.wln.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.sd.business.entity.sku.po.Sku;
|
|
|
import com.sd.business.entity.sku.po.SkuClassify;
|
|
|
+import com.sd.business.entity.sku.po.SkuSpec;
|
|
|
+import com.sd.business.service.department.DepartmentService;
|
|
|
import com.sd.business.service.sku.SkuClassifyService;
|
|
|
+import com.sd.business.service.sku.SkuService;
|
|
|
+import com.sd.business.service.sku.SkuSpecService;
|
|
|
import com.sd.wln.service.WlnSkuService;
|
|
|
import com.sd.wln.util.WlnUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -23,24 +33,26 @@ public class WlnSkuServiceImpl implements WlnSkuService {
|
|
|
@Autowired
|
|
|
private SkuClassifyService skuClassifyService;
|
|
|
|
|
|
- @Override
|
|
|
- public boolean SyncSkuClassify() {
|
|
|
+ @Autowired
|
|
|
+ private SkuService skuService;
|
|
|
|
|
|
- int page = 1;
|
|
|
- int size;
|
|
|
- List<JSONObject> list = new ArrayList<>();
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecService skuSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DepartmentService departmentService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean syncSkuClassify() {
|
|
|
+ List<JSONObject> list;
|
|
|
|
|
|
// 查询万里牛sku分类
|
|
|
- do {
|
|
|
- try {
|
|
|
- List<JSONObject> skuClassifyList = WlnUtil.getSkuClassifyList(page, 50);
|
|
|
- list.addAll(skuClassifyList);
|
|
|
- size = skuClassifyList.size();
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("sku分类同步错误", e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- } while (size >= 50);
|
|
|
+ try {
|
|
|
+ list = WlnUtil.getSkuClassifyList();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("sku分类同步错误", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
// 查询数据库sku分类
|
|
|
List<SkuClassify> mysqlSkuClassifyList = skuClassifyService.list();
|
|
@@ -77,4 +89,105 @@ public class WlnSkuServiceImpl implements WlnSkuService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean syncSku() {
|
|
|
+ int page = 1;
|
|
|
+ int size;
|
|
|
+ List<JSONObject> wlnSkuList = new ArrayList<>();
|
|
|
+ do {
|
|
|
+ List<JSONObject> itemList;
|
|
|
+ try {
|
|
|
+ itemList = WlnUtil.getSkuList(page, 200);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("sku同步失败", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ page++;
|
|
|
+ size = itemList.size();
|
|
|
+ wlnSkuList.addAll(itemList);
|
|
|
+ } while (size >= 200);
|
|
|
+
|
|
|
+ Map<String, Long> skuClassifyMap = skuClassifyService.list().stream()
|
|
|
+ .collect(Collectors.toMap(SkuClassify::getWlnCatagoryid, BaseIdPo::getId));
|
|
|
+
|
|
|
+ Map<String, Sku> skuMap = skuService.list().stream().collect(
|
|
|
+ Collectors.toMap(Sku::getCode, Function.identity()));
|
|
|
+
|
|
|
+ Map<Long, Map<String, SkuSpec>> specMap = skuSpecService.list().stream().collect(
|
|
|
+ Collectors.groupingBy(SkuSpec::getSkuId, Collectors.toMap(SkuSpec::getCode, Function.identity())));
|
|
|
+
|
|
|
+ List<Sku> newSkuList = new ArrayList<>();
|
|
|
+ List<SkuSpec> newSkuSpecList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (JSONObject wlnSku : wlnSkuList) {
|
|
|
+ Sku sku = skuMap.get(wlnSku.getString("goods_code"));
|
|
|
+ if (sku == null) {
|
|
|
+ sku = createSku(wlnSku, skuClassifyMap);
|
|
|
+ newSkuList.add(sku);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<JSONObject> specList = wlnSku.getJSONArray("specs").toJavaList(JSONObject.class);
|
|
|
+ for (JSONObject wlnSkuSpec : specList) {
|
|
|
+ String code = wlnSkuSpec.getString("spec_code");
|
|
|
+ Map<String, SkuSpec> skuSpecMap = specMap.get(sku.getId());
|
|
|
+ if (skuSpecMap != null && skuSpecMap.get(code) != null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ SkuSpec skuSpec = createSkuSpec(wlnSkuSpec, sku);
|
|
|
+ newSkuSpecList.add(skuSpec);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ skuService.saveBatch(newSkuList);
|
|
|
+ skuSpecService.saveBatch(newSkuSpecList);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Sku createSku(JSONObject wlnSku, Map<String, Long> skuClassifyMap) {
|
|
|
+ String catagoryId = wlnSku.getString("catagory_id");
|
|
|
+ Long skuClassifyId = skuClassifyMap.get(catagoryId);
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(catagoryId) && skuClassifyId == null) {
|
|
|
+ if (!syncSkuClassify()) {
|
|
|
+ throw new ServiceException("sku分类同步失败");
|
|
|
+ }
|
|
|
+ skuClassifyMap = skuClassifyService.list().stream()
|
|
|
+ .collect(Collectors.toMap(SkuClassify::getWlnCatagoryid, BaseIdPo::getId));
|
|
|
+ skuClassifyId = skuClassifyMap.get(catagoryId);
|
|
|
+ if (skuClassifyId == null) {
|
|
|
+ throw new ServiceException("未知万里牛分类id:" + catagoryId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Sku sku = new Sku();
|
|
|
+ sku.setId(IdWorker.getId());
|
|
|
+ sku.setSource(2);
|
|
|
+ sku.setSkuClassifyId(skuClassifyId);
|
|
|
+ sku.setCode(wlnSku.getString("goods_code"));
|
|
|
+ sku.setName(wlnSku.getString("goods_name"));
|
|
|
+ sku.setBrand(wlnSku.getString("brand_name"));
|
|
|
+ sku.setMainImgUrl(wlnSku.getString("pic"));
|
|
|
+ sku.setType(Objects.equals(sku.getBrand(), "胜德科技") ? 1 : 0);
|
|
|
+ return sku;
|
|
|
+ }
|
|
|
+
|
|
|
+ private SkuSpec createSkuSpec(JSONObject wlnSku, Sku sku) {
|
|
|
+ SkuSpec skuSpec = new SkuSpec();
|
|
|
+ skuSpec.setId(IdWorker.getId());
|
|
|
+ skuSpec.setSkuId(sku.getId());
|
|
|
+ skuSpec.setSpecImgUrl(wlnSku.getString("pic"));
|
|
|
+ skuSpec.setCode(wlnSku.getString("spec_code"));
|
|
|
+ skuSpec.setName(sku.getName() + ":" + wlnSku.getString("spec1") + wlnSku.getString("spec2"));
|
|
|
+ skuSpec.setBarCode(wlnSku.getString("barcode"));
|
|
|
+ skuSpec.setLength(wlnSku.getBigDecimal("length"));
|
|
|
+ skuSpec.setWidth(wlnSku.getBigDecimal("width"));
|
|
|
+ skuSpec.setHeight(wlnSku.getBigDecimal("height"));
|
|
|
+ skuSpec.setNetWeight(wlnSku.getBigDecimal("weight"));
|
|
|
+ skuSpec.setRemark(wlnSku.getString("spec1") + wlnSku.getString("spec2"));
|
|
|
+ return skuSpec;
|
|
|
+ }
|
|
|
+
|
|
|
}
|