Bladeren bron

外协价格库excel导入(导入null报错解决)以及新增数据

sl 1 jaar geleden
bovenliggende
commit
b23a6d6738

+ 10 - 7
module_kzks/src/main/java/org/jeecg/modules/costModelWxPrice/controller/CostModelWxPriceController.java

@@ -3,6 +3,7 @@ package org.jeecg.modules.costModelWxPrice.controller;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -75,7 +76,7 @@ public class CostModelWxPriceController extends JeecgController<CostModelWxPrice
 	
 	/**
 	 *   添加
-	 *
+	 *  添加重复的数据报错不予录入   思路和导入excel相同  --sl
 	 * @param costModelWxPrice
 	 * @return
 	 */
@@ -86,14 +87,17 @@ public class CostModelWxPriceController extends JeecgController<CostModelWxPrice
 	public Result<String> add(@RequestBody CostModelWxPrice costModelWxPrice) {
 		try {
 			CostModelWxPrice wxp = costModelWxPrice;
-			List<CostModelWxPrice> list = costModelWxPriceService.getWXPAllDetailList();
 			boolean noRepeat = true;
+			//前端就不会让录入为null
+//			if((wxp.getWxProject()==null)&(wxp.getDanjia()==null)&(wxp.getUnit()==null)&(wxp.getWxCompany()==null)){
+//				System.out.println("当前数据内容都为空,不能录入");
+//				noRepeat = false;
+//			}
+			List<CostModelWxPrice> list = costModelWxPriceService.getWXPAllDetailList();
 			for (CostModelWxPrice wxp1 : list) {
-				if ((wxp.getWxProject().equals(wxp1.getWxProject())) & (wxp.getWxCompany().equals(wxp1.getWxCompany())) & (wxp.getDanjia().equals(wxp1.getDanjia())) & (wxp.getUnit().equals(wxp1.getUnit()))) {
-					System.out.println("进入判断");
+				if((Objects.equals(wxp.getWxProject(), wxp1.getWxProject()))&(Objects.equals(wxp.getDanjia(), wxp1.getDanjia()))&(Objects.equals(wxp.getUnit(), wxp1.getUnit()))&(Objects.equals(wxp.getWxCompany(), wxp1.getWxCompany()))){
 					noRepeat = false;
-					System.out.println("当前元素重复,进行删除");
-					System.out.println();
+					//System.out.println("当前元素重复,进行删除");
 					break;//因为wxpNew元素已经被删除了,所有此段循环可以跳过了
 				}
 			}
@@ -109,7 +113,6 @@ public class CostModelWxPriceController extends JeecgController<CostModelWxPrice
 			return Result.error("添加失败!");
 		}
 
-
 	}
 	
 	/**

+ 20 - 39
module_kzks/src/main/java/org/jeecg/modules/costModelWxPrice/service/impl/CostModelWxPriceServiceImpl.java

@@ -45,6 +45,8 @@ public class CostModelWxPriceServiceImpl extends ServiceImpl<CostModelWxPriceMap
     /**
      * 通过excel导入数据
      *如果数据重复,删除重复的数据,然后在写入数据库
+     * 当前是取数据库中所有的数据进行比较,如果确定哪个字段是必须有值不能为null,可以根据该字段找出来相应数据再进行比较  --sl
+     *
      * @param request
      * @param response
      * @return
@@ -62,68 +64,47 @@ public class CostModelWxPriceServiceImpl extends ServiceImpl<CostModelWxPriceMap
             try {
                 //1.从excel中获取到数据放入到list中
                 List<CostModelWxPrice> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
-                System.out.println("从excel表中获取的数据");
-                list.forEach(System.out::println);
-                System.out.println(list.getClass());
-                System.out.println(list.getClass().getName());
+//                System.out.println("从excel表中获取的数据");
+//                list.forEach(System.out::println);
 
                 //2.新建一个list来给list中的数据去重
                 List<CostModelWxPrice> listNew = new ArrayList<>();
                 for(CostModelWxPrice wxp:list){
-//                    System.out.println(wxp);
-//                    System.out.println(wxp.getWxProject());
-//                    System.out.println(wxp.getWxCompany());
-//                    System.out.println(wxp.getDanjia().getClass());
-//                    String wxpj = wxp.getWxProject();  //外协项目
-//                    String wxcom = wxp.getWxCompany();  //外协单位
-//                    String dj = wxp.getDanjia();   //单价
-//                    String unit = wxp.getUnit();   //计量单位
                     if (!listNew.contains(wxp)) {
                         listNew.add(wxp);
                     }
                 }
-                System.out.println("列表去重后的数据");
-                listNew.forEach(System.out::println);
-                System.out.println(listNew.getClass());
-                System.out.println(listNew.getClass().getName());
+//                System.out.println("列表去重后的数据");
+//                listNew.forEach(System.out::println);
 
                 //3.从数据库中读取全部表中数据
                 List<CostModelWxPrice> list1 = getWXPAllDetailList();
-                System.out.println("从数据库中取出的数据");
-                list1.forEach(System.out::println);
-                System.out.println(list1.getClass());
-                System.out.println(list1.getClass().getName());
+//                System.out.println("从数据库中取出的数据");
+//                list1.forEach(System.out::println);
 
                 Iterator<CostModelWxPrice> iterator = listNew.iterator();
                 //4.判断去重后的listNew中是否有数据已经在数据库中记录过了,如果已经保存过直接删掉
                 while(iterator.hasNext()){
                     CostModelWxPrice wxpNew = iterator.next();
-                    System.out.println("进入第一层循环");
-                    System.out.println(wxpNew);
-                    if((wxpNew.getWxProject()!=null)&(wxpNew.getDanjia()!=null)&(wxpNew.getUnit()!=null)&(wxpNew.getWxCompany()!=null)){
-                        for(CostModelWxPrice wxp1:list1){
-                            System.out.println("进入第二层循环判断数据是否已经存在");
-//                        System.out.println(wxp1);
-                            if((wxpNew.getWxProject().equals(wxp1.getWxProject()))&(wxpNew.getDanjia().equals(wxp1.getDanjia()))&(wxpNew.getUnit().equals(wxp1.getUnit()))&(wxpNew.getWxCompany().equals(wxp1.getWxCompany()))){
-                                System.out.println("进入判断");
+                    if((wxpNew.getWxProject()==null)&(wxpNew.getDanjia()==null)&(wxpNew.getUnit()==null)&(wxpNew.getWxCompany()==null)){
+                        //System.out.println("当前数据内容都为空,删除");
+                        iterator.remove();
+                    }
+                    else{
+                        for(CostModelWxPrice wxp1:list1) {
+                            if((Objects.equals(wxpNew.getWxProject(), wxp1.getWxProject()))&(Objects.equals(wxpNew.getDanjia(), wxp1.getDanjia()))&(Objects.equals(wxpNew.getUnit(), wxp1.getUnit()))&(Objects.equals(wxpNew.getWxCompany(), wxp1.getWxCompany()))){
                                 iterator.remove();
-                                System.out.println("当前元素重复,进行删除");
+                                //System.out.println("当前元素重复,进行删除");
                                 //listNew.forEach(System.out::println);  //foreach中不能remove,不然会报错java.util.ConcurrentModificationException
-                                System.out.println();
                                 break;//因为wxpNew元素已经被删除了,所有此段循环可以跳过了
                             }
-                            System.out.println("第二层循环还在");
                         }
                     }
-                    else{//如果外协价格表中的四个字段中有值为null,则删除该条记录
-                        iterator.remove();
-                    }
-
                 }
-                System.out.println("最终需要存入的数据");
-                listNew.forEach(System.out::println);
-                System.out.println(listNew.getClass());
-                System.out.println(listNew.getClass().getName());
+//                System.out.println("最终需要存入的数据");
+//                listNew.forEach(System.out::println);
+//                System.out.println(listNew.getClass());
+//                System.out.println(listNew.getClass().getName());
 
                 //update-begin-author:taoyan date:20190528 for:批量插入数据
                 long start = System.currentTimeMillis();