package com.maersk.newdragon.controller;
import com.maersk.newdragon.service.DownLoadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@RestController
@RequestMapping("/downLoad")
public class DownLoadExcelController {
@Autowired
private DownLoadFileService downLoadFileService;
@RequestMapping(value = "/reportExcel")
public void downLoadReportExcel(HttpServletRequest request, HttpServletResponse response,String type,String ids) {
downLoadFileService.downLoadReportExcel(request, response,type,ids);
}
}
package com.maersk.newdragon.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.maersk.common.core.util.IOUtil;
import com.maersk.common.dao.entity.NdLinertrackingctnrinfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
@AllArgsConstructor
public class DownLoadFileService {
private static final String ddlist = "ddlist";
private static final String pierlist = "pierlist";
private static final String outerWarehouselist = "outerWarehouselist";
private static final String dndFeeList = "dndFeeList";
private final StringRedisTemplate stringRedisTemplate;
public void downLoadReportExcel(HttpServletRequest request, HttpServletResponse response, String type, String ids) {
ServletOutputStream outputStream = null;
List<NdLinertrackingctnrinfo> businessFlows = null;
List<NdLinertrackingctnrinfo> businessFlows1 = new ArrayList<>();
try {
outputStream = response.getOutputStream();
if (type.equals("1")) {
IOUtil.setFileDownloadHeader(response, "DnDCostMonitoring.xlsx");
businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(ddlist), NdLinertrackingctnrinfo.class);
} else if (type.equals("2")) {
IOUtil.setFileDownloadHeader(response, "TerminalManagement.xlsx");
businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(pierlist), NdLinertrackingctnrinfo.class);
} else if (type.equals("3")) {
IOUtil.setFileDownloadHeader(response, "ExternalWarehouseManagement.xlsx");
businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(outerWarehouselist), NdLinertrackingctnrinfo.class);
} else if (type.equals("4")) {
IOUtil.setFileDownloadHeader(response, "DnDExpenseReport.xlsx");
businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(dndFeeList), NdLinertrackingctnrinfo.class);
}
String[] id = ids.split(",");
for (int j = 0; j < id.length; j++) {
for (int i = 0; i < businessFlows.size(); i++) {
if (businessFlows.get(i).getId().longValue() == Long.parseLong(id[j])) {
businessFlows1.add(businessFlows.get(i));
}
}
}
Workbook workbook = createWb(businessFlows1);
workbook.write(outputStream);
outputStream.flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
IOUtil.close(outputStream);
}
}
private Workbook createWb(List<NdLinertrackingctnrinfo> businessFlows) {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet1");
String[] header = new String[]{"免堆期", "免箱期", "异常提醒", "备注", "费用结算方", "箱型", "码头", "离开码头堆场时间", "码头总存放天数", "用箱总天数", "还空时间", "外堆场", "进入外堆场时间", "离开外堆场时间", "外堆场总堆存天数", "码头堆存费", "外堆场堆存费"};
Row row = sheet.createRow(0);
for (int i = 0; i < header.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(header[i]);
}
if (CollectionUtils.isEmpty(businessFlows)) return wb;
for (int i = 0; i < businessFlows.size(); i++) {
row = sheet.createRow(i + 1);
NdLinertrackingctnrinfo info = businessFlows.get(i);
for (int j = 0; j < header.length; j++) {
Cell cell = row.createCell(j);
switch (j) {
case 0:
cell.setCellValue(info.getStorage() == null ? "null" : info.getStorage());
break;
case 1:
cell.setCellValue(info.getFreeBoxs() == null ? "null" : info.getFreeBoxs().toString());
break;
case 2:
cell.setCellValue(info.getErrorMsg() == null ? "" : info.getErrorMsg());
break;
case 3:
cell.setCellValue(info.getRemarks() == null ? "" : info.getRemarks());
break;
case 4:
cell.setCellValue(info.getSettlementParty() == null ? "" : info.getSettlementParty());
break;
case 5:
cell.setCellValue(info.getCtnrCode() == null ? "" : info.getCtnrCode());
break;
case 6:
cell.setCellValue(info.getDtp() == null ? "" : info.getDtp());
break;
case 7:
cell.setCellValue(info.getSTCSstatustime() == null ? "" : info.getSTCSstatustime());
break;
case 8:
cell.setCellValue(info.getStorageDays() == null ? "" : info.getStorageDays().toString());
break;
case 9:
cell.setCellValue(info.getBoxDays() == null ? "" : info.getBoxDays().toString());
break;
case 10:
cell.setCellValue(info.getStatustime() == null ? "" : info.getStatustime());
break;
case 11:
cell.setCellValue(info.getDtpCode() == null ? "" : info.getDtpCode());
break;
case 12:
cell.setCellValue(info.getDSCHstatustime() == null ? "" : info.getDSCHstatustime());
break;
case 13:
cell.setCellValue(info.getSTCSstatustime() == null ? "" : info.getSTCSstatustime());
break;
case 14:
cell.setCellValue(info.getOuterYardDays() == null ? "" : info.getOuterYardDays().toString());
break;
case 15:
cell.setCellValue(info.getPortFee() == null ? "" : info.getPortFee().toString());
break;
case 16:
cell.setCellValue(info.getOutsideYardFee() == null ? "" : info.getOutsideYardFee().toString());
break;
default:
break;
}
}
}
return wb;
}
}