上傳excel,比對後批量修改(臨時表)
大概思路
- 讀取excel信息
- 將該數據保存在臨時表中
- 比對臨時表與客戶表的數據,得出結果
- 根據比對結果批量修改客戶表數據
- 將比對不成功的結果導出
- 刪除臨時表
具體思路
- 獲取excel信息 List<List< Object>> listob = UploadExcelUtil.ExcelOfUpload(request, response);
- 將listob 對象轉換爲List< TemporaryClient> listofTemporaryClient_1 對象
- 批量導入listofTemporaryClient_1 數據進臨時表temporary_client
- 將臨時表(temporary_client)中的客戶姓名,手機號與客戶表(client)中的客戶姓名,手機號相匹配,再加上客戶表中的created_time小於等於臨時表中的check_time。將比對後的結果查詢出來爲listofTemporaryClient
- 根據上面比對的結果listofTemporaryClient,根據c_id批量修改數據庫裏的checkTime,state=1
- 查詢出在臨時表中比對不成功的數據listofTemporaryClient_2
- 刪除臨時表裏的數據
- 導出listofTemporaryClient_2的數據,方便管理查詢
控制層 SpringMVC
@RequestMapping(value = "BankDataUpload.do", produces = "application/text; charset=utf-8")
public void BankDataUpload(HttpServletRequest request, HttpServletResponse response){
List<TemporaryClient> list=new ArrayList<TemporaryClient>();
try {
list = sysUploadServiceimpl.BankDataUpload(request, response);
} catch (Exception e) {
e.printStackTrace();
}
try {
ExportExcel<TemporaryClient> ee = new ExportExcel<TemporaryClient>();
String[] headers = { "申請日期", "覈准日期", "客戶稱謂", "手機號碼"};
String fileName = "比對失敗的表格";
ee.exportExcel(headers, list, fileName, response);
} catch (Exception e) {
System.out.println("導出excel表異常");
}
}
mapper層(Dao層)
與數據庫操作有關,插入臨時表,比對數據,批量修改客戶表,找出比對不成功的數據,刪除臨時表
//將數據批量插入臨時表TemporaryClient
int insertListofTemporaryClient(List<TemporaryClient> listofTemporaryClient_1);
//找出臨時表與client表中關聯的數據,即成功的數據
List<TemporaryClient> TrueByTemporaryClient(Integer gid);
//根據比對出來的c_id,check_time修改客戶狀態爲status=1;
boolean UpdateTemporaryClient(@Param("listofTemporaryClient")List<TemporaryClient> listofTemporaryClient);
//找出臨時表與client表中不關聯的數據,即修改失敗的數據,導出excel表
List<TemporaryClient> FalseByTemporaryClient(@Param("listofTemporaryClient")List<TemporaryClient> listofTemporaryClient);
//刪除臨時表
boolean deleteTemporaryClient();
service層 SysUploadService
// 上傳銀行數據
List<TemporaryClient> BankDataUpload(HttpServletRequest request, HttpServletResponse response) throws Exception;
實現類 SysUploadServiceimpl
public List<TemporaryClient> BankDataUpload(HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 1、獲取excel信息 List<List< Object>> listob
List<List<Object>> listob = UploadExcelUtil.ExcelOfUpload(request, response);
List<TemporaryClient> listofTemporaryClient_1 = new ArrayList<TemporaryClient>();
List<TemporaryClient> listofTemporaryClient_2 = new ArrayList<TemporaryClient>();
// 商品map<商品名稱,商品id>
List<Goodsmanage> listgoodsmanage = dao.getGoodsName();
Map<String, Integer> mapofgid = UploadExcelUtil.getMapOfGoodmanessage(listgoodsmanage);
//2、將listob 對象轉換爲List< TemporaryClient> listofTemporaryClient_1 對象
List<TemporaryClient> listofTemporaryClient_1 = new ArrayList<TemporaryClient>();
List<TemporaryClient> listofTemporaryClient_2 = new ArrayList<TemporaryClient>();
Integer g_id = 0;
for (int i = 0; i < listob.size(); i++) {
try {
List<Object> lo = listob.get(i);
TemporaryClient temporary = new TemporaryClient();
temporary.setCreatedTime(String.valueOf(lo.get(0)).substring(0, 10));
temporary.setCheckTime(String.valueOf(lo.get(1)).substring(0, 10));
temporary.setCname(String.valueOf(lo.get(2)));
temporary.setCtel(String.valueOf(lo.get(3)));
if (g_id == 0) {
g_id = mapofgid.get(String.valueOf(lo.get(4)));
}
temporary.setGid(g_id);
listofTemporaryClient_1.add(temporary);
} catch (Exception e) {
System.out.println("----Excel表出現異常 _____行數 " + i);
e.printStackTrace();
}
}
List<TemporaryClient> listofTemporaryClient = new ArrayList<TemporaryClient>();
try {
// 批量插入銀行數據,做爲臨時表
int num = dao.insertListofTemporaryClient(listofTemporaryClient_1);
System.out.println("導入數據數量" + num);
// 臨時表與client表比對,得出c_id ,check_time,tem_id
listofTemporaryClient = dao.TrueByTemporaryClient(g_id);
System.out.println("-----比對成功,比對的數量爲______" + listofTemporaryClient.size());
if(!listofTemporaryClient.isEmpty()){
boolean Result = dao.UpdateTemporaryClient(listofTemporaryClient);
System.out.println("修改結果爲------" + Result);
listofTemporaryClient_2 = dao.FalseByTemporaryClient(listofTemporaryClient);
System.out.println("------比對失敗的數量爲——————" + listofTemporaryClient_2.size());
}else if(listofTemporaryClient.isEmpty()){
System.out.println("比對失敗,結果爲空");
return listofTemporaryClient_1;
}
} catch(BadSqlGrammarException e){
System.out.println("比對數據爲空,不能修改");
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
boolean Re = dao.deleteTemporaryClient();
System.out.println("刪除臨時表---" + Re);
}
return listofTemporaryClient_2;
}
mybatis 層的sql語句
- 將excel裏的數據轉換成List< TemporaryClient> listofTemporaryClient_1, 批量插入臨時表,採用foreach批量插入數據庫。
<!-- 批量插入銀行數據,做爲臨時表 -->
<insert id="insertListofTemporaryClient" parameterType="java.util.List" useGeneratedKeys="false">
insert into temporary_client
( created_time,check_time,c_name,c_tel,g_id)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.createdTime},
#{item.checkTime},
#{item.cname},
#{item.ctel},
#{item.gid}
)
</foreach>
</insert>
- 將臨時表(temporary_client)中的客戶姓名,手機號與客戶表(client)中的客戶姓名,手機號相匹配,再加上客戶表中的created_time小於等於臨時表中的check_time。
<select id="TrueByTemporaryClient" resultType="cn.sys.entity.TemporaryClient">
select DISTINCT c.c_id as cid,tm.check_time as checkTime,tm.tem_id as temId from temporary_client tm , client c
where c.g_id=#{gid} and c.state =0 and tm.check_time >=c.created_time
and REPLACE(c.c_tel,SUBSTR(c.c_tel FROM 4 FOR 4),"****")=tm.c_tel
and LEFT(c.c_name,1)=left(tm.c_name,1)
</select>
- 根據上面比對的結果,根據c_id批量修改數據庫裏的checkTime,state=1
<update id="UpdateTemporaryClient" parameterType="java.util.List">
update client
<trim prefix="set" suffixOverrides=",">
<trim prefix="state =case" suffix="end," >
<foreach collection="listofTemporaryClient" item="i" index="index">
when c_id=#{i.cid} then 1
</foreach>
</trim>
<trim prefix="check_time =case" suffix="end,">
<foreach collection="listofTemporaryClient" item="i" index="index">
when c_id=#{i.cid} then #{i.checkTime}
</foreach>
</trim>
</trim>
where
<foreach collection="listofTemporaryClient" separator="or" item="i" index="index" >
c_id=#{i.cid}
</foreach>
</update>
- 查詢出在臨時表中比對不成功的數據,tem_id not in (),導出該數據的excel形式,方便管理查詢
<select id="FalseByTemporaryClient" resultType="cn.sys.entity.TemporaryClient">
SELECT created_time as createdTime ,check_time as checkTime,c_name as cname,c_tel as ctel from temporary_client where tem_id not in
<foreach collection="listofTemporaryClient" item="i" open="(" separator="," close=")" >
#{i.temId}
</foreach>
</select>
- 刪除臨時表裏的數據
<delete id="deleteTemporaryClient" parameterType="cn.sys.entity.TemporaryClient" >
DELETE from temporary_client
</delete>