TPssrProtectionController.java 19 KB

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