Browse Source

批量导入-xls导入接口开发

anderx 6 years ago
parent
commit
96afc5a497

+ 3 - 4
src/main/java/com/goafanti/common/controller/PublicController.java

@@ -15,9 +15,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -580,8 +578,8 @@ public class PublicController extends BaseController {
           int firstRowNum = sheet.getFirstRowNum();
           //获得当前sheet的结束行
           int lastRowNum = sheet.getLastRowNum();
-          //循环除了第行的所有行
-          for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++){
+          //循环除了第2行的所有行
+          for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
           //获得当前行
           Row row = sheet.getRow(rowNum);
           if(row == null){
@@ -623,6 +621,7 @@ public class PublicController extends BaseController {
           try {
           //获取excel文件的io流
           InputStream is = file.getInputStream();
+          //如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
           workbook = new XSSFWorkbook(is);
           } catch (IOException e) {
           }

+ 12 - 0
src/main/java/com/goafanti/common/dao/TemporaryReceivablesMapper.java

@@ -2,6 +2,8 @@ package com.goafanti.common.dao;
 
 import com.goafanti.common.model.TemporaryReceivables;
 import com.goafanti.common.model.TemporaryReceivablesExample;
+import com.goafanti.order.bo.TemporaryReceivablesBo;
+
 import java.util.List;
 import org.apache.ibatis.annotations.Param;
 
@@ -81,4 +83,14 @@ public interface TemporaryReceivablesMapper {
 	List<TemporaryReceivables> selectByaid(String aid);
 
 	void deleteNormalByaid(String aid);
+
+
+	List<String> getRepeatOrderNo(List<TemporaryReceivablesBo> list);
+
+	void insertBatch(List<TemporaryReceivablesBo> list);
+
+	void updateRepeatOrderNo(String adminId);
+
+
+	
 }

+ 18 - 0
src/main/java/com/goafanti/common/mapper/TemporaryReceivablesMapper.xml

@@ -424,4 +424,22 @@
 	and b.order_dep= #{depId}
 	</if>
   </select>
+  
+    <insert id="insertBatch" parameterType="com.goafanti.common.model.TemporaryReceivables">
+    insert into temporary_receivables (id, order_no, corporate_name, amount, receivables_time,
+     aid, remarks, status, create_time, financial_pay_no) values
+      <foreach item="item" index="index" collection="list" separator=",">
+      (#{item.id,jdbcType=INTEGER}, #{item.orderNo,jdbcType=VARCHAR}, #{item.corporateName,jdbcType=VARCHAR}, 
+      #{item.amount,jdbcType=DECIMAL}, #{item.receivablesTime,jdbcType=TIMESTAMP}, #{item.aid,jdbcType=VARCHAR}, 
+      #{item.remarks,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER}, #{item.createTime,jdbcType=TIMESTAMP}, 
+      #{item.financialPayNo,jdbcType=VARCHAR})
+    </foreach>
+  </insert>
+  
+  <update id="updateRepeatOrderNo" >
+	update temporary_receivables x ,(select a.id from temporary_receivables a left join (select * from (select order_no,amount,aid,count(*) counts from temporary_receivables
+	group by order_no,amount,aid)a where a.counts>1 and a.aid='060583e4-82a2-4f72-8141-ae44806844e7') b  on a.order_no=b.order_no 
+	where a.amount=b.amount and a.aid='060583e4-82a2-4f72-8141-ae44806844e7')y  set status=1
+	where x.id=y.id
+  </update>
 </mapper>

+ 10 - 0
src/main/java/com/goafanti/order/bo/TemporaryReceivablesBo.java

@@ -13,4 +13,14 @@ public class TemporaryReceivablesBo extends TemporaryReceivables {
 		this.receivablesTimes = receivablesTimes;
 	}
 
+	@Override
+	public String toString() {
+		return "TemporaryReceivablesBo [receivablesTimes=" + receivablesTimes + ", getReceivablesTimes()="
+				+ getReceivablesTimes() + ", getId()=" + getId() + ", getOrderNo()=" + getOrderNo()
+				+ ", getCorporateName()=" + getCorporateName() + ", getAmount()=" + getAmount()
+				+ ", getReceivablesTime()=" + getReceivablesTime() + ", getAid()=" + getAid() + ", getRemarks()="
+				+ getRemarks() + ", getStatus()=" + getStatus() + ", getCreateTime()=" + getCreateTime()
+				+ ", getFinancialPayNo()=" + getFinancialPayNo()+ "]";
+	}
+	
 }

+ 31 - 2
src/main/java/com/goafanti/order/controller/AdminOrderReceivablesApiController.java

@@ -4,13 +4,17 @@ package com.goafanti.order.controller;
 
 
 
+
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import com.goafanti.admin.service.AttachmentService;
 import com.goafanti.common.bo.Result;
@@ -116,8 +120,8 @@ public class AdminOrderReceivablesApiController extends CertifyApiController {
 		return res;
 	}
 	
-	 /**
-     * 下载回款批量导入Excel模板
+	/**
+	 * 	下载回款批量导入Excel模板
      * 
      * @param response
      * @return
@@ -142,4 +146,29 @@ public class AdminOrderReceivablesApiController extends CertifyApiController {
         }
         return res;
     }
+	/**
+	 * 	Excel批量导入回款
+     * 
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/import" , method = RequestMethod.POST)
+    public Result importTeacher(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {
+    	Result res=new Result();
+    	//判断文件是否存在
+        if(null == file){
+      	  res.getError().add(buildError("文件不存在!"));
+      	  return res;
+        }
+    	String fileName = file.getOriginalFilename();
+    	if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
+        	  res.getError().add(buildError("格式不正确"));
+        	  return res;
+          }
+    	orderReceivablesService.batchListReceivables(file);
+         res.data(1); 
+    	return res;
+    	
+    }
+    
 }

+ 4 - 0
src/main/java/com/goafanti/order/service/OrderReceivablesService.java

@@ -1,5 +1,7 @@
 package com.goafanti.order.service;
 
+import org.springframework.web.multipart.MultipartFile;
+
 import com.goafanti.core.mybatis.page.Pagination;
 import com.goafanti.order.bo.ReceivablesListBo;
 import com.goafanti.order.bo.TemporaryReceivablesBo;
@@ -18,4 +20,6 @@ public interface OrderReceivablesService {
 
 	Pagination<ReceivablesListBo>  listReceivables(String contractNo, String orderNo, String nickname, String depId,Integer pageSize,Integer pageNo);
 
+	void batchListReceivables(MultipartFile file);
+
 }

+ 128 - 0
src/main/java/com/goafanti/order/service/impl/OrderReceivablesServiceImpl.java

@@ -1,5 +1,8 @@
 package com.goafanti.order.service.impl;
 
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -10,8 +13,15 @@ import java.util.Map;
 import java.util.UUID;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import com.goafanti.common.bo.Error;
 import com.goafanti.common.constant.AFTConstants;
@@ -238,5 +248,123 @@ public class OrderReceivablesServiceImpl extends BaseMybatisDao<TemporaryReceiva
 		if(StringUtils.isNotBlank(depId))map.put("depId", depId);
 		return (Pagination<ReceivablesListBo>) findPage("selectReceivablesList", "selectReceivablesCount", map, pageNo, pageSize);
 	}
+	@Override
+	public void batchListReceivables(MultipartFile file) {
+		 //获得Workbook工作薄对象
+        Workbook workbook = getWorkBook(file);
+        //创建一个对象,用来存储数据
+        List<TemporaryReceivablesBo> list=new ArrayList<>();
+        if(workbook != null){
+        	for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){
+        	//获得当前sheet工作表
+        		Sheet sheet = workbook.getSheetAt(sheetNum);
+        		if(sheet == null){
+        			continue;
+        		}
+        		//获得当前sheet的开始行
+        		int firstRowNum = sheet.getFirstRowNum();
+        		//获得当前sheet的结束行
+        		int lastRowNum = sheet.getLastRowNum();
+        		//循环除了第2行的所有行
+        		for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
+	        			//获得当前行
+	        				Row row = sheet.getRow(rowNum);
+	        				if(row == null){
+	        					continue;
+	        				}
+		        	//获得当前行的开始列
+		        	int firstCellNum = row.getFirstCellNum();
+		        	//获得当前行的列数
+		        	int lastCellNum = row.getLastCellNum();
+			        //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
+		        	TemporaryReceivablesBo trb=new TemporaryReceivablesBo();
+			        for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
+				        Cell cell = row.getCell(cellNum);
+				    	try {
+				        switch (cellNum) {
+						case 0:
+							trb.setFinancialPayNo(getCellValue(cell));
+							break;
+						case 1:
+							trb.setOrderNo(getCellValue(cell));
+							break;
+						case 2:
+							trb.setCorporateName(getCellValue(cell));
+							break;
+						case 3:
+							trb.setAmount(new BigDecimal(getCellValue(cell)));
+							break;
+						case 4:
+							trb.setReceivablesTime(DateUtils.StringToDate(getCellValue(cell), AFTConstants.YYYYMMDD));
+							break;
+						case 5:
+							trb.setRemarks(getCellValue(cell));
+							break;
+						}
+				    	} catch (ParseException e) {
+				    		throw new BusinessException(new Error("表格第"+(rowNum+1)+"行输入内容不正确。"));
+						}
+			        }
+			        trb.setAid(TokenManager.getAdminId());
+			        list.add(trb);
+        		}
+        	}
+        	temporaryReceivablesMapper.insertBatch(list);
+        	//计算重复的单
+        	temporaryReceivablesMapper.updateRepeatOrderNo(TokenManager.getAdminId());
+        }
+    }
+      
+        public static Workbook getWorkBook(MultipartFile file) {
+        //创建Workbook工作薄对象,表示整个excel
+        Workbook workbook = null;
+        try {
+        //获取excel文件的io流
+        InputStream is = file.getInputStream();
+        //如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
+        if(file.getOriginalFilename().matches("^.+\\.(?i)(xls)$"))workbook = new HSSFWorkbook(is);
+        if(file.getOriginalFilename().matches("^.+\\.(?i)(xlsx)$"))workbook = new XSSFWorkbook(is);
+        } catch (IOException e) {
+        }
+        return workbook;
+        }
+        @SuppressWarnings("deprecation")
+		public static String getCellValue(Cell cell){
+        String cellValue = "";
+        if(cell == null){
+        return cellValue;
+        }
+        //把数字当成String来读,避免出现1读成1.0的情况
+        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
+        cell.setCellType(Cell.CELL_TYPE_STRING);
+        }
+        //判断数据的类型
+        switch (cell.getCellType()){
+        case Cell.CELL_TYPE_NUMERIC: //数字
+        cellValue = String.valueOf(cell.getNumericCellValue());
+        break;
+        case Cell.CELL_TYPE_STRING: //字符串
+        cellValue = String.valueOf(cell.getStringCellValue());
+        break;
+        case Cell.CELL_TYPE_BOOLEAN: //Boolean
+        cellValue = String.valueOf(cell.getBooleanCellValue());
+        break;
+        case Cell.CELL_TYPE_FORMULA: //公式
+        cellValue = String.valueOf(cell.getCellFormula());
+        break;
+        case Cell.CELL_TYPE_BLANK: //空值
+        cellValue = "未知";
+        break;
+        case Cell.CELL_TYPE_ERROR: //故障
+        cellValue = "非法字符";
+        break;
+        default:
+        cellValue = "未知类型";
+        break;
+        }
+        return cellValue;
+        
+		
+	}
 
 }