上传excel,比对后批量修改(临时表)(三)

大概思路

  1. 读取excel信息
  2. 将该数据保存在临时表中
  3. 比对临时表与客户表的数据,得出结果
  4. 根据比对结果批量修改客户表数据
  5. 将比对不成功的结果导出
  6. 删除临时表

具体思路

  1. 获取excel信息 List<List< Object>> listob = UploadExcelUtil.ExcelOfUpload(request, response);
  2. 将listob 对象转换为List< TemporaryClient> listofTemporaryClient_1 对象
  3. 批量导入listofTemporaryClient_1 数据进临时表temporary_client
  4. 将临时表(temporary_client)中的客户姓名,手机号与客户表(client)中的客户姓名,手机号相匹配,再加上客户表中的created_time小于等于临时表中的check_time。将比对后的结果查询出来为listofTemporaryClient
  5. 根据上面比对的结果listofTemporaryClient,根据c_id批量修改数据库里的checkTime,state=1
  6. 查询出在临时表中比对不成功的数据listofTemporaryClient_2
  7. 删除临时表里的数据
  8. 导出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语句

  1. 将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>
  1. 将临时表(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>
  1. 根据上面比对的结果,根据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>
  1. 查询出在临时表中比对不成功的数据,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>
  1. 删除临时表里的数据
   <delete id="deleteTemporaryClient" parameterType="cn.sys.entity.TemporaryClient" >
   DELETE from temporary_client
   </delete>
发布了15 篇原创文章 · 获赞 4 · 访问量 2158
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章