Преглед изворни кода

采购物料、个人产品库批量导入方法修改及优化

dongbw пре 8 година
родитељ
комит
5121baf476

+ 8 - 3
src/main/java/com/uas/platform/b2b/ps/ProductUtils.java

@@ -36,8 +36,13 @@ public class ProductUtils {
      */
     public static List<Long> update(List<Product> products) throws Exception {
         String res = HttpUtil.doPost(PRODUCT_PUBLIC_SERVICE_URL + "/product/update/b2b", FlexJsonUtils.toJsonDeep(products));
-        if (null == res) {
-            throw new RuntimeException("更新物料失败");
+        if (null != res) {
+            try {
+                // 保存成功物料的id
+                return JSON.parseArray(res, Long.class);
+            } catch (Exception e) {
+                throw new RuntimeException("批量更新物料出现异常");
+            }
         }
         return null;
     }
@@ -61,7 +66,7 @@ public class ProductUtils {
                 return ids.get(0);
             }
         } else {
-            throw new RuntimeException("更新物料失败");
+            throw new RuntimeException("单个更新物料异常");
         }
         return null;
     }

+ 41 - 28
src/main/java/com/uas/platform/b2b/service/impl/ProductUsersServiceImpl.java

@@ -163,6 +163,7 @@ public class ProductUsersServiceImpl implements ProductUsersService {
         List<ProductUsers> productUsers = new ArrayList<ProductUsers>();
         List<String> alters = new ArrayList<String>();
         List<String> existedProducts = new ArrayList<>();
+        List<Product> products = new ArrayList<>();
         Long enuu = SystemSession.getUser().getEnterprise().getUu();
         Long useruu = SystemSession.getUser().getUserUU();
         Sheet sheet = workbook.getSheetAt(0);
@@ -175,7 +176,6 @@ public class ProductUsersServiceImpl implements ProductUsersService {
                 if (row != null && row.getCell(0) != null && row.getCell(0).getCellType() != Cell.CELL_TYPE_BLANK) {
                     total++;
                     Product product = new Product();
-                    ProductUsers prod = new ProductUsers();
                     // 物料编号
                     if (row.getCell(0) != null) {
                         row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
@@ -247,16 +247,7 @@ public class ProductUsersServiceImpl implements ProductUsersService {
                         }
                     }
                     // 判断物料编号是否存在
-                    if(product.getCode() != null ) {
-                        List<Product> prods = productDao.findByEnUUAndCode(enuu, product.getCode());
-                        if(prods.size() > 0) {
-                            // 生成随机物料编码
-                            SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
-                            String code = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
-                            product.setCode(code);
-                        }
-
-                    } else {
+                    if (product.getCode() == null) {
                         // 生成随机物料编码
                         SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
                         String code = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
@@ -292,11 +283,7 @@ public class ProductUsersServiceImpl implements ProductUsersService {
                                     }
                                 }
                             }
-                            Long proId = ProductUtils.updateOne(product);
-                            prod.setDate(new Date());
-                            prod.setEnuu(enuu);
-                            prod.setPrid(proId);
-                            prod.setUseruu(useruu);
+                            products.add(product);
                         } else {
                             // 已存在,直接更新
                             Product oldProd = prods.get(0);
@@ -304,16 +291,23 @@ public class ProductUsersServiceImpl implements ProductUsersService {
                             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());
-                            ProductUtils.updateOne(oldProd);
-                            prod.setDate(new Date());
-                            prod.setEnuu(enuu);
-                            prod.setPrid(oldProd.getId());
-                            prod.setUseruu(useruu);
+                            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)) {
@@ -321,20 +315,35 @@ public class ProductUsersServiceImpl implements ProductUsersService {
                                 status.setB2bDisabled(Constant.NO);
                                 productStatusDao.save(status);
                             }
-                            existedProducts.add(prods.get(0).getCode());
+                            existedProducts.add(product.getCode());
                         }
-                        productUsers.add(prod);
+//                        productUsers.add(prod);
                     } else {
 	                    alters.add(existProds.get(0).getProduct().getTitle());
                     }
                 }
             }
             modelMap.put("total", total);
-            if (!CollectionUtils.isEmpty(productUsers)) {
+            if (!CollectionUtils.isEmpty(products)) {
                 try {
-                    ProductUtils.updateProductUsersByBatch(productUsers);
-//                    productUsers = productUsersDao.save(productUsers);
-                    modelMap.put("success", productUsers.size());
+                    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(alters.size() > 0)
                     	modelMap.put("alters", alters);
                     if (existedProducts.size() > 0) {
@@ -404,7 +413,11 @@ public class ProductUsersServiceImpl implements ProductUsersService {
 
     @Override
     public void deleteByBatch(List<Long> idList) throws Exception {
-        ProductUtils.deleteProductUserByBatch(idList);
+        try {
+            ProductUtils.deleteProductUserByBatch(idList);
+        } catch (Exception e) {
+            throw new RuntimeException("访问接口方法异常");
+        }
 //        List<ProductUsers> prods = productUsersDao.findAll(idList);
 //        productUsersDao.delete(prods);
     }

+ 6 - 3
src/main/java/com/uas/platform/b2b/service/impl/PurcProductServiceImpl.java

@@ -45,6 +45,7 @@ public class PurcProductServiceImpl implements PurcProductService {
 		List<String> alters = new ArrayList<String>();
 		List<String> infos = new ArrayList<String>();
 		List<Product> products = new ArrayList<Product>();
+		List<Product> oldProducts = new ArrayList<>();
 		List<Product> productsInfo = new ArrayList<Product>();
 		Sheet sheet = workbook.getSheetAt(0);
 		int rowNum = sheet.getLastRowNum();
@@ -242,7 +243,8 @@ public class PurcProductServiceImpl implements PurcProductService {
 								status.setB2bDisabled(Constant.NO);
 								productStatusDao.save(status);
 							}
-							ProductUtils.updateOne(oldProd);
+//							ProductUtils.updateOne(oldProd);
+                            oldProducts.add(oldProd);
 						}
 						// else if (oldProd.getIsPurchase() != null) {
 						// if (!oldProd.getIsPurchase().equals(Constant.YES)) {
@@ -299,12 +301,13 @@ public class PurcProductServiceImpl implements PurcProductService {
 					modelMap.put("products", productsInfo);
 				}
 			}
-			if (!CollectionUtils.isEmpty(products)) {
+			if (!CollectionUtils.isEmpty(products) || !CollectionUtils.isEmpty(oldProducts)) {
 				try {
+				    products.addAll(oldProducts);
 					ProductUtils.update(products);
 					modelMap.put("success", products.size());
 				} catch (Exception e) {
-					modelMap.put("error", "存在编号相同的物料");
+					modelMap.put("error", "上传失败,可能存在编号相同的物料");
 				}
 			}
 		}