上传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>