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.pssr.domain.*; 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.HistoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.impl.identity.Authentication; import org.activiti.engine.runtime.ProcessInstance; 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 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/overhaulValve") public class TPssrOverhaulValveController extends BaseController { @Resource private TPssrOverhaulValveMapper tPssrOverhaulValveMapper; @Autowired private ITPssrFileService tPssrFileService; @Autowired private ITPssrTurndownService tPssrTurndownService; @Autowired private ITPssrOverhaulValveService tPssrOverhaulValveService; @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-fm"; @Autowired private ISysDeptService iSysDeptService; @Autowired private ISysUserService userService; /** * 批量导入 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulExchanger: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(); TPssrOverhaulValve entity = new TPssrOverhaulValve(); 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.setChangeType(cellValue); } else if (j == 1) { entity.setItem(cellValue); } else if (j == 2) { entity.setPlant(cellValue); } else if (j == 3) { entity.setValvePosition(cellValue); } else if (j == 4) { entity.setType(cellValue); } else if (j == 5) { entity.setPipeSize(cellValue); } else if (j == 6) { entity.setValveClass(cellValue); } else if (j == 7) { entity.setPidNo(cellValue); } else if (j == 8) { 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 (TPssrOverhaulValve t : list ) { failNum++; try { tPssrOverhaulValveService.insertTPssrOverhaulValve(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)); } @PutMapping("/turnDownValve") public AjaxResult turnDownValve(@RequestBody List tPssrOverhaulValve) { if (CollectionUtils.isNotEmpty(tPssrOverhaulValve)) { String userId = getUserId().toString(); Long subId = tPssrOverhaulValve.get(0).getSubId(); // 修改已选择数据的状态 for (TPssrOverhaulValve item : tPssrOverhaulValve) { TPssrOverhaulValve blind = new TPssrOverhaulValve(); blind.setId(item.getId()); blind.setApproveStatus(1L); blind.setUpdatedate(new Date()); blind.setUpdaterCode(getUserId().toString()); tPssrOverhaulValveService.updateTPssrOverhaulValve(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("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:overhaulValve:list')") @GetMapping("/list") public TableDataInfo list(TPssrOverhaulValve tPssrOverhaulValve) { if ("1".equals(tPssrOverhaulValve.getIdentifyingPerson())) { tPssrOverhaulValve.setIdentifyingPerson(getUserId().toString()); } startPage(); List list = tPssrOverhaulValveService.selectTPssrOverhaulValveList(tPssrOverhaulValve); list.forEach(item -> { item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "jxxm-fm")); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "jxxm-fm")); }); return getDataTable(list); } /** * 导出检修项目-阀门列表 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:export')") @Log(title = "检修项目-阀门", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(TPssrOverhaulValve tPssrOverhaulValve) { List list = tPssrOverhaulValveService.selectTPssrOverhaulValveList(tPssrOverhaulValve); return AjaxResult.success(exportTmpl(list)); } public String exportTmpl(List list) { OutputStream out = null; String filename = null; try { String tempUrl = "static/word/pssr/jxxmfm.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 (TPssrOverhaulValve t : list) { Row row = sheet.createRow(rowIndex); row.setHeight((short) 800); row.createCell(0).setCellValue(num); row.createCell(1).setCellValue(t.getChangeType()); row.createCell(2).setCellValue(t.getItem()); row.createCell(3).setCellValue(t.getPlant()); row.createCell(4).setCellValue(t.getValvePosition()); row.createCell(5).setCellValue(t.getType()); row.createCell(6).setCellValue(t.getPipeSize()); row.createCell(7).setCellValue(t.getValveClass()); row.createCell(8).setCellValue(t.getPidNo()); row.createCell(9).setCellValue(DateUtils.dateTime(t.getDoneDate())); row.createCell(10); try { SysUser sysUser = sysUserService.selectUserById(Long.valueOf(t.getIdentifyingPerson())); String confirm1 = sysUser.getSignUrl(); ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 10, 1, 1); } catch (NumberFormatException e) { throw new RuntimeException(e); } row.createCell(11).setCellValue(t.getRemarks()); //渲染样式 for (int i = 0; i < 12; i++) { row.getCell(i).setCellStyle(originalStyle); } num++; rowIndex++; } filename = ExcelUtil.encodingFilename("OverhaulValve"); out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename)); wb.write(out); wb.close(); } catch (IOException e) { e.printStackTrace(); } return filename; } /** * 获取检修项目-阀门详细信息 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { TPssrOverhaulValve item = tPssrOverhaulValveService.selectTPssrOverhaulValveById(id); item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "jxxm-fm")); if (item.getApproveStatus() != 2) item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "jxxm-fm")); return AjaxResult.success(item); } /** * 新增检修项目-阀门 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:add')") @Log(title = "检修项目-阀门", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody TPssrOverhaulValve tPssrOverhaulValve) { tPssrOverhaulValve.setApproveStatus(0L); return toAjax(tPssrOverhaulValveService.insertTPssrOverhaulValve(tPssrOverhaulValve)); } /** * 修改检修项目-阀门 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:edit')") @Log(title = "检修项目-阀门", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody TPssrOverhaulValve tPssrOverhaulValve) { tPssrFileService.updateFileRelevance(tPssrOverhaulValve.getFileIds(), "jxxm-fm", tPssrOverhaulValve.getId(), tPssrOverhaulValve.getSubId()); return toAjax(tPssrOverhaulValveService.updateTPssrOverhaulValve(tPssrOverhaulValve)); } /** * 删除检修项目-阀门 */ @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:remove')") @Log(title = "检修项目-阀门", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tPssrOverhaulValveService.deleteTPssrOverhaulValveByIds(ids)); } @PreAuthorize("@ss.hasPermi('pssr:overhaulValve:edit')") @Log(title = "检修项目-阀门", businessType = BusinessType.UPDATE) @PutMapping("/confirmValve") public AjaxResult confirmValve(@RequestBody TPssrOverhaulValve tPssrOverhaulValve) { if (tPssrOverhaulValve.getIds() != null && tPssrOverhaulValve.getIds().length > 0) { for (Long id : tPssrOverhaulValve.getIds()) { TPssrOverhaulValve valve = tPssrOverhaulValveService.selectTPssrOverhaulValveById(id); if (valve.getDoneDate() == null) { valve.setDoneDate(new Date()); } valve.setApproveStatus(2L); valve.setUpdatedate(new Date()); valve.setUpdaterCode(String.valueOf(getUserId())); tPssrOverhaulValveService.updateTPssrOverhaulValve(valve); } } else { TPssrOverhaulValve valve = new TPssrOverhaulValve(); valve.setSubId(tPssrOverhaulValve.getSubId()); valve.setIdentifyingPerson(getUserId().toString()); valve.setApproveStatus(1L); for (TPssrOverhaulValve overhaulValve : tPssrOverhaulValveService.selectTPssrOverhaulValveList(valve)) { if (overhaulValve.getDoneDate() == null) { overhaulValve.setDoneDate(new Date()); } overhaulValve.setApproveStatus(2L); overhaulValve.setUpdatedate(new Date()); overhaulValve.setUpdaterCode(String.valueOf(getUserId())); tPssrOverhaulValveService.updateTPssrOverhaulValve(overhaulValve); } } //查询当前待审批的确认人 TPssrOverhaulExchanger entity = new TPssrOverhaulExchanger(); entity.setSubId(tPssrOverhaulValve.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(tPssrOverhaulValve.getSubId()); TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString()); return AjaxResult.success(); } }