Browse Source

sku同步

24282 1 year ago
parent
commit
076ec6905e

+ 71 - 0
sd-business/src/main/java/com/sd/business/controller/department/DepartmentController.java

@@ -0,0 +1,71 @@
+package com.sd.business.controller.department;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.core.domain.BaseSelectDto;
+import com.sd.business.entity.department.dto.DepartmentDto;
+import com.sd.business.entity.department.dto.DepartmentSelectDto;
+import com.sd.business.entity.department.vo.DepartmentVo;
+import com.sd.business.service.department.DepartmentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+
+/**
+ * <p>
+ * 事业部 前端控制器
+ * </p>
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@RestController
+@RequestMapping("/department")
+public class DepartmentController {
+
+    @Autowired
+    private DepartmentService departmentService;
+
+    /**
+     * 事业部分页
+     */
+    @PostMapping("/page")
+    public Page<DepartmentVo> page(@RequestBody DepartmentSelectDto dto) {
+        return departmentService.getPage(dto);
+    }
+
+    /**
+     * 事业部明细
+     */
+    @PostMapping("/detail")
+    public DepartmentVo detail(@RequestBody BaseSelectDto dto) {
+        return departmentService.detail(dto.getId());
+    }
+
+    /**
+     * 事业部新增
+     */
+    @PostMapping("/add")
+    public void add(@RequestBody DepartmentDto departmentDto) {
+        departmentService.add(departmentDto);
+    }
+
+    /**
+     * 事业部编辑
+     */
+    @PostMapping("/edit")
+    public void edit(@RequestBody DepartmentDto departmentDto) {
+        departmentService.edit(departmentDto);
+    }
+
+    /**
+     * 事业部删除
+     */
+    @PostMapping("/delete")
+    public void delete(@RequestBody BaseSelectDto dto) {
+        departmentService.delete(dto.getId());
+    }
+
+}

+ 17 - 0
sd-business/src/main/java/com/sd/business/entity/department/dto/DepartmentDto.java

@@ -0,0 +1,17 @@
+package com.sd.business.entity.department.dto;
+
+import com.sd.business.entity.department.po.Department;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 事业部新增编辑入参实体
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@Getter
+@Setter
+public class DepartmentDto extends Department {
+
+}

+ 17 - 0
sd-business/src/main/java/com/sd/business/entity/department/dto/DepartmentSelectDto.java

@@ -0,0 +1,17 @@
+package com.sd.business.entity.department.dto;
+
+import com.ruoyi.common.core.domain.BaseSelectDto;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 事业部列表查询入参实体
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@Getter
+@Setter
+public class DepartmentSelectDto extends BaseSelectDto {
+
+}

+ 98 - 0
sd-business/src/main/java/com/sd/business/entity/department/po/Department.java

@@ -0,0 +1,98 @@
+package com.sd.business.entity.department.po;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.ruoyi.common.core.domain.BasePo;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.math.BigDecimal;
+
+/**
+ * <p>
+ * 事业部
+ * </p>
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@Getter
+@Setter
+@TableName("department")
+public class Department extends BasePo {
+
+    /**
+     * 管理员账号
+     */
+    private String adminUsername;
+
+    /**
+     * 管理员密码
+     */
+    private String adminPassword;
+
+    /**
+     * 事业部
+     */
+    private String name;
+
+    /**
+     * 事业部电话
+     */
+    private String telephone;
+
+    /**
+     * 事业部地址
+     */
+    private String address;
+
+    /**
+     * 联系人
+     */
+    private String contactPerson;
+
+    /**
+     * 联系人电话
+     */
+    private String contactNumber;
+
+    /**
+     * 售价体系 字典:department_sellingPriceSystem
+     */
+    private String sellingPriceSystem;
+
+    /**
+     * 下单模式 字典:department_orderMode
+     */
+    private String orderMode;
+
+    /**
+     * 佣金规则 字典:department_commissionRule
+     */
+    private String commissionRule;
+
+    /**
+     * 状态 0禁用 1启用
+     */
+    private Integer status;
+
+    /**
+     * 信用额度
+     */
+    private BigDecimal lineCredit;
+
+    /**
+     * 已用额度
+     */
+    private BigDecimal usedCredit;
+
+    /**
+     * 万里牛仓库编码 多个用,拼接 字典:wlnWarehouseCoding
+     */
+    private String wlnWarehouseCoding;
+
+    /**
+     * 万里牛品牌 多个用,拼接 字典:wlnBrand
+     */
+    private String wlnBrand;
+
+}

+ 17 - 0
sd-business/src/main/java/com/sd/business/entity/department/vo/DepartmentVo.java

@@ -0,0 +1,17 @@
+package com.sd.business.entity.department.vo;
+
+import com.sd.business.entity.department.po.Department;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 事业部列表查询返回值实体
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@Getter
+@Setter
+public class DepartmentVo extends Department {
+
+}

+ 6 - 2
sd-business/src/main/java/com/sd/business/entity/sku/po/Sku.java

@@ -2,7 +2,6 @@ package com.sd.business.entity.sku.po;
 
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.ruoyi.common.core.domain.BasePo;
-import java.util.Date;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -11,7 +10,7 @@ import lombok.Setter;
  * sku
  * </p>
  *
- * @author 
+ * @author
  * @since 2023-07-03
  */
 @Getter
@@ -20,6 +19,11 @@ import lombok.Setter;
 public class Sku extends BasePo {
 
     /**
+     * 类型 0非集团SKU 1集团SKU
+     */
+    private Integer type;
+
+    /**
      * 来源 1自定义添加 2万里牛
      */
     private Integer source;

+ 10 - 0
sd-business/src/main/java/com/sd/business/entity/sku/po/SkuSpec.java

@@ -41,6 +41,11 @@ public class SkuSpec extends BasePo {
     private String code;
 
     /**
+     * 条码
+     */
+    private String barCode;
+
+    /**
      * 品名
      */
     private String name;
@@ -80,4 +85,9 @@ public class SkuSpec extends BasePo {
      */
     private Long bomSpecId;
 
+    /**
+     * 描述
+     */
+    private String remark;
+
 }

+ 26 - 0
sd-business/src/main/java/com/sd/business/mapper/department/DepartmentMapper.java

@@ -0,0 +1,26 @@
+package com.sd.business.mapper.department;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.utils.wrapper.IWrapper;
+import com.sd.business.entity.department.po.Department;
+import com.sd.business.entity.department.vo.DepartmentVo;
+import org.apache.ibatis.annotations.Param;
+
+
+/**
+ * <p>
+ * 事业部 Mapper 接口
+ * </p>
+ *
+ * @author
+ * @since 2023-07-05
+ */
+public interface DepartmentMapper extends BaseMapper<Department> {
+
+    /**
+     * 事业部分页
+     */
+    Page<DepartmentVo> getPage(@Param("page") Page<Object> page, @Param("ew") IWrapper<Department> wrapper);
+
+}

+ 46 - 0
sd-business/src/main/java/com/sd/business/service/department/DepartmentService.java

@@ -0,0 +1,46 @@
+package com.sd.business.service.department;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.core.service.BaseService;
+import com.sd.business.entity.department.dto.DepartmentDto;
+import com.sd.business.entity.department.dto.DepartmentSelectDto;
+import com.sd.business.entity.department.po.Department;
+import com.sd.business.entity.department.vo.DepartmentVo;
+
+
+/**
+ * <p>
+ * 事业部 服务类
+ * </p>
+ *
+ * @author
+ * @since 2023-07-05
+ */
+public interface DepartmentService extends BaseService<Department> {
+
+    /**
+     * 事业部分页
+     */
+    Page<DepartmentVo> getPage(DepartmentSelectDto dto);
+
+    /**
+     * 事业部明细
+     */
+    DepartmentVo detail(Long id);
+
+    /**
+     * 事业部新增
+     */
+    void add(DepartmentDto departmentDto);
+
+    /**
+     * 事业部编辑
+     */
+    void edit(DepartmentDto departmentDto);
+
+    /**
+     * 事业部删除
+     */
+    void delete(Long id);
+
+}

+ 57 - 0
sd-business/src/main/java/com/sd/business/service/department/impl/DepartmentServiceImpl.java

@@ -0,0 +1,57 @@
+package com.sd.business.service.department.impl;
+
+import cn.hutool.core.bean.BeanUtil;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.wrapper.IWrapper;
+import com.sd.business.entity.department.dto.DepartmentDto;
+import com.sd.business.entity.department.dto.DepartmentSelectDto;
+import com.sd.business.entity.department.po.Department;
+import com.sd.business.entity.department.vo.DepartmentVo;
+import com.sd.business.mapper.department.DepartmentMapper;
+import com.sd.business.service.department.DepartmentService;
+import org.springframework.stereotype.Service;
+
+
+/**
+ * <p>
+ * 事业部 服务实现类
+ * </p>
+ *
+ * @author
+ * @since 2023-07-05
+ */
+@Service
+public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
+
+    @Override
+    public Page<DepartmentVo> getPage(DepartmentSelectDto dto) {
+        IWrapper<Department> wrapper = getWrapper();
+        wrapper.orderByDesc("d", Department::getId);
+        Page<DepartmentVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
+        return page;
+    }
+
+    @Override
+    public DepartmentVo detail(Long id) {
+        Department Department = this.getById(id);
+        DepartmentVo result = BeanUtil.toBean(Department, DepartmentVo.class);
+        return result;
+    }
+
+    @Override
+    public void add(DepartmentDto departmentDto) {
+        this.save(departmentDto);
+    }
+
+    @Override
+    public void edit(DepartmentDto departmentDto) {
+        this.updateById(departmentDto);
+    }
+
+    @Override
+    public void delete(Long id) {
+        this.removeById(id);
+    }
+
+}

+ 29 - 0
sd-business/src/main/resources/mapper/department/DepartmentMapper.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sd.business.mapper.department.DepartmentMapper">
+    <select id="getPage" resultType="com.sd.business.entity.department.vo.DepartmentVo">
+        select d.id,
+               d.admin_username,
+               d.admin_password,
+               d.name,
+               d.telephone,
+               d.address,
+               d.contact_person,
+               d.contact_number,
+               d.selling_price_system,
+               d.order_mode,
+               d.commission_rule,
+               d.status,
+               d.line_credit,
+               d.used_credit,
+               d.wln_warehouse_coding,
+               d.wln_brand,
+               d.create_user,
+               d.create_time,
+               d.update_user,
+               d.update_time
+        from department d
+            ${ew.customSqlSegment}
+    </select>
+
+</mapper>

+ 39 - 0
sd-wln/src/main/java/com/sd/wln/entity/GoodsSpecParam.java

@@ -0,0 +1,39 @@
+
+package com.sd.wln.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 商品分类参数实体类
+ */
+@Getter
+@Setter
+public class GoodsSpecParam extends Signature {
+
+    /**
+     * 规格编码
+     */
+    private String spec_code;
+
+    /**
+     * 商品编码
+     */
+    private String item_code;
+
+    /**
+     * 规格修改时间
+     */
+    private Long modify_time;
+
+    /**
+     * 条码
+     */
+    private String bar_code;
+
+    /**
+     * 是否需要查询商品自定义属性(默认不传为false)
+     */
+    private String need_properties;
+
+}

+ 1 - 1
sd-wln/src/main/java/com/sd/wln/entity/Signature.java

@@ -76,7 +76,7 @@ public class Signature implements Serializable {
     /**
      * 生成签名
      */
-    public void generate(String param) {
+    public void generateSign(String param) {
         long time = System.currentTimeMillis();
         String str = WlnConstant.APP_SECRET
                 + "_app=" + WlnConstant.APP_KEY + "&"

+ 8 - 1
sd-wln/src/main/java/com/sd/wln/scheduled/WlnSyncTask.java

@@ -18,7 +18,14 @@ public class WlnSyncTask {
     @Scheduled(cron = "0/1 * * * * ?")
     public void syncSku() {
         for (int i = 0; i < 3; i++) {
-            boolean flag = wlnSkuService.SyncSkuClassify();
+            boolean flag = wlnSkuService.syncSkuClassify();
+            if (flag) {
+                break;
+            }
+        }
+
+        for (int i = 0; i < 3; i++) {
+            boolean flag = wlnSkuService.syncSku();
             if (flag) {
                 break;
             }

+ 9 - 1
sd-wln/src/main/java/com/sd/wln/service/WlnSkuService.java

@@ -2,6 +2,14 @@ package com.sd.wln.service;
 
 public interface WlnSkuService {
 
-    boolean SyncSkuClassify();
+    /**
+     * 同步sku分类
+     */
+    boolean syncSkuClassify();
+
+    /**
+     * 同步sku
+     */
+    boolean syncSku();
 
 }

+ 128 - 15
sd-wln/src/main/java/com/sd/wln/service/impl/WlnSkuServiceImpl.java

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

+ 22 - 7
sd-wln/src/main/java/com/sd/wln/util/WlnUtil.java

@@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.MapUtil;
 import com.sd.wln.constants.WlnConstant;
+import com.sd.wln.entity.GoodsSpecParam;
 import com.sd.wln.entity.SkuClassifyParam;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -23,27 +24,41 @@ public class WlnUtil {
     // 万里牛路径前缀
     private static final String PREFIX = "https://open-api.hupun.com/api/";
 
-    public static List<JSONObject> getSkuClassifyList(Integer page, Integer limit) throws Exception {
-
+    public static List<JSONObject> getSkuClassifyList() throws Exception {
         SkuClassifyParam param = new SkuClassifyParam();
-        param.setPage(page);
-        param.setLimit(limit);
         param.setCom_uid(WlnConstant.COM_UID);
-
         String linkString = MapUtil.createLinkString(MapUtil.beanToMap(param));
-        param.generate(linkString);
+        param.generateSign(linkString);
 
         String result = sendPost(PREFIX + "/erp/goods/catagory/query", param);
-
         JSONObject json = JSONObject.parseObject(result);
         Integer code = json.getInteger("code");
         if (code != 0) {
             throw new ServiceException(result);
         }
+        return json.getJSONArray("data").toJavaList(JSONObject.class);
+    }
 
+    public static List<JSONObject> getSkuList(Integer page, Integer limit) throws Exception {
+        GoodsSpecParam param = new GoodsSpecParam();
+        param.setPage(page);
+        param.setLimit(limit);
+        param.setModify_time(965381503L);
+        param.generateSign(MapUtil.createLinkString(MapUtil.beanToMap(param)));
+        String result = sendPost(PREFIX + "/erp/goods/spec/open/query/goodswithspeclist", MapUtil.beanToMap(param));
+        JSONObject json = JSONObject.parseObject(result);
+        Integer code = json.getInteger("code");
+        if (code != 0) {
+            throw new ServiceException(result);
+        }
         return json.getJSONArray("data").toJavaList(JSONObject.class);
     }
 
+    // public static void main(String[] args) throws Exception {
+    //     List<JSONObject> skuList = getSkuList(1, 200);
+    //     // List<JSONObject> skuClassifyList = getSkuClassifyList();
+    //     System.out.println();
+    // }
 
     /**
      * post请求