java多线程处理导入数据拆分List集合 同步处理插入数据

时间:2022-09-10 18:32:46

 

原文:https://www.2cto.com/kf/201612/581174.html

 

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

<em>/**
</em><em> * Created by admin
</em><em>
*/
</em>@RestController
@RequestMapping(
"/importData")
public class HpImportController extends BaseController{
private static final Logger <em>logger </em>= Logger.getLogger(HpImportController.class);

@Reference(interfaceClass
= SidCustManageService.class,version = Constants.<em>SERVICE_VERSION</em>)
private SidCustManageService sidCustManageService;



@RequestMapping(value
= "importDate")
@ResponseBody
public Response getOrdertDate(HttpServletRequest request,MultipartFile importData){


//创建Excel工作薄
try {
HSSFWorkbook hwb
= new HSSFWorkbook(importData.getInputStream());
//得到第一个工作表
HSSFSheet sheet = hwb.getSheetAt(0);
HSSFRow row
= null;

SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
List
<sidcustpersoncontactinfo> list = new ArrayList<sidcustpersoncontactinfo>();

//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for(int i = 0; i < hwb.getNumberOfSheets(); i++) {
sheet
= hwb.getSheetAt(i);

//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
row
= sheet.getRow(j);
String loginName
= ExcelUtils.getStringValue(row, 0);
String agentName
= ExcelUtils.getStringValue(row, 1);
String pwd
= "123123";
String amount
= ExcelUtils.getStringValue(row, 3);
String cust_lev
= ExcelUtils.getStringValue(row,4);
String name
= ExcelUtils.getStringValue(row, 5);
String tel
= ExcelUtils.getStringValue(row, 6);
String qq
= ExcelUtils.getStringValue(row, 7);
String birday
= ExcelUtils.getStringValue(row, 8);
String email
= ExcelUtils.getStringValue(row, 9);
String createTp
= ExcelUtils.getStringValue(row, 10);
String bankAccountName
= ExcelUtils.getStringValue(row, 11);
String bankNo
= ExcelUtils.getStringValue(row, 12);
String bankName
= ExcelUtils.getStringValue(row, 13);
String bankType
= ExcelUtils.getStringValue(row, 14);


SidCustPersonContactInfo custCpci
= new SidCustPersonContactInfo();
custCpci.setCustCode(loginName);
custCpci.setAmount(Double.valueOf(amount));
custCpci.setPasswd(pwd);
custCpci.setBankName(bankName);
custCpci.setBankNo(bankNo);
custCpci.setBankAccountName(bankAccountName);
if(!birday.equals("")){
custCpci.setBirthDate(sdf.parse(birday));
}
custCpci.setFirstName(name);
custCpci.setNickName(name);
custCpci.setQq(qq);
custCpci.setBankType(bankType);
custCpci.setEmailAddress(email);
custCpci.setMobileNumber(tel);
custCpci.setHallId(Long.valueOf(
5));
list.add(custCpci);

}
}


int count = 50;
int listSize = list.size();
int RunSize = (listSize / count)+1;

ThreadPoolExecutor executor
= new ScheduledThreadPoolExecutor(RunSize);
CountDownLatch countDownLatch
= new CountDownLatch(RunSize);
List
<sidcustpersoncontactinfo> newList = null ;
for (int i = 0; i < RunSize; i++) {
if((i+1)==RunSize){
int startIndex = (i*count);;
int endIndex = list.size();
newList
=list.subList(startIndex,endIndex);
}
else{
int startIndex = i*count;;
int endIndex = (i+1)*count;
newList
=list.subList(startIndex,endIndex);
}

ExecupteHp hpRunnable
= new ExecupteHp(newList);
executor.execute(hpRunnable);
}

countDownLatch.await();
executor.shutdown();


}
catch (Exception e){
<em>logger</em>.info("导入数据开始.");
e.printStackTrace();
}
return Response.ok(GlobalEnum.<em>GLOBAL_ENUM_SUCCESS_CODE</em>.getValue() , SidCustEnum.<em>SID_CRST_ERROR_SID_SUCCESS_07</em>.getValue());

}



class ExecupteHp implements Runnable{
private List <sidcustpersoncontactinfo> list;
public ExecupteHp (List <sidcustpersoncontactinfo> list){
this.list = list ;
}

@Override
public void run() {
if(null!=list){
for (SidCustPersonContactInfo scpc : list){
System.
<em>out</em>.print("更新会员联系方式" +scpc.getFirstName() + "---"+scpc.getCustCode() );
SidCustContactInfo custContactInfo
= new SidCustContactInfo();
custContactInfo.setCustCode(scpc.getCustCode());
custContactInfo.setEmailAddress(scpc.getEmailAddress());
custContactInfo.setMobileNumber(scpc.getMobileNumber());
custContactInfo.setFirstName(scpc.getFirstName());
custContactInfo.setNicName(scpc.getNickName());
custContactInfo.setQq(scpc.getQq());
custContactInfoService.updateByContactByCustCode(custContactInfo);

}
}

}
}



}