package com.cpms.project.process.controller; import com.alibaba.fastjson2.JSON; import com.cpms.common.annotation.Log; import com.cpms.common.core.controller.BaseController; import com.cpms.common.core.domain.AjaxResult; import com.cpms.common.core.page.TableDataInfo; import com.cpms.common.enums.BusinessType; import com.cpms.common.utils.DateUtils; import com.cpms.common.utils.file.ExcelUtils; import com.cpms.common.utils.poi.ExcelUtil; import com.cpms.project.process.domain.TValveCsocsc; import com.cpms.project.process.domain.TValveNonc; import com.cpms.project.process.service.ITValveCsocscService; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * 阀门铅封开关状态记录Controller * * @author admin * @date 2024-04-11 */ @RestController @RequestMapping("/process/csocsc") public class TValveCsocscController extends BaseController { @Autowired private ITValveCsocscService tValveCsocscService; /** * 查询阀门铅封开关状态记录列表 */ @PreAuthorize("@ss.hasPermi('process:csocsc:list')") @GetMapping("/list") public TableDataInfo list(TValveCsocsc tValveCsocsc) { startPage(); List list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc); return getDataTable(list); } /** * 导出阀门铅封开关状态记录列表 */ @PreAuthorize("@ss.hasPermi('process:csocsc:export')") @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, TValveCsocsc tValveCsocsc) { List list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc); ExcelUtil util = new ExcelUtil(TValveCsocsc.class); util.exportExcel(response, list, "阀门铅封开关状态记录数据"); } /** * 导出阀门铅封开关状态记录列表 */ @PreAuthorize("@ss.hasPermi('process:csocsc:export')") @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.EXPORT) @PostMapping("/exportTmpl") public void exportTmpl(HttpServletResponse response, TValveCsocsc tValveCsocsc) { List list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc); try { String tempUrl = "static/template/process/csocscExport.xlsx"; // 模板文件 InputStream is = null; is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl); XSSFWorkbook wb1 = null; wb1 = new XSSFWorkbook(is); SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 1000); Sheet sheet; if (wb instanceof SXSSFWorkbook) { SXSSFWorkbook sxssfWorkbook = (SXSSFWorkbook) wb; sheet = sxssfWorkbook.getXSSFWorkbook().getSheetAt(0); } else { sheet = wb.getSheetAt(0); } //填充数据 int rowIndex = 5; int num = 1; Row originalRow = sheet.getRow(5); Cell originalcell = originalRow.getCell(0); // 获取单元格样式 CellStyle originalStyle = originalcell.getCellStyle(); for (TValveCsocsc t: list ) { Row row = sheet.createRow(rowIndex); row.createCell(0).setCellValue(num); row.createCell(1).setCellValue(t.getUnit()); row.createCell(2).setCellValue(t.getVtNo()); row.createCell(3).setCellValue(t.getPidNo()); row.createCell(4).setCellValue(t.getLocationDes()); row.createCell(5).setCellValue(t.getMedium()); row.createCell(6).setCellValue(t.getValveType()); row.createCell(7).setCellValue(t.getValveSize()); row.createCell(8).setCellValue(t.getIdentifier()); row.createCell(9).setCellValue(t.getValvePosition()); row.createCell(10).setCellValue(t.getFirmlySecured()); row.createCell(11).setCellValue(t.getResponsibility()); row.createCell(12).setCellValue(t.getCheckedBy()); row.createCell(13).setCellValue(t.getCheckBeforeSu()); row.createCell(14).setCellValue(t.getPidStatus()); row.createCell(15).setCellValue(t.getRiskLevel()); row.createCell(16).setCellValue(t.getSheReview()); row.createCell(17).setCellValue(t.getRemarks()); //渲染样式 for (int i = 0; i < 18; i++) { row.getCell(i).setCellStyle(originalStyle); } num++; rowIndex++; } // 生成文件返回下载地址 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); wb.write(response.getOutputStream()); wb.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取阀门铅封开关状态记录详细信息 */ @PreAuthorize("@ss.hasPermi('process:csocsc:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return success(tValveCsocscService.selectTValveCsocscById(id)); } /** * 新增阀门铅封开关状态记录 */ @PreAuthorize("@ss.hasPermi('process:csocsc:add')") @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody TValveCsocsc tValveCsocsc) { return toAjax(tValveCsocscService.insertTValveCsocsc(tValveCsocsc)); } /** * 修改阀门铅封开关状态记录 */ @PreAuthorize("@ss.hasPermi('process:csocsc:edit')") @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody TValveCsocsc tValveCsocsc) { return toAjax(tValveCsocscService.updateTValveCsocsc(tValveCsocsc)); } /** * 删除阀门铅封开关状态记录 */ @PreAuthorize("@ss.hasPermi('process:csocsc:remove')") @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tValveCsocscService.deleteTValveCsocscByIds(ids)); } @Log(title = "阀门铅封开关状态记录批量导入", businessType = BusinessType.INSERT) @PostMapping("/importData") public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException { //获取操作人员ID Long userId = getUserId(); //报错行数统计 List failRow = new ArrayList<>(); Workbook workbook = ExcelUtils.getWorkBook(file); Sheet sheet = workbook.getSheetAt(0); List list = new ArrayList<>(); int rowNum = sheet.getPhysicalNumberOfRows(); int failNumber = 0; for (int i = 2; i < rowNum; i++) { try { logger.info("读取行数:" + i); Row row = sheet.getRow(i); int cellNum = row.getLastCellNum(); TValveCsocsc entity = new TValveCsocsc(); for (int j = 0; j < cellNum; j++) { Cell cell = row.getCell(j); if (cell == null) { continue; } String cellValue = ExcelUtils.getCellValue(cell); logger.info("cellValue:" + cellValue); if (j == 0) { entity.setUnit(cellValue); } else if (j == 1) { entity.setVtNo(cellValue); } else if (j == 2) { entity.setPidNo(cellValue); } else if (j == 3) { entity.setLocationDes(cellValue); } else if (j == 4) { entity.setMedium(cellValue); } else if (j == 5) { entity.setValveType(cellValue); } else if (j == 6) { entity.setValveSize(cellValue); } else if (j == 7) { entity.setIdentifier(cellValue); } else if (j == 8) { entity.setValvePosition(cellValue); } else if (j == 9) { entity.setFirmlySecured(cellValue); } else if (j == 10) { entity.setResponsibility(cellValue); } else if (j == 11) { entity.setCheckedBy(cellValue); } else if (j == 12) { entity.setPidStatus(cellValue); } else if (j == 13) { entity.setRiskLevel(cellValue); } else if (j == 14) { entity.setCheckDate(DateUtils.parseDate(cellValue)); } else if (j == 15) { entity.setRemarks(cellValue); } } entity.setCreaterCode(String.valueOf(userId)); logger.info("entity:" + entity); list.add(entity); } catch (Exception e) { failNumber++; logger.info("e:" + JSON.toJSONString(e)); failRow.add(i + 1); } } int successNumber = 0; int failNum = 0; for (TValveCsocsc t : list ) { failNum++; try { //根据使用证、注册编号、位号,进行数据更新 add(t); successNumber++; } catch (Exception e) { failNumber++; logger.info("e:" + e); failRow.add(failNum + 1); } } logger.info("list:" + JSON.toJSONString(list)); logger.info("successNumber:" + successNumber); logger.info("failNumber:" + failNumber); logger.info("failRow:" + failRow); return AjaxResult.success(String.valueOf(successNumber), failRow); } }