Browse Source

增加商城平台物料上传到ERP的功能

yujia 6 years ago
parent
commit
d1ade73be7

+ 2 - 1
gradle.properties

@@ -1,3 +1,4 @@
 org.gradle.jvmargs=-Xmx1024m
 fastjsonVersion=1.2.35
-mallApiVersion=0.0.2-SNAPSHOT
+mallApiVersion=0.0.5-SNAPSHOT
+platformVersion=0.0.1-SNAPSHOT

+ 3 - 3
src/main/java/com/uas/erp/schedular/b2b/domain/AcceptNotify.java

@@ -1,13 +1,13 @@
 package com.uas.erp.schedular.b2b.domain;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.uas.erp.schedular.core.Status;
 import com.uas.erp.schedular.util.StringUtil;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * 买家ERP的收料通知单
  */

+ 14 - 0
src/main/java/com/uas/erp/schedular/mall/constant/StringConstant.java

@@ -0,0 +1,14 @@
+package com.uas.erp.schedular.mall.constant;
+
+/**
+ * description
+ *
+ * @author yuj 2018-05-17 10:04
+ */
+public class StringConstant {
+
+
+    public static String CMP_TYPE = "COMPONENT";
+
+    public static String PRODUCT_TYPE = "PRODUCT";
+}

+ 3 - 0
src/main/java/com/uas/erp/schedular/mall/domain/Prod.java

@@ -176,4 +176,7 @@ public class Prod extends KeyEntity{
 		this.pr_cop = pr_cop;
 		return this;
 	}
+
+	public Prod() {
+	}
 }

+ 230 - 3
src/main/java/com/uas/erp/schedular/mall/task/ProductTask.java

@@ -1,8 +1,19 @@
 package com.uas.erp.schedular.mall.task;
 
+import com.uas.api.b2c_erp.baisc.service.ComponentService;
+import com.uas.api.b2c_erp.seller.model.Prod;
+import com.uas.api.b2c_erp.seller.service.ProdService;
+import com.uas.erp.schedular.mall.constant.StringConstant;
 import com.uas.erp.schedular.task.support.Method;
 import com.uas.erp.schedular.task.support.TaskMapping;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * 商城同步物料信息
@@ -13,8 +24,224 @@ import org.springframework.stereotype.Component;
 @TaskMapping(title = "商城物料")
 public class ProductTask extends AbstractTask {
 
-    @TaskMapping(title = "根据原厂型号获取产品", cron = "*/5 * * * * ?", method = Method.DOWNLOAD)
-    public void updateMallProduct() {
-        System.err.println("sffffasfsf");
+    @Autowired
+    private ProdService prodService;
+
+    @Autowired
+    private ComponentService componentService;
+
+    @TaskMapping(title = "将平台物料信息复制商城物料库", cron = "0 0 12 * * ?", method = Method.DOWNLOAD)
+    public void importMallProductToErpProduct() {
+        int i = 1;
+        List<Prod> prods = prodService.findNewProdUploadMallERP(i);
+//        while (i <= totalPages) {
+//            List<String> sqls = new ArrayList<>();
+//            for (com.uas.api.b2c_erp.seller.model.Prod prod : prods) {
+//                String sqlString = prod.toSqlString();
+//                String sql = sqlString.replaceAll("'null'", "''");
+//                sqls.add(sql);
+//            }
+//            List<String> list = batchSaveProd(sqls, StringConstant.PRODUCT_TYPE);
+//            if (!CollectionUtils.isEmpty(list)) {
+//                prodService.updateNewProdDowned(list);
+//            }
+//            prods = prodService.findNewProdUploadMallERP(i);
+//            int nextTotalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//            if (totalPages == nextTotalPages) {
+//                i++;
+//            } else {
+//                i = 1;
+//                totalPages = nextTotalPages;
+//            }
+//            System.out.println(i + "------------ importMallProductToErpProduct" + totalPages);
+//        }
+    }
+
+    @TaskMapping(title = "将平台器件信息复制商城物料库", cron = "*/5 * * * * ?", method = Method.DOWNLOAD)
+    public void importMallComponentToErpProduct() {
+//        int i = 1;
+//        List<com.uas.api.b2c_erp.seller.model.Prod> prods = componentService.findNewComponentUploadMallERP(i);
+//        int totalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//        while (i <= totalPages) {
+//            List<String> sqls = new ArrayList<>();
+//            for (com.uas.api.b2c_erp.seller.model.Prod prod : prods) {
+//                String sqlString = prod.toSqlString();
+//                String sql = sqlString.replaceAll("'null'", "''");
+//                sqls.add(sql);
+//            }
+//            List<String> list = batchSaveProd(sqls, StringConstant.CMP_TYPE);
+//            if (!CollectionUtils.isEmpty(list)) {
+//                componentService.updateNewComponentDowned(list);
+//            }
+//            prods = componentService.findNewComponentUploadMallERP(i);
+//            int nextTotalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//            if (totalPages == nextTotalPages) {
+//                i++;
+//            } else {
+//                i = 1;
+//                totalPages = nextTotalPages;
+//            }
+//            System.out.println(i + "------------importMallComponentToErpProduct " + totalPages);
+//        }
+    }
+
+        /**
+         * 解析物料编号
+         * @param sqls
+         * @return
+         */
+        public List<String> analyzeMaterialNum(List<String> sqls) {
+            List<String> list = new ArrayList<>();
+            if (!CollectionUtils.isEmpty(sqls)) {
+                for (String sql : sqls) {
+                    String substring = sql.substring(220);
+                    String[] split = substring.split("'");
+                    if (!StringUtils.isEmpty(split[0])) {
+                        list.add(split[0]);
+                    }
+                }
+            }
+            return list;
+        }
+
+        /**
+         * 解析型号和品牌,用中划线链接
+         * @param sqls
+         * @return
+         */
+        public List<String> analyzeMaterialCodeAndBrand(List<String> sqls) {
+            List<String> list = new ArrayList<>();
+            if (!CollectionUtils.isEmpty(sqls)) {
+                for (String sql : sqls) {
+                    String code = analyzeMaterialCode(sql);
+                    String brand = analyzeMaterialBrand(sql);
+                    list.add(code + "-----" + brand);
+                }
+            }
+            return list;
+        }
+
+
+        /**
+         * 解析出型号
+         * @param sql 拼接的插入数据库
+         * @return
+         */
+    public String analyzeMaterialCode(String sql) {
+        if (!StringUtils.isEmpty(sql)) {
+            String[] split = sql.split("','已审核','余佳','");
+            String first = split[0];
+            if (!StringUtils.isEmpty(first)) {
+                String[] split1 = first.split("'");
+                return split1[split1.length - 1];
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+
+    /**
+     * 解析品牌
+     * @param sql 拼接的插入数据库
+     * @return
+     */
+    public String analyzeMaterialBrand(String sql) {
+        if (!StringUtils.isEmpty(sql)) {
+            String[] split = sql.split("','已审核','余佳','");
+            String first = split[split.length - 1];
+            if (!StringUtils.isEmpty(first)) {
+                String[] split1 = first.split("'");
+                return split1[0];
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+
+    /**
+     * 拆分批量保存
+     * @param sqls
+     * @return
+     */
+    public List<String> splitList(List<String> sqls, String type) {
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(sqls)) {
+            if (sqls.size() < 2) {
+                try {
+                    if (sqls.size() > 0) {
+                        jdbcTemplate.execute(sqls.get(0));
+                        List<String> list1 = null;
+                        if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                            list1 = analyzeMaterialCodeAndBrand(sqls);
+                        } else {
+                            list1 = analyzeMaterialNum(sqls);
+                        }
+                        list.addAll(list1);
+                    }
+                } catch (Exception e) {
+                    String message = e.getMessage();
+                    if (!StringUtils.isEmpty(message)) {
+                        boolean brand_orispeccode = message.contains("PRODUCT_BRAND_ORISPECCODE");
+                        if (brand_orispeccode) {
+                            List<String> list1 = null;
+                            if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                                list1 = analyzeMaterialCodeAndBrand(sqls);
+                            } else {
+                                list1 = analyzeMaterialNum(sqls);
+                            }
+                            list.addAll(list1);
+                        }
+                    }
+                    e.printStackTrace();
+                }
+            } else {
+                List<String> sql1 = new ArrayList<>();
+                List<String> sql2 = new ArrayList<>();
+                int size = sqls.size()/2;
+                for (int i = 0; i < sqls.size(); i++) {
+                    if (i < size) {
+                        sql1.add(sqls.get(i));
+                    } else {
+                        sql2.add(sqls.get(i));
+                    }
+                }
+                List<String> list3 = batchSaveProd(sql1, type);
+                if (list3 != null) {
+                    list.addAll(list3);
+                }
+                List<String> list4 = batchSaveProd(sql2, type);
+                if (list4 != null) {
+                    list.addAll(list4);
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * 批量保存物料信息
+     * @param sqls 需要保存的信息
+     * @return 以保存的物料编号
+     */
+    public List<String> batchSaveProd(List<String> sqls, String type) {
+        List<String> list = null;
+        try {
+            jdbcTemplate.batchExecute(sqls);
+            if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                list = analyzeMaterialCodeAndBrand(sqls);
+            } else {
+                list = analyzeMaterialNum(sqls);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            list = splitList(sqls, type);
+        }
+        return list;
     }
 }

+ 275 - 0
src/test/java/com/uas/erp/test/ProdTest.java

@@ -0,0 +1,275 @@
+package com.uas.erp.test;
+
+import com.uas.api.b2c_erp.baisc.service.ComponentService;
+import com.uas.api.b2c_erp.seller.model.Prod;
+import com.uas.api.b2c_erp.seller.service.ProdService;
+import com.uas.api.crypto.util.SecretUtil;
+import com.uas.erp.schedular.UasSchedulingApplication;
+import com.uas.erp.schedular.entity.Master;
+import com.uas.erp.schedular.mall.constant.StringConstant;
+import com.uas.erp.schedular.mall.task.AbstractTask;
+import com.uas.erp.schedular.util.ContextHolder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * description
+ *
+ * @author yuj 2018-05-15 18:54
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = UasSchedulingApplication.class)
+public class ProdTest extends AbstractTask {
+
+    @Autowired
+    private ProdService prodService;
+
+    @Autowired
+    private ComponentService componentService;
+
+    @Test
+    public void uploadProduct() {
+        Master master = new Master();
+        master.setMa_user("USOFT_MALL");
+        master.setMa_uu(10042875L);
+        master.setMa_accesssecret("b8d3184fde5c444f87dc55c123f1a690");
+        master.setMa_env("test");
+        master.setMa_name("USOFT_MALL");
+        SecretUtil.set(Long.toString(master.getMa_uu()), master.getMa_accesssecret());
+        ContextHolder.setMaster(master);
+
+        int i = 0;
+        List<String> sqls = null;
+        List<Prod> prods = prodService.findNewProdUploadMallERP(i);
+        while (!CollectionUtils.isEmpty(prods)) {
+            sqls = new ArrayList<>();
+            for (com.uas.api.b2c_erp.seller.model.Prod prod : prods) {
+                String sqlString = prod.toSqlString();
+                String sql = sqlString.replaceAll("'null'", "''");
+                sqls.add(sql);
+            }
+            List<String> list = batchSaveProd(sqls, StringConstant.PRODUCT_TYPE);
+//            if (!CollectionUtils.isEmpty(list)) {
+//                prodService.updateNewProdDowned(list);
+//            }
+            prods = prodService.findNewProdUploadMallERP(i);
+            System.out.println(prods.size());
+        }
+//        int totalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//        while (i <= totalPages) {
+//            List<String> sqls = new ArrayList<>();
+//
+//
+//            int nextTotalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//            if (totalPages == nextTotalPages) {
+//                i++;
+//            } else {
+//                i = 1;
+//                totalPages = nextTotalPages;
+//            }
+//            System.out.println(i + "------------" + totalPages);
+    }
+
+    @Test
+    public void uploadProduct1() {
+        Master master = new Master();
+        master.setMa_user("USOFT_MALL");
+        master.setMa_uu(10042875L);
+        master.setMa_accesssecret("b8d3184fde5c444f87dc55c123f1a690");
+        master.setMa_env("test");
+        master.setMa_name("USOFT_MALL");
+        SecretUtil.set(Long.toString(master.getMa_uu()), master.getMa_accesssecret());
+        ContextHolder.setMaster(master);
+
+        int i = 1;
+        List<Prod> prods = componentService.findNewComponentUploadMallERP(i);
+//        int totalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+        while (!CollectionUtils.isEmpty(prods)) {
+            List<String> sqls = new ArrayList<>();
+            for (com.uas.api.b2c_erp.seller.model.Prod prod : prods) {
+                String sqlString = prod.toSqlString();
+                String sql = sqlString.replaceAll("'null'", "''");
+                sqls.add(sql);
+            }
+            List<String> list = batchSaveProd(sqls, StringConstant.CMP_TYPE);
+//            componentService.updateNewComponentDowned(list);
+            prods = componentService.findNewComponentUploadMallERP(i);
+//            int nextTotalPages = (int) Math.ceil((double) prods.getTotalElements() / 500);
+//            if (totalPages == nextTotalPages) {
+//                i++;
+//            } else {
+//                i = 1;
+//                totalPages = nextTotalPages;
+//            }
+//            System.out.println(i + "------------" + totalPages);
+        }
+    }
+
+    /**
+     * 拆分批量保存
+     * @param sqls
+     * @return
+     */
+    public List<String> splitList(List<String> sqls, String type) {
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(sqls)) {
+            if (sqls.size() < 2) {
+                try {
+                    if (sqls.size() > 0) {
+                        jdbcTemplate.execute(sqls.get(0));
+                        List<String> list1 = null;
+                        if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                            list1 = analyzeMaterialCodeAndBrand(sqls);
+                        } else {
+                            list1 = analyzeMaterialNum(sqls);
+                        }
+                        list.addAll(list1);
+                    }
+                } catch (Exception e) {
+                    String message = e.getMessage();
+                    if (!StringUtils.isEmpty(message)) {
+                        boolean brand_orispeccode = message.contains("PRODUCT_BRAND_ORISPECCODE");
+                        if (brand_orispeccode) {
+                            List<String> list1 = null;
+                            if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                                list1 = analyzeMaterialCodeAndBrand(sqls);
+                            } else {
+                                list1 = analyzeMaterialNum(sqls);
+                            }
+                            list.addAll(list1);
+                        }
+                    }
+                    e.printStackTrace();
+                }
+            } else {
+                List<String> sql1 = new ArrayList<>();
+                List<String> sql2 = new ArrayList<>();
+                int size = sqls.size()/2;
+                for (int i = 0; i < sqls.size(); i++) {
+                    if (i < size) {
+                        sql1.add(sqls.get(i));
+                    } else {
+                        sql2.add(sqls.get(i));
+                    }
+                }
+                List<String> list3 = batchSaveProd(sql1, type);
+                if (list3 != null) {
+                    list.addAll(list3);
+                }
+                List<String> list4 = batchSaveProd(sql2, type);
+                if (list4 != null) {
+                    list.addAll(list4);
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * 批量保存物料信息
+     * @param sqls 需要保存的信息
+     * @return 以保存的物料编号
+     */
+    public List<String> batchSaveProd(List<String> sqls, String type) {
+        List<String> list = null;
+        try {
+            jdbcTemplate.batchExecute(sqls);
+            if (type.equals(StringConstant.PRODUCT_TYPE)) {
+                list = analyzeMaterialCodeAndBrand(sqls);
+            } else {
+                list = analyzeMaterialNum(sqls);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            list = splitList(sqls, type);
+        }
+        return list;
+    }
+
+
+    /**
+     * 解析物料编号
+     * @param sqls
+     * @return
+     */
+    public List<String> analyzeMaterialNum(List<String> sqls) {
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(sqls)) {
+            for (String sql : sqls) {
+                String substring = sql.substring(220);
+                String[] split = substring.split("'");
+                if (!StringUtils.isEmpty(split[0])) {
+                    list.add(split[0]);
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * 解析型号和品牌,用中划线链接
+     * @param sqls
+     * @return
+     */
+    public List<String> analyzeMaterialCodeAndBrand(List<String> sqls) {
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(sqls)) {
+            for (String sql : sqls) {
+                String code = analyzeMaterialCode(sql);
+                String brand = analyzeMaterialBrand(sql);
+                list.add(code + "-----" + brand);
+            }
+        }
+        return list;
+    }
+
+
+    /**
+     * 解析出型号
+     * @param sql 拼接的插入数据库
+     * @return
+     */
+    public String analyzeMaterialCode(String sql) {
+        if (!StringUtils.isEmpty(sql)) {
+            String[] split = sql.split("','已审核','余佳','");
+            String first = split[0];
+            if (!StringUtils.isEmpty(first)) {
+                String[] split1 = first.split("'");
+                return split1[split1.length - 1];
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+
+    /**
+     * 解析品牌
+     * @param sql 拼接的插入数据库
+     * @return
+     */
+    public String analyzeMaterialBrand(String sql) {
+        if (!StringUtils.isEmpty(sql)) {
+            String[] split = sql.split("','已审核','余佳','");
+            String first = split[split.length - 1];
+            if (!StringUtils.isEmpty(first)) {
+                String[] split1 = first.split("'");
+                return split1[0];
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+}