上傳excel,比對後批量修改(雙重map比對)(二)

大概流程原理

1.讀取excel信息
2.得出時間、商品類別
3.根據時間、商品類別查找數據庫信息並將其封裝爲map<createdTime,List< Client>>
4.封裝爲雙重map<createdTime,map<>>
5.比對excel信息得到id,根據map.get(key).get(key)
6.將比對後的結果封裝到list中
7.批量修改。

具體思路

1、通過工具類 ExcelOfUpload 裏的 ExcelOfUpload 方法讀取 excel 的信息;
2、查出商品集合List,在封裝成map;
3、再通過 set 保存 excel 裏的下卡日期,商品名字。gid=mapofgid.get(商品名字);;
4、在數據庫按時間查出中可符合條件(時間,商品類別gid)的客戶信息。將 map.put(‘日期’,List<客戶>)保存;
5、將查出的 Map<String,List>封裝爲 Map<String,Map<describeption,
cid>> ;
6、 然 後 通 過 excel 數 據 與 上 述 查 詢 出 來 的 客 戶 信 息 Map<String,
Map<describeption,cid>>進行比對,即可獲取該 excel 數據的數據庫該條數據
的 cid,Integer cid=map1.get(time_1).get(describeption)。 String checkTime = String.valueOf(lo.get(1)).substring(0, 10);
7、最後將 cid 、checkTime 封裝到 List;
8、將上面封裝封裝好 list 傳入 mybatis 部分,進行批量修改數據庫信息;修改數據庫客戶數據爲 status=1,check_time 爲銀行傳過來的日期。

控制層 springMVC

通過HttpServletRequest request,、HttpServletResponse response,在實現類中具體實現。

@ResponseBody
	@RequestMapping(value = "fileUpload.do", produces = "application/text; charset=utf-8")
	public String UploadExcel(HttpServletRequest request, HttpServletResponse response) {
		String str = null;
		try {
			str = sysUploadServiceimpl.BankDataUpload(request, response);
			if(str==null){
				str=str+"數據導入成功";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

service層 SysUploadService

	//上傳個人的銀行二維碼
    String QRCodeMsUpload(HttpServletRequest request,HttpServletResponse response) throws   Exception;
   //查出商品的名字,id
   List<Goodsmanage>  getGoodsName();	
   //根據excel表裏的日期,比對後的商品id,查出數據庫裏的商品
	List<Client> getListOfClient(@Param("createdTime") String createdTime,@Param("listgid") List<Integer> listgid);
	//批量修改
	boolean UpdateAllBankDatas(List<Client> list2);
	    

實現類 SysUploadServiceimpl

public String BankDataUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String data=null;
		//1、通過工具類 ExcelOfUpload 裏的 ExcelOfUpload 方法讀取 excel 的信息;
		List<List<Object>> listob=UploadExcelUtil.ExcelOfUpload(request, response);
		
		//2、查出商品集合List<Goodsmanage>,在封裝成Map<String, Integer> mapofgid;
		 List<Goodsmanage> list=dao.getGoodsName();
		 Map<String, Integer> mapofgid=UploadExcelUtil.getMapOfGoodmanessage(list);     //商品map 
		 
		 //3、再通過 set 保存 excel 裏的下卡日期,商品名字。gid=mapofgid.get(商品名字);
		LinkedHashSet<String> setofdate = new LinkedHashSet<String>();   //日期
		LinkedHashSet<String> setofgid = new LinkedHashSet<String>();     //商品名稱
		for (int i = 0; i < listob.size(); i++) {
				setofdate.add(String.valueOf(listob.get(i).get(0)).substring(0, 10));
				setofgid.add(String.valueOf(listob.get(i).get(4)));		
		}		
		Map<String, Integer> mapofdate=UploadExcelUtil.mapofdate(setofdate);   //封裝map(日期,數值)
		Iterator<String> it2 = setofgid.iterator(); 
		List<Integer> listgid=new ArrayList<Integer>();       //獲取上傳Excel的商品id
		while (it2.hasNext()) {  
			  Integer gid=mapofgid.get(it2.next());
			  listgid.add(gid);	//根據map獲取商品id
			} 
			
			//4、在數據庫按時間查出中可符合條件(時間,商品類別gid)的客戶信息。將 map.put('日期',List<客戶>)保存;
		Map<String,List<Client>> mapoflistclient=new HashMap<String,List<Client>>();  //將按日期查出的list<client>對象保存爲map
		 for (Map.Entry<String,Integer> entry : mapofdate.entrySet()) {	  
			 try {
				 List<Client>  listofclient=dao.getListOfClient(entry.getKey(), listgid);
	  	          mapoflistclient.put(entry.getKey(), listofclient);    //封裝map
			} catch (Exception e) {
				data=data+"從數據庫中根據日期,商品id讀取客戶信息異常";
				System.out.println("從數據庫中根據日期,商品id讀取客戶信息異常");
				e.printStackTrace();
			}        	              
	    }

		//5、將數據庫中查詢的數據Map<String,List<Client>>封裝成 Map<String, Map<describeption,cid>>
		 Map<String, Map<String,Integer>> map1=UploadExcelUtil.Getmapoflistclient(mapoflistclient); 	
		 
		 //	 List<Client> list2爲要批量修改的數據 含check_time,c_id
		List<Client> list2=new ArrayList<Client>();
		
         //6、 通 過 excel 數 據 與 上 述 查 詢 出 來 的 客 戶 信 息Map<String,Map<describeption,cid>>進行比對,即可獲取該 excel 數據的數據庫該條數據的 cid,Integer cid =map1 .get(time_1). get(describeption) 。 String checkTime = String.valueOf(lo.get(1)).substring(0, 10);
         
		 for (int i = 0; i < listob.size(); i++) {
			 try {
				 List<Object> lo = listob.get(i);
				System.out.println(lo.toString());
				   Client client=new Client();
				   String createdTime = String.valueOf(lo.get(0)).substring(0, 10);	
				 String checkTime = String.valueOf(lo.get(1)).substring(0, 10);	
					 client.setCheckTime(checkTime);
					String cname=(String.valueOf(lo.get(2))).substring(0, 1);	
					StringBuilder tel=new StringBuilder((String.valueOf(lo.get(3)))) ;
					tel=tel.replace(3, 7, "****");
					String describeption=cname+tel;
					Integer cid=map1.get(createdTime).get(describeption);
					client.setCid(cid);
					
                   //7、最後將 cid 、checkTime 封裝到 List<Client>;
					list2.add(client);		
			} catch (Exception e) {
				System.out.println("第"+i+"行格式異常");
				e.printStackTrace();
		 	}		  
    	}
		 try {
			 if(! list2.isEmpty()){
				   //8、將上面封裝封裝好 list 傳入 mybatis 部分,進行批量修改數據庫信息;修改數據庫客戶數據爲 status=1,check_time 爲銀行傳過來的日期。
				   
				   boolean Result=dao.UpdateAllBankDatas(list2);
				  	data=data+"_____根據銀行數據修改數據庫數據 ---"+Result;
			 }else{
				 data=data+"_____銀行數據已修改完 ---"; 
			 }
		} catch (Exception e) {
			data=data+"批量修改客戶狀態異常";
			e.printStackTrace();
			}
		return data;
	}

工具類

將excel裏的信息封裝成listob形式。

List<List> listob=UploadExcelUtil.ExcelOfUpload(request, response);

public static List<List<Object>> ExcelOfUpload(HttpServletRequest request, HttpServletResponse response){
		MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
		MultipartFile file = multipartRequest.getFile("upfile");
		if (file.isEmpty()) {
			try {
				throw new Exception("文件不存在!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		InputStream in = null;
		try {
			in = file.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<List<Object>> listob = null;
		try {
			listob = new ExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listob;
	}

將商品集合封裝成map。

根據foreach遍歷List< Goodsmanage> list,獲取gId(商品id),name(商品名字),並將其封裝到map中 mapofgid.put(name, gId);可根據商品名字直接獲取商品id ,g_id =mapofgid. get(name),以減少數據庫查詢的次數。

	//將商品名字,id設置爲map,方便後面調用
	public static Map<String, Integer> getMapOfGoodmanessage(List<Goodsmanage> list){
		Map<String, Integer> mapofgid = new HashMap<String, Integer>();
		 try {	
			 for (Goodsmanage goodsmanage : list) {
				Integer  gId=goodsmanage.getgId();
				String name=goodsmanage.getName();
				mapofgid.put(name, gId);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return mapofgid;
	}

將數據庫查詢出來的數據封裝成map1。

數據庫根據日期,List查出來的客戶信息
查詢查來的客戶信息爲list ,將其根據時間封裝爲map裏的value=list,key=createdTime,在將list裏的信息在封裝爲map,則其爲雙重map(map裏面嵌套map)

map<“日期”,map< List< Client>> mapoflistclient,通過遍歷該mapoflistclient得到value= List< Client>。在通過foreach遍歷出List得到tel,cname,cid,通過String describeption=cname+tel; 將describeption,cid封裝在map2中,map2.put(describeption, client.getCid());最後 map1.put(entry.getKey(), map2) 。

    //將查詢出來的數據封裝成Map<String, Map<String,Integer>>型
	public static Map<String, Map<String,Integer>> Getmapoflistclient(Map<String,List<Client>> mapoflistclient){
		Map<String,  Map<String,Integer>> map1 = new HashMap<String, Map<String,Integer>>();
		 for (Map.Entry<String,List<Client>> entry : mapoflistclient.entrySet()) {	    
				//System.out.println("___Key = " + entry.getKey() ); 
		            List<Client> list1=entry.getValue();
		            Map<String,Integer> map2=new  HashMap<String,Integer>();
		            for (Client client : list1) {
		            	try {
		            		StringBuilder tel=new StringBuilder(client.getCtel()) ;
							tel=tel.replace(3, 7, "****");
							String cname=client.getCname().substring(0, 1);  //名字不能爲空
							String describeption=cname+tel;
		                    map2.put(describeption, client.getCid());
						} catch (Exception e) {
							e.printStackTrace();	
						}	
					}
		            map1.put(entry.getKey(), map2) ;	
		 }
		 return map1;
	}

mybatis層 sql語句

獲取商品gid,gname。用於封裝List爲map,根據gname可以知道gid

<select id="getGoodsName" resultType="cn.sys.entity.Goodsmanage">
		select g_id as gId, name from goodsmanage
	</select>

根據createdTime,listgid查出符合條件的客戶信息。在實現類裏將其封裝成Map<String,List> 。

    <select id="getListOfClient"  resultType="cn.sys.entity.Client">
      select c_id as  cid ,created_time as createdTime ,c_name as  cname,c_tel as ctel
       from client where created_time=#{createdTime}  and state=0 and del=0 and g_id in
       <foreach collection="listgid"  item="i"  open="(" separator="," close=")" >
              #{i}
          </foreach>
    </select>

根據List< Client > list2 裏有gid,checkTime兩個變量,批量修改數據庫客戶信息checkTime,state=1。

    <update id="UpdateAllBankDatas" parameterType="list">
    update client
           <trim prefix="set" suffixOverrides=",">
            <trim prefix="state =case" suffix="end," >
                <foreach collection="list" item="i" index="index">
                         when c_id=#{i.cid} then 1
                </foreach>
             </trim>            
            <trim prefix="check_time =case" suffix="end,">
                <foreach collection="list" item="i" index="index">                     
                         when c_id=#{i.cid} then #{i.checkTime}                   
                </foreach>
             </trim>
            </trim>
           where
           <foreach collection="list" separator="or" item="i" index="index" >
             c_id=#{i.cid}
         </foreach>
    </update>

發佈了15 篇原創文章 · 獲贊 4 · 訪問量 2160
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章