package zy.cloud.wms.manager.service.impl; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Cools; import com.core.common.R; import com.core.exception.CoolException; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import zy.cloud.wms.manager.entity.Item; import zy.cloud.wms.manager.mapper.ItemReportMapper; import zy.cloud.wms.manager.entity.ItemReport; import zy.cloud.wms.manager.service.ItemReportService; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import zy.cloud.wms.manager.service.ItemService; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.Date; @Service("itemReportService") public class ItemReportServiceImpl extends ServiceImpl implements ItemReportService { @Autowired private ItemService itemService; /** * 初始化生成report * @param report * @param userId */ @Override public void init(ItemReport report, Long userId) { /** * 控管与参数初始化 */ Date now = new Date(); Item id = itemService.selectOne(new EntityWrapper() .eq("id", report.getItemId())); if (Cools.isEmpty(id)) { throw new CoolException("找不到该项目,请联系管理员"); } report.setItemName(id.getName()); /** * 如果前端返回原因,表示有异常,将状态设置为true */ if (report.getDeliverReason() == null || report.getDeliverReason().equals("")) { report.setDeliverIssue(false); }else { report.setDeliverIssue(true); } if (report.getQualityReason() == null || report.getQualityReason().equals("")) { report.setQualityIssue(false); }else { report.setQualityIssue(true); } if (report.getDesignReason() == null || report.getDesignReason().equals("")) { report.setDesignIssue(false); }else { report.setDesignIssue(true); } if (report.getInstallReason() == null || report.getInstallReason().equals("")) { report.setInstallIssue(false); }else { report.setInstallIssue(true); } report.setUpdateTime(now); report.setUpdateBy(userId); report.setCreateTime(now); report.setCreateBy(userId); insert(report); } @Override public HttpServletResponse makeReport(Item item, ItemReport itemReport, HttpServletResponse response) { FileInputStream in = null; ServletOutputStream out = null; try { /** * 一些初始化 */ in = new FileInputStream("src/main/resources/excel/analysis_report.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(in); XSSFSheet mainSheet = wb.getSheet("报告"); /** * 开始设置第10和11合并行的所有列, 角标从0开始,rownum9实际上等于10 */ XSSFRow row10 = mainSheet.getRow(9); row10.getCell(1).setCellValue(item.getUuid()); row10.getCell(3).setCellValue(item.getEndTime0$()); row10.getCell(5).setCellValue(item.getRealEndTime0$()); XSSFRow row1 = mainSheet.getRow(0); row1.getCell(0).setCellValue(item.getName()+"订单总结分析报告"); /** * 开始设置第12行 */ XSSFRow row12 = mainSheet.getRow(11); row12.getCell(1).setCellValue(item.getUuid()); row12.getCell(3).setCellValue(item.getPlandeAmt()); row12.getCell(5).setCellValue(item.getRealdeAmt()); /** *开始设置第14行 */ XSSFRow row14 = mainSheet.getRow(13); row14.getCell(1).setCellValue(item.getName()); row14.getCell(3).setCellValue(item.getPlaninAmt()); row14.getCell(5).setCellValue(item.getRealinAmt()); /** * 第16行 */ XSSFRow row16 = mainSheet.getRow(15); row16.getCell(1).setCellValue(item.getType$()); /** * 第18行 */ XSSFRow row17 = mainSheet.getRow(17); row17.getCell(1).setCellValue(item.getSalesman()); row17.getCell(2).setCellValue(itemReport.getIssue()); /** * 第20行 */ XSSFRow row19 = mainSheet.getRow(19); row19.getCell(1).setCellValue(item.getLeader()); /** * 第22行 */ XSSFRow row22 = mainSheet.getRow(21); row22.getCell(1).setCellValue(item.getOriginArea()); /** * 第29行 */ XSSFRow row29 = mainSheet.getRow(28); row29.getCell(1).setCellValue(itemReport.getDeliverIssue$()); row29.getCell(2).setCellValue(itemReport.getDeliverReason()); /** * 第31行 */ XSSFRow row31 = mainSheet.getRow(30); row31.getCell(1).setCellValue(itemReport.getQualityIssue$()); row31.getCell(2).setCellValue(itemReport.getQualityReason()); /** * 33行 */ XSSFRow row33 = mainSheet.getRow(32); row33.getCell(1).setCellValue(itemReport.getDesignIssue$()); row33.getCell(2).setCellValue(itemReport.getDesignReason()); /** * 35行 */ XSSFRow row35 = mainSheet.getRow(34); row35.getCell(1).setCellValue(itemReport.getInstallIssue$()); row35.getCell(2).setCellValue(itemReport.getInstallReason()); /** * 第45行 */ XSSFRow row45 = mainSheet.getRow(44); row45.getCell(1).setCellValue(itemReport.getConclusion()); // wb.write(out); out = response.getOutputStream(); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(item.getName()+"报告.xlsx")); ServletOutputStream outputStream = response.getOutputStream(); wb.write(outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return response; } }