接口联调

This commit is contained in:
mashuai 2024-12-25 14:32:34 +08:00
parent d112ee1e81
commit 6ced630d20
5 changed files with 485 additions and 1 deletions

View File

@ -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<Map<String, Object>> result, List<String> 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<String> 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<Map<String, Object>> result, HSSFCellStyle contentStyle, int nColumn, int rowNum) {
for (Map<String, Object> resultRow : result) {
List<Object> 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<String> 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<Map<String, Object>> result, List<String> 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<String> 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<Map<String, Object>> result,
HSSFCellStyle contentStyle, int nColumn) {
for (Map<String, Object> resultRow : result) {
HSSFRow row = sheet.createRow(rowNum++);
row.setHeightInPoints(15);
List<Object> 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<Object> map2List(Map<String, Object> map) {
List<Object> list = new ArrayList<>();
for (Map.Entry<String, Object> 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;
}
}

View File

@ -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")

View File

@ -32,4 +32,6 @@ public class LeaseApplyRequestVo extends BaseEntity {
@ApiModelProperty(value = "领料-机具规格详情列表")
private List<LeaseApplyDetails> leaseApplyDetailsList;
private List<LeaseOutVo> leaseOutVoList;
}

View File

@ -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<LeaseApplyInfo> optionalInfo = Optional.ofNullable(leaseApplyInfoMapper.selectLeaseApplyInfoById(id));
LeaseApplyRequestVo leaseApplyRequestVo = new LeaseApplyRequestVo();
optionalInfo.ifPresent(info -> {
leaseApplyRequestVo.setLeaseApplyInfo(info);
// 获取领料单详情
List<LeaseOutVo> 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<LeaseOutVo> 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<LeaseOutVo> list, String filename,String projectName,String unit)
throws Exception {
if (list != null) {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
int size = list.size();
for (int i = 0; i < size; i++) {
LeaseOutVo bean = list.get(i);
Map<String, Object> maps = outReceiveDetailsBeanToMap(bean);
results.add(maps);
}
List<String> 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<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
List<String> 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<String, Object> outReceiveDetailsBeanToMap(LeaseOutVo bean) {
Map<String, Object> maps = new LinkedHashMap<String, Object>();
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<String> receiveDetailsHeader() {
ArrayList<String> list = new ArrayList<String>();
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;
}
}

View File

@ -269,4 +269,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
lod.parent_id = #{id}
and mt.type_id = #{typeId}
</select>
<select id="selectLeaseOutDetailsList" resultType="com.bonus.material.lease.domain.vo.LeaseOutVo">
SELECT
mt1.type_name AS typeName,
mt.type_name AS typeModelName,
mt.unit_name AS unit,
lod.out_num AS num,
mm.ma_code AS maCode,
mt.rated_load AS ratedLoad,
mt.test_load AS testLoad,
mt.holding_time AS holdingTime,
lod.create_time AS testTime,
pcd.check_result AS checkResult,
lad.remark AS remark,
DATE_SUB(DATE_ADD(lod.create_time, INTERVAL 1 YEAR), INTERVAL 1 DAY) AS nextTestTime -- 计算 nextTestTime
FROM
lease_out_details lod
LEFT JOIN ma_type mt ON lod.type_id = mt.type_id AND mt.del_flag = '0'
LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id AND mt1.del_flag = '0'
LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
LEFT JOIN lease_apply_details lad ON lod.type_id = lad.type_id
LEFT JOIN purchase_check_details pcd ON lod.type_id = pcd.type_id
WHERE lod.parent_id = #{id}
GROUP BY lod.type_id, mm.ma_code
</select>
</mapper>