package com.ruoyi.project.pssr.controller; import com.alibaba.fastjson.JSON; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.file.ExcelUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.project.pssr.domain.TPssrSafetyBleed; import com.ruoyi.project.pssr.mapper.TPssrSafetyBleedMapper; import com.ruoyi.project.pssr.service.ITPssrFileService; import com.ruoyi.project.pssr.service.ITPssrSafetyBleedService; import com.ruoyi.project.pssr.service.ITPssrTurndownService; import com.ruoyi.project.system.domain.SysDept; import com.ruoyi.project.system.domain.SysUser; import com.ruoyi.project.system.service.ISysDeptService; import com.ruoyi.project.system.service.ISysUserService; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFSheet; 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.annotation.Resource; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; /** * 安全设施-紧急泄放阀Controller * * @author ssy * @date 2024-09-18 */ @RestController @RequestMapping("/pssr/safetyBleed") public class TPssrSafetyBleedController extends BaseController { @Resource private TPssrSafetyBleedMapper tPssrSafetyBleedMapper; @Autowired private ITPssrFileService tPssrFileService; @Autowired private ITPssrTurndownService tPssrTurndownService; @Autowired private ITPssrSafetyBleedService tPssrSafetyBleedService; @Autowired private ISysUserService sysUserService; private String forShort = "aqss-bd"; @Autowired private ISysDeptService iSysDeptService; @Autowired private ISysUserService userService; /** * 批量导入 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:add')") @PostMapping("/importData") public AjaxResult importInterlockData(MultipartFile file, Long subId) throws IOException { //获取操作人员ID Long userId = getUserId(); //报错行数统计 List failRow =new ArrayList(); Workbook workbook = ExcelUtils.getWorkBook(file); Sheet sheet = workbook.getSheetAt(0); List list = new ArrayList(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //部门查询 List dept = iSysDeptService.selectDeptList(new SysDept()); 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.getPhysicalNumberOfCells(); TPssrSafetyBleed entity = new TPssrSafetyBleed(); entity.setDeptId(userService.selectUserById(getUserId()).getDeptId()); entity.setSubId(subId); entity.setApproveStatus(0L); for (int j = 0; j < cellNum; j++) { Cell cell = row.getCell(j); String cellValue = ExcelUtils.getCellValue(cell); logger.info("cellValue:" + cellValue); if (j == 0) { entity.setUnit(cellValue); } else if (j == 1) { entity.setPidNo(cellValue); } else if (j == 2) { entity.setDevNo(cellValue); } else if (j == 3) { entity.setVerify(cellValue); } else if (j == 4) { entity.setInValidity(cellValue); } else if (j == 5) { entity.setInstallLocation(cellValue); } else if (j == 6) { entity.setInstallAccuracy(cellValue); } else if (j == 7) { entity.setPutUse(cellValue); } else if (j == 8) { entity.setSetPressure(cellValue); } else if (j == 9) { entity.setUniformPressure(cellValue); } else if (j == 10) { if (cellValue.length() > 3) { entity.setConfirmationTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue)); } } else if (j == 11) { entity.setRemarks(cellValue); } } entity.setCreaterCode(userId.toString()); logger.info("entity:" + entity); list.add(entity); }catch (Exception e){ failNumber++; failRow.add(i+1); } } int successNumber = 0; int failNum = 0; for (TPssrSafetyBleed t : list ) { failNum++; try { tPssrSafetyBleedService.insertTPssrSafetyBleed(t); successNumber++; }catch (Exception e){ failNumber++; logger.info("e:" + e); failRow.add(failNum+1); } } logger.info("list:" + JSON.toJSONString(list)); logger.info("successNumber:" +String.valueOf(successNumber)); logger.info("failNumber:" +String.valueOf(failNumber)); logger.info("failRow:" +String.valueOf(failRow)); return AjaxResult.success("导入成功行数:" + String.valueOf(successNumber)); } /** * 查询安全设施-紧急泄放阀列表 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:list')") @GetMapping("/list") public TableDataInfo list(TPssrSafetyBleed tPssrSafetyBleed) { startPage(); List list = tPssrSafetyBleedService.selectTPssrSafetyBleedList(tPssrSafetyBleed); list.forEach(item -> { item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort)); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), forShort)); }); return getDataTable(list); } /** * 导出安全设施-紧急泄放阀列表 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:export')") @Log(title = "安全设施-紧急泄放阀", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(TPssrSafetyBleed tPssrSafetyBleed) { List list = tPssrSafetyBleedService.selectTPssrSafetyBleedList(tPssrSafetyBleed); return AjaxResult.success(exportTmpl(list)); } public String exportTmpl(List list) { OutputStream out = null; String filename = null; try { String tempUrl = "static/word/pssr/aqssxff.xlsx"; // 模板文件 InputStream is = null; is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl); XSSFWorkbook wb = null; wb = new XSSFWorkbook(is); XSSFSheet sheet = wb.getSheetAt(0); //填充数据 int rowIndex = 3; int num = 1; Row originalRow = sheet.getRow(3); Cell originalcell = originalRow.getCell(0); // 获取单元格样式 CellStyle originalStyle = originalcell.getCellStyle(); for (TPssrSafetyBleed t : list) { Row row = sheet.createRow(rowIndex); row.setHeight((short) 800); row.createCell(0).setCellValue(num); row.createCell(1).setCellValue(t.getUnit()); row.createCell(2).setCellValue(t.getPidNo()); row.createCell(3).setCellValue(t.getDevNo()); row.createCell(4).setCellValue(t.getVerify()); row.createCell(5).setCellValue(t.getInValidity()); row.createCell(6).setCellValue(t.getInstallLocation()); row.createCell(7).setCellValue(t.getInstallAccuracy()); row.createCell(8).setCellValue(t.getPutUse()); row.createCell(9).setCellValue(t.getSetPressure()); row.createCell(10).setCellValue(t.getUniformPressure()); row.createCell(11); row.createCell(12); try { SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(t.getConfirmer1())); SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(t.getConfirmer2())); String confirm1 = sysUser1.getSignUrl(); String confirm2 = sysUser2.getSignUrl(); ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 11, 1, 1); ExcelUtils.insertPicture(wb, sheet, confirm2, row.getRowNum(), 12, 1, 1); } catch (NumberFormatException e) { throw new RuntimeException(e); } row.createCell(13).setCellValue(DateUtils.dateTime(t.getConfirmationTime())); row.createCell(14).setCellValue(t.getRemarks()); //渲染样式 for (int i = 0; i < 15; i++) { row.getCell(i).setCellStyle(originalStyle); } num++; rowIndex++; } filename = ExcelUtil.encodingFilename("SafetyBleed"); out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename)); wb.write(out); wb.close(); } catch (IOException e) { e.printStackTrace(); } return filename; } /** * 获取安全设施-紧急泄放阀详细信息 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { TPssrSafetyBleed item = tPssrSafetyBleedService.selectTPssrSafetyBleedById(id); item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort)); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), forShort)); return AjaxResult.success(item); } /** * 新增安全设施-紧急泄放阀 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:add')") @Log(title = "安全设施-紧急泄放阀", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody TPssrSafetyBleed tPssrSafetyBleed) { tPssrSafetyBleed.setApproveStatus(0L); return toAjax(tPssrSafetyBleedService.insertTPssrSafetyBleed(tPssrSafetyBleed)); } /** * 修改安全设施-紧急泄放阀 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')") @Log(title = "安全设施-紧急泄放阀", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody TPssrSafetyBleed tPssrSafetyBleed) { tPssrFileService.updateFileRelevance(tPssrSafetyBleed.getFileIds(), forShort, tPssrSafetyBleed.getId(), tPssrSafetyBleed.getSubId()); return toAjax(tPssrSafetyBleedService.updateTPssrSafetyBleed(tPssrSafetyBleed)); } /** * 修改安全设施-紧急泄放阀 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')") @Log(title = "安全设施-紧急泄放阀", businessType = BusinessType.UPDATE) @PutMapping("/editBatch") public AjaxResult editb(@RequestBody TPssrSafetyBleed tPssrSafetyBleed) { return toAjax(tPssrSafetyBleedMapper.updateTPssrSafetyBleedByIds(tPssrSafetyBleed)); } /** * 删除安全设施-紧急泄放阀 */ @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:remove')") @Log(title = "安全设施-紧急泄放阀", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tPssrSafetyBleedService.deleteTPssrSafetyBleedByIds(ids)); } }