|
@@ -3,8 +3,8 @@ package com.rongwei.sfcommon.sys.service.impl;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.system.SystemUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -19,23 +19,31 @@ import com.rongwei.rwadmincommon.system.service.SysConfigFeignService;
|
|
|
import com.rongwei.rwadmincommon.system.service.SysUserOrgService;
|
|
|
import com.rongwei.rwadmincommon.system.service.SysUserService;
|
|
|
import com.rongwei.rwcommon.base.QueryPar;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
import com.rongwei.rwcommon.vo.MailDo;
|
|
|
-import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
|
|
|
import com.rongwei.sfcommon.sys.dao.PointCheckDao;
|
|
|
import com.rongwei.sfcommon.sys.service.*;
|
|
|
import com.rongwei.sfcommon.utils.MlConstants;
|
|
|
import org.apache.commons.beanutils.BeanUtils;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.util.*;
|
|
|
-
|
|
|
+import java.util.stream.Collectors;
|
|
|
import static com.rongwei.sfcommon.utils.CommonUtil.streamCodeGeneration;
|
|
|
|
|
|
@Service("pointCheckService")
|
|
@@ -468,6 +476,180 @@ public class PointCheckServiceImpl extends ServiceImpl<PointCheckDao, PointCheck
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void excelExport(PointCheckExcelData excelData, HttpServletResponse response) {
|
|
|
+ logger.info("Excel导出开始...");
|
|
|
+ //1. 整理主子表数据
|
|
|
+ List<PointCheckVo> mains =pointCheckDao.getExcelDataByids(excelData.getIds());
|
|
|
+ //2.子表数据
|
|
|
+ Map<String, List<PointCheckItemDo>> subMaps = pointCheckItemService.list(new LambdaQueryWrapper<PointCheckItemDo>()
|
|
|
+ .eq(PointCheckItemDo::getDeleted, "0")
|
|
|
+ .in(PointCheckItemDo::getPointcheckid, excelData.getIds())).stream().collect(Collectors.groupingBy(PointCheckItemDo::getPointcheckid));
|
|
|
+
|
|
|
+ //3.获取点检记录模板
|
|
|
+ String fullPath = pointCheckItemService.getExcelTempPath(excelData.getPagePartId(),excelData.getFileTemplateName());
|
|
|
+
|
|
|
+ // fullPath="C:\\360安全浏览器下载\\"+excelData.getFileTemplateName();
|
|
|
+ XSSFWorkbook workbook =null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ InputStream fileInputStream = new FileInputStream(fullPath);
|
|
|
+ workbook = new XSSFWorkbook(fileInputStream);
|
|
|
+ XSSFSheet tempSheet = workbook.getSheetAt(0);
|
|
|
+ excelData.setStartIndex(0);
|
|
|
+ //处理文件数据填充
|
|
|
+ for (PointCheckVo main:mains){
|
|
|
+ this.fillFirstData(tempSheet,workbook,main,subMaps.get(main.getId()),excelData);
|
|
|
+ };
|
|
|
+
|
|
|
+ workbook.removeSheetAt(0);
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ //文件名称
|
|
|
+ String filename = excelData.getFileName();
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ filename = new String(filename.getBytes(), "ISO8859-1");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
|
|
+ response.addHeader("Pargam", "no-cache");
|
|
|
+ response.addHeader("Cache-Control", "no-cache");
|
|
|
+
|
|
|
+ // 获取ServletOutputStream对象,用于将Workbook内容写入响应
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
+ workbook.write(outputStream);
|
|
|
+ // 刷新输出流,确保所有数据都被发送
|
|
|
+ outputStream.flush();
|
|
|
+ // 关闭Workbook对象(注意:在某些情况下,你可能需要在finally块中关闭它,以确保即使发生异常也能关闭)
|
|
|
+ workbook.close();
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("导出失败", ex);
|
|
|
+ throw new CustomException("导出失败");
|
|
|
+ }
|
|
|
+ logger.info("Excel导出结束...");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填充模板数据
|
|
|
+ *
|
|
|
+ * @param tempSheet
|
|
|
+ * @param workbook
|
|
|
+ * @param pointCheckVo
|
|
|
+ * @param pointCheckItemDos
|
|
|
+ * @param excelData
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void fillFirstData(XSSFSheet tempSheet, XSSFWorkbook workbook, PointCheckVo pointCheckVo, List<PointCheckItemDo> pointCheckItemDos, PointCheckExcelData excelData) {
|
|
|
+ XSSFSheet sheet =null;
|
|
|
+ Boolean saveOneSheet = excelData.getSaveOneSheet();
|
|
|
+ int indexstart =excelData.getStartIndex();
|
|
|
+ //新增一个sheet 中
|
|
|
+ if (saveOneSheet && indexstart==0){
|
|
|
+ sheet = workbook.createSheet();
|
|
|
+ }else if (saveOneSheet && indexstart >0){
|
|
|
+ sheet = workbook.getSheetAt(1);
|
|
|
+ }else {
|
|
|
+ sheet = workbook.createSheet();
|
|
|
+ }
|
|
|
+
|
|
|
+ XSSFRow row =null;
|
|
|
+ XSSFRow catchRow = tempSheet.getRow(8);
|
|
|
+ XSSFRow detailTieleRow = tempSheet.getRow(5);
|
|
|
+ XSSFCell cell =null;
|
|
|
+
|
|
|
+ Short detailHight = 700;
|
|
|
+ CellRangeAddress cellRangeAddress =null; // 合并单元格
|
|
|
+
|
|
|
+ for (int i = indexstart; i < 8+pointCheckItemDos.size()+indexstart; i++) {
|
|
|
+ row = sheet.createRow(i);
|
|
|
+ for (int j = 0; j < 7; j++) {
|
|
|
+ cell = row.createCell(j);
|
|
|
+ if (i-indexstart <=6){
|
|
|
+ cell.setCellStyle(tempSheet.getRow(i-indexstart).getCell(j).getCellStyle());
|
|
|
+ }else if (i== 7+pointCheckItemDos.size()+indexstart){
|
|
|
+ cell.setCellStyle(tempSheet.getRow(8).getCell(j).getCellStyle());
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(tempSheet.getRow(5).getCell(j).getCellStyle());
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if (i== 7+pointCheckItemDos.size()+indexstart){
|
|
|
+ row.setHeight(tempSheet.getRow(8).getHeight());
|
|
|
+ }else if (i-indexstart > 6){
|
|
|
+ row.setHeight(detailHight);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ //填充主表数据
|
|
|
+ sheet.getRow(indexstart+0).getCell(0).setCellValue(pointCheckVo.getPointcheckname()); //点检名称
|
|
|
+ sheet.getRow(indexstart+1).getCell(0).setCellValue(pointCheckVo.getTenantid()); //所属工厂
|
|
|
+ sheet.getRow(indexstart+1).getCell(2).setCellValue(pointCheckVo.getWorkshopname()); //车间名称
|
|
|
+ sheet.getRow(indexstart+1).getCell(4).setCellValue(pointCheckVo.getPointcheckcode()); //记录编号
|
|
|
+ sheet.getRow(indexstart+2).getCell(0).setCellValue(pointCheckVo.getCheckitemname()); //设备名称
|
|
|
+ sheet.getRow(indexstart+2).getCell(2).setCellValue(pointCheckVo.getCheckitemcode()); //设备编号
|
|
|
+ sheet.getRow(indexstart+2).getCell(4).setCellValue(pointCheckVo.getTemplatetypename()); //检查类型
|
|
|
+ sheet.getRow(indexstart+3).getCell(0).setCellValue(pointCheckVo.getCreateusername()); //点检人员
|
|
|
+ sheet.getRow(indexstart+3).getCell(2).setCellValue(pointCheckVo.getChecktime()); // 点检时间
|
|
|
+ sheet.getRow(indexstart+3).getCell(4).setCellValue(pointCheckVo.getShiftname()); //班次
|
|
|
+ sheet.getRow(indexstart+4).getCell(0).setCellValue(pointCheckVo.getConfirmoruser()); //点检确认人
|
|
|
+ sheet.getRow(indexstart+4).getCell(2).setCellValue("点检时是否停机:"+pointCheckVo.getClosingdownname()==null?"":pointCheckVo.getClosingdownname()); //点检是否停机
|
|
|
+ sheet.getRow(indexstart+4).getCell(4).setCellValue("结果:"+pointCheckVo.getCheckresultname()==null?"":pointCheckVo.getCheckresultname()); //点检结果
|
|
|
+
|
|
|
+ for (int i = 0; i <7; i++) {
|
|
|
+ sheet.getRow(indexstart+5).getCell(i).setCellValue(detailTieleRow.getCell(i).getStringCellValue());
|
|
|
+ }
|
|
|
+ //填充子表数据
|
|
|
+ for (int i = 0; i < pointCheckItemDos.size(); i++) {
|
|
|
+ row = sheet.getRow(indexstart+7 + i);
|
|
|
+ row.getCell(0).setCellValue(pointCheckItemDos.get(i).getCheckposition());
|
|
|
+ row.getCell(1).setCellValue(pointCheckItemDos.get(i).getCheckcontent());
|
|
|
+ row.getCell(2).setCellValue(pointCheckItemDos.get(i).getChecktype());
|
|
|
+ row.getCell(3).setCellValue(pointCheckItemDos.get(i).getCheckstandard());
|
|
|
+ row.getCell(4).setCellValue(pointCheckItemDos.get(i).getManagement());
|
|
|
+ row.getCell(5).setCellValue(pointCheckItemDos.get(i).getQuotavalue()==null?null:pointCheckItemDos.get(i).getQuotavalue().toPlainString());
|
|
|
+ row.getCell(6).setCellValue(pointCheckItemDos.get(i).getRemark());
|
|
|
+ }
|
|
|
+
|
|
|
+ //异常数据填充
|
|
|
+ row= sheet.getRow(pointCheckItemDos.size() + 7+indexstart);
|
|
|
+ row.getCell(0).setCellValue(catchRow.getCell(0).getStringCellValue());
|
|
|
+ row.getCell(2).setCellValue(catchRow.getCell(2).getStringCellValue());
|
|
|
+
|
|
|
+ //单元格合并
|
|
|
+ cellRangeAddress = new CellRangeAddress(indexstart, indexstart, 0, 6);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+
|
|
|
+ for (int i = 1; i <= 4; i++) {
|
|
|
+ cellRangeAddress = new CellRangeAddress(indexstart+i, indexstart+i, 0, 1);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ cellRangeAddress = new CellRangeAddress(indexstart+i, indexstart+i, 2, 3);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ cellRangeAddress = new CellRangeAddress(indexstart+i, indexstart+i, 4, 6);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ };
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
+ cellRangeAddress = new CellRangeAddress(indexstart+5, indexstart+6, i, i);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ }
|
|
|
+ cellRangeAddress = new CellRangeAddress(7+pointCheckItemDos.size()+indexstart, 7+pointCheckItemDos.size()+indexstart, 2, 6);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+
|
|
|
+
|
|
|
+ //设置列的宽度
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
+ sheet.setColumnWidth(i, tempSheet.getColumnWidth(i));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (excelData.getSaveOneSheet()){
|
|
|
+ excelData.setStartIndex( 8+pointCheckItemDos.size()+indexstart);
|
|
|
+ }
|
|
|
|
|
|
+ };
|
|
|
|
|
|
}
|