TPssrProtectionController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package com.ruoyi.project.pssr.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ruoyi.common.utils.DateUtils;
  4. import com.ruoyi.common.utils.StringUtils;
  5. import com.ruoyi.common.utils.file.ExcelUtils;
  6. import com.ruoyi.common.utils.poi.ExcelUtil;
  7. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  8. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  9. import com.ruoyi.framework.web.controller.BaseController;
  10. import com.ruoyi.framework.web.domain.AjaxResult;
  11. import com.ruoyi.framework.web.page.TableDataInfo;
  12. import com.ruoyi.project.listener.pssr.ConfirmTaskCreateListener;
  13. import com.ruoyi.project.pssr.domain.TPssrApprove;
  14. import com.ruoyi.project.pssr.domain.TPssrProtection;
  15. import com.ruoyi.project.pssr.domain.TPssrSubcontent;
  16. import com.ruoyi.project.pssr.domain.TPssrTurndown;
  17. import com.ruoyi.project.pssr.mapper.TPssrProtectionMapper;
  18. import com.ruoyi.project.pssr.service.*;
  19. import com.ruoyi.project.system.domain.SysDept;
  20. import com.ruoyi.project.system.domain.SysUser;
  21. import com.ruoyi.project.system.service.ISysDeptService;
  22. import com.ruoyi.project.system.service.ISysUserService;
  23. import org.activiti.engine.HistoryService;
  24. import org.activiti.engine.RuntimeService;
  25. import org.activiti.engine.impl.identity.Authentication;
  26. import org.activiti.engine.runtime.ProcessInstance;
  27. import org.apache.commons.collections4.CollectionUtils;
  28. import org.apache.poi.ss.usermodel.*;
  29. import org.apache.poi.xssf.usermodel.XSSFSheet;
  30. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.security.access.prepost.PreAuthorize;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.multipart.MultipartFile;
  35. import javax.annotation.Resource;
  36. import java.io.FileOutputStream;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.io.OutputStream;
  40. import java.text.SimpleDateFormat;
  41. import java.util.*;
  42. /**
  43. * 人身防护Controller
  44. *
  45. * @author ssy
  46. * @date 2024-09-18
  47. */
  48. @RestController
  49. @RequestMapping("/pssr/protection")
  50. public class TPssrProtectionController extends BaseController {
  51. @Resource
  52. private TPssrProtectionMapper tPssrProtectionMapper;
  53. @Autowired
  54. private ITPssrFileService tPssrFileService;
  55. @Autowired
  56. private ITPssrTurndownService tPssrTurndownService;
  57. @Autowired
  58. private ITPssrProtectionService tPssrProtectionService;
  59. @Autowired
  60. private ITPssrApproveService tPssrApproveService;
  61. @Autowired
  62. private ITPssrSubcontentService tPssrSubcontentService;
  63. @Autowired
  64. private RuntimeService runtimeService;
  65. @Autowired
  66. private HistoryService historyService;
  67. @Autowired
  68. private ISysUserService sysUserService;
  69. private String forShort = "rsfh";
  70. @Autowired
  71. private ISysDeptService iSysDeptService;
  72. @Autowired
  73. private ISysUserService userService;
  74. /**
  75. * 批量导入
  76. */
  77. @PreAuthorize("@ss.hasPermi('pssr:protection:add')")
  78. @PostMapping("/importData")
  79. public AjaxResult importInterlockData(MultipartFile file, Long subId) throws IOException
  80. {
  81. //获取操作人员ID
  82. Long userId = getUserId();
  83. //报错行数统计
  84. List<Integer> failRow =new ArrayList<Integer>();
  85. Workbook workbook = ExcelUtils.getWorkBook(file);
  86. Sheet sheet = workbook.getSheetAt(0);
  87. List<TPssrProtection> list = new ArrayList<TPssrProtection>();
  88. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  89. //部门查询
  90. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  91. int rowNum = sheet.getPhysicalNumberOfRows();
  92. int failNumber = 0;
  93. for (int i = 2; i <= rowNum; i++) {
  94. try {
  95. logger.info("读取行数:" + i);
  96. Row row = sheet.getRow(i);
  97. int cellNum = row.getLastCellNum();
  98. TPssrProtection entity = new TPssrProtection();
  99. entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
  100. entity.setSubId(subId);
  101. entity.setApproveStatus(0L);
  102. for (int j = 0; j < cellNum; j++) {
  103. Cell cell = row.getCell(j);
  104. String cellValue = ExcelUtils.getCellValue(cell);
  105. logger.info("cellValue:" + cellValue);
  106. if (j == 0) {
  107. entity.setCheckContent(cellValue);
  108. } else if (j == 1) {
  109. entity.setCheckResult(cellValue);
  110. } else if (j == 2) {
  111. if (cellValue.length() > 3) {
  112. entity.setConfirmationDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  113. }
  114. } else if (j == 3) {
  115. entity.setRemarks(cellValue);
  116. }
  117. }
  118. entity.setCreaterCode(userId.toString());
  119. logger.info("entity:" + entity);
  120. list.add(entity);
  121. }catch (Exception e){
  122. failNumber++;
  123. failRow.add(i+1);
  124. }
  125. }
  126. int successNumber = 0;
  127. int failNum = 0;
  128. for (TPssrProtection t : list
  129. ) {
  130. failNum++;
  131. try {
  132. tPssrProtectionService.insertTPssrProtection(t);
  133. successNumber++;
  134. }catch (Exception e){
  135. failNumber++;
  136. logger.info("e:" + e);
  137. failRow.add(failNum+1);
  138. }
  139. }
  140. logger.info("list:" + JSON.toJSONString(list));
  141. logger.info("successNumber:" +String.valueOf(successNumber));
  142. logger.info("failNumber:" +String.valueOf(failNumber));
  143. logger.info("failRow:" +String.valueOf(failRow));
  144. return AjaxResult.success("导入成功行数:" + String.valueOf(successNumber));
  145. }
  146. /**
  147. * 查询人身防护列表
  148. */
  149. @PreAuthorize("@ss.hasPermi('pssr:protection:list')")
  150. @GetMapping("/list")
  151. public TableDataInfo list(TPssrProtection tPssrProtection) {
  152. startPage();
  153. List<TPssrProtection> list = tPssrProtectionService.selectTPssrProtectionList(tPssrProtection);
  154. list.forEach(item -> {
  155. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "rsfh"));
  156. if (item.getApproveStatus() != 2)
  157. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "rsfh"));
  158. });
  159. return getDataTable(list);
  160. }
  161. /**
  162. * 导出人身防护列表
  163. */
  164. @PreAuthorize("@ss.hasPermi('pssr:protection:export')")
  165. @Log(title = "人身防护", businessType = BusinessType.EXPORT)
  166. @GetMapping("/export")
  167. public AjaxResult export(TPssrProtection tPssrProtection) {
  168. List<TPssrProtection> list = tPssrProtectionService.selectTPssrProtectionList(tPssrProtection);
  169. return AjaxResult.success(exportTmpl(list));
  170. }
  171. public String exportTmpl(List<TPssrProtection> list) {
  172. OutputStream out = null;
  173. String filename = null;
  174. try {
  175. String tempUrl = "static/word/pssr/rsfh1.xlsx"; // 模板文件
  176. InputStream is = null;
  177. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  178. XSSFWorkbook wb = null;
  179. wb = new XSSFWorkbook(is);
  180. XSSFSheet sheet = wb.getSheetAt(0);
  181. //填充数据
  182. int rowIndex = 2;
  183. int num = 1;
  184. Row originalRow = sheet.getRow(2);
  185. Cell originalcell = originalRow.getCell(0);
  186. // 获取单元格样式
  187. CellStyle originalStyle = originalcell.getCellStyle();
  188. for (TPssrProtection t : list) {
  189. Row row = sheet.createRow(rowIndex);
  190. row.setHeight((short) 800);
  191. row.createCell(0).setCellValue(num);
  192. row.createCell(1).setCellValue(t.getCheckContent());
  193. row.createCell(2).setCellValue(t.getCheckResult());
  194. row.createCell(3);
  195. row.createCell(4);
  196. try {
  197. SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(t.getConfirm1()));
  198. SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(t.getConfirm2()));
  199. String confirm1 = sysUser1.getSignUrl();
  200. String confirm2 = sysUser2.getSignUrl();
  201. ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 3, 1, 1);
  202. ExcelUtils.insertPicture(wb, sheet, confirm2, row.getRowNum(), 4, 1, 1);
  203. } catch (NumberFormatException e) {
  204. throw new RuntimeException(e);
  205. }
  206. row.createCell(5).setCellValue(DateUtils.dateTime(t.getConfirmationDate()));
  207. row.createCell(6).setCellValue(t.getRemarks());
  208. //渲染样式
  209. for (int i = 0; i < 7; i++) {
  210. row.getCell(i).setCellStyle(originalStyle);
  211. }
  212. num++;
  213. rowIndex++;
  214. }
  215. filename = "PSSR_06_人身防护_现场管道和设备保温防护检查表_" + UUID.randomUUID().toString() + ".xlsx";
  216. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  217. wb.write(out);
  218. wb.close();
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. }
  222. return filename;
  223. }
  224. /**
  225. * 获取人身防护详细信息
  226. */
  227. @PreAuthorize("@ss.hasPermi('pssr:protection:query')")
  228. @GetMapping(value = "/{id}")
  229. public AjaxResult getInfo(@PathVariable("id") Long id) {
  230. TPssrProtection item = tPssrProtectionService.selectTPssrProtectionById(id);
  231. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "rsfh"));
  232. if (item.getApproveStatus() != 2)
  233. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "rsfh"));
  234. return AjaxResult.success(item);
  235. }
  236. /**
  237. * 新增人身防护
  238. */
  239. @PreAuthorize("@ss.hasPermi('pssr:protection:add')")
  240. @Log(title = "人身防护", businessType = BusinessType.INSERT)
  241. @PostMapping
  242. public AjaxResult add(@RequestBody TPssrProtection tPssrProtection) {
  243. tPssrProtection.setApproveStatus(0L);
  244. tPssrProtection.setCreaterCode(String.valueOf(getUserId()));
  245. tPssrProtection.setCreatedate(new Date());
  246. return toAjax(tPssrProtectionService.insertTPssrProtection(tPssrProtection));
  247. }
  248. /**
  249. * 修改人身防护
  250. */
  251. @PreAuthorize("@ss.hasPermi('pssr:protection:edit')")
  252. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  253. @PutMapping
  254. public AjaxResult edit(@RequestBody TPssrProtection tPssrProtection) {
  255. tPssrFileService.updateFileRelevance(tPssrProtection.getFileIds(), "rsfh", tPssrProtection.getId(), tPssrProtection.getSubId());
  256. return toAjax(tPssrProtectionService.updateTPssrProtection(tPssrProtection));
  257. }
  258. /**
  259. * 修改人身防护
  260. */
  261. @PreAuthorize("@ss.hasPermi('pssr:protection:edit')")
  262. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  263. @PutMapping("/editBatch")
  264. public AjaxResult editb(@RequestBody TPssrProtection tPssrProtection) {
  265. return toAjax(tPssrProtectionMapper.updateTPssrProtectionByIds(tPssrProtection));
  266. }
  267. /**
  268. * 删除人身防护
  269. */
  270. @PreAuthorize("@ss.hasPermi('pssr:protection:remove')")
  271. @Log(title = "人身防护", businessType = BusinessType.DELETE)
  272. @DeleteMapping("/{ids}")
  273. public AjaxResult remove(@PathVariable Long[] ids) {
  274. return toAjax(tPssrProtectionService.deleteTPssrProtectionByIds(ids));
  275. }
  276. /**
  277. * 确认人身防护
  278. */
  279. @PreAuthorize("@ss.hasPermi('pssr:protection:edit')")
  280. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  281. @PutMapping("/confirmProtection")
  282. public AjaxResult confirmProtection(@RequestBody TPssrProtection tPssrProtection) {
  283. long queryStatus = 0;
  284. long approveStatus = 0;
  285. Date date = null;
  286. TPssrProtection protection = new TPssrProtection();
  287. if (tPssrProtection.getTaskType() == 4) {
  288. //确认人1确认
  289. queryStatus = 1;
  290. approveStatus = 3;
  291. protection.setConfirm1(getUserId().toString());
  292. date = new Date();
  293. } else if (tPssrProtection.getTaskType() == 5) {
  294. //确认人2确认
  295. queryStatus = 3;
  296. approveStatus = 2;
  297. protection.setConfirm2(getUserId().toString());
  298. date = new Date();
  299. }
  300. if (tPssrProtection.getIds() != null && tPssrProtection.getIds().length > 0) {
  301. for (Long id : tPssrProtection.getIds()) {
  302. protection = tPssrProtectionService.selectTPssrProtectionById(id);
  303. if (queryStatus == 3) {
  304. protection.setConfirmationDate(new Date());
  305. }
  306. protection.setApproveStatus(approveStatus);
  307. protection.setUpdatedate(new Date());
  308. protection.setUpdaterCode(String.valueOf(getUserId()));
  309. tPssrProtectionService.updateTPssrProtection(protection);
  310. }
  311. } else {
  312. protection.setSubId(tPssrProtection.getSubId());
  313. protection.setApproveStatus(queryStatus);
  314. for (TPssrProtection item : tPssrProtectionService.selectTPssrProtectionList(protection)) {
  315. if (queryStatus == 3) {
  316. item.setConfirmationDate(new Date());
  317. }
  318. item.setApproveStatus(approveStatus);
  319. item.setUpdatedate(new Date());
  320. item.setUpdaterCode(String.valueOf(getUserId()));
  321. tPssrProtectionService.updateTPssrProtection(item);
  322. }
  323. }
  324. //查询当前待审批的确认人
  325. TPssrProtection entity = new TPssrProtection();
  326. entity.setSubId(tPssrProtection.getSubId());
  327. entity.setApproveStatus(queryStatus);
  328. TPssrProtection pssrProtection = tPssrProtectionService.selectAllConfirmedPersonBySubId(entity);
  329. if (pssrProtection != null) {
  330. //如果当前用户还有待审批任务
  331. if (tPssrProtection.getTaskType() == 4 && StringUtils.isNotEmpty(pssrProtection.getConfirm1())) {
  332. if (pssrProtection.getConfirm1().contains(getUserId().toString())) {
  333. return AjaxResult.success();
  334. }
  335. }
  336. if (tPssrProtection.getTaskType() == 5 && StringUtils.isNotEmpty(pssrProtection.getConfirm2())) {
  337. if (pssrProtection.getConfirm2().contains(getUserId().toString())) {
  338. return AjaxResult.success();
  339. }
  340. }
  341. }
  342. //无待审批任务结束当前用户流程
  343. // 因为流程关系所以approve一定会有且只有一条数据
  344. TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrProtection.getSubId());
  345. TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
  346. return AjaxResult.success();
  347. }
  348. /**
  349. * 驳回人身防护
  350. */
  351. @PutMapping("/turnDownProtection")
  352. public AjaxResult turnDownProtection(@RequestBody List<TPssrProtection> tPssrProtection) {
  353. if (CollectionUtils.isNotEmpty(tPssrProtection)) {
  354. String userId = getUserId().toString();
  355. Long subId = tPssrProtection.get(0).getSubId();
  356. // 修改已选择数据的状态
  357. for (TPssrProtection item : tPssrProtection) {
  358. TPssrProtection blind = new TPssrProtection();
  359. blind.setId(item.getId());
  360. blind.setApproveStatus(1L);
  361. blind.setUpdatedate(new Date());
  362. blind.setUpdaterCode(getUserId().toString());
  363. tPssrProtectionService.updateTPssrProtection(blind);
  364. // 新增驳回原因数据
  365. TPssrTurndown turndown = new TPssrTurndown();
  366. turndown.setForShort(forShort);
  367. turndown.setSubId(item.getSubId());
  368. turndown.setItemId(item.getId());
  369. turndown.setReason(item.getReason());
  370. turndown.setCreatedate(new Date());
  371. turndown.setCreaterCode(getUserId().toString());
  372. tPssrTurndownService.insertTPssrTurndown(turndown);
  373. }
  374. // 查询当前流程
  375. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);
  376. try {
  377. runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
  378. historyService.deleteHistoricProcessInstance(approve.getProcessId());
  379. } catch (Exception e) {
  380. logger.info("无运行时流程");
  381. }
  382. // 驳回 查询所有待审批的人员
  383. TPssrProtection blind = new TPssrProtection();
  384. blind.setSubId(subId);
  385. blind.setApproveStatus(1L);
  386. Set<String> confirmUsers1 = new HashSet<>();
  387. Set<String> confirmUsers2 = new HashSet<>();
  388. for (TPssrProtection item : tPssrProtectionService.selectTPssrProtectionList(blind)) {
  389. // 确认人1
  390. confirmUsers1.add(item.getConfirm1());
  391. //确认人2
  392. confirmUsers2.add(item.getConfirm2());
  393. }
  394. // 开始申请流程
  395. long businessKey = approve.getApproveId();
  396. //开始工作流、监听
  397. Authentication.setAuthenticatedUserId(userId);//设置当前申请人
  398. Map<String, Object> variables = new HashMap<>();
  399. variables.put("applyUser", userId);
  400. variables.put("confirmUsers1", confirmUsers1);
  401. variables.put("confirmUsers2", confirmUsers2);
  402. variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
  403. variables.put("chargePerson", approve.getSubCharge());
  404. //采用key来启动流程定义并设置流程变量,返回流程实例
  405. ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);
  406. // 修改审批表和sub表
  407. approve.setProcessId(pi.getProcessInstanceId());
  408. approve.setApproveStatus(1L);
  409. approve.setUpdatedate(new Date());
  410. approve.setUpdaterCode(getUserId().toString());
  411. tPssrApproveService.updateTPssrApprove(approve);
  412. TPssrSubcontent subcontent = new TPssrSubcontent();
  413. subcontent.setId(approve.getSubId());
  414. subcontent.setApproveStatus(1L);
  415. subcontent.setUpdatedate(new Date());
  416. subcontent.setUpdaterCode(getUserId().toString());
  417. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  418. return AjaxResult.success();
  419. }
  420. return AjaxResult.error();
  421. }
  422. }