1.组装数据:CountDownLatch多线程分页查询组装数据
@Override
public Map<String, Object> exportBmInformationMainModelList(Map<String, Object> queryParams) {
List<List<Object>> data = new ArrayList<List<Object>>(); // 需要导出的数据
BmInformationMainQueryCondition condition = new BmInformationMainQueryCondition();
if (null == queryParams.get("businessOrderTypeNo")) {
return new HashMap<String, Object>();
}
if(queryParams.get("infoMainNo")==null ||queryParams.get("infoMainNo").toString().equals("null")){
queryParams.remove("infoMainNo");
}
BeanToMapUtils.mapToBean(queryParams, condition);
BmBusinessDetailQueryCondition detailCondition=new BmBusinessDetailQueryCondition();
detailCondition.setOrgId(condition.getOrgId());
detailCondition.setOrderTypeNo(condition.getBusinessOrderTypeNo());
List<BmBusinessDetailQueryItem> businessDetailQueryItems=bmBusinessDetailManager.getBusinessDetail(detailCondition);
ArrayList title=new ArrayList<List<String>>();
List<String> headCell1 = new ArrayList<String>();
headCell1.add("单据编号");
title.add(headCell1); //表头
List<String> headCell2 = new ArrayList<String>();
headCell2.add("经营组织");
title.add(headCell2);
List<String> headCell3 = new ArrayList<String>();
headCell3.add("客户名称");
title.add(headCell3); //表头
List<String> headCell4 = new ArrayList<String>();
headCell4.add("供应商名称");
title.add(headCell4); //表头
List<String> headCell5 = new ArrayList<String>();
headCell5.add("品牌");
title.add(headCell5); //表头
List<String> headCell6 = new ArrayList<String>();
headCell6.add("运单号");
title.add(headCell6); //表头
List<String> headCell7 = new ArrayList<String>();
headCell7.add("VIN码");
title.add(headCell7); //表头
List<String> headCell8 = new ArrayList<String>();
headCell8.add("起始地");
title.add(headCell8); //表头
List<String> headCell9 = new ArrayList<String>();
headCell9.add("目的地");
title.add(headCell9); //表头
List<String> headCell10 = new ArrayList<String>();
headCell10.add("业务类型");
title.add(headCell10); //表头
List<String> headCell11 = new ArrayList<String>();
headCell11.add("状态");
title.add(headCell11); //表头
List<String> headCell12 = new ArrayList<String>();
headCell12.add("是否计费");
title.add(headCell12); //表头
List<String> headCell13 = new ArrayList<String>();
headCell13.add("运抵时间");
title.add(headCell13); //表头
List<String> headCell14 = new ArrayList<String>();
headCell14.add("业务日期");
title.add(headCell14); //表头
ArrayList<String> otherAttributeList=new ArrayList<String>();
if(businessDetailQueryItems.size()>0){
for(BmBusinessDetailQueryItem item:businessDetailQueryItems){
if(!title.contains(item.getAttNameCn())){
List<String> headCell = new ArrayList<String>();
headCell.add(item.getAttNameCn());
title.add(headCell); // 表头
otherAttributeList.add(item.getAtt());
}
}
}
List<String> headCell15 = new ArrayList<String>();
headCell15.add("来源系统业务单号");
title.add(headCell15); //表头
List<String> headCell16 = new ArrayList<String>();
headCell16.add("客户订单号");
title.add(headCell16); //表头
List<String> headCell17 = new ArrayList<String>();
headCell17.add("会计主体");
title.add(headCell17); //表头
List<String> headCell18 = new ArrayList<String>();
headCell18.add("创建时间");
title.add(headCell18); //表头
BigInteger rowNum=new BigInteger("0");
int times=0;
List<Map<String, Object>> bmInformationMainRumNum=calculateCommonService.nativeSqlFind("QueryBmInformationMainCount",null,queryParams);
if(bmInformationMainRumNum.size()>0){
rowNum=(BigInteger) bmInformationMainRumNum.get(0).get("rowNum");
}
if(rowNum.longValue()>0){
times=(int) Math.ceil((double)rowNum.doubleValue()/(double)10000);
final CountDownLatch barrier=new CountDownLatch(times);
for(int i=0;i<times;i++){
Map map=new HashMap();
map.putAll(queryParams);
map.put("startNum", i*10000);
new QueryBmInformationMainThread(barrier,otherAttributeList, data, map).start();
}
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Map<String, Object> result = new HashMap<String, Object>();
result.put("headList", title);
result.put("dataList", data);
return result;
}
2.分页查询线程
class QueryBmInformationMainThread extends Thread{
private CountDownLatch countDownLatch ;
private ArrayList<String> otherAttributeList;
private List<List<Object>> data;
private Map<String, Object> queryParams;
public QueryBmInformationMainThread(CountDownLatch countDownLatch, ArrayList<String> otherAttributeList,List<List<Object>> data,Map<String, Object> queryParams){
this.otherAttributeList=otherAttributeList;
this.data=data;
this.queryParams=queryParams;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
List<Map<String, Object>> thisTimesList=calculateCommonService.nativeSqlFind("QueryBmInformationMainList",null,queryParams);
List<List<Object>> thisResult=new ArrayList<List<Object>>();
if (thisTimesList.size() > 0) {
for (Map<String,Object> item : thisTimesList) {
ArrayList<Object> info=new ArrayList<Object>();
info.add(item.get("INFO_MAIN_NO"));
info.add(item.get("COMPANY_NAME"));
info.add(item.get("CUST_NAME"));
info.add(item.get("SUP_NAME"));
info.add(item.get("ATTRIBUTE_018"));
info.add(item.get("ATTRIBUTE_002"));
info.add(item.get("ATTRIBUTE_003"));
info.add(item.get("ATTRIBUTE_010"));
info.add(item.get("ATTRIBUTE_012"));
if(item.get("BUSINESS_TYPE")!=null && item.get("BUSINESS_TYPE").equals("TRANSPORT")){
info.add("运输");
}
else if(item.get("BUSINESS_TYPE")!=null && item.get("BUSINESS_TYPE").equals("STORAGE")){
info.add("仓储");
}
if(item.get("SUBMIT_STATUS")!=null && item.get("SUBMIT_STATUS").equals("submit")){
info.add("审核");
}
else if(item.get("SUBMIT_STATUS")!=null && item.get("SUBMIT_STATUS").equals("cancel")){
info.add("未审核");
}
if(item.get("IS_COMPLETE_CALC")!=null && item.get("IS_COMPLETE_CALC").equals("Y")){
info.add("是");
}
else if(item.get("IS_COMPLETE_CALC")!=null && item.get("IS_COMPLETE_CALC").equals("N")){
info.add("否");
}
info.add(item.get("ATTRIBUTE_006"));
//业务日期格式化年月日
SimpleDateFormat ymd =new SimpleDateFormat("yyyy-MM-dd" );
info.add(ymd.format(item.get("BUSINESS_TIME")));
for(String attr:otherAttributeList){
info.add(item.get(humpToUnderline(attr)));
}
info.add(item.get("BUSINESS_SOURCE_NO"));
info.add(item.get("CUST_ORDER_NO"));
info.add(item.get("LEGAL_ENTITY_NAME"));
//业务日期格式化年月日
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
info.add(sdf.format(item.get("CREATE_TIME")));
thisResult.add(info);
}
}
synchronized(data){
data.addAll(thisResult);
}
countDownLatch.countDown();
}
}
3.EasyExcel导出
@ResponseBody
@RequestMapping(value = "/exportBusinessOrderInfoExcel")
public void exportBusinessOrderInfoExcel(@RequestParam Map<String, Object> params) throws Exception{
if(null == params.get("businessOrderTypeNo")||"".equals(params.get("businessOrderTypeNo"))){
return;
}
params.put("orgId", UserContextHolder.getCurrentUser().getOrgId());
Map<String, Object> result = bmBusinessOrderInfoService.exportBmInformationMainModelList(params);
if (EmptyUtils.isEmpty(result)) {
return;
}
List<List<String>> headerList = (List<List<String>>) result.get("headList"); // 需要导出的数据
List<List<Object>> dataList = (List<List<Object>>) result.get("dataList"); // 需要导出的数据
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(new Date());
String excelName = "业务单据" + date + ".xlsx";
String sheetName = "业务单据";
excelName = URLEncoder.encode(excelName, "UTF-8");
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
ServletOutputStream out = response.getOutputStream();
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + excelName);
response.setContentType("application/vnd.ms-excel");
ExcelWriter writer = EasyExcelFactory.getWriter(out);
// 写在sheet1
Sheet sheet1 = new Sheet(1, 3);
sheet1.setSheetName(sheetName);
sheet1.setHead(headerList);
// or 设置自适应宽度
sheet1.setAutoWidth(Boolean.TRUE);
writer.write1(dataList, sheet1);
writer.finish();
out.flush();
out.close();
}