diff --git a/bonus-common-biz/src/main/java/com/bonus/common/biz/config/PoiOutPage.java b/bonus-common-biz/src/main/java/com/bonus/common/biz/config/PoiOutPage.java new file mode 100644 index 00000000..f7e1e3a7 --- /dev/null +++ b/bonus-common-biz/src/main/java/com/bonus/common/biz/config/PoiOutPage.java @@ -0,0 +1,311 @@ +package com.bonus.common.biz.config; + +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.RegionUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * 导出模版样式 + * @author ma_sh + */ +public class PoiOutPage { + + /** + * 生成带有样式的 Excel 文件 + * @param result + * @param list + * @param filename + * @return + */ + public static HSSFWorkbook excel(List> result, List list, String filename) { + // 获取工作簿对象 + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + HSSFCellStyle titleStyle = createTitleStyle(workbook); + HSSFCellStyle headerStyle = createHeaderStyle(workbook); + HSSFCellStyle contentStyle = createCellStyle(workbook); + + // 设置默认列宽 + sheet.setDefaultColumnWidth(15); + + // 设置工作簿名称 + workbook.setSheetName(0, filename); + + // 如果数据为空,创建一个空的工作表 + if (result == null || result.size() == 0) { + createEmptySheet(sheet, list, titleStyle, headerStyle, filename); + } else { + // 如果数据不为空,创建工作表并填充数据 + int nColumn = list.size(); + int rowNum = createTitleRow(sheet, titleStyle, filename, nColumn); + rowNum = createHeaderRow(sheet, headerStyle, list, rowNum); + createDataRows(sheet, result, contentStyle, nColumn, rowNum); + } + + return workbook; + } + + private static int createTitleRow(HSSFSheet sheet, HSSFCellStyle titleStyle, String filename, int nColumn) { + HSSFRow row = sheet.createRow(0); + row.setHeightInPoints(30); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (short) (nColumn - 1))); + HSSFCell cell = row.createCell(0); + cell.setCellStyle(titleStyle); + cell.setCellValue(filename); + return 1; // 下一行是表头 + } + + private static int createHeaderRow(HSSFSheet sheet, HSSFCellStyle headerStyle, List list, int rowNum) { + HSSFRow row = sheet.createRow(rowNum); + row.setHeightInPoints(20); + for (int j = 0; j < list.size(); j++) { + HSSFCell cell = row.createCell(j); + String header = list.get(j); + cell.setCellStyle(headerStyle); + cell.setCellValue(header != null ? header : ""); + } + return rowNum + 1; + } + + private static void createDataRows(HSSFSheet sheet, List> result, HSSFCellStyle contentStyle, int nColumn, int rowNum) { + for (Map resultRow : result) { + List rowData = map2List(resultRow); + HSSFRow row = sheet.createRow(rowNum++); + row.setHeightInPoints(15); + + // 第一列:单位数据 + if (rowData.size() != nColumn) { + HSSFCell cell = row.createCell(0); + cell.setCellStyle(contentStyle); + cell.setCellValue(rowData.get(0).toString()); + + sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, (short) (nColumn - 1))); + continue; + } + + // 填充数据行 + for (int j = 0; j < nColumn; j++) { + HSSFCell cell = row.createCell(j); + Object data = rowData.get(j); + if (data != null) { + setCellValue(cell, contentStyle, data); + } else { + cell.setCellStyle(contentStyle); + cell.setCellValue(""); + } + } + } + } + + private static void setCellValue(HSSFCell cell, HSSFCellStyle contentStyle, Object data) { + if (isNumeric(data)) { + contentStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); + cell.setCellStyle(contentStyle); + cell.setCellValue(Double.parseDouble(data.toString())); + } else { + cell.setCellStyle(contentStyle); + cell.setCellValue(data.toString()); + } + } + + private static void createEmptySheet(HSSFSheet sheet, List list, HSSFCellStyle titleStyle, HSSFCellStyle headerStyle, String filename) { + int nColumn = list.size(); + int rowNum = createTitleRow(sheet, titleStyle, filename, nColumn); + createHeaderRow(sheet, headerStyle, list, rowNum); + } + + + /** + * 生成带有样式的 Excel 文件 + * + * @param result 数据内容(每行是一个 Map) + * @param list 表头 + * @param filename 文件名 + * @param unit 单位信息 + * @param projectName 项目名称 + * @return HSSFWorkbook 生成的 Excel 文件 + */ + public static HSSFWorkbook excelForcheck(List> result, List list, + String filename, String unit, String projectName) { + // 创建工作簿和工作表 + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + sheet.setDefaultColumnWidth(15); // 设置列宽 + + // 创建样式 + HSSFCellStyle titleStyle = createTitleStyle(workbook); + HSSFCellStyle headerStyle = createHeaderStyle(workbook); + HSSFCellStyle contentStyle = createCellStyle(workbook); + + // 设置工作簿名称 + workbook.setSheetName(0, filename); + + // 填充标题行 + int rowNum = 0; + rowNum = createTitleRow(sheet, rowNum, filename, titleStyle, list.size()); + rowNum = createProjectInfoRow(sheet, rowNum, projectName, unit, titleStyle, list.size()); + + // 填充表头 + rowNum = createHeaderRow(sheet, rowNum, list, headerStyle); + + // 填充数据行 + if (result != null && !result.isEmpty()) { + rowNum = createDataRows(sheet, rowNum, result, contentStyle, list.size()); + } else { + // 如果没有数据,则仅显示表头 + rowNum++; + } + + return workbook; + } + + /** + * 创建标题行 + */ + private static int createTitleRow(HSSFSheet sheet, int rowNum, String filename, HSSFCellStyle titleStyle, int nColumn) { + HSSFRow row = sheet.createRow(rowNum++); + row.setHeightInPoints(30); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (short) (nColumn - 1))); + HSSFCell cell = row.createCell(0); + cell.setCellStyle(titleStyle); + cell.setCellValue(filename); + return rowNum; + } + + /** + * 创建项目名称和单位信息行 + */ + private static int createProjectInfoRow(HSSFSheet sheet, int rowNum, String projectName, String unit, + HSSFCellStyle titleStyle, int nColumn) { + HSSFRow row = sheet.createRow(rowNum++); + row.setHeightInPoints(30); + sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (nColumn - 1))); + HSSFCell cell = row.createCell(0); + cell.setCellStyle(titleStyle); + cell.setCellValue(projectName + " " + unit); + + // 添加边框 + CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (nColumn - 1)); + // 设置边框样式 + RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet); + RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet); + RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet); + RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet); + + return rowNum; + } + + /** + * 创建表头行 + */ + private static int createHeaderRow(HSSFSheet sheet, int rowNum, List list, HSSFCellStyle headerStyle) { + HSSFRow row = sheet.createRow(rowNum++); + row.setHeightInPoints(20); + for (int j = 0; j < list.size(); j++) { + HSSFCell cell = row.createCell(j); + String header = list.get(j); + cell.setCellStyle(headerStyle); + cell.setCellValue(header != null ? header : ""); + } + return rowNum; + } + + /** + * 创建数据行 + */ + private static int createDataRows(HSSFSheet sheet, int rowNum, List> result, + HSSFCellStyle contentStyle, int nColumn) { + for (Map resultRow : result) { + HSSFRow row = sheet.createRow(rowNum++); + row.setHeightInPoints(15); + List rowData = map2List(resultRow); + + for (int j = 0; j < nColumn; j++) { + HSSFCell cell = row.createCell(j); + Object data = rowData.get(j); + setCellData(cell, data, contentStyle); + } + } + return rowNum; + } + + /** + * 设置单元格数据,并自动判断数据类型 + */ + private static void setCellData(HSSFCell cell, Object data, HSSFCellStyle contentStyle) { + if (data != null) { + if (isNumeric(data)) { + cell.setCellStyle(contentStyle); + cell.setCellValue(Double.parseDouble(data.toString())); + } else { + cell.setCellStyle(contentStyle); + cell.setCellValue(data.toString()); + } + } else { + cell.setCellStyle(contentStyle); + cell.setCellValue(""); + } + } + + /** + * 判断是否是数字 + */ + private static boolean isNumeric(Object data) { + if (data == null) { + return false; + } + return data.toString().matches("^(-?\\d+)(\\.\\d+)?$"); + } + + /** + * 将 Map 转换为 List + */ + private static List map2List(Map map) { + List list = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + list.add(entry.getValue()); + } + return list; + } + + // 以下是创建样式的方法(可根据需要调整) + + private static HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) { + HSSFCellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + HSSFFont font = workbook.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 14); + style.setFont(font); + return style; + } + + private static HSSFCellStyle createHeaderStyle(HSSFWorkbook workbook) { + HSSFCellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + HSSFFont font = workbook.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 12); + style.setFont(font); + return style; + } + + private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook) { + HSSFCellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + HSSFFont font = workbook.createFont(); + font.setFontHeightInPoints((short) 10); + style.setFont(font); + return style; + } +} diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseApplyInfoController.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseApplyInfoController.java index 3993b88a..10f64f92 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseApplyInfoController.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseApplyInfoController.java @@ -73,6 +73,27 @@ public class LeaseApplyInfoController extends BaseController { return success(leaseApplyInfoService.selectLeaseApplyInfoById(id, keyWord)); } + /** + * 获取领料出库单详细信息 + * @param leaseApplyInfo + * @return + */ + @ApiOperation(value = "获取领料出库单详细信息") + //@RequiresPermissions("lease:info:query") + @GetMapping(value = "/getInfo") + public AjaxResult getInfo(LeaseApplyInfo leaseApplyInfo) { + return success(leaseApplyInfoService.getInfo(leaseApplyInfo.getId())); + } + + @ApiOperation(value = "导出领料出库单详情") + //@RequiresPermissions("lease:info:export") + @SysLog(title = "领料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料任务详情") + @PostMapping("/exportInfo") + public void exportInfo(HttpServletResponse response, LeaseApplyInfo leaseApplyInfo) { + LeaseApplyRequestVo leaseApplyRequestVo = leaseApplyInfoService.getInfo(leaseApplyInfo.getId()); + leaseApplyInfoService.exportInfo(leaseApplyRequestVo, response); + } + @ApiOperation(value = "导出领料任务详情") @PreventRepeatSubmit //@RequiresPermissions("lease:info:export") diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseApplyRequestVo.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseApplyRequestVo.java index 69665a50..605c0677 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseApplyRequestVo.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseApplyRequestVo.java @@ -32,4 +32,6 @@ public class LeaseApplyRequestVo extends BaseEntity { @ApiModelProperty(value = "领料-机具规格详情列表") private List leaseApplyDetailsList; + private List leaseOutVoList; + } diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseApplyInfoServiceImpl.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseApplyInfoServiceImpl.java index 8bd83749..c0e5d5df 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseApplyInfoServiceImpl.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseApplyInfoServiceImpl.java @@ -1,10 +1,13 @@ package com.bonus.material.lease.service.impl; +import java.io.OutputStream; +import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; import cn.hutool.core.collection.CollectionUtil; +import com.bonus.common.biz.config.PoiOutPage; import com.bonus.common.biz.constant.MaterialConstants; import com.bonus.common.biz.enums.HttpCodeEnum; import com.bonus.common.biz.enums.LeaseTaskStatusEnum; @@ -21,12 +24,15 @@ import com.bonus.material.lease.domain.LeaseApplyDetails; import com.bonus.common.biz.domain.lease.LeaseOutDetails; import com.bonus.material.lease.domain.vo.LeaseApplyRequestVo; import com.bonus.common.biz.domain.lease.LeaseOutRequestVo; +import com.bonus.material.lease.domain.vo.LeaseOutVo; import com.bonus.material.lease.mapper.LeaseApplyDetailsMapper; import com.bonus.material.lease.service.ILeaseOutDetailsService; import com.bonus.material.task.domain.TmTask; import com.bonus.material.task.domain.TmTaskAgreement; import com.bonus.material.task.mapper.TmTaskAgreementMapper; import com.bonus.material.task.mapper.TmTaskMapper; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Service; import com.bonus.material.lease.mapper.LeaseApplyInfoMapper; @@ -44,6 +50,7 @@ import javax.servlet.http.HttpServletResponse; * @date 2024-10-16 */ @Service +@Slf4j public class LeaseApplyInfoServiceImpl implements ILeaseApplyInfoService { @Resource @@ -342,11 +349,129 @@ public class LeaseApplyInfoServiceImpl implements ILeaseApplyInfoService { @Override public LeaseApplyRequestVo getInfo(Long id) { - return null; + try { + Optional optionalInfo = Optional.ofNullable(leaseApplyInfoMapper.selectLeaseApplyInfoById(id)); + LeaseApplyRequestVo leaseApplyRequestVo = new LeaseApplyRequestVo(); + + optionalInfo.ifPresent(info -> { + leaseApplyRequestVo.setLeaseApplyInfo(info); + // 获取领料单详情 + List details = leaseApplyDetailsMapper.selectLeaseOutDetailsList(id); + if (!CollectionUtils.isEmpty(details)) { + leaseApplyRequestVo.setLeaseOutVoList(details); + } + }); + return leaseApplyRequestVo; + } catch (Exception e) { + // 记录异常日志 + System.err.println("Error occurred while selecting lease apply info by ID: " + id + e.getMessage()); + throw new RuntimeException("Failed to select lease apply info", e); + } } @Override public void exportInfo(LeaseApplyRequestVo leaseApplyRequestVo, HttpServletResponse response) { + try { + String fileName = "施工机具设备出库检验记录表"; + String projectName = ""; + String unit = ""; + if (leaseApplyRequestVo != null && leaseApplyRequestVo.getLeaseApplyInfo() != null) { + projectName ="领用工程:" + leaseApplyRequestVo.getLeaseApplyInfo().getProjectName(); + unit ="使用单位:" + leaseApplyRequestVo.getLeaseApplyInfo().getLeaseUnit(); + } + List list = leaseApplyRequestVo.getLeaseOutVoList(); + expOutExcel(response,list,fileName,projectName,unit); + } catch (Exception e) { + log.error(e.toString(), e); + } + } + /** + * 导出领料单 + * @param response + * @param list + * @param filename + * @param projectName + * @param unit + * @throws Exception + */ + private void expOutExcel(HttpServletResponse response, List list, String filename,String projectName,String unit) + throws Exception { + if (list != null) { + List> results = new ArrayList>(); + int size = list.size(); + for (int i = 0; i < size; i++) { + LeaseOutVo bean = list.get(i); + Map maps = outReceiveDetailsBeanToMap(bean); + results.add(maps); + } + List headers = receiveDetailsHeader(); + HSSFWorkbook workbook = PoiOutPage.excelForcheck(results, headers, filename,projectName,unit); + OutputStream out = null; + response.setContentType("application/vnd.ms-excel;charset=UTF-8"); + response.addHeader("Content-Disposition", + "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls"); + response.setHeader("Pragma", "No-cache"); + out = response.getOutputStream(); + workbook.write(out); + out.flush(); + out.close(); + }else{ + List> results = new ArrayList>(); + List headers = receiveDetailsHeader(); + HSSFWorkbook workbook = PoiOutPage.excel(results, headers, filename); + OutputStream out = null; + response.setContentType("application/vnd.ms-excel;charset=UTF-8"); + response.addHeader("Content-Disposition", + "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls"); + response.setHeader("Pragma", "No-cache"); + out = response.getOutputStream(); + workbook.write(out); + out.flush(); + out.close(); + } + } + + /** + * 领料单数据转换 + * @param bean + * @return + */ + private Map outReceiveDetailsBeanToMap(LeaseOutVo bean) { + Map maps = new LinkedHashMap(); + maps.put("typeName", bean.getTypeName()); + maps.put("typeModelName", bean.getTypeModelName()); + maps.put("unit", bean.getUnit()); + maps.put("num", bean.getNum()); + maps.put("maCode", bean.getMaCode()); + maps.put("ratedLoad", bean.getRatedLoad()); + maps.put("testLoad", bean.getTestLoad()); + maps.put("holdingTime", bean.getHoldingTime()); + maps.put("testTime", bean.getTestTime()); + maps.put("nextTestTime", bean.getNextTestTime()); + maps.put("checkResult", bean.getCheckResult()); + maps.put("remark", bean.getRemark()); + return maps; + } + + /** + * 领料单表头 + * @return + */ + private List receiveDetailsHeader() { + ArrayList list = new ArrayList(); + list.add("机具名称"); + list.add("规格型号"); + list.add("单位"); + list.add("数量"); + list.add("设备编码"); + list.add("额定载荷KN"); + list.add("试验载荷KN"); + list.add("持荷时间min"); + list.add("试验日期"); + list.add("下次试验日期"); + list.add("检验结论"); + list.add("备注"); + return list; } } diff --git a/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseApplyDetailsMapper.xml b/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseApplyDetailsMapper.xml index 584962f6..25ef3fed 100644 --- a/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseApplyDetailsMapper.xml +++ b/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseApplyDetailsMapper.xml @@ -269,4 +269,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" lod.parent_id = #{id} and mt.type_id = #{typeId} + +