package com.ruoyi.project.pssr.controller; import com.alibaba.fastjson.JSON; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; 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.listener.pssr.ConfirmTaskCreateListener; import com.ruoyi.project.pssr.domain.TPssrApprove; import com.ruoyi.project.pssr.domain.TPssrOverhaulExchanger; import com.ruoyi.project.pssr.domain.TPssrSubcontent; import com.ruoyi.project.pssr.domain.TPssrTurndown; import com.ruoyi.project.pssr.mapper.TPssrOverhaulExchangerMapper; import com.ruoyi.project.pssr.service.*; 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.activiti.engine.*; import org.activiti.engine.impl.identity.Authentication; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.apache.commons.collections4.CollectionUtils; 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.*; /** * 检修项目-换热器Controller * * @author ssy * @date 2024-09-18 */ @RestController @RequestMapping("/pssr/overhaulExchanger") public class TPssrOverhaulExchangerController extends BaseController { @Resource private TPssrOverhaulExchangerMapper tPssrOverhaulExchangerMapper; @Autowired private ITPssrFileService tPssrFileService; @Autowired private ITPssrTurndownService tPssrTurndownService; @Autowired private ITPssrOverhaulExchangerService tPssrOverhaulExchangerService; @Autowired private ITPssrApproveService tPssrApproveService; @Autowired private RuntimeService runtimeService; @Autowired private HistoryService historyService; @Autowired private ITPssrSubcontentService tPssrSubcontentService; @Autowired private ISysUserService sysUserService; private String forShort = "jxxm-hrq"; @Autowired private ISysDeptService iSysDeptService; @Autowired private ISysUserService userService; /** * 查询检修项目-换热器列表 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:list')") @GetMapping("/list") public TableDataInfo list(TPssrOverhaulExchanger tPssrOverhaulExchanger) { try { TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(tPssrOverhaulExchanger.getSubId()); if (approve != null) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = processEngine.getTaskService()//获取任务service .createTaskQuery()//创建查询对象 .taskAssignee(getUserId().toString()) .processInstanceId(approve.getProcessId()).singleResult(); if (task != null) { if (task.getName().equals("确认人")){ tPssrOverhaulExchanger.setConfirmedPerson(getUserId().toString()); } } } } catch (Exception e) { e.printStackTrace(); logger.error("待办确认人查询报错:{}",e.getMessage()); } if ("1".equals(tPssrOverhaulExchanger.getConfirmedPerson())) { tPssrOverhaulExchanger.setConfirmedPerson(getUserId().toString()); } startPage(); List list = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerList(tPssrOverhaulExchanger); list.forEach(item -> { item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "jxxm-hrq")); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "jxxm-hrq")); }); return getDataTable(list); } /** * 导出检修项目-换热器列表 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:export')") @Log(title = "检修项目-换热器", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(TPssrOverhaulExchanger tPssrOverhaulExchanger) { List list = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerList(tPssrOverhaulExchanger); if (list.size() != 0) { return AjaxResult.success(exportTmpl(list, tPssrOverhaulExchanger.getDevType())); } else { return AjaxResult.error("暂无可导出数据"); } } public String exportTmpl(List list, Long devType) { OutputStream out = null; String filename = null; try { String tempUrl = "static/word/pssr/jxxmhrq.xlsx"; // 模板文件 InputStream is = null; is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl); XSSFWorkbook wb = null; wb = new XSSFWorkbook(is); XSSFSheet sheet = wb.getSheetAt(0); switch (devType.toString()) { case "1": filename = "PSSR_01_检修项目_换热器" + ".xlsx"; sheet.getRow(0).getCell(0).setCellValue("蒸汽裂解装置换热器检修清单"); break; case "2": filename = "PSSR_01_检修项目_电仪" + ".xlsx"; sheet.getRow(0).getCell(0).setCellValue("蒸汽裂解装置电仪检修清单"); break; case "3": filename = "PSSR_01_检修项目_反应器" + ".xlsx"; sheet.getRow(0).getCell(0).setCellValue("蒸汽裂解装置反应器检修清单"); break; case "4": filename = "PSSR_01_检修项目_裂解炉" + ".xlsx"; sheet.getRow(0).getCell(0).setCellValue("蒸汽裂解装置裂解炉检修清单"); break; case "5": filename = "PSSR_01_检修项目_其他" + ".xlsx"; sheet.getRow(0).getCell(0).setCellValue("蒸汽裂解装置其他检修清单"); break; } //填充数据 int rowIndex = 3; int num = 1; Row originalRow = sheet.getRow(3); Cell originalcell = originalRow.getCell(0); // 获取单元格样式 CellStyle originalStyle = originalcell.getCellStyle(); for (TPssrOverhaulExchanger t : list) { Row row = sheet.createRow(rowIndex); row.setHeight((short) 800); row.createCell(0).setCellValue(num); row.createCell(1).setCellValue(t.getTagNo()); row.createCell(2).setCellValue(t.getEquipmentName()); row.createCell(3).setCellValue(t.getPidNo()); row.createCell(4).setCellValue(t.getWorkDes()); row.createCell(5).setCellValue(t.getCompletionStatus()); row.createCell(6); try { SysUser sysUser = sysUserService.selectUserById(Long.valueOf(t.getConfirmedPerson())); String confirm1 = sysUser.getSignUrl(); ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 6, 1, 1); } catch (Exception e) { } row.createCell(7).setCellValue(DateUtils.dateTime(t.getCompletionDate())); row.createCell(8).setCellValue(t.getRemarks()); //渲染样式 for (int i = 0; i < 9; i++) { row.getCell(i).setCellStyle(originalStyle); } num++; rowIndex++; } out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename)); wb.write(out); wb.close(); } catch (IOException e) { e.printStackTrace(); } return filename; } /** * 获取检修项目-换热器详细信息 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { TPssrOverhaulExchanger item = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerById(id); item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "jxxm-hrq")); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "jxxm-hrq")); return AjaxResult.success(item); } /** * 新增检修项目-换热器 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:add')") @Log(title = "检修项目-换热器", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody TPssrOverhaulExchanger tPssrOverhaulExchanger) { tPssrOverhaulExchanger.setApproveStatus(0L); return toAjax(tPssrOverhaulExchangerService.insertTPssrOverhaulExchanger(tPssrOverhaulExchanger)); } //导入时判断更新或新增 private int insertOrUpdate(TPssrOverhaulExchanger item) { TPssrOverhaulExchanger entity = new TPssrOverhaulExchanger(); entity.setSubId(item.getSubId()); entity.setTagNo(item.getTagNo()); List list = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerList(entity); if (CollectionUtils.isNotEmpty(list)) { item.setId(list.get(0).getId()); return tPssrOverhaulExchangerService.updateTPssrOverhaulExchanger(item); }else{ return tPssrOverhaulExchangerService.insertTPssrOverhaulExchanger(item); } } /** * 修改检修项目-换热器 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:edit')") @Log(title = "检修项目-换热器", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody TPssrOverhaulExchanger tPssrOverhaulExchanger) { TPssrOverhaulExchanger entity = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerById(tPssrOverhaulExchanger.getId()); if (entity.getApproveStatus() != 1 && entity.getApproveStatus() != 0) { return AjaxResult.error("当前状态不可修改!"); } tPssrFileService.updateFileRelevance(tPssrOverhaulExchanger.getFileIds(), "jxxm-hrq", tPssrOverhaulExchanger.getId(), tPssrOverhaulExchanger.getSubId()); return toAjax(tPssrOverhaulExchangerService.updateTPssrOverhaulExchanger(tPssrOverhaulExchanger)); } /** * 修改检修项目-换热器 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:edit')") @Log(title = "检修项目-换热器", businessType = BusinessType.UPDATE) @PutMapping("/editBatch") public AjaxResult editb(@RequestBody TPssrOverhaulExchanger tPssrOverhaulExchanger) { return toAjax(tPssrOverhaulExchangerMapper.updateTPssrOverhaulExchangerByIds(tPssrOverhaulExchanger)); } /** * 删除检修项目-换热器 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:remove')") @Log(title = "检修项目-换热器", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tPssrOverhaulExchangerService.deleteTPssrOverhaulExchangerByIds(ids)); } /** * 确认检修项目-换热器 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:edit')") @Log(title = "检修项目-换热器", businessType = BusinessType.UPDATE) @PutMapping("/confirmExchanger") public AjaxResult confirmExchanger(@RequestBody TPssrOverhaulExchanger tPssrOverhaulExchanger) { if (tPssrOverhaulExchanger.getIds() != null && tPssrOverhaulExchanger.getIds().length > 0) { for (Long id : tPssrOverhaulExchanger.getIds()) { TPssrOverhaulExchanger exchanger = tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerById(id); exchanger.setCompletionDate(new Date()); exchanger.setApproveStatus(2L); exchanger.setUpdatedate(new Date()); exchanger.setUpdaterCode(String.valueOf(getUserId())); tPssrOverhaulExchangerService.updateTPssrOverhaulExchanger(exchanger); } } else { TPssrOverhaulExchanger exchanger = new TPssrOverhaulExchanger(); exchanger.setSubId(tPssrOverhaulExchanger.getSubId()); exchanger.setConfirmedPerson(getUserId().toString()); exchanger.setApproveStatus(1L); for (TPssrOverhaulExchanger overhaulExchanger : tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerList(exchanger)) { overhaulExchanger.setCompletionDate(new Date()); overhaulExchanger.setApproveStatus(2L); overhaulExchanger.setUpdatedate(new Date()); overhaulExchanger.setUpdaterCode(String.valueOf(getUserId())); tPssrOverhaulExchangerService.updateTPssrOverhaulExchanger(overhaulExchanger); } } //查询当前待审批的确认人 TPssrOverhaulExchanger entity = new TPssrOverhaulExchanger(); entity.setSubId(tPssrOverhaulExchanger.getSubId()); entity.setApproveStatus(1L); TPssrOverhaulExchanger exchanger = tPssrOverhaulExchangerService.selectAllConfirmedPersonBySubId(entity); if (exchanger != null) { String confirmedPerson = exchanger.getConfirmedPerson(); logger.info("===========confirmedPerson:{}", confirmedPerson); //如果当前用户还有待审批任务 if (confirmedPerson.contains(getUserId().toString())) { return AjaxResult.success(); } } //无待审批任务结束当前用户流程 // 因为流程关系所以approve一定会有且只有一条数据 TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrOverhaulExchanger.getSubId()); TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString()); return AjaxResult.success(); } @PutMapping("/turnDownExchanger") public AjaxResult turnDownExchanger(@RequestBody List tPssrOverhaulExchanger) { if (CollectionUtils.isNotEmpty(tPssrOverhaulExchanger)) { String userId = getUserId().toString(); Long subId = tPssrOverhaulExchanger.get(0).getSubId(); // 修改已选择数据的状态 for (TPssrOverhaulExchanger item : tPssrOverhaulExchanger) { TPssrOverhaulExchanger blind = new TPssrOverhaulExchanger(); blind.setId(item.getId()); blind.setApproveStatus(1L); blind.setUpdatedate(new Date()); blind.setUpdaterCode(getUserId().toString()); tPssrOverhaulExchangerService.updateTPssrOverhaulExchanger(blind); // 新增驳回原因数据 TPssrTurndown turndown = new TPssrTurndown(); turndown.setForShort(forShort); turndown.setSubId(item.getSubId()); turndown.setItemId(item.getId()); turndown.setReason(item.getReason()); turndown.setCreatedate(new Date()); turndown.setCreaterCode(getUserId().toString()); tPssrTurndownService.insertTPssrTurndown(turndown); } // 查询当前流程 TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId); try { runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr1confirm"); historyService.deleteHistoricProcessInstance(approve.getProcessId()); } catch (Exception e) { logger.info("无运行时流程"); } // 驳回 查询所有待审批的人员 //查询所有确认人 TPssrOverhaulExchanger exchanger = new TPssrOverhaulExchanger(); exchanger.setSubId(approve.getSubId()); exchanger.setApproveStatus(1L); TPssrOverhaulExchanger overhaul = tPssrOverhaulExchangerService.selectAllConfirmedPersonBySubId(exchanger); String confirmers = null; if (overhaul != null) { confirmers = overhaul.getConfirmedPerson(); } logger.info("=======================confirmers:{}", confirmers); Set confirmUsers1 = new HashSet<>(); if (StringUtils.isNotEmpty(confirmers)) { confirmUsers1.addAll(Arrays.asList(confirmers.split(","))); } // 开始申请流程 long businessKey = approve.getApproveId(); //开始工作流、监听 Authentication.setAuthenticatedUserId(userId);//设置当前申请人 Map variables = new HashMap<>(); variables.put("applyUser", userId); variables.put("confirmUsers", new ArrayList<>(confirmUsers1)); variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件 variables.put("chargePerson", approve.getSubCharge()); //采用key来启动流程定义并设置流程变量,返回流程实例 ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr1confirm", String.valueOf(businessKey), variables); // 修改审批表和sub表 approve.setProcessId(pi.getProcessInstanceId()); approve.setApproveStatus(1L); approve.setUpdatedate(new Date()); approve.setUpdaterCode(getUserId().toString()); tPssrApproveService.updateTPssrApprove(approve); TPssrSubcontent subcontent = new TPssrSubcontent(); subcontent.setId(approve.getSubId()); subcontent.setApproveStatus(1L); subcontent.setUpdatedate(new Date()); subcontent.setUpdaterCode(getUserId().toString()); tPssrSubcontentService.updateTPssrSubcontent(subcontent); return AjaxResult.success(); } return AjaxResult.error(); } /** * 批量导入 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger:add')") @PostMapping("/importData") public AjaxResult importInterlockData(MultipartFile file, Long subId, Long devType) 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.getLastCellNum(); TPssrOverhaulExchanger entity = new TPssrOverhaulExchanger(); entity.setDeptId(userService.selectUserById(getUserId()).getDeptId()); entity.setSubId(subId); entity.setDevType(devType); 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.setTagNo(cellValue); } else if (j == 1) { entity.setEquipmentName(cellValue); } else if (j == 2) { entity.setWorkDes(cellValue); } else if (j == 3) { entity.setPidNo(cellValue); }else if (j == 4) { entity.setConfirmedPerson(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 (TPssrOverhaulExchanger t : list ) { failNum++; try { this.add(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)); } }