Эх сурвалжийг харах

个人产品库excel批量导入物料逻辑修改,分批上传。

dongbw 8 жил өмнө
parent
commit
b2e35e998f

+ 186 - 177
src/main/java/com/uas/platform/b2b/service/impl/ProductUsersServiceImpl.java

@@ -59,6 +59,11 @@ public class ProductUsersServiceImpl implements ProductUsersService {
     @Autowired
     private ProductStatusDao productStatusDao;
 
+    /**
+     * 物料单次请求数据量大小
+     */
+    private static final Double ONE_TIME_DATA_SIZE = 1000d;
+
     @Override
     public SPage<ProductUsers> findProductInfoByPageInfo(PageInfo pageInfo) {
         Page<ProductUsers> products = findAllProudctByPageInfo(pageInfo);
@@ -166,214 +171,218 @@ public class ProductUsersServiceImpl implements ProductUsersService {
         List<Product> products = new ArrayList<>();
         Long enuu = SystemSession.getUser().getEnterprise().getUu();
         Long useruu = SystemSession.getUser().getUserUU();
+        // 生成随机物料编码
+        StringBuilder codeString = new StringBuilder();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmsssss");
+        codeString.append("PR").append(sdf.format(new Date()));
         Sheet sheet = workbook.getSheetAt(0);
         int rowNum = sheet.getLastRowNum();
         Row headerRow = sheet.getRow(0);
         int total = 0;
         if (headerRow != null) {
-            for (int r = 3; r <= rowNum; r++) {
-                Row row = sheet.getRow(r);
-                if (row != null) {
-                    total++;
-                    Product product = new Product();
-                    // 物料编号
-                    if (row.getCell(0) != null) {
-                        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
-                        product.setCode(row.getCell(0).getStringCellValue().trim());
-                    }
-
-                    // 物料名称
-                    if (row.getCell(1) != null && row.getCell(1).getCellType() != Cell.CELL_TYPE_BLANK) {
-                        row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
-                        product.setTitle(row.getCell(1).getStringCellValue().trim());
-                    } else {
-                        modelMap.put("error", "第" + (r + 1) + "行物料名称未填写");
-                        return modelMap;
-                    }
-
-                    // 物料规格
-                    if (row.getCell(2) != null && row.getCell(2).getCellType() != Cell.CELL_TYPE_BLANK) {
-                        row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
-                        product.setSpec(row.getCell(2).getStringCellValue().trim());
-                    } else {
-                        modelMap.put("error", "第" + (r + 1) + "行物料规格未填写");
-                        return modelMap;
-                    }
+            // 根据每次发送数据量,计算发送几次
+            int times = (int) Math.ceil((rowNum - 2) / ONE_TIME_DATA_SIZE);
+            int r = 3;
+            for (int i = 1; i <= times; i++) {
+                for (; r <= (rowNum <= i * ONE_TIME_DATA_SIZE ? rowNum : i * ONE_TIME_DATA_SIZE); r++) {
+                    Row row = sheet.getRow(r);
+                    if (row != null) {
+                        total++;
+                        Product product = new Product();
+                        // 物料编号
+                        if (row.getCell(0) != null && row.getCell(1).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
+                            product.setCode(row.getCell(0).getStringCellValue().trim());
+                        } else {
+                            product.setCode(codeString.toString() + (r - 2));
+                        }
 
-                    // 原厂型号
-                    if (row.getCell(3) != null && row.getCell(3).getCellType() != Cell.CELL_TYPE_BLANK) {
-                        row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
-                        product.setCmpCode(row.getCell(3).getStringCellValue().trim());
-                    } else {
-                        modelMap.put("error", "第" + (r + 1) + "行物料型号未填写");
-                        return modelMap;
-                    }
+                        // 物料名称
+                        if (row.getCell(1) != null && row.getCell(1).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
+                            product.setTitle(row.getCell(1).getStringCellValue().trim());
+                        } else {
+                            modelMap.put("error", "第" + (r + 1) + "行物料名称未填写");
+                            return modelMap;
+                        }
 
-                    // 品牌
-                    if (row.getCell(4) != null && row.getCell(4).getCellType() != Cell.CELL_TYPE_BLANK) {
-                        row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
-                        product.setBrand(row.getCell(4).getStringCellValue().trim());
-                    } else {
-                        modelMap.put("error", "第" + (r + 1) + "行品牌未填写");
-                        return modelMap;
-                    }
+                        // 物料规格
+                        if (row.getCell(2) != null && row.getCell(2).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
+                            product.setSpec(row.getCell(2).getStringCellValue().trim());
+                        } else {
+                            modelMap.put("error", "第" + (r + 1) + "行物料规格未填写");
+                            return modelMap;
+                        }
 
-                    // 单位
-                    if (row.getCell(5) != null && row.getCell(5).getCellType() != Cell.CELL_TYPE_BLANK) {
-                        row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
-                        if (null != row.getCell(5).getStringCellValue()
-                                && !row.getCell(5).getStringCellValue().trim().equals("")) {
-                            product.setUnit(row.getCell(5).getStringCellValue());
+                        // 原厂型号
+                        if (row.getCell(3) != null && row.getCell(3).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
+                            product.setCmpCode(row.getCell(3).getStringCellValue().trim());
+                        } else {
+                            modelMap.put("error", "第" + (r + 1) + "行物料型号未填写");
+                            return modelMap;
                         }
-                    } else {
-                        modelMap.put("error", "第" + (r + 1) + "行单位未填写");
-                        return modelMap;
-                    }
-                    // 最小包装量
-                    if (row.getCell(6) != null) {
-                        row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
-                        if (null != row.getCell(6).getStringCellValue().trim()
-                                && !row.getCell(6).getStringCellValue().trim().equals("")) {
-                            product.setMinPack(Double.valueOf(row.getCell(6).getStringCellValue()));
+
+                        // 品牌
+                        if (row.getCell(4) != null && row.getCell(4).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
+                            product.setBrand(row.getCell(4).getStringCellValue().trim());
+                        } else {
+                            modelMap.put("error", "第" + (r + 1) + "行品牌未填写");
+                            return modelMap;
                         }
-                    }
-                    // 最小订购量
-                    if (row.getCell(7) != null) {
-                        row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
-                        if (null != row.getCell(7).getStringCellValue().trim()
-                                && !row.getCell(7).getStringCellValue().trim().equals("")) {
-                            product.setMinOrder(Double.valueOf(row.getCell(7).getStringCellValue()));
+
+                        // 单位
+                        if (row.getCell(5) != null && row.getCell(5).getCellType() != Cell.CELL_TYPE_BLANK) {
+                            row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
+                            if (null != row.getCell(5).getStringCellValue()
+                                    && !row.getCell(5).getStringCellValue().trim().equals("")) {
+                                product.setUnit(row.getCell(5).getStringCellValue());
+                            }
+                        } else {
+                            modelMap.put("error", "第" + (r + 1) + "行单位未填写");
+                            return modelMap;
                         }
-                    }
-                    // 交货周期
-                    if (row.getCell(8) != null) {
-                        row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
-                        if (null != row.getCell(8).getStringCellValue().trim()
-                                && !row.getCell(8).getStringCellValue().trim().equals("")) {
-                            product.setLeadtime(Double.valueOf(row.getCell(8).getStringCellValue()));
+                        // 最小包装量
+                        if (row.getCell(6) != null) {
+                            row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
+                            if (null != row.getCell(6).getStringCellValue().trim()
+                                    && !row.getCell(6).getStringCellValue().trim().equals("")) {
+                                product.setMinPack(Double.valueOf(row.getCell(6).getStringCellValue()));
+                            }
                         }
-                    }
-                    // 交货提前期
-                    if (row.getCell(9) != null) {
-                        row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
-                        if (null != row.getCell(9).getStringCellValue().trim()
-                                && !row.getCell(9).getStringCellValue().trim().equals("")) {
-                            product.setLtinstock(Double.valueOf(row.getCell(9).getStringCellValue()));
+                        // 最小订购量
+                        if (row.getCell(7) != null) {
+                            row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
+                            if (null != row.getCell(7).getStringCellValue().trim()
+                                    && !row.getCell(7).getStringCellValue().trim().equals("")) {
+                                product.setMinOrder(Double.valueOf(row.getCell(7).getStringCellValue()));
+                            }
                         }
-                    }
-                    // 判断物料编号是否存在
-                    if (product.getCode() == null) {
-                        // 生成随机物料编码
-                        SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
-                        String code = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
-                        product.setCode(code);
-                    }
-                    List<ProductUsers> existProds = productUsersDao.findByEnuuAndUseruuAndTitleAndCmpCodeAndBrand
-                            (enuu, useruu, product.getTitle(), product.getCmpCode(), product.getBrand());
-                    if(CollectionUtils.isEmpty(existProds)) {
-                        List<Product> prods = productDao.findByEnUUAndCode(enuu, product.getCode());
-                        if (CollectionUtils.isEmpty(prods)) {
-                            prods = productDao.findByTitleAndCmpCodeAndBrandAndEnUU(
-                                    product.getTitle(), product.getCmpCode(), product.getBrand(), enuu);
+                        // 交货周期
+                        if (row.getCell(8) != null) {
+                            row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
+                            if (null != row.getCell(8).getStringCellValue().trim()
+                                    && !row.getCell(8).getStringCellValue().trim().equals("")) {
+                                product.setLeadtime(Double.valueOf(row.getCell(8).getStringCellValue()));
+                            }
                         }
-                        if(CollectionUtils.isEmpty(prods)) {// 如果非标不存在再查询标准
-                            prods = productDao.findByTitleAndPcmpcodeAndPbrandAndEnUU(product.getTitle(), product.getCmpCode(), product.getBrand(), SystemSession.getUser().getEnterprise().getUu());
+                        // 交货提前期
+                        if (row.getCell(9) != null) {
+                            row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
+                            if (null != row.getCell(9).getStringCellValue().trim()
+                                    && !row.getCell(9).getStringCellValue().trim().equals("")) {
+                                product.setLtinstock(Double.valueOf(row.getCell(9).getStringCellValue()));
+                            }
                         }
-                        if (CollectionUtils.isEmpty(prods)) {
-                            // 如果物料不存在,先生成物料信息
-                            product.setEnUU(SystemSession.getUser().getEnterprise().getUu());
-                            product.setUserUU(SystemSession.getUser().getUserUU());
-                            product.setSourceApp("B2B");
-                            product.setIsPubsale(Constant.NO);
-                            product.setIsShow(Constant.NO);
-                            product.setStandard(Constant.NO);
-                            product.setCreateTime(new Date());
-                            List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
-                            if(!CollectionUtils.isEmpty(roles)) {
-                                for(Role role : roles) {
-                                    if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
-                                        product.setIsSale(Constant.YES);
-                                    } else if(role.getDesc().equals("采购员")) {
-                                        product.setIsPurchase(Constant.YES);
+                        List<ProductUsers> existProds = productUsersDao.findByEnuuAndUseruuAndTitleAndCmpCodeAndBrand
+                                (enuu, useruu, product.getTitle(), product.getCmpCode(), product.getBrand());
+                        if(CollectionUtils.isEmpty(existProds)) {
+                            List<Product> prods = productDao.findByEnUUAndCode(enuu, product.getCode());
+                            if (CollectionUtils.isEmpty(prods)) {
+                                prods = productDao.findByTitleAndCmpCodeAndBrandAndEnUU(
+                                        product.getTitle(), product.getCmpCode(), product.getBrand(), enuu);
+                            }
+                            if(CollectionUtils.isEmpty(prods)) {// 如果非标不存在再查询标准
+                                prods = productDao.findByTitleAndPcmpcodeAndPbrandAndEnUU(product.getTitle(), product.getCmpCode(), product.getBrand(), SystemSession.getUser().getEnterprise().getUu());
+                            }
+                            if (CollectionUtils.isEmpty(prods)) {
+                                // 如果物料不存在,先生成物料信息
+                                product.setEnUU(SystemSession.getUser().getEnterprise().getUu());
+                                product.setUserUU(SystemSession.getUser().getUserUU());
+                                product.setSourceApp("B2B");
+                                product.setIsPubsale(Constant.NO);
+                                product.setIsShow(Constant.NO);
+                                product.setStandard(Constant.NO);
+                                product.setCreateTime(new Date());
+                                List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
+                                if(!CollectionUtils.isEmpty(roles)) {
+                                    for(Role role : roles) {
+                                        if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
+                                            product.setIsSale(Constant.YES);
+                                        } else if(role.getDesc().equals("采购员")) {
+                                            product.setIsPurchase(Constant.YES);
+                                        }
                                     }
                                 }
-                            }
-                            products.add(product);
-                        } else {
-                            // 已存在,直接更新
-                            Product oldProd = prods.get(0);
-                            oldProd.setCode(product.getCode());
-                            oldProd.setCmpCode(product.getCmpCode());
-                            oldProd.setBrand(product.getBrand());
-                            oldProd.setTitle(product.getTitle());
-                            oldProd.setSpec(product.getSpec());
-                            oldProd.setUnit(product.getUnit());
-                            oldProd.setMinOrder(product.getMinOrder());
-                            oldProd.setMinPack(product.getMinPack());
-                            oldProd.setLeadtime(product.getLeadtime());
-                            oldProd.setLtinstock(product.getLtinstock());
-                            List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
-                            if(!CollectionUtils.isEmpty(roles)) {
-                                for(Role role : roles) {
-                                    if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
-                                        oldProd.setIsSale(Constant.YES);
-                                    } else if(role.getDesc().equals("采购员")) {
-                                        oldProd.setIsPurchase(Constant.YES);
+                                products.add(product);
+                            } else {
+                                // 已存在,直接更新
+                                Product oldProd = prods.get(0);
+                                oldProd.setCode(product.getCode());
+                                oldProd.setCmpCode(product.getCmpCode());
+                                oldProd.setBrand(product.getBrand());
+                                oldProd.setTitle(product.getTitle());
+                                oldProd.setSpec(product.getSpec());
+                                oldProd.setUnit(product.getUnit());
+                                oldProd.setMinOrder(product.getMinOrder());
+                                oldProd.setMinPack(product.getMinPack());
+                                oldProd.setLeadtime(product.getLeadtime());
+                                oldProd.setLtinstock(product.getLtinstock());
+                                List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
+                                if(!CollectionUtils.isEmpty(roles)) {
+                                    for(Role role : roles) {
+                                        if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
+                                            oldProd.setIsSale(Constant.YES);
+                                        } else if(role.getDesc().equals("采购员")) {
+                                            oldProd.setIsPurchase(Constant.YES);
+                                        }
                                     }
                                 }
+                                products.add(oldProd);
+                                // 如果已存在,且被禁用,修改为可用
+                                List<ProductStatus> productStatuses = productStatusDao.findByPrIdAndB2bDisabled(oldProd.getId(), Constant.YES);
+                                if (!CollectionUtils.isEmpty(productStatuses)) {
+                                    ProductStatus status = productStatuses.get(0);
+                                    status.setB2bDisabled(Constant.NO);
+                                    productStatusDao.save(status);
+                                }
+                                existedProducts.add(product.getTitle());
                             }
-                            products.add(oldProd);
-                            // 如果已存在,且被禁用,修改为可用
-                            List<ProductStatus> productStatuses = productStatusDao.findByPrIdAndB2bDisabled(oldProd.getId(), Constant.YES);
-                            if (!CollectionUtils.isEmpty(productStatuses)) {
-                                ProductStatus status = productStatuses.get(0);
-                                status.setB2bDisabled(Constant.NO);
-                                productStatusDao.save(status);
-                            }
-                            existedProducts.add(product.getCode());
-                        }
 //                        productUsers.add(prod);
-                    } else {
-	                    alters.add(existProds.get(0).getProduct().getTitle());
+                        } else {
+                            alters.add(existProds.get(0).getProduct().getTitle());
+                        }
                     }
                 }
-            }
-            modelMap.put("total", total);
-            if (!CollectionUtils.isEmpty(products)) {
-                try {
-                    List<Long> idList = ProductUtils.update(products);
-                    if (!CollectionUtils.isEmpty(idList)) {
-                        for (Long id: idList) {
-                            ProductUsers prodUser = productUsersDao.findByEnuuAndUseruuAndPrid(enuu, useruu, id);
-                            if (null == prodUser) {
-                                ProductUsers prod = new ProductUsers();
-                                prod.setDate(new Date());
-                                prod.setEnuu(enuu);
-                                prod.setPrid(id);
-                                prod.setUseruu(useruu);
-                                productUsers.add(prod);
+                modelMap.put("total", total);
+                if (!CollectionUtils.isEmpty(products)) {
+                    try {
+                        List<Long> idList = ProductUtils.update(products);
+                        if (!CollectionUtils.isEmpty(idList)) {
+                            for (Long id: idList) {
+                                ProductUsers prodUser = productUsersDao.findByEnuuAndUseruuAndPrid(enuu, useruu, id);
+                                if (null == prodUser) {
+                                    ProductUsers prod = new ProductUsers();
+                                    prod.setDate(new Date());
+                                    prod.setEnuu(enuu);
+                                    prod.setPrid(id);
+                                    prod.setUseruu(useruu);
+                                    productUsers.add(prod);
+                                }
+                            }
+                            if (!CollectionUtils.isEmpty(productUsers)) {
+                                ProductUtils.updateProductUsersByBatch(productUsers);
+                                modelMap.put("success", productUsers.size());
                             }
                         }
-                        if (!CollectionUtils.isEmpty(productUsers)) {
-                            ProductUtils.updateProductUsersByBatch(productUsers);
-                            modelMap.put("success", productUsers.size());
+                        if(alters.size() > 0)
+                            modelMap.put("alters", alters);
+                        if (existedProducts.size() > 0) {
+                            modelMap.put("existed", existedProducts);
                         }
+                    } catch (Exception e) {
+                        modelMap.put("error", "物料添加失败");
                     }
-                    if(alters.size() > 0)
-                    	modelMap.put("alters", alters);
+                } else {
+                    modelMap.put("success", 0);
                     if (existedProducts.size() > 0) {
                         modelMap.put("existed", existedProducts);
                     }
-                } catch (Exception e) {
-                    modelMap.put("error", "物料添加失败");
-                }
-            } else {
-                modelMap.put("success", 0);
-                if (existedProducts.size() > 0) {
-                    modelMap.put("existed", existedProducts);
+                    if(alters.size() > 0)
+                        modelMap.put("alters", alters);
                 }
-	            if(alters.size() > 0)
-		            modelMap.put("alters", alters);
             }
         }
         return modelMap;